Class: Pec::Handler::Networks

Inherits:
Base
  • Object
show all
Defined in:
lib/pec/handler/networks.rb,
lib/pec/handler/networks/ip_address.rb,
lib/pec/handler/networks/option_base.rb,
lib/pec/handler/networks/allowed_address_pairs.rb

Defined Under Namespace

Classes: AllowedAddressPairs, IpAddress, OptionBase

Constant Summary collapse

NAME =
0
CONFIG =
1

Class Method Summary collapse

Class Method Details

.build(host) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pec/handler/networks.rb', line 12

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

  host.networks.each do |network|
    validate(network)
    Pec::Logger.notice "port create start : #{network[NAME]}"
    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
  {
    networks: ports.map {|port| { uuid: nil, port: port.id }},
    user_data: {
      'write_files' => user_data
    }
  }
end

.create_port(host, network) ⇒ Object



41
42
43
44
# File 'lib/pec/handler/networks.rb', line 41

def create_port(host, network)
  attribute = gen_port_attribute(host, network)
  Yao::Port.create(attribute)
end

.gen_port_attribute(host, network) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pec/handler/networks.rb', line 46

def gen_port_attribute(host, network)
  ip = IP.new(network[CONFIG]['ip_address'])
  subnet = Yao::Subnet.list.find {|s|s.cidr == ip.network.to_s}
  attribute = {
    name: network[NAME],
    network_id: subnet.network_id
  }

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

  network[CONFIG].keys.each do |k|
    Pec::Handler::Networks.constants.each do |c|
      if Object.const_get("Pec::Handler::Networks::#{c}").kind == k &&
          ops = Object.const_get("Pec::Handler::Networks::#{c}").build(network)
        attribute.deep_merge!(ops)
      end
    end
  end

  attribute
end

.gen_user_data(network, port) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/pec/handler/networks.rb', line 70

def gen_user_data(network, port)
  path = network[CONFIG]['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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pec/handler/networks.rb', line 80

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[CONFIG]['ip_address']).netmask.to_s,
      "ipaddr"  => port.fixed_ips.first['ip_address']
    }
  ) if network[CONFIG]['bootproto'] == "static"

  # delete option column
  mask_column = Pec::Handler::Networks.constants.map {|c| Object.const_get("Pec::Handler::Networks::#{c}").kind }
  mask_config = network[CONFIG].select {|k,v| !mask_column.include?(k)}

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

.security_group(host) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/pec/handler/networks.rb', line 105

def security_group(host)
  tenant = Yao::Tenant.list.find {|t| t.name == host.tenant }
  ids = host.security_group.map do |name|
    sg = Yao::SecurityGroup.list.find {|sg| sg.name == name && tenant.id == sg.tenant_id }
    raise "security group #{name} is not found" unless sg
    sg.id
  end
  { security_groups: ids }
end

.validate(network) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/pec/handler/networks.rb', line 32

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