mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-23 22:50:09 +03:00
M #-: Refactor code (#1663)
Remove vCenter specific code from CLI/OCA (cherry picked from commit a3b6167a0435fb10300cc0cdbd70e9d8155b545d)
This commit is contained in:
parent
a9f0e0c9b2
commit
59615eefb5
@ -347,7 +347,6 @@ AllCops:
|
||||
- src/oca/ruby/opennebula/template.rb
|
||||
- src/oca/ruby/opennebula/group.rb
|
||||
- src/oca/ruby/opennebula/virtual_network.rb
|
||||
- src/oca/ruby/opennebula/virtual_machine_ext.rb
|
||||
- src/oca/ruby/opennebula/security_group_pool.rb
|
||||
- src/oca/ruby/opennebula/pool_element.rb
|
||||
- src/oca/ruby/opennebula/document_pool.rb
|
||||
|
@ -60,85 +60,6 @@ module OpenNebula::VirtualMachineExt
|
||||
IMAGE_ID IMAGE IMAGE_UNAME IMAGE_UID
|
||||
LN_TARGET TM_MAD TYPE OPENNEBULA_MANAGED]
|
||||
|
||||
def save_as_linked_clones(name, _desc, _opts = {})
|
||||
deploy_id = self['DEPLOY_ID']
|
||||
host_id = self['HISTORY_RECORDS/HISTORY[last()]/HID']
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
|
||||
vm = RbVmomi::VIM::VirtualMachine(vi_client.vim, deploy_id)
|
||||
|
||||
disks = vm.config.hardware.device.grep(
|
||||
RbVmomi::VIM::VirtualMachine
|
||||
)
|
||||
disks.select { |x| x.backing.parent == nil }.each do |disk|
|
||||
spec = {
|
||||
deviceChange: [
|
||||
{
|
||||
operation: :remove,
|
||||
device: disk
|
||||
},
|
||||
{
|
||||
operation: :add,
|
||||
fileOperation: :create,
|
||||
device: disk.dup.tap { |x|
|
||||
x.backing = x.backing.dup
|
||||
x.backing.fileName = "[#{disk.backing.datastore.name}]"
|
||||
x.backing.parent = disk.backing
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
vm.ReconfigVM_Task(spec: spec).wait_for_completion
|
||||
end
|
||||
|
||||
relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(diskMoveType: :moveChildMostDiskBacking)
|
||||
|
||||
spec = RbVmomi::VIM.VirtualMachineCloneSpec(
|
||||
location: relocateSpec,
|
||||
powerOn: false,
|
||||
template: true)
|
||||
|
||||
new_template = vm.CloneVM_Task(
|
||||
folder: vm.parent,
|
||||
name: name,
|
||||
spec: spec
|
||||
).wait_for_completion
|
||||
|
||||
new_vm_template_ref = new_template._ref
|
||||
|
||||
one_client = OpenNebula::Client.new
|
||||
importer = VCenterDriver::VmImporter.new(one_client, vi_client)
|
||||
|
||||
importer.retrieve_resources({})
|
||||
importer.get_indexes(new_vm_template_ref)
|
||||
|
||||
importer.process_import(
|
||||
new_vm_template_ref,
|
||||
{
|
||||
new_vm_template_ref.to_s => {
|
||||
:type => 'default',
|
||||
:linked_clone => '1',
|
||||
:copy => '0',
|
||||
:name => '',
|
||||
:folder => ''
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
begin
|
||||
importer.output[:success][0][:id][0]
|
||||
rescue StandardError => e
|
||||
message = 'Creating linked clones template' \
|
||||
" failed due to \"#{e.message}\".\n"
|
||||
|
||||
if VCenterDriver::CONFIG[:debug_information]
|
||||
message += " #{e.backtrace}\n"
|
||||
end
|
||||
|
||||
raise message
|
||||
end
|
||||
end
|
||||
|
||||
def save_as_template(name, desc, opts = {})
|
||||
opts = {
|
||||
:persistent => false,
|
||||
@ -180,7 +101,22 @@ module OpenNebula::VirtualMachineExt
|
||||
use_linked_clones = self['USER_TEMPLATE/VCENTER_LINKED_CLONES']
|
||||
|
||||
if use_linked_clones && use_linked_clones.downcase == 'yes'
|
||||
return save_as_linked_clones(name, desc, opts)
|
||||
deploy_id = self['DEPLOY_ID']
|
||||
vm_id = self['ID']
|
||||
host_id = self['HISTORY_RECORDS/HISTORY[last()]/HID']
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
|
||||
vm = VCenterDriver::VirtualMachine.new(
|
||||
vi_client,
|
||||
deploy_id,
|
||||
vm_id
|
||||
)
|
||||
|
||||
return vm.save_as_linked_clones(
|
||||
name,
|
||||
deploy_id,
|
||||
vi_client
|
||||
)
|
||||
end
|
||||
|
||||
# --------------------------------------------------------------
|
||||
@ -360,7 +296,7 @@ module OpenNebula::VirtualMachineExt
|
||||
|
||||
raise rc.message if OpenNebula.is_error?(rc)
|
||||
|
||||
binfo.merge!(backup_info) do |key, old_val, new_val|
|
||||
binfo.merge!(backup_info) do |_key, old_val, new_val|
|
||||
new_val.nil? ? old_val : new_val
|
||||
end
|
||||
|
||||
|
@ -1864,6 +1864,90 @@ end
|
||||
end
|
||||
end
|
||||
|
||||
def save_as_linked_clones(name, deploy_id, vi_client)
|
||||
vm = RbVmomi::VIM::VirtualMachine(vi_client.vim, deploy_id)
|
||||
|
||||
disks = vm.config.hardware.device.grep(
|
||||
RbVmomi::VIM::VirtualMachine
|
||||
)
|
||||
disks.select {|x| x.backing.parent.nil? }.each do |disk|
|
||||
spec = {
|
||||
:deviceChange => [
|
||||
{
|
||||
:operation => :remove,
|
||||
:device => disk
|
||||
},
|
||||
{
|
||||
:operation => :add,
|
||||
:fileOperation => :create,
|
||||
:device => disk.dup.tap do |x|
|
||||
x.backing = x.backing.dup
|
||||
x.backing.fileName =
|
||||
"[#{disk.backing.datastore.name}]"
|
||||
x.backing.parent = disk.backing
|
||||
end
|
||||
}
|
||||
]
|
||||
}
|
||||
vm.ReconfigVM_Task(
|
||||
:spec => spec
|
||||
).wait_for_completion
|
||||
end
|
||||
|
||||
relocateSpec = RbVmomi::VIM.VirtualMachineRelocateSpec(
|
||||
:diskMoveType => :moveChildMostDiskBacking
|
||||
)
|
||||
|
||||
spec = RbVmomi::VIM.VirtualMachineCloneSpec(
|
||||
:location => relocateSpec,
|
||||
:powerOn => false,
|
||||
:template => true
|
||||
)
|
||||
|
||||
new_template = vm.CloneVM_Task(
|
||||
:folder => vm.parent,
|
||||
:name => name,
|
||||
:spec => spec
|
||||
).wait_for_completion
|
||||
|
||||
new_vm_template_ref = new_template._ref
|
||||
|
||||
one_client = OpenNebula::Client.new
|
||||
importer = VCenterDriver::VmImporter.new(
|
||||
one_client,
|
||||
vi_client
|
||||
)
|
||||
|
||||
importer.retrieve_resources({})
|
||||
importer.get_indexes(new_vm_template_ref)
|
||||
|
||||
importer.process_import(
|
||||
new_vm_template_ref,
|
||||
{
|
||||
new_vm_template_ref.to_s => {
|
||||
:type => 'default',
|
||||
:linked_clone => '1',
|
||||
:copy => '0',
|
||||
:name => '',
|
||||
:folder => ''
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
begin
|
||||
importer.output[:success][0][:id][0]
|
||||
rescue StandardError => e
|
||||
message = 'Creating linked clones template' \
|
||||
" failed due to \"#{e.message}\".\n"
|
||||
|
||||
if VCenterDriver::CONFIG[:debug_information]
|
||||
message += " #{e.backtrace}\n"
|
||||
end
|
||||
|
||||
raise message
|
||||
end
|
||||
end
|
||||
|
||||
# Returns an array of actions to be included in :deviceChange
|
||||
def calculate_add_nic_spec(nic, unumber = nil)
|
||||
mac = nic['MAC']
|
||||
|
Loading…
x
Reference in New Issue
Block a user