Class: Pec::Configure::Ethernet

Inherits:
Object
  • Object
show all
Defined in:
lib/pec/configure/ethernet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Ethernet

Returns a new instance of Ethernet.



5
6
7
8
9
10
11
12
# File 'lib/pec/configure/ethernet.rb', line 5

def initialize(config)
  @name       = config[0];
  @bootproto  = config[1]["bootproto"];
  @ip_address = config[1]["ip_address"];
  @options    = config[1].reject do |k,v|
    { k => v } if k == "bootproto" || k == "ip_address"
  end
end

Instance Attribute Details

#bootprotoObject (readonly)

Returns the value of attribute bootproto.



4
5
6
# File 'lib/pec/configure/ethernet.rb', line 4

def bootproto
  @bootproto
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



4
5
6
# File 'lib/pec/configure/ethernet.rb', line 4

def ip_address
  @ip_address
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/pec/configure/ethernet.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/pec/configure/ethernet.rb', line 4

def options
  @options
end

Class Method Details

.check_network_key(name, config) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/pec/configure/ethernet.rb', line 71

def check_network_key(name, config)
  net = config[1]
  case
  when (net["bootproto"] == "static" && net["ip_address"].nil?)
    raise(Pec::Errors::Ethernet, "skip! #{name}: ip_address is required by bootproto static")
  when (net["bootproto"] != "static" && net["bootproto"] != "dhcp")
    raise(Pec::Errors::Ethernet, "skip! #{name}: bootproto set the value dhcp or static")
  end
  true
end

.check_require_key(name, config) ⇒ Object



66
67
68
69
# File 'lib/pec/configure/ethernet.rb', line 66

def check_require_key(name, config)
  raise(Pec::Errors::Ethernet, "skip! #{name}: bootproto is required!") if config[1]["bootproto"].nil?
  true
end

.load(name, config) ⇒ Object



62
63
64
# File 'lib/pec/configure/ethernet.rb', line 62

def load(name, config)
  self.new(config) if check_require_key(name, config) && check_network_key(name, config)
end

Instance Method Details

#config_nameObject



29
30
31
# File 'lib/pec/configure/ethernet.rb', line 29

def config_name
  @options["name"] || @name
end

#device_nameObject



33
34
35
# File 'lib/pec/configure/ethernet.rb', line 33

def device_name
  @options["device"] || @name
end

#get_port_content(ports) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pec/configure/ethernet.rb', line 14

def get_port_content(ports)
  base = {
    "bootproto" => @bootproto,
    "name"      => config_name,
    "device"    => device_name,
    "type"      => type,
    "onboot"    => onboot,
    "hwaddr"    => mac_address(ports),
  }
  base.merge!({ "netmask" => netmask(ports), "ipaddr" => port_ip_address(ports) }) if static?
  base.merge!(@options) if @options
  base.map {|k,v| "#{k.upcase}=#{v}"}.join("\n")
end

#mac_address(ports) ⇒ Object



45
46
47
# File 'lib/pec/configure/ethernet.rb', line 45

def mac_address(ports)
   ports.find { |p| p.device_name == @name }.mac_address
end

#netmask(ports) ⇒ Object



49
50
51
# File 'lib/pec/configure/ethernet.rb', line 49

def netmask(ports)
  ports.find { |p| p.device_name == @name }.netmask(@ip_address.split("/").last)
end

#onbootObject



41
42
43
# File 'lib/pec/configure/ethernet.rb', line 41

def onboot
  @options["onboot"] || 'yes'
end

#port_ip_address(ports) ⇒ Object



53
54
55
# File 'lib/pec/configure/ethernet.rb', line 53

def port_ip_address(ports)
  ports.find { |p| p.device_name == @name }.ip_address
end

#static?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pec/configure/ethernet.rb', line 57

def static?
  @bootproto == "static"
end

#typeObject



37
38
39
# File 'lib/pec/configure/ethernet.rb', line 37

def type
  @options["type"] || 'Ethernet'
end