-
Notifications
You must be signed in to change notification settings - Fork 9
/
xapi_guest_create.rb
431 lines (368 loc) · 15.7 KB
/
xapi_guest_create.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#
# Author:: Jesse Nelson (<spheromak@gmail.com>)
#
# Copyright:: Copyright (c) 2012 Jesse Nelson
#
# License:: Apache License, Version 2.0
#
# 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 'chef/knife/xapi_base'
class Chef
class Knife
class XapiGuestCreate < Knife
require 'timeout'
include Chef::Knife::XapiBase
Chef::Knife::XapiBase.set_defaults(
domain: '',
ssh_user: 'root',
ssh_port: '22',
ping_timeout: 600,
connect_timeout: 600,
install_repo: 'http://isoredirect.centos.org/centos/6/os/x86_64/',
kernel_params: 'graphical utf8',
xapi_disk_size: '8g',
xapi_cpus: '1',
xapi_mem: '1g',
bootstrap_template: 'chef-full',
template_file: false,
run_list: [],
json_attributes: {}
)
deps do
require 'chef/knife/bootstrap'
Chef::Knife::Bootstrap.load_deps
end
banner 'knife xapi guest create NAME [NETWORKS] (options)'
option :xapi_vm_template,
short: '-T Template Name Label',
long: '--xapi-vm-template',
proc: proc { |key| Chef::Config[:knife][:xapi_vm_template] = key },
description: 'xapi template name to create from. accepts an string or regex'
option :install_repo,
short: "-R If you're using a builtin template you will need to specify a repo url",
long: '--install-repo',
description: 'Install repo for this template (if needed)',
proc: proc { |key| Chef::Config[:knife][:install_repo] = key }
option :macaddress,
short: '-m MAC Address',
long: '--mac-address',
description: 'Use a pre-generated MAC address (optional)',
proc: proc { |key| Chef::Config[:knife][:macaddress] = key }
option :xapi_sr,
short: '-S Storage repo to provision VM from',
long: '--xapi-sr',
proc: proc { |key| Chef::Config[:knife][:xapi_sr] = key },
description: 'The Xen SR to use, If blank will use pool/hypervisor default'
option :kernel_params,
short: '-B Set of kernel boot params to pass to the vm',
long: '--kernel-params',
description: "You can add more boot options to the vm e.g.: \"ks='http://foo.local/ks'\"",
proc: proc { |key| Chef::Config[:knife][:kernel_params] = key }
option :xapi_skip_disk,
long: '--xapi-skip-disk',
proc: proc { |key| Chef::Config[:knife][:xapi_skip_disk] = key },
description: "Don't try to add disks to the new VM"
option :xapi_disk_size,
short: '-D Size of disk. 1g 512m etc',
long: '--xapi-disk-size',
description: "The size of the root disk, use 'm' 'g' 't' if no unit specified assumes g",
proc: proc { |key| Chef::Config[:knife][:xapi_disk_size] = key.to_s }
option :xapi_cpus,
short: '-C Number of VCPUs to provision',
long: '--xapi-cpus',
description: 'Number of VCPUS this vm should have 1 4 8 etc',
proc: proc { |key| Chef::Config[:knife][:xapi_cpus] = key.to_s }
option :xapi_mem,
short: '-M Ammount of memory to provision',
long: '--xapi-mem',
description: 'Ammount of memory the VM should have specify with m g etc 512m, 2g if no unit spcified it assumes gigabytes',
proc: proc { |key| Chef::Config[:knife][:xapi_mem] = key.to_s }
option :chef_node_name,
short: '-N NAME',
long: '--node-name NAME',
description: 'The Chef node name for your new node'
option :ssh_key_name,
short: '-S KEY',
long: '--ssh-key KEY',
proc: proc { |key| Chef::Config[:knife][:ssh_key_name] = key },
description: 'The SSH key id'
option :ssh_user,
short: '-x USERNAME',
long: '--ssh-user USERNAME',
description: 'The ssh username',
proc: proc { |key| Chef::Config[:knife][:ssh_user] = key }
option :ssh_password,
short: '-P PASSWORD',
long: '--ssh-password PASSWORD',
proc: proc { |key| Chef::Config[:knife][:ssh_password] = key },
description: 'The ssh password'
option :ssh_port,
short: '-p PORT',
long: '--ssh-port PORT',
description: 'The ssh port'
option :ping_timeout,
long: '--ping-timeout SECONDS',
description: 'Seconds to timeout waiting for ip from guest'
option :connect_timeout,
long: '--connect-timeout SECONDS',
description: 'Seconds to timeout while trying to connect to a guest'
option :bootstrap_version,
long: '--bootstrap-version VERSION',
description: 'The version of Chef to install'
option :bootstrap_template,
short: '-d Template Name',
long: '--bootstrap-template Template Name',
description: 'Bootstrap using a specific template'
option :template_file,
short: '-F FILEPATH',
long: '--template-file TEMPLATE',
description: 'Full path to location of template to use'
option :json_attributes,
short: '-j JSON_ATTRIBS',
long: '--json-attributes',
description: 'A JSON string to be added to the first run of chef-client',
proc: lambda { |o| JSON.parse(o) }
option :run_list,
short: '-r RUN_LIST',
long: '--run-list RUN_LIST',
description: 'Comma separated list of roles/recipes to apply',
proc: lambda { |o| o.split(/[\s,]+/) }
def tcp_test_ssh(hostname)
tcp_socket = TCPSocket.new(hostname, locate_config_value(:ssh_port))
readable = IO.select([tcp_socket], nil, nil, 5)
if readable
Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
yield
true
else
false
end
rescue SocketError
sleep 2
false
rescue Errno::ETIMEDOUT
false
rescue Errno::EPERM
false
rescue Errno::ECONNREFUSED
sleep 2
false
rescue Errno::EHOSTUNREACH
sleep 2
false
ensure
tcp_socket && tcp_socket.close
end
def wait_for_guest_ip(vm_ref)
guest_ip = get_guest_ip(vm_ref)
if pingable?(guest_ip)
ui.msg "found guest_ip = #{guest_ip}"
return guest_ip
else
ui.msg "#{guest_ip} is not pingable, trying to get guest ip again"
guest_ip = get_guest_ip(vm_ref)
ui.msg "found guest_ip = #{guest_ip}"
return guest_ip
end
end
def pingable?(guest_ip, timeout = 5)
sleep(20)
system "ping -c 1 -t #{timeout} #{guest_ip} >/dev/null"
end
def get_guest_ip(vm_ref)
timeout(locate_config_value(:ping_timeout).to_i) do
ui.msg 'Waiting for guest ip address'
guest_ip = ''
while guest_ip.empty?
print('.')
sleep @initial_sleep_delay ||= 10
vgm = xapi.VM.get_guest_metrics(vm_ref)
next if 'OpaqueRef:NULL' == vgm
networks = xapi.VM_guest_metrics.get_networks(vgm)
if networks.key?('0/ip')
guest_ip = networks['0/ip']
end
end
puts "\n"
return guest_ip
end
rescue Timeout::Error
ui.msg 'Timeout waiting for XAPI to report IP address '
end
def resolve_sr_ref
ui_sr = locate_config_value(:xapi_sr)
sr_ref = nil
if locate_config_value(:xapi_sr)
if is_uuid?(ui_sr)
sr_ref = get_sr_by_uuid(ui_sr)
else
sr_ref = get_sr_by_name(ui_sr)
end
else
sr_ref = find_default_sr
end
if sr_ref.nil?
ui.error "SR specified not found or can't be used Aborting"
fail(vm_ref) if sr_ref.nil?
end
sr_ref
end
def run
server_name = @name_args[0]
domainname = locate_config_value(:domain)
if domainname.empty?
fqdn = server_name
else
fqdn = "#{server_name}.#{domainname}"
end
# get the template vm we are going to build from
template_ref = find_template(locate_config_value(:xapi_vm_template))
sr_ref = resolve_sr_ref
if locate_config_value(:xapi_sr)
Chef::Log.debug "Copying Guest from Template: #{h.color(template_ref, :bold, :cyan)}"
task = xapi.Async.VM.copy(template_ref, fqdn, sr_ref)
else
Chef::Log.debug "Cloning Guest from Template: #{h.color(template_ref, :bold, :cyan)}"
task = xapi.Async.VM.clone(template_ref, fqdn)
end
ui.msg 'Waiting on Template Clone'
vm_ref = get_task_ref(task)
Chef::Log.debug "New VM ref: #{vm_ref}"
# TODO: lift alot of this
begin
xapi.VM.set_name_description(vm_ref, "VM from knife-xapi as #{server_name} by #{ENV['USER']}")
# configure the install repo
repo = locate_config_value(:install_repo)
# make sure we don't clobber existing params
other_config = {}
record = xapi.VM.get_record(vm_ref)
if record.key? 'other_config'
other_config = record['other_config']
end
other_config['install-repository'] = repo
# remove any disk config/xml template might be trying to do (ubuntu)
other_config.delete_if {|k,v| k=="disks"}
Chef::Log.debug "Other_config: #{other_config.inspect}"
xapi.VM.set_other_config(vm_ref, other_config)
cpus = locate_config_value(:xapi_cpus).to_s
xapi.VM.set_VCPUs_max(vm_ref, cpus)
xapi.VM.set_VCPUs_at_startup(vm_ref, cpus)
memory_size = input_to_bytes(locate_config_value(:xapi_mem)).to_s
# static-min <= dynamic-min = dynamic-max = static-max
xapi.VM.set_memory_limits(vm_ref, memory_size, memory_size, memory_size, memory_size)
#
# setup the Boot args
#
boot_args = locate_config_value(:kernel_params)
# if no hostname param set hostname to given vm name
boot_args << " hostname=#{server_name}" unless boot_args.match(/hostname=.+\s?/)
# if domainname is supplied we put that in there as well
# ubuntu/debian wants domain rhat wants dnsdomain
boot_args << " domain=#{domainname}" unless boot_args.match(/domain=.+\s?/)
boot_args << " dnsdomain=#{domainname}" unless boot_args.match(/dnsdomain=.+\s?/)
xapi.VM.set_PV_args(vm_ref, boot_args)
# TODO: validate that the vm gets a network here
networks = @name_args[1..-1]
# if the user has provided networks
if networks.length >= 1
clear_vm_vifs(xapi.VM.get_record(vm_ref))
networks.each_with_index do |net, index|
add_vif_by_name(vm_ref, index, net)
end
end
Chef::Log.debug "SR: #{h.color sr_ref, :cyan}"
unless locate_config_value(:xapi_skip_disk)
disk_size = locate_config_value(:xapi_disk_size)
# setup disks
if !disk_size.nil? && disk_size.to_i > 0
# when a template already has disks, we decide the position number based on it.
position = xapi.VM.get_VBDs(vm_ref).length
# Create the VDI
vdi_ref = create_vdi("#{server_name}-root", sr_ref, locate_config_value(:xapi_disk_size))
fail(vm_ref) unless vdi_ref
# Attach the VDI to the VM
# if its position is 0 set it bootable
position == 0 ? bootable = true : bootable = false
vbd_ref = create_vbd(vm_ref, vdi_ref, position, bootable)
fail(vm_ref) unless vbd_ref
end
end
ui.msg "Provisioning new Guest: #{h.color(fqdn, :bold, :cyan)}"
ui.msg "Boot Args: #{h.color boot_args, :bold, :cyan}"
ui.msg "Install Repo: #{ h.color(repo, :bold, :cyan)}"
ui.msg "Memory: #{ h.color(locate_config_value(:xapi_mem).to_s, :bold, :cyan)}"
ui.msg "CPUs: #{ h.color(locate_config_value(:xapi_cpus).to_s, :bold, :cyan)}"
ui.msg "Disk: #{ h.color(disk_size.to_s, :bold, :cyan)}"
provisioned = xapi.VM.provision(vm_ref)
ui.msg "Starting new Guest #{h.color(provisioned, :cyan)} "
start(vm_ref)
exit 0 unless locate_config_value(:run_list)
rescue => e
ui.msg "#{h.color 'ERROR:'} #{h.color(e.message, :red)}"
# have to use join here to pass a string to highline
puts 'Nested backtrace:'
ui.msg "#{h.color(e.backtrace.join("\n"), :yellow)}"
raise(vm_ref)
end
if locate_config_value(:run_list).empty?
unless locate_config_value(:template_file) or
locate_config_value(:bootstrap_template) != 'chef-full'
exit 0
end
end
guest_addr = wait_for_guest_ip(vm_ref)
if guest_addr.nil? || guest_addr.empty?
ui.msg('ip seems wrong using host+domain name instead')
guest_addr = "#{server_name}.#{domainname}"
end
ui.msg "Trying to connect to guest @ #{guest_addr} "
begin
timeout(locate_config_value(:connect_timeout).to_i) do
print('.') until tcp_test_ssh(guest_addr) do
sleep @initial_sleep_delay ||= 10
ui.msg("#{ h.color 'OK!', :green}")
end
end
rescue Timeout::Error
ui.msg 'Timeout trying to login Wont bootstrap'
raise
end
begin
bootstrap = Chef::Knife::Bootstrap.new
bootstrap.name_args = [guest_addr]
bootstrap.config[:run_list] = locate_config_value(:run_list)
bootstrap.config[:ssh_user] = locate_config_value(:ssh_user)
bootstrap.config[:ssh_port] = locate_config_value(:ssh_port)
bootstrap.config[:ssh_password] = locate_config_value(:ssh_password)
bootstrap.config[:identity_file] = locate_config_value(:identity_file)
bootstrap.config[:chef_node_name] = config[:chef_node_name] || fqdn
bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes)
bootstrap.config[:distro] = locate_config_value(:bootstrap_template)
bootstrap.config[:use_sudo] = true unless locate_config_value(:ssh_user) == 'root'
bootstrap.config[:template_file] = locate_config_value(:template_file)
bootstrap.config[:environment] = config[:environment]
bootstrap.config[:host_key_verify] = false
bootstrap.config[:run_list] = locate_config_value(:run_list)
bootstrap.run
rescue => e
ui.msg "#{h.color 'ERROR:'} #{h.color(e.message, :red)}"
puts 'Nested backtrace:'
ui.msg "#{h.color(e.backtrace.join("\n"), :yellow)}"
raise
end
end
end
end
end