1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-19 06:50:07 +03:00

F #1757: new vcenter vm constructors, methods and small fixes

F #1757: commenting new constructors, some visual changes
This commit is contained in:
semedi 2018-02-26 11:35:58 +01:00 committed by Tino Vázquez
parent 78652e3716
commit 33136931af
24 changed files with 161 additions and 77 deletions

View File

@ -67,7 +67,7 @@ end
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
if vm.has_snapshots?
STDERR.puts "'disk-saveas' operation is not supported for VMs with system snapshots."

View File

@ -62,15 +62,16 @@ begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
if !!vm_ref && !vm_ref.empty?
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
else
# we try to get vcenter item
vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client)
# If no VM object retrieved, raise an exception
raise "Could not find the undeployed VM in vCenter's inventory using it's name" if !vcenter_vm
vm_ref = vcenter_vm._ref
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
end
rescue Exception => e
vi_client.close_connection if vi_client

View File

@ -60,7 +60,7 @@ disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
vm.one_item = one_vm

View File

@ -58,7 +58,7 @@ begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid )
# Cannot resize if VM has snapshots
if vm.has_snapshots?

View File

@ -436,7 +436,7 @@ class ClusterComputeResource
vms.each do |vm_ref,info|
begin
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, @vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(@vi_client, vm_ref)
esx_host = esx_hosts[info["runtime.host"]._ref]
info[:esx_host_name] = esx_host[:name]
info[:esx_host_cpu] = esx_host[:cpu]

View File

@ -27,7 +27,7 @@ def self.import_wild(host_id, vm_ref, one_vm, template)
raise "Could not get OpenNebula HostPool: #{hpool.message}"
end
vcenter_vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vcenter_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm_name = vcenter_vm["name"]
wild = true

View File

@ -806,7 +806,7 @@ class Template
# Determine the location path for the template
vcenter_template = VCenterDriver::VirtualMachine.new_from_ref(template_ref, vi_client)
vcenter_template = VCenterDriver::VirtualMachine.new_without_id(vi_client, template_ref)
item = vcenter_template.item
folders = []
while !item.instance_of? RbVmomi::VIM::Datacenter
@ -857,7 +857,7 @@ class Template
# Get the OpenNebula's template hash
one_tmp[:one] = template_to_one(template, vcenter_uuid, template_ccr_ref, template_ccr_name, import_name, host_id)
return one_tmp
rescue
rescue Exception => e
return nil
end
end
@ -883,9 +883,13 @@ class VirtualMachine < Template
include Memoize
def initialize(item=nil, vi_client=nil)
@item = item
def initialize(vi_client, ref, one_id)
if (ref)
@item = RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref)
end
@vi_client = vi_client
@vm_id = one_id
@locking = true
@vm_info = nil
end
@ -909,11 +913,11 @@ class VirtualMachine < Template
# @return OpenNebula::VirtualMachine or XMLElement
def one_item
if !@one_item
vm_id = get_vm_id
raise "Unable to find vm_id." if vm_id.nil?
@one_item = VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
if @vm_id != -1
@one_item = VIHelper.one_item(OpenNebula::VirtualMachine, @vm_id)
else
raise "VCenterDriver::Virtualmachine: OpenNebula ID is mandatory for this vm!"
end
end
@one_item
@ -1133,14 +1137,11 @@ class VirtualMachine < Template
# @param one_item OpenNebula::VirtualMachine
# @param vi_client VCenterDriver::VIClient
# @return String vmware ref
def clone_vm(one_item, vi_client)
@one_item = one_item
@vi_client = vi_client
def clone_vm(drv_action)
vcenter_name = get_vcenter_name
vc_template_ref = one_item['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
vc_template = RbVmomi::VIM::VirtualMachine(vi_client.vim, vc_template_ref)
vc_template_ref = drv_action['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
vc_template = RbVmomi::VIM::VirtualMachine(@vi_client.vim, vc_template_ref)
ds = get_ds
@ -1148,7 +1149,7 @@ class VirtualMachine < Template
disk_move_type = :moveAllDiskBackingsAndDisallowSharing
if ds.instance_of? RbVmomi::VIM::Datastore
use_linked_clones = one_item['USER_TEMPLATE/VCENTER_LINKED_CLONES']
use_linked_clones = drv_action['USER_TEMPLATE/VCENTER_LINKED_CLONES']
if use_linked_clones && use_linked_clones.downcase == "yes"
# Check if all disks in template has delta disks
disks = vc_template.config
@ -1169,7 +1170,7 @@ class VirtualMachine < Template
# Specify vm folder in vSpere's VM and Templates view F#4823
vcenter_vm_folder = nil
vcenter_vm_folder = one_item["USER_TEMPLATE/VCENTER_VM_FOLDER"]
vcenter_vm_folder = drv_action["USER_TEMPLATE/VCENTER_VM_FOLDER"]
vcenter_vm_folder_object = nil
dc = cluster.get_dc
if !!vcenter_vm_folder && !vcenter_vm_folder.empty?
@ -1376,16 +1377,20 @@ class VirtualMachine < Template
xpath = "TEMPLATE/NIC[OPENNEBULA_MANAGED=\"NO\" or OPENNEBULA_MANAGED=\"no\"]"
unmanaged_nics = one_item.retrieve_xmlelements(xpath)
if !unmanaged_nics.empty?
index = 0
self["config.hardware.device"].each_with_index do |device|
if is_nic?(device)
# Edit capacity setting new size in KB
device.macAddress = unmanaged_nics[index]["MAC"]
device_change << { :device => device, :operation => :edit }
index += 1
begin
if !unmanaged_nics.empty?
index = 0
self["config.hardware.device"].each_with_index do |device|
if is_nic?(device)
# Edit capacity setting new size in KB
device.macAddress = unmanaged_nics[index]["MAC"]
device_change << { :device => device, :operation => :edit }
index += 1
end
end
end
rescue Exception => e
raise "There is a problem with your vm NICS, make sure that they are working properly. Error: #{e.message}"
end
# Save in extraconfig the key for unmanaged disks
@ -3080,11 +3085,96 @@ class VirtualMachine < Template
end
end
# TODO check with uuid
def self.new_from_ref(ref, vi_client)
self.new(RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref), vi_client)
# STATIC MEMBERS AND CONSTRUCTORS
###############################################################################################
def self.get_id(opts = {})
id = -1
if (opts[:name])
matches = opts[:name].match(/^one-(\d*)(-(.*))?$/)
id = matches[1] if matches
end
if id == -1
one_vm = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualMachinePool,
"DEPLOY_ID",
opts[:ref],
opts[:vc_uuid],
opts[:pool])
id = one_vm["ID"] if one_vm
end
return id
end
# Try to build the vcenterdriver virtualmachine without
# any opennebula id or object, this constructor can find
# inside the opennebula pool until match
#
# @param vi_client [vi_client] the vcenterdriver client that allows the connection
# @param ref [String] vcenter ref to the vm
# @param opts [Hash] object with pairs that could contain multiple option
# :vc_uuid: give the vcenter uuid directly
# :name: the vcenter vm name for extract the opennebula id
#
# @return [vcenterdriver::vm] the virtual machine
def self.new_from_ref(vi_client, ref, opts = {})
unless opts[:vc_uuid]
opts[:vc_uuid] = vi_client.vim.serviceContent.about.instanceUuid
end
opts[:ref] = ref
vm_id = VCenterDriver::VirtualMachine.get_id(opts)
self.new(vi_client, ref, vm_id)
end
# build a vcenterdriver virtual machine from a template
# this function is used to instantiate vcenter vms
#
# @param vi_client [vi_client] the vcenterdriver client that allows the connection
# @param drv_action [xmleleent] driver_action that contains the info
# @param id [int] the if of the opennebula virtual machine
#
# @return [vcenterdriver::vm] the virtual machine
def self.new_from_clone(vi_client, drv_action, id )
spawn = self.new(vi_client, nil, id).tap do |vm|
vm.clone_vm(drv_action)
end
return spawn
end
# build a vcenterdriver virtual machine
# with the opennebula object linked
#
# @param vi_client [vi_client] the vcenterdriver client that allows the connection
# @param ref [String] vcenter ref to the vm
# @param one_item [one::vm] xmlelement of opennebula
#
# @return [vcenterdriver::vm] the virtual machine
def self.new_one(vi_client, ref, one_item)
id = one_item["ID"] || one_item["VM/ID"] || -1
self.new(vi_client, ref, id).tap do |vm|
vm.one_item = one_item
end
end
# build a vcenterdriver virtual machine
# without opennebula object link, use id = -1 instead
#
# @param vi_client [vi_client] the vcenterdriver client that allows the connection
# @param ref [String] vcenter ref to the vm
#
# @return [vcenterdriver::vm] the virtual machine
def self.new_without_id(vi_client, ref)
self.new(vi_client, ref, -1)
end
###############################################################################################
end # class VirtualMachine
end # module VCenterDriver

View File

@ -42,10 +42,10 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm.one_item = drv_action.retrieve_xmlelements('VM').first
# Setting one_item with info with the vm_xml including DISK to be added
one_item = drv_action.retrieve_xmlelements("VM").first
vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
vm.attach_disk
rescue Exception => e

View File

@ -41,10 +41,10 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
# Setting one_item with info with the vm_xml including NIC to be added
vm.one_item = drv_action.retrieve_xmlelements("VM").first
one_item = drv_action.retrieve_xmlelements("VM").first
vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
vm.attach_nic

View File

@ -42,7 +42,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id)
vm.one_item = drv_action.retrieve_xmlelements('VM').first

View File

@ -44,24 +44,18 @@ host_id = drv_action["HISTORY_RECORDS/HISTORY/HID"]
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
if deploy_id && !deploy_id.empty?
# VM is not new, we just need to reconfigure it and to power it on
vm = VCenterDriver::VirtualMachine.new_from_ref(deploy_id, vi_client)
# Setting one_item is optional, but it saves a couple of API calls if
# we already have it
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
vm.one_item = one_vm
vm = VCenterDriver::VirtualMachine.new_one(vi_client, deploy_id, one_vm)
else
# VM is new
vm = VCenterDriver::VirtualMachine.new
# Clone the VM from template and provide XML info
vc_template_ref = drv_action['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
vm.clone_vm(drv_action, vi_client)
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
vm.one_item = one_vm
# VM is new
# Clone the VM from template and provide XML info
vm = VCenterDriver::VirtualMachine.new_from_clone(vi_client, drv_action, vm_id)
# Set reference to template disks and nics in VM template for detach ops
vm.reference_unmanaged_devices(vc_template_ref)

View File

@ -41,7 +41,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
raise "vCenter doesn't allow to remove a virtual disk if it's part of a "\
"snapshot of the virtual machine." if vm.has_snapshots?
@ -53,4 +53,4 @@ rescue Exception => e
exit -1
ensure
vi_client.close_connection if vi_client
end
end

View File

@ -41,10 +41,9 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
# Setting one_item with info with the vm_xml including NIC to be added
vm.one_item = drv_action.retrieve_xmlelements("VM").first
one_item = drv_action.retrieve_xmlelements("VM").first
vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
vm.detach_nic

View File

@ -54,7 +54,7 @@ begin
dst_host.info
# required vcenter objects
vc_vm = VCenterDriver::VirtualMachine.new_from_ref(vm["/VM/DEPLOY_ID"], vi_client)
vc_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm["/VM/DEPLOY_ID"])
vc_host = VCenterDriver::ClusterComputeResource.new_from_ref(dst_host["/HOST/TEMPLATE/VCENTER_CCR_REF"],vi_client).item
config = {:cluster => vc_host }

View File

@ -32,6 +32,7 @@ require 'vcenter_driver'
vm_ref = ARGV[0]
vc_cluster_name = ARGV[1]
vm_id = ARGV[2]
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name)
host_id = host['ID']
@ -39,7 +40,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vi_client)
vm.monitor_poll_vm

View File

@ -38,7 +38,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.reboot
@ -50,4 +50,4 @@ rescue Exception => e
exit -1
ensure
vi_client.close_connection if vi_client
end
end

View File

@ -31,6 +31,7 @@ require 'vcenter_driver'
vm_ref = ARGV[0]
vc_cluster_name = ARGV[-1]
vm_id = ARGV[-2]
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name)
host_id = host['ID']
@ -38,7 +39,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id)
vm.regenerate_context
@ -50,4 +51,4 @@ rescue Exception => e
exit -1
ensure
vi_client.close_connection if vi_client
end
end

View File

@ -38,7 +38,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.reset
@ -50,4 +50,4 @@ rescue Exception => e
exit -1
ensure
vi_client.close_connection if vi_client
end
end

View File

@ -38,7 +38,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.poweron

View File

@ -57,7 +57,7 @@ end
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.suspend

View File

@ -62,7 +62,7 @@ begin
vm_ref = vcenter_vm._ref
end
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.shutdown #Undeploy, Poweroff or Terminate

View File

@ -44,11 +44,9 @@ snap_name = drv_action["VM/TEMPLATE/SNAPSHOT[ACTIVE='YES']/NAME"]
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
one_vm = drv_action.retrieve_xmlelements("VM").first
vm.one_item = one_vm
vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_vm)
persistent_disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[PERSISTENT=YES]")
@ -69,4 +67,4 @@ rescue Exception => e
exit -1
ensure
vi_client.close_connection if vi_client
end
end

View File

@ -39,7 +39,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.delete_snapshot(snap_id)

View File

@ -39,7 +39,7 @@ host_id = host['ID']
begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
vm.revert_snapshot(snap_id)