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
|
# File 'lib/vmm_importer.rb', line 43
def import(selected)
import_tmplt = selected['IMPORT_TEMPLATE']
import_tmplt = Base64.decode64(import_tmplt) if import_tmplt
vm_ref = selected['DEPLOY_ID'] || selected[:wild]['DEPLOY_ID']
vm = selected[:one_item] || build
template = selected[:template] || import_tmplt
template = "DEPLOY_ID = #{vm_ref}\n" + template
host_id = selected[:host] || @list.keys[0]
vc_uuid = @vi_client.vim.serviceContent.about.instanceUuid
vc_name = @vi_client.vim.host
dpool, ipool, npool, hpool = create_pools
vc_vm = VCenterDriver::VirtualMachine.new_without_id(@vi_client,
vm_ref)
if vc_vm.snapshots? && vc_vm.disk_keys.empty?
raise 'Disk metadata not present and snapshots exist, '\
'cannot import this VM'
end
vname = vc_vm['name']
type = { :object => 'VM', :id => vname }
error, template_disks = vc_vm.import_vcenter_disks(vc_uuid, dpool,
ipool, type)
raise error unless error.empty?
template << template_disks
opts = {
:vi_client => @vi_client,
:vc_uuid => vc_uuid,
:npool => npool,
:hpool => hpool,
:vcenter => vc_name,
:template_moref => vm_ref,
:vm_object => vc_vm,
:ipv4 => selected[:ipv4],
:ipv6 => selected[:ipv6]
}
error, template_nics, ar_ids =
vc_vm.import_vcenter_nics(opts)
opts = { :uuid => vc_uuid, :npool => npool, :error => error }
Raction.delete_ars(ar_ids, opts) unless error.empty?
template << template_nics
template << "VCENTER_ESX_HOST = #{vc_vm['runtime.host.name']}\n"
dc_ref = vc_vm.datacenter.item._ref
ds_ref = template.match(/^VCENTER_DS_REF *= *"(.*)" *$/)[1]
ds_one = dpool.select do |e|
e['TEMPLATE/TYPE'] == 'SYSTEM_DS' &&
e['TEMPLATE/VCENTER_DS_REF'] == ds_ref &&
e['TEMPLATE/VCENTER_DC_REF'] == dc_ref &&
e['TEMPLATE/VCENTER_INSTANCE_ID'] == vc_uuid
end.first
opts[:error] = "ds with ref #{ds_ref} is not imported, aborting"
Raction.delete_ars(ar_ids, opts) unless ds_one
rc = vm.allocate(template)
if OpenNebula.is_error?(rc)
Raction.delete_ars(ar_ids, opts.merge(:error => rc.message))
end
rc = vm.deploy(host_id, false, ds_one.id)
if OpenNebula.is_error?(rc)
Raction.delete_ars(ar_ids, opts.merge(:error => rc.message))
end
vc_vm.one_item = vm
request_vnc(vc_vm)
vc_vm.reference_all_disks
vm.id
end
|