Class: Pec::Builder::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/pec/builder/port.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#user_data ⇒ Object (readonly)

Returns the value of attribute user_data.



4
5
6
# File 'lib/pec/builder/port.rb', line 4

def user_data
  @user_data
end

Instance Method Details

#allowed_address_pairs(network) ⇒ Object



112
113
114
115
116
117
# File 'lib/pec/builder/port.rb', line 112

def allowed_address_pairs(network)
  pairs = network[1]['allowed_address_pairs'].map do |pair|
    { ip_address: pair['ip_address'] }
  end
  { allowed_address_pairs: pairs }
end

#build(host) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pec/builder/port.rb', line 5

def build(host)
  ports = []
  @user_data = []

  host.networks.each do |network|
    validate(network)
    Pec::Logger.notice "port create start : #{network[0]}"
    port = create_port(host, network)
    Pec::Logger.notice "assgin ip : #{port.fixed_ips.first["ip_address"]}"
    ports << port
    @user_data << gen_user_data(network, port)
  end
  {
    nics: ports.map {|port| { port_id: port.id }}
  }
end

#create_port(host, network) ⇒ Object



31
32
33
34
35
36
# File 'lib/pec/builder/port.rb', line 31

def create_port(host, network)
  ip = IP.new(network[1]['ip_address'])
  subnet = Pec.neutron.subnets.find {|s|s.cidr == ip.network.to_s}
  attribute = gen_port_attribute(host, network, subnet, ip)
  Pec.neutron.ports.create(attribute)
end

#fixed_ip(subnet, ip) ⇒ Object

after port options



97
98
99
100
101
102
103
# File 'lib/pec/builder/port.rb', line 97

def fixed_ip(subnet, ip)
  { 
    fixed_ips: [
      { subnet_id: subnet.id, ip_address: ip.to_addr}
    ]
  }
end

#gen_port_attribute(host, network, subnet, ip) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pec/builder/port.rb', line 38

def gen_port_attribute(host, network, subnet, ip)
  attribute = {
    name: network[0],
    network_id: subnet.network_id
  }

  attribute.merge!(
    fixed_ip(subnet, ip) 
  ) if ip.to_s != subnet.cidr

  attribute.merge!(
    security_group(host)
  ) if host.security_group

  attribute.merge!(
    allowed_address_pairs(network)
  ) if network[1]['allowed_address_pairs']
  attribute
end

#gen_user_data(network, port) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/pec/builder/port.rb', line 58

def gen_user_data(network, port)
  path = network[1]['path'] || "/etc/sysconfig/network-scripts/ifcfg-#{port.name}"
  {
    'content' => ifcfg_config(network, port),
    'owner' => "root:root",
    'path' => path,
    'permissions' => "0644"
  }
end

#ifcfg_config(network, port) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pec/builder/port.rb', line 68

def ifcfg_config(network, port)
  base = {
    "name"      => port.name,
    "device"    => port.name,
    "type"      => 'Ethernet',
    "onboot"    => 'yes',
    "hwaddr"    => port.mac_address
  }
  
  base.merge!(
    {
      "netmask" => IP.new(network[1]['ip_address']).netmask.to_s,
      "ipaddr"  => port.fixed_ips.first['ip_address'].split("/").first
    }
  ) if network[1]['bootproto'] == "static"

  # delete options
  %w(allowed_address_pairs ip_address).each {|name| network[1].delete(name)}
  
  base.merge!(
    network[1]
  )

  base.map {|k,v| "#{k.upcase}=#{v}"}.join("\n")
end

#security_group(host) ⇒ Object



105
106
107
108
109
110
# File 'lib/pec/builder/port.rb', line 105

def security_group(host)
  ids =  host.security_group.map do |name|
    Pec.neutron.security_groups.find {|sg| sg.name == name}.id
  end
  { security_groups: ids }
end

#validate(network) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/pec/builder/port.rb', line 22

def validate(network)
  %w(
    bootproto
    ip_address
  ).each do |k|
    raise "network key #{k} is require" unless network[1][k]
  end
end