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_flavor(name) ⇒ Object



55
56
57
58
59
# File 'lib/pec/vm_director.rb', line 55

def get_flavor(name)
  flavor_ref = @flavor.get_ref(name)
  puts "flavor:#{name} not fond!" if flavor_ref.nil?
  flavor_ref
end

#get_image(name) ⇒ Object



61
62
63
64
65
# File 'lib/pec/vm_director.rb', line 61

def get_image(name)
  image_ref = @image.get_ref(name)
  puts "image:#{name} not fond!" if image_ref.nil?
  image_ref
end

#get_ports(config) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pec/vm_director.rb', line 24

def get_ports(config)
  config.networks.map do |ether|
    ip = IP.new(ether.ip_address)
    _subnet = get_subnet(ip)
    return false unless _subnet

    _port = Pec::Network::Port.new(ether.name, ip.to_addr, _subnet)

    res = case
    when _port.exists? && !_port.used?
      _port.replace(ip)
    when !_port.exists?
      _port.create(ip)
    when _port.used?
      false
    end

    unless res
      puts "ip addess:#{ip.to_addr} can't create port!"
      return false
    end
    _port
  end if config.networks
end

#get_subnet(ip) ⇒ Object



49
50
51
52
53
# File 'lib/pec/vm_director.rb', line 49

def get_subnet(ip)
  _subnet = @subnet.fetch(ip.network.to_s)
  puts "ip addess:#{ip.to_addr} subnet not fond!" if _subnet.nil?
  _subnet
end

#make(config) ⇒ Object



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

def make(config)
  ports = get_ports(config)
  flavor_ref = get_flavor(config.flavor)
  image_ref = get_image(config.image)
  return false unless flavor_ref && image_ref
  options = {
    "user_data" => Pec::Configure::UserData.make(config, ports),
    "security_groups" => config.security_group
  }

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