Class: Pec::Director

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

Class Method Summary collapse

Class Method Details

.destroy(host_name, options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pec/director.rb', line 29

def self.destroy(host_name, options)
  Pec.configure.each do |host|
    next if host_name && host.name != host_name
    Pec.compute.set_tenant(host.tenant)
    
    server = Pec.compute.servers.find {|s|s.name == host.name}
    unless server
      Pec::Logger.notice "not be created #{host.name}"
      next
    end
    
    if options[:force] || Thor.new.yes?("#{host.name}: Are you sure you want to destroy the '#{host.name}' VM? [y/N]")
      Pec::Logger.info "#{host.name} is deleted!" if Pec.compute.servers.destroy(server.id)
    end
  end

  rescue Excon::Errors::Error => e
    excon_err_message(e)
  rescue => e
    Pec::Logger.critical(e)
end

.excon_err_message(e) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/pec/director.rb', line 82

def self.excon_err_message(e)
  if e.response
    JSON.parse(e.response[:body]).each { |e,m| Pec::Logger.critical("#{e}:#{m["message"]}") }
  else
    Pec::Logger.critical(e)
  end
end

.make(host_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/pec/director.rb', line 3

def self.make(host_name)
  Pec.configure.each do |host|
    next if host_name && host.name != host_name
    Pec::Logger.info "make start #{host.name}"
    Pec.compute.set_tenant(host.tenant)
    Pec.neutron.set_tenant_patch(host.tenant)

    attribute = { name: host.name}
    host.keys.each do |k|
      Pec::Handler.constants.each do |c|
        if Object.const_get("Pec::Handler::#{c}").kind == k
          attribute.deep_merge!(Object.const_get("Pec::Handler::#{c}").build(host))
        end
      end
    end
    
    attribute[:user_data] = "#cloud-config\n" + attribute[:user_data].to_yaml if attribute[:user_data]
    Pec::Logger.info "create success! #{host.name}" if Pec.compute.servers.create(attribute)
  end

  rescue Excon::Errors::Error => e
    excon_err_message(e)
  rescue => e
    Pec::Logger.critical(e)
end

.status(host_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pec/director.rb', line 51

def self.status(host_name)
  Pec.configure.each do |host|
    next if host_name && host.name != host_name
    if server = Pec.compute.servers.find {|s|s.name == host.name}
      puts sprintf(" %-35s %-10s %-10s %-10s %-10s %-35s %-48s",
        host.name,
        server.state,
        Pec.identity.tenants.find_by_id(server.tenant_id),
        Pec.compute.flavors.get(server.flavor['id']).name,
        server.availability_zone,
        server.os_ext_srv_attr_host,
        server.addresses.map do |ethers|                                                                                                    
          ethers[1].map do |ether|                                                                                                           
            ether["addr"]                                                                                                                 
          end                                                                                                                             
        end.flatten.join(",")
      )
    else
      puts sprintf(" %-35s %-10s",
        host.name,
        "uncreated"
      )
    end
  end

  rescue Excon::Errors::Error => e
    excon_err_message(e)
  rescue => e
    Pec::Logger.critical(e)
end