Class: Pec::Handler::UserData::Nic::Ubuntu

Inherits:
Base
  • Object
show all
Defined in:
lib/pec/handler/user_data/nic/ubuntu.rb

Constant Summary

Constants inherited from Base

Base::CONFIG, Base::NAME

Class Method Summary collapse

Methods inherited from Base

safe_merge

Class Method Details

.default_path(port) ⇒ Object



40
41
42
# File 'lib/pec/handler/user_data/nic/ubuntu.rb', line 40

def default_path(port)
  "/etc/network/interfaces"
end

.gen_user_data(networks, ports) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pec/handler/user_data/nic/ubuntu.rb', line 6

def gen_user_data(networks, ports)
  port_content = [
    "auto lo\niface lo inet loopback"
  ]

  networks.map do |network|
    port = ports.find {|p|p.name == network[NAME]}
    port_content << ifcfg_config(network, port)
  end
  [
    {
      'content' => port_content.join("\n"),
      'owner' => "root:root",
      'path' => networks.first[CONFIG]['path'] || default_path(nil),
      'permissions' => "0644"
    }
  ]
end

.ifcfg_config(network, port) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pec/handler/user_data/nic/ubuntu.rb', line 25

def ifcfg_config(network, port)
  base = {
    "auto"                    => port.name,
    "iface #{port.name} inet" => network[CONFIG]['bootproto'],
  }
  base.merge!(
    {
      "address"  => port.fixed_ips.first['ip_address'],
      "netmask" => IP.new(network[CONFIG]['ip_address']).netmask.to_s,
      "hwaddress ether"  => port.mac_address
    }
  ) if network[CONFIG]['bootproto'] == "static"
  safe_merge(base, network).reject {|k,v| k == "bootproto"}.map {|k,v| "#{k} #{v}"}.join("\n")
end