/
opennebula.rb
205 lines (179 loc) · 6.96 KB
/
opennebula.rb
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- encoding: utf-8 -*-
#
# Author:: Andrew J. Brown (<anbrown@blackberry.com>)
#
# Copyright (C) 2014, BlackBerry, Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'fog'
require 'kitchen'
module Kitchen
module Driver
# Opennebula driver for Kitchen.
#
# @author Andrew J. Brown <anbrown@blackberry.com>
class Opennebula < Kitchen::Driver::Base
default_config :opennebula_endpoint,
ENV.fetch('ONE_XMLRPC', 'http://127.0.0.1:2633/RPC2')
default_config :oneauth_file,
ENV.fetch('ONE_AUTH', "#{ENV['HOME']}/.one/one_auth")
default_config :vm_hostname do |driver|
randstr = 8.times.collect{[*'a'..'z',*('0'..'9')].sample}.join
"#{driver.instance.name}-#{randstr}"
end
default_config :public_key_path do
[
File.expand_path('~/.ssh/id_rsa.pub'),
File.expand_path('~/.ssh/id_dsa.pub'),
File.expand_path('~/.ssh/identity.pub'),
File.expand_path('~/.ssh/id_ecdsa.pub')
].find { |path| File.exist?(path) }
end
default_config :username, 'local'
default_config :memory, 512
default_config :vcpu, 1
default_config :cpu, 1
default_config :user_variables, { }
default_config :context_variables, { }
default_config :wait_for, 600
default_config :no_ssh_tcp_check, false
default_config :no_ssh_tcp_check_sleep, 120
default_config :no_passwordless_sudo_check, false
default_config :no_passwordless_sudo_sleep, 120
def initialize(config)
super
Fog.timeout = config[:wait_for].to_i
end
def create(state)
conn = opennebula_connect
# Ensure we can authenticate with OpenNebula
rc = conn.client.get_version
raise(rc.message) if OpenNebula.is_error?(rc)
# Check if VM is already created.
if state[:vm_id] && !conn.list_vms({:id => state[:vm_id]}).empty?
info("OpenNebula instance #{instance.to_str} already created.")
return
end
if config[:template_id].nil? and config[:template_name].nil?
raise "template_name or template_id not specified in .kitchen.yml"
elsif !config[:template_id].nil? and !config[:template_name].nil?
raise "Only one of template_name or template_id should be specified in .kitchen.yml"
end
newvm = conn.servers.new
if config[:template_id]
newvm.flavor = conn.flavors.get config[:template_id]
elsif config[:template_name]
filter = {
:name => config[:template_name],
:uname => config[:template_uname],
:uid => config[:template_uid]
}
newvm.flavor = conn.flavors.get_by_filter filter
if !newvm.flavor.nil? and newvm.flavor.length > 1
raise 'More than one template found. Please restrict using template_uname'
end
newvm.flavor = newvm.flavor.first unless newvm.flavor.nil?
end
if newvm.flavor.nil?
raise "Could not find template to create VM. -- Verify your template filters and one_auth credentials"
end
newvm.name = config[:vm_hostname]
newvm.flavor.user_variables = {} if newvm.flavor.user_variables.nil? || newvm.flavor.user_variables.empty?
config[:user_variables].each do |key, val|
newvm.flavor.user_variables[key.to_s] = val
end
newvm.flavor.context = {} if newvm.flavor.context.nil? || newvm.flavor.context.empty?
newvm.flavor.context['SSH_PUBLIC_KEY'] = File.read(config[:public_key_path]).chomp
newvm.flavor.context['TEST_KITCHEN'] = "YES"
# Support for overriding context variables in the VM template
config[:context_variables].each do |key, val|
newvm.flavor.context[key.to_s] = val
end
newvm.flavor.memory = config[:memory]
newvm.flavor.vcpu = config[:vcpu]
newvm.flavor.cpu = config[:cpu]
# TODO: Set up NIC and disk if not specified in template
vm = newvm.save
vm.wait_for { ready? }
state[:vm_id] = vm.id
state[:hostname] = vm.ip
state[:username] = config[:username]
tcp_check(state)
passwordless_sudo_check(state)
info("OpenNebula instance #{instance.to_str} created.")
end
def tcp_check(state)
instance.transport.connection(state).wait_until_ready unless config[:no_ssh_tcp_check]
sleep(config[:no_ssh_tcp_check_sleep]) if config[:no_ssh_tcp_check]
debug("SSH ready on #{instance.to_str}")
end
def passwordless_sudo_check(state)
if config[:no_passwordless_sudo_check]
sleep(config[:no_passwordless_sudo_sleep])
else
wait_for_passwordless_sudo(state)
end
debug("Passwordless sudo ready on #{instance.to_str}")
end
def wait_for_passwordless_sudo(state)
retries = config[:passwordless_sudo_timeout] || 300
retry_interval = config[:passwordless_sudo_retry_interval] || 10
begin
instance.transport.connection(state) do |conn|
conn.execute('sudo -n true')
end
rescue Kitchen::Transport::SshFailed => e
if (e.message.eql? "SSH exited (1) for command: [sudo -n true]") && (retries >= 0)
sleep retry_interval
retry
end
raise ActionFailed, e.message
end
end
def converge(state)
super
end
def verify(state)
super
end
def destroy(state)
conn = opennebula_connect
conn.servers.destroy(state[:vm_id])
end
protected
def opennebula_connect()
opennebula_creds = nil
if ENV.has_key?('ONE_AUTH')
if File.exist?(ENV['ONE_AUTH'])
opennebula_creds = File.read(ENV['ONE_AUTH'])
else
opennebula_creds = ENV['ONE_AUTH']
end
elsif File.exist?(config[:oneauth_file])
opennebula_creds = File.read(config[:oneauth_file])
else
raise ActionFailed, "Could not find one_auth file #{config[:oneauth_file]}"
end
opennebula_username = opennebula_creds.split(':')[0]
opennebula_password = opennebula_creds.split(':')[1]
conn = Fog::Compute.new( {
:provider => 'OpenNebula',
:opennebula_username => opennebula_username,
:opennebula_password => opennebula_password,
:opennebula_endpoint => config[:opennebula_endpoint]
} )
conn
end
end
end
end