1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

B #1762: instantiate to persistent basic refactor

B #1762: mvds for instantiate as persistent
This commit is contained in:
sergio semedi 2018-11-23 14:52:33 +01:00 committed by Ruben S. Montero
parent 5fcb9910bc
commit 17334789d4
5 changed files with 55 additions and 19 deletions

View File

@ -731,6 +731,7 @@ ImageTemplate * Image::clone_template(const string& new_name) const
tmpl->replace("PATH", source);
tmpl->replace("FSTYPE", fs_type);
tmpl->replace("SIZE", size_mb);
tmpl->erase("VCENTER_IMPORTED");
if ( is_persistent() )
{

View File

@ -184,7 +184,7 @@ Request::ErrorCode VMTemplateClone::clone(int source_id, const string &name,
oss << name << "-disk-" << ndisk;
ec = img_clone.request_execute(img_id, oss.str(), -1,
(*disk)->is_managed(), new_img_id, img_att);
true, new_img_id, img_att);
if ( ec != SUCCESS)
{

View File

@ -95,11 +95,11 @@ begin
vm.poweroff_hard if vm.is_powered_on?
vm.remove_all_snapshots if vm.has_snapshots?
vm.disks.each do |disk_id, disk|
vm.ndetach_disk(disk) if disk.persistent?
if vm.instantiated_as_persistent?
vm.convert_to_template
else
vm.destroy
end
vm.destroy
end
rescue Exception => e

View File

@ -60,21 +60,20 @@ disk_id = img_path.split(".")[-1]
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
disk = vm.disk(disk_id)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
disk = vm.disk(disk_id)
vmperst = vm.instantiated_as_persistent?
if disk
# Don't detach persistent disks if the VM has snapshots
if disk && !vm.has_snapshots?
vm.one_item = one_vm
# Don't detach persistent disks if the VM has snapshots
# or the instantiate to persistent is used, we need the entire template and save it in delete process!
if !vm.has_snapshots? && !vm.instantiated_as_persistent?
vm.ndetach_disk(disk)
# Do not detach persistent unmanaged disk, we need them for mark as a template
vm.ndetach_disk(disk) if disk.managed? || !vmperst
if disk.cloned?
ds = VCenterDriver::Datastore.new(disk.ds)
ds.move_virtual_disk(disk, im_path, dsid, vi_client)
end
if disk.cloned?
ds = VCenterDriver::Datastore.new(disk.ds)
ds.move_virtual_disk(disk, im_path, dsid, vi_client)
end
end

View File

@ -672,7 +672,7 @@ class VirtualMachine < VCenterDriver::Template
# @item is populated
@item = vm
reference_unmanaged_devices(vc_template_ref)
reference_unmanaged_devices(vc_template_ref) unless instantiated_as_persistent?
return self['_ref']
end
@ -982,7 +982,7 @@ class VirtualMachine < VCenterDriver::Template
vcenter_disk = vcenter_disks.select{|d| d[:key] == template_disk[:key] && d[:device].deviceInfo.summary == template_disk[:device].deviceInfo.summary}.first
end
raise "disk with path #{unmanaged_disk_source} not found in the vCenter VM" if !defined?(vcenter_disk) || vcenter_disk.empty?
raise "disk with path #{unmanaged_disk_source} not found in the vCenter VM" if !vcenter_disk
reference = {}
reference[:key] = "opennebula.disk.#{unmanaged_disk["DISK_ID"]}"
@ -2187,6 +2187,40 @@ class VirtualMachine < VCenterDriver::Template
end
end
# Remove the MAC addresses so they cannot be in conflict
# with OpenNebula assigned mac addresses.
# We detach all nics from the VM
def convert_to_template()
detach_all_nics
# We attach new NICs where the MAC address is assigned by vCenter
nic_specs = []
nics = one_item.retrieve_xmlelements("TEMPLATE/NIC")
nics.each do |nic|
if (nic["OPENNEBULA_MANAGED"] && nic["OPENNEBULA_MANAGED"].upcase == "NO")
nic_specs << calculate_add_nic_spec_autogenerate_mac(nic)
end
end
# Reconfigure VM to add unmanaged nics
spec_hash = {}
spec_hash[:deviceChange] = nic_specs
spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash)
@item.ReconfigVM_Task(:spec => spec).wait_for_completion
# Convert VM to template in vCenter
mark_as_template
# Edit the Opennebula template
one_client = OpenNebula::Client.new
template_id = one_item['TEMPLATE/TEMPLATE_ID']
new_template = OpenNebula::Template.new_with_id(template_id, one_client)
new_template.info
# Update the template reference
new_template.update("VCENTER_TEMPLATE_REF=#{@item._ref}", true)
end
# TODO
def nresize_unmanaged_disks
spec = {deviceChange: []}
@ -2195,7 +2229,9 @@ class VirtualMachine < VCenterDriver::Template
spec[:deviceChange] << d.config(:resize)
end
@item.ReconfigVM_Task(:spec => spec).wait_for_completion
if !spec[:deviceChange].empty?
@item.ReconfigVM_Task(:spec => spec).wait_for_completion
end
end
#TODO