Class: Pec::VmDirector

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

Instance Method Summary collapse

Constructor Details

#initialize ⇒ VmDirector

Returns a new instance of VmDirector.



3
4
5
6
7
8
9
# File 'lib/pec/vm_director.rb', line 3

def initialize
    @subnet = Pec::Network::Subnet.new
    @flavor = Pec::Compute::Flavor.new
    @image = Pec::Compute::Image.new
    @security_group = Pec::Compute::Security_Group.new
    @compute = Pec::Compute::Server.new
end

Instance Method Details

#get_ports(config) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pec/vm_director.rb', line 25

def get_ports(config)
  config.networks.map do |ether|
    begin
      ip = IP.new(ether.ip_address)
    rescue ArgumentError => e
      raise(Pec::Errors::Port, "ip:#{ether.ip_address} #{e}")
    end

    subnet = @subnet.fetch(ip.network.to_s)
    raise(Pec::Errors::Subnet, "subnet:#{ip.network.to_s} is not fond!") unless subnet

    port = Pec::Network::Port.new(ether.name, ip.to_addr, subnet, get_security_group_id(config.security_group))
    raise(Pec::Errors::Port, "ip addess:#{ip.to_addr} can't create port!") unless port.assign!(ip)
    port
  end if config.networks
end

#get_security_group_id(security_groups) ⇒ Object



42
43
44
45
46
47
# File 'lib/pec/vm_director.rb', line 42

def get_security_group_id(security_groups)
  security_groups.map do |name|
    sg = @security_group.fetch(name)
    sg["id"] if sg
  end if security_groups
end

#make(config) ⇒ Object



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

def make(config)
  if @compute.exists?(config.name)
    puts "skip create server! name:#{config.name} is exists!"
    return true
  end

  ports = get_ports(config)
  flavor_ref = @flavor.get_ref(config.flavor)
  image_ref = @image.get_ref(config.image)
  options = { "user_data" => Pec::Configure::UserData.make(config, ports) }

  @compute.create(config.name, image_ref, flavor_ref, ports, options)
end