Class: Pec::Handler::Networks

Inherits:
Object
  • Object
show all
Extended by:
Core
Defined in:
lib/pec/handler/networks.rb,
lib/pec/handler/networks/ip_address.rb,
lib/pec/handler/networks/allowed_address_pairs.rb

Defined Under Namespace

Classes: AllowedAddressPairs, IpAddress

Constant Summary collapse

NAME =
0
CONFIG =
1

Instance Attribute Summary

Attributes included from Core

#kind

Class Method Summary collapse

Methods included from Core

build, post_build, recover

Class Method Details

.build(host) ⇒ Object



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

def build(host)
  ports = []
  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
  end
  {
    networks: ports.map {|port| { uuid: nil, port: port.id }}
  }
end

.create_port(host, network) ⇒ Object



48
49
50
51
# File 'lib/pec/handler/networks.rb', line 48

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

.gen_port_attribute(host, network) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pec/handler/networks.rb', line 53

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

  Pec.processor_matching(network[CONFIG], Pec::Handler::Networks) do |klass|
    ops = klass.build(network)
    attribute.deep_merge!(ops) if ops
  end

  attribute
end

.recover(attribute) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pec/handler/networks.rb', line 26

def recover(attribute)
  return unless attribute[:networks]

  Pec::Logger.notice "start port recovery"
  attribute[:networks].each do |port|
    if port[:port]
      Yao::Port.destroy(port[:port])
      Pec::Logger.notice "port delete id:#{port[:port]}"
    end
  end
  Pec::Logger.notice "complete port recovery"
end

.security_group(host) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/pec/handler/networks.rb', line 73

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



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

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