Class: Chef::Knife::XapiVdiAttach

Inherits:
Chef::Knife show all
Includes:
XapiBase
Defined in:
lib/chef/knife/xapi_vdi_attach.rb

Instance Attribute Summary

Attributes included from XapiBase

#defaults

Instance Method Summary collapse

Methods included from XapiBase

#add_vif_by_name, #cleanup, #clear_vm_vifs, #color_kv, #create_vbd, #create_vdi, defaults, #destroy_vdi, #detach_vdi, #fail, #find_default_sr, #find_template, #generate_mac, #get_all_vdis, get_default, #get_guest_ip, #get_host_ref, #get_sr_by_name, #get_sr_by_uuid, #get_task_ref, #get_template, #get_vbd_by_uuid, #get_vbds_from_vdi, #get_vdi_by_name_label, #get_vdi_by_uuid, #h, included, #input_to_bytes, #is_uuid?, #locate_config_value, #print_record, #print_vdi_info, set_defaults, #start, #stop, #user_select, #wait_on_task, #xapi, #yes_no?

Instance Method Details

#runObject



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
# File 'lib/chef/knife/xapi_vdi_attach.rb', line 40

def run
   vm_name = @name_args[0]
   vdi_name = @name_args[1]

   # There is no matchs with VM and VDI's name label
   if vm_name.nil? || vdi_name.nil?
	    ui.msg 'Error: No VM Name or VDI Name specified...'
     ui.msg 'Usage: ' + banner
	    exit 1
   end

   # Get VM's ref from its name label
   vm_ref = xapi.VM.get_by_name_label(vm_name)
   if vm_ref.empty?
     ui.msg ui.color "Could not find a vm named #{vm_name}", :red
     exit 1
   end
   vm_ref = vm_ref.shift

   # Get VDI's ref from its name label or UUID
   vdis = []
   if config[:uuid]
     vdis << xapi.VDI.get_by_uuid(vdi_name)
   else
     vdis = xapi.VDI.get_by_name_label(vdi_name)
   end

   if vdis.empty?
     ui.msg "VDI not found: #{h.color vdi_name, :red}"
     exit 1
     # When multiple VDI matches
     Chef::Log.debug "VDI Length: #{vdis.inspect}\nType:#{vdi.class}"
    elsif vdis.length > 1
     ui.msg 'Multiple VDI matches found use guest list if you are unsure'
     vdi_ref = user_select(vdis)
   else
     vdi_ref = vdis.first
   end

   position = xapi.VM.get_VBDs(vm_ref).length

   # Attach intended VDI to specific VM
   if vdi_ref == :all
     vdis.each do |vdi_ref|
       create_vbd(vm_ref, vdi_ref, position, config[:boot])
       position += 1
     end
   else
     create_vbd(vm_ref, vdi_ref, position, config[:boot])
   end
end