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
|
# File 'lib/chef/knife/xapi_guest_create.rb', line 209
def run
server_name = @name_args[0]
domainname = locate_config_value(:domain)
if domainname.empty?
fqdn = server_name
else
fqdn = "#{server_name}.#{domainname}"
end
template_ref = find_template( locate_config_value(:xapi_vm_template) )
Chef::Log.debug "Cloning Guest from Template: #{h.color(template_ref, :bold, :cyan )}"
task = xapi.Async.VM.clone(template_ref, fqdn)
ui.msg "Waiting on Template Clone"
vm_ref = get_task_ref(task)
Chef::Log.debug "New VM ref: #{vm_ref}"
begin
xapi.VM.set_name_description(vm_ref, "VM from knife-xapi as #{server_name} by #{ENV['USER']}")
repo = locate_config_value(:install_repo)
other_config = Hash.new
record = xapi.VM.get_record(vm_ref)
if record.has_key? "other_config"
other_config = record["other_config"]
end
other_config["install-repository"] = repo
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
xapi.VM.set_memory_limits(vm_ref, memory_size, memory_size, memory_size, memory_size)
boot_args = locate_config_value(:kernel_params)
boot_args << " hostname=#{server_name}" unless boot_args.match(/hostname=.+\s?/)
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 )
networks = @name_args[1..-1]
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
unless locate_config_value(:xapi_skip_disk)
sr_ref = nil
if locate_config_value(:xapi_sr)
sr_ref = get_sr_by_name( locate_config_value(:xapi_sr) )
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
Chef::Log.debug "SR: #{h.color sr_ref, :cyan}"
disk_size = locate_config_value(:xapi_disk_size)
if disk_size != nil and disk_size.to_i > 0
position = xapi.VM.get_VBDs(vm_ref).length
vdi_ref = create_vdi("#{server_name}-root", sr_ref, locate_config_value(:xapi_disk_size) )
fail(vm_ref) unless vdi_ref
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 Exception => e
ui.msg "#{h.color 'ERROR:'} #{h.color( e.message, :red )}"
puts "Nested backtrace:"
ui.msg "#{h.color( e.backtrace.join("\n"), :yellow)}"
fail(vm_ref)
end
if locate_config_value(:run_list).empty?
unless ( locate_config_value(:template_file) or locate_config_value(:bootstrap_template) )
exit 0
end
end
guest_addr = wait_for_guest_ip(vm_ref)
if guest_addr.nil? or 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(480) do
print(".") until tcp_test_ssh(guest_addr) {
sleep @initial_sleep_delay ||= 10
ui.msg( "#{ h.color "OK!", :green}" )
}
end
rescue Timeout::Error
ui.msg "Timeout trying to login Wont bootstrap"
fail
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 Exception => e
ui.msg "#{h.color 'ERROR:'} #{h.color( e.message, :red )}"
puts "Nested backtrace:"
ui.msg "#{h.color( e.backtrace.join("\n"), :yellow)}"
fail
end
end
|