Class: NovaWhiz

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ NovaWhiz

Returns a new instance of NovaWhiz.



7
8
9
10
11
12
13
14
# File 'lib/novawhiz.rb', line 7

def initialize(opts)
  @os = OpenStack::Connection.create(
    :username => opts[:username],
    :api_key => opts[:password],
    :authtenant => opts[:authtenant],
    :auth_url => opts[:auth_url],
    :service_type => "compute")
end

Instance Attribute Details

#osObject

Returns the value of attribute os.



5
6
7
# File 'lib/novawhiz.rb', line 5

def os
  @os
end

Instance Method Details

#boot(opts) ⇒ Object

boot an instance and return creds



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/novawhiz.rb', line 98

def boot(opts)
  opts[:flavor] ||= 'standard.xsmall'
  opts[:image]  ||= /Ubuntu Precise/
  opts[:sec_groups] ||= ['default']
  opts[:key_name] ||= 'default'
  raise 'no name provided' if !opts[:name] or opts[:name].empty?

  private_key = new_key opts[:name]
  server = @os.create_server(
    :imageRef => image_id(opts[:image]),
    :flavorRef => flavor_id(opts[:flavor]),
    :key_name => opts[:name],
    :security_groups => opts[:sec_groups],
    :name => opts[:name])

  wait(300) do
    server = @os.server(server.id)
    raise 'error booting vm' if server.status == 'ERROR'
    server.status == 'ACTIVE'
  end
  sleep 60

  {
    :ip => public_ip(server),
    :user => 'ubuntu',
    :key => private_key
  }
end

#default_user(node) ⇒ Object



56
57
58
# File 'lib/novawhiz.rb', line 56

def default_user(node)
  'ubuntu'
end

#delete_if_exists(name) ⇒ Object



65
66
67
68
# File 'lib/novawhiz.rb', line 65

def delete_if_exists(name)
  s = server_by_name name
  s.delete! if s
end

#delete_keypair_if_exists(name) ⇒ Object



60
61
62
63
# File 'lib/novawhiz.rb', line 60

def delete_keypair_if_exists(name)
  kp_names = @os.keypairs.values.map { |v| v[:name] }
  @os.delete_keypair(name) if kp_names.include? name
end

#flavor_id(name) ⇒ Object



16
17
18
19
20
# File 'lib/novawhiz.rb', line 16

def flavor_id(name)
  flavors = @os.flavors.select { |f| f[:name] == name }
  raise "ambiguous/unknown flavor: #{name}" unless flavors.length == 1
  flavors.first[:id]
end

#image_id(reg) ⇒ Object



22
23
24
25
26
# File 'lib/novawhiz.rb', line 22

def image_id(reg)
  images = @os.images.select { |i| i[:name] =~ reg }
  raise "ambiguous/unknown image: #{reg} : #{images.inspect}" unless images.length == 1
  images.first[:id]
end

#keypair_name(server) ⇒ Object



52
53
54
# File 'lib/novawhiz.rb', line 52

def keypair_name(server)
  server.key_name
end

#new_key(name) ⇒ Object



28
29
30
31
# File 'lib/novawhiz.rb', line 28

def new_key(name)
  key = @os.create_keypair :name => name
  key[:private_key]
end

#public_ip(server) ⇒ Object



33
34
35
# File 'lib/novawhiz.rb', line 33

def public_ip(server)
  server.accessipv4
end

#run_command(creds, cmd) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/novawhiz.rb', line 70

def run_command(creds, cmd)
  res = Net::SSH::Simple.sync do
    ssh(creds[:ip], '/bin/sh', :user => creds[:user], :key_data => [creds[:key]], :timeout => 3600, :global_known_hosts_file => ['/dev/null'], :user_known_hosts_file => ['/dev/null']) do |e,c,d|
      case e
      when :start
        c.send_data "#{cmd}\n"
        c.eof!
      when :stdout
        # read the input line-wise (it *will* arrive fragmented!)
        (@buf ||= '') << d
        while line = @buf.slice!(/(.*)\r?\n/)
          yield line.chomp if block_given?
        end
      when :stderr
        (@buf ||= '') << d
        while line = @buf.slice!(/(.*)\r?\n/)
          yield line.chomp if block_given?
        end
      end
    end
  end
  if res.exit_code != 0
    raise "command #{cmd} failed on #{creds[:ip]}:\n#{res.stdout}\n#{res.stderr}"
  end
  res
end

#server_by_name(name) ⇒ Object



45
46
47
48
49
50
# File 'lib/novawhiz.rb', line 45

def server_by_name(name)
  @os.servers.each do |s|
    return @os.server(s[:id]) if s[:name] == name
  end
  nil
end

#wait(timeout, interval = 10) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/novawhiz.rb', line 37

def wait(timeout, interval=10)
  while timeout > 0 do
    return if yield
    sleep interval
    timeout -= interval
  end
end