Class: Pec::Configure::Ethernet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config) ⇒ Ethernet

Returns a new instance of Ethernet.



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

def initialize(name, config)
  check_require_key(name, config)
  check_network_key(name, 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

#bootproto ⇒ Object (readonly)

Returns the value of attribute bootproto.



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

def bootproto
  @bootproto
end

#ip_address ⇒ Object (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

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#check_network_key(name, config) ⇒ Object



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

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



64
65
66
67
# File 'lib/pec/configure/ethernet.rb', line 64

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

#config_name ⇒ Object



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

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

#device_name ⇒ Object



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

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

#get_port_content(ports) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pec/configure/ethernet.rb', line 17

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



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

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

#netmask(ports) ⇒ Object



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

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

#onboot ⇒ Object



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

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

#port_ip_address(ports) ⇒ Object



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

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

#static? ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pec/configure/ethernet.rb', line 60

def static?
  @bootproto == "static"
end

#type ⇒ Object



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

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