class XenApi
XenApi Session Manager
Public Class Methods
Initalize the API by login to XenServer.
server_path
-
Server Address
server_port
-
Server API Port, useful while oeprate over SSH
username
-
Username, usually root
password
-
Password, the password!
# File xenapi.rb, line 32 def initialize(server_path, server_port = 443, use_ssl = true, username = 'root', password) # This is where the connection is made # https://stelfox.net/blog/2012/02/rubys-xmlrpc-client-and-ssl/ connection_param = { host: server_path, port: server_port, use_ssl: use_ssl, path: '/' } @connect = XMLRPC::Client.new_from_hash(connection_param) # This is the SSL Check Bypassing Mechanism @connect.http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless use_ssl == false @session = @connect.call('session.login_with_password', username, password)['Value'] end
Public Instance Methods
Tag a network
network_uuid
-
OpaqueRef of the Network
tag
-
the name tag
# File xenapi.rb, line 1010 def network_add_tag(network_uuid, tag) network_opaqueref = network_get_ref(network_uuid) network_opaqueref.key?('Value') ? @connect.call('network.add_tags', @session, network_opaqueref['Value'], tag) : network_opaqueref end
Create a Internal Network
name
-
Name of the Network
# File xenapi.rb, line 987 def network_create(name) vbd_object = { name_label: name, MTU: 1500, other_config: {} } result = @connect.call('network.create', @session, vbd_object) result['Value'] = network_get_uuid(result['Value'])['Value'] result end
Destroy a Internal Network
network_uuid
-
OpaqueRef of the Network
# File xenapi.rb, line 1001 def network_destroy(network_uuid) network_opaqueref = network_get_ref(network_uuid) network_opaqueref.key?('Value') ? @connect.call('network.destroy', @session, network_opaqueref['Value']) : network_opaqueref end
Get details of the network
# File xenapi.rb, line 979 def network_get_detail(network_uuid) network_opaqueref = network_get_ref(network_uuid) network_opaqueref.key?('Value') ? @connect.call('network.get_record', @session, network_opaqueref['Value']) : network_opaqueref end
Get Network OpaqueRef
# File xenapi.rb, line 1054 def network_get_ref(network_uuid) @connect.call('network.get_by_uuid', @session, network_uuid) end
Get Network uuid
# File xenapi.rb, line 1048 def network_get_uuid(network_opaqueref) @connect.call('network.get_uuid', @session, network_opaqueref) end
List all network
# File xenapi.rb, line 969 def network_list result = @connect.call('network.get_all', @session) result['Value'].map! do |ref| network_get_uuid(ref)['Value'] end result end
UNTag a network
network_uuid
-
OpaqueRef of the Network
tag
-
the name tag
# File xenapi.rb, line 1019 def network_rm_tag(network_uuid, tag) network_opaqueref = network_get_ref(network_uuid) network_opaqueref.key?('Value') ? @connect.call('network.remove_tags', @session, network_opaqueref['Value'], tag) : network_opaqueref end
Search network by Tag
tag
-
the name tag
# File xenapi.rb, line 1035 def network_search_by_tag(tag) networks = network_list['Value'] networks.select! do |network_opaqueref| network_get_tags(network_opaqueref)['Value'].include?(tag) end networks.map! do |ref| network_get_uuid(ref)['Value'] end Messages.success_nodesc_with_payload(networks) end
Logout
# File xenapi.rb, line 50 def session_logout @connect.call('session.logout', @session) end
Add a tag to the specified VDI
sr_uuid
-
SR UUID
tag
-
Tag
# File xenapi.rb, line 1109 def sr_add_tag(sr_uuid, tag) sr_opaqueref = sr_get_ref(sr_uuid) sr_opaqueref.key?('Value') ? @connect.call('VDI.add_tags', @session, vdi_get_ref(vdi_uuid)['Value'], tag) : sr_opaqueref end
Find Default SR
# File xenapi.rb, line 1126 def sr_get_default default_pool = @connect.call('pool.get_all', @session) default_sr = @connect.call('pool.get_default_SR', @session, default_pool['Value'][0]) if default_pool['Status'] == 'Success' res_hash = { 'UUID' => sr_get_uuid(default_sr['Value'])['Value'], 'REF' => default_sr['Value'] } Messages.success_nodesc_with_payload(res_hash) end
Get SR Record
sr_uuid
-
UUID of SR
# File xenapi.rb, line 1086 def sr_get_record(sr_uuid) sr_opaqueref = sr_get_ref(sr_uuid) sr_opaqueref.key?('Value') ? @connect.call('SR.get_record', @session, sr_opaqueref['Value']) : sr_opaqueref end
Get Network OpaqueRef
# File xenapi.rb, line 1167 def sr_get_ref(sr_uuid) @connect.call('SR.get_by_uuid', @session, sr_uuid) end
Get SR Connection by uuid
# File xenapi.rb, line 1093 def sr_get_type(sr_uuid) sr_opaqueref = sr_get_ref(sr_uuid) sr_opaqueref.key?('Value') ? @connect.call('SR.get_type', @session, sr_opaqueref['Value']) : sr_opaqueref end
Get SR Connection type by ref
# File xenapi.rb, line 1100 def sr_get_type2(sr_opaqueref) @connect.call('SR.get_type', @session, sr_opaqueref) end
Get Network uuid
# File xenapi.rb, line 1161 def sr_get_uuid(sr_opaqueref) @connect.call('SR.get_uuid', @session, sr_opaqueref) end
List all SRs in the system
iso_sr
-
include SRs for ISO file? 'include' // 'exclude' // 'only'
uuid_mode
-
results in UUID? true / false
# File xenapi.rb, line 1066 def sr_list(iso_sr, uuid_mode) all_sr = @connect.call('SR.get_all', @session) if iso_sr == 'exclude' all_sr['Value'].select! do |opaqueref| check_sr_is_vdisr_by_ref(opaqueref) if iso_sr == 'exclude' check_sr_is_iso(opaqueref) if iso_sr == 'only' true if iso_sr == 'include' end end if uuid_mode == true all_sr['Value'].map! do |ref| sr_get_uuid(ref)['Value'] end end all_sr end
Unset SR tags
sr_uuid
-
SR UUID
tag
-
Tag
# File xenapi.rb, line 1119 def sr_rm_tag(sr_uuid, tag) sr_opaqueref = sr_get_ref(sr_uuid) sr_opaqueref.key?('Value') ? @connect.call('SR.remove_tags', @session, vdi_get_ref(vdi_uuid)['Value'], tag) : sr_opaqueref end
Search SR by tag
tag
-
Tag
Returns Matched VM
# File xenapi.rb, line 1148 def sr_search_by_tag(tag) all_sr = sr_list('include', false)['Value'] result = all_sr.select do |sr_ref| sr_get_tags(sr_ref)['Value'].include?(tag) end result.map! do |ref| vdi_get_uuid(ref)['Value'] end Messages.success_nodesc_with_payload(result) end
Cancel a task, important after Crashes
task_opaqueref
-
Task Reference
# File xenapi.rb, line 623 def task_cancel(task_opaqueref) @connect.call('task.cancel', @session, task_opaqueref) end
Destroy a task, important after working on a Async Task
task_opaqueref
-
Task Reference
# File xenapi.rb, line 615 def task_destroy(task_opaqueref) @connect.call('task.destroy', @session, task_opaqueref) end
Task Errors
task_opaqueref
-
Task Reference
# File xenapi.rb, line 607 def task_get_error(task_opaqueref) @connect.call('task.get_error_info', @session, task_opaqueref) end
Get Task Record
task_opaqueref
-
Task Reference
# File xenapi.rb, line 583 def task_get_record(task_opaqueref) @connect.call('task.get_record', @session, task_opaqueref) end
Task Result
task_opaqueref
-
Task Reference
# File xenapi.rb, line 599 def task_get_result(task_opaqueref) @connect.call('task.get_result', @session, task_opaqueref) end
Task Status
task_opaqueref
-
Task Reference
# File xenapi.rb, line 591 def task_get_status(task_opaqueref) @connect.call('task.get_status', @session, task_opaqueref) end
All Task
# File xenapi.rb, line 575 def task_list_all_records @connect.call('task.get_all_records', @session) end
Create a Virtual Block Device (Plugged Instantly)
vm_opaqueref
-
VM Reference
vdi_opaqueref
-
VDI Reference
device_slot
-
VBD Device place
# File xenapi.rb, line 846 def vbd_create(vm_uuid, vdi_uuid, device_slot) if check_vm_entity_validity(vm_uuid) || check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else vbd_object = { VM: vm_get_ref(vm_uuid)['Value'], VDI: vdi_get_ref(vdi_uuid)['Value'], userdevice: device_slot, bootable: false, mode: 'RW', type: 'Disk', empty: false, qos_algorithm_type: '', qos_algorithm_params: {} } result = @connect.call('VBD.create', @session, vbd_object) result['Value'] = vbd_get_uuid(result['Value'])['Value'] result end end
Get detail of the specified VBD by OpaqueRef
# File xenapi.rb, line 829 def vbd_get_detail(vbd_opaqueref) @connect.call('VBD.get_record', @session, vbd_opaqueref) end
Get detail of the specified VBD by UUID
# File xenapi.rb, line 835 def vbd_get_detail2(vbd_uuid) vbd_opaqueref = vbd_get_ref(vbd_uuid) vbd_opaqueref.key?('Value') ? @connect.call('VBD.get_record', @session, vbd_opaqueref['Value']) : vbd_opaqueref end
Get VBD OpaqueRef
# File xenapi.rb, line 875 def vbd_get_ref(vbd_uuid) @connect.call('VBD.get_by_uuid', @session, vbd_uuid) end
Get VBD uuid
# File xenapi.rb, line 869 def vbd_get_uuid(vbd_opaqueref) @connect.call('VBD.get_uuid', @session, vbd_opaqueref) end
Get all VBD on the system
# File xenapi.rb, line 819 def vbd_list result = @connect.call('VBD.get_all', @session) result['Value'].map! do |ref| vbd_get_uuid(ref)['Value'] end result end
Add a tag to the specified VDI
vdi_uuid
-
VDI UUID
tag
-
Tag
# File xenapi.rb, line 747 def vdi_add_tag(vdi_uuid, tag) if check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else @connect.call('VDI.add_tags', @session, vdi_get_ref(vdi_uuid)['Value'], tag) end end
Destroy the specified VDI, by UUID
vdi_uuid
-
VDI Reference
# File xenapi.rb, line 724 def vdi_destroy(vdi_uuid) if check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else vdi_task_token = @connect.call('Async.VDI.destroy', @session, vdi_get_ref(vdi_uuid)['Value']) async_task_manager(vdi_task_token, false) end end
Destroy the specified VDI, by REF
vdi_opaqueref
-
VDI Reference
# File xenapi.rb, line 737 def vdi_destroy2(vdi_opaqueref) vdi_task_token = @connect.call('Async.VDI.destroy', @session, vdi_opaqueref) async_task_manager(vdi_task_token, false) end
Get detail of the specified VDI
vdi_uuid
-
VDI Reference
# File xenapi.rb, line 687 def vdi_get_record(vdi_uuid) if check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else vdi_opaqueref = vdi_get_ref(vdi_uuid)['Value'] record = @connect.call('VDI.get_record', @session, vdi_opaqueref) begin record['Value']['snapshot_time'] = record['Value']['snapshot_time'].to_time.to_s rescue NoMethodError true end record end end
Get VDI OpaqueRef
# File xenapi.rb, line 807 def vdi_get_ref(vbd_uuid) @connect.call('VDI.get_by_uuid', @session, vbd_uuid) end
Get VDI uuid
# File xenapi.rb, line 801 def vdi_get_uuid(vbd_opaqueref) @connect.call('VDI.get_uuid', @session, vbd_opaqueref) end
Get a list of all VDI
iso_cd
-
Ignore ISO file? possible values are inclue, exclude, only
# File xenapi.rb, line 634 def vdi_list(iso_cd) all_records = @connect.call('VDI.get_all', @session) # Filter Away Snapshots all_records['Value'].select! do |vdi_opaqueref| !check_vdi_is_a_snapshot(vdi_opaqueref) end # Filter Away XS-Tools all_records['Value'].select! do |vdi_opaqueref| !check_vdi_is_xs_iso(vdi_opaqueref) end all_records['Value'].select! do |vdi_opaqueref| !check_vdi_is_iso(vdi_opaqueref) if iso_cd == 'exclude' check_vdi_is_iso(vdi_opaqueref) if iso_cd == 'only' true if iso_cd == 'include' end all_records['Value'].map! do |ref| vdi_get_uuid(ref)['Value'] end all_records end
Get a list of all Snapshot VDI
# File xenapi.rb, line 657 def vdi_list_snapshot all_records = @connect.call('VDI.get_all', @session) # Filter Away Snapshots all_records['Value'].select! do |vdi_opaqueref| check_vdi_is_a_snapshot(vdi_opaqueref) end all_records['Value'].map! do |ref| vdi_get_uuid(ref)['Value'] end all_records end
Get XS-TOOLS VDI
# File xenapi.rb, line 671 def vdi_list_tools all_records = @connect.call('VDI.get_all', @session) # Filter Away all butXS-Tools all_records['Value'].select! do |vdi_opaqueref| check_vdi_is_xs_iso(vdi_opaqueref) end all_records['Value'].map! do |ref| vdi_get_uuid(ref)['Value'] end all_records end
Resize the specified VDI
vdi_uuid
-
VDI Reference
new_vdi_size
-
New Size of the VDI
# File xenapi.rb, line 707 def vdi_resize(vdi_uuid, new_vdi_size) if check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else begin vdi_opaqueref = vdi_get_ref(vdi_uuid)['Value'] @connect.call('VDI.resize_online', @session, vdi_opaqueref, new_vdi_size) rescue RuntimeError Messages.error_unsupported end end end
Unset VDI tags
vdi_uuid
-
VDI UUID
tag
-
Tag
# File xenapi.rb, line 760 def vdi_rm_tag(vdi_uuid, tag) if check_vdi_entity_validity(vdi_uuid) Messages.error_not_permitted else @connect.call('VDI.remove_tags', @session, vdi_get_ref(vdi_uuid)['Value'], tag) end end
Search VDI by tag
tag
-
Tag
Returns Matched VM
# File xenapi.rb, line 787 def vdi_search_by_tag(tag) all_vdi = vdi_list('include')['Value'] result = all_vdi.select do |vdi_uuid| vdi_opaqueref = vdi_get_ref(vdi_uuid)['Value'] vdi_get_tags(vdi_opaqueref)['Value'].include?(tag) end result.map! do |ref| vdi_get_uuid(ref)['Value'] end Messages.success_nodesc_with_payload(result) end
Create a VIF
vm_uuid
-
OpaqueRef of target VM
net_uuid
-
Network to be plugged
slot
-
Where the VIF is 'inserted'
# File xenapi.rb, line 906 def vif_create(vm_uuid, net_uuid, slot) net_opaqueref = network_get_ref(net_uuid) if check_vm_entity_validity(vm_uuid) || net_opaqueref['Status'] != 'Success' Messages.error_not_permitted else vbd_object = { device: slot.to_s, network: net_opaqueref['Value'], VM: vm_get_ref(vm_uuid)['Value'], MAC: '', MTU: '1500', other_config: {}, qos_algorithm_type: '', qos_algorithm_params: {} } result = @connect.call('VIF.create', @session, vbd_object) result.key?('Value') ? result['Value'] = vif_get_uuid(result['Value'])['Value'] : nil result end end
Destroy the specified VIF
vif_uuid
-
OpaqueRef of the VIF
# File xenapi.rb, line 930 def vif_destroy(vif_uuid) vif_opaqueref = vif_get_ref(vif_uuid) vif_opaqueref.key?('Value') ? @connect.call('VIF.destroy', @session, vif_opaqueref['Value']) : vif_opaqueref end
Get details of the specified VIF
vif_uuid
-
OpaqueRef of the VIF
# File xenapi.rb, line 896 def vif_get_detail(vif_uuid) vif_opaqueref = vif_get_ref(vif_uuid) vif_opaqueref.key?('Value') ? @connect.call('VIF.get_record', @session, vif_opaqueref['Value']) : vif_opaqueref end
Get VIF OpaqueRef
# File xenapi.rb, line 959 def vif_get_ref(vif_uuid) @connect.call('VIF.get_by_uuid', @session, vif_uuid) end
Get VIF uuid
# File xenapi.rb, line 953 def vif_get_uuid(vif_opaqueref) @connect.call('VIF.get_uuid', @session, vif_opaqueref) end
List all VIF
# File xenapi.rb, line 885 def vif_list result = @connect.call('VIF.get_all', @session) result['Value'].map! do |ref| vif_get_uuid(ref)['Value'] end result end
Plug the VIF
vif_uuid
-
OpaqueRef of the VIF
# File xenapi.rb, line 938 def vif_plug(vif_uuid) vif_opaqueref = vif_get_ref(vif_uuid) vif_opaqueref.key?('Value') ? @connect.call('VIF.plug', @session, vif_opaqueref['Value']) : vif_opaqueref end
UnPlug the VIF
vif_uuid
-
OpaqueRef of the VIF
# File xenapi.rb, line 946 def vif_unplug(vif_uuid) vif_opaqueref = vif_get_ref(vif_uuid) vif_opaqueref.key?('Value') ? @connect.call('VIF.unplug', @session, vif_opaqueref['Value']) : vif_opaqueref end
Add 'other config', useful for official PV Instance template
vm_uuid
-
VM UUID
key
-
Config Key
value
-
Config Value
# File xenapi.rb, line 433 def vm_add_other_config(vm_uuid, key, value) vm_opaqueref = vm_get_ref(vm_uuid) vm_opaqueref.key?('Value') ? @connect.call('VM.add_to_other_config', @session, vm_opaqueref['Value'], key, value) : vm_opaqueref end
Add a tag to the specified VM
vm_uuid
-
VM UUID
tag
-
Tag
# File xenapi.rb, line 473 def vm_add_tag(vm_uuid, tag) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] @connect.call('VM.add_tags', @session, vm_opaqueref, tag) end end
Clone the target Virtual Machine Returns the reference point of new vm APIDoc P111, Copy tends to be more guaranteed.
old_vm_uuid
-
Source VM UUID
new_vm_name
-
Name of the new VM
Returns: the record of new vm
# File xenapi.rb, line 328 def vm_clone(old_vm_uuid, new_vm_name) if check_vm_entity_validity(old_vm_uuid) || new_vm_name.nil? || new_vm_name == '' Messages.error_not_permitted else old_vm_opaqueref = vm_get_ref(old_vm_uuid)['Value'] # The NULL Reference is required to fulfill the requirement. task_token = @connect.call('Async.VM.copy', @session, old_vm_opaqueref, new_vm_name, 'OpaqueRef:NULL') result = async_task_manager(task_token, true) if result.key?('Status') && result['Status'] == 'Error' result else Messages.success_nodesc_with_payload(vm_get_uuid(result['value'])['Value']) end end end
Clone from PV Template
vm_tpl_uuid
-
Source Template UUID
new_vm_name
-
Name of the new VM
pv_boot_param
-
Boot Command Line
repo_url
-
URL of the Distro Repo
distro
-
Distro Family, acceptable values are debian and rhel
distro_release
-
Release Name of the specified Debian/Ubuntu Release. example: jessie(Debian 8), trusty(Ubuntu 14.04)
Returns: The record of new vm
# File xenapi.rb, line 354 def vm_clone_from_template(vm_tpl_uuid, new_vm_name, pv_boot_param, repo_url, distro, distro_release, network_uuid, dsk_size) if check_vm_template_validity(vm_tpl_uuid) || new_vm_name.nil? || new_vm_name == '' || repo_url.start_with?('https://') Messages.error_not_permitted else vm_tpl_opaqueref = vm_get_ref(vm_tpl_uuid)['Value'] # Step0.1: Copy from template. # The NULL Reference is required to fulfill the params requirement. default_sr = sr_get_default['Value'] task_token = @connect.call('Async.VM.copy', @session, vm_tpl_opaqueref, new_vm_name, default_sr['REF']) # Step0.2: get new vm reference point result = async_task_manager(task_token, true)['value'] vmuuid = vm_get_uuid(result)['Value'] disk = '<provision><disk device=\0\ size=\' + dsk_size.to_s + '\ sr=\' + default_sr['UUID'] + '\ bootable=\true\ type=\system\/></provision>' vm_set_other_config(vmuuid, 'disks', disk) # Step 1 : Set boot paramaters, For configuring the kickstart definition @connect.call('VM.provision', @session, result) @connect.call('VM.set_PV_args', @session, result, pv_boot_param) # Step 2 : Set repository URL, important for install. Always skip step if previous step has error # 2.1: Handling Debian-based if distro == 'debianlike' || distro == 'rhlike' || distro == 'sleslike' # Set New Value s = vm_set_other_config(vmuuid, 'install-repository', repo_url) unless s['Status'] != 'Success' s = vm_set_other_config(vmuuid, 'debian-release', distro_release) if distro == 'debianlike' end # Other distro is HVM so we ignore it. else Messages.error_unsupported end if s['Status'] == 'Error' Messages.error_unknown(s) else s = vif_create(vmuuid, network_uuid, 0) if s['Status'] == 'Error' Messages.error_unknown(s) else # callback the new vm OpaqueRef Messages.success_nodesc_with_payload(vm_get_uuid(result)['Value']) end end end end
Erase the target Virtual Machine, along with related VDIs
old_vm_uuid
-
VM UUID
# File xenapi.rb, line 401 def vm_destroy(old_vm_uuid) if check_vm_entity_validity(old_vm_uuid) Messages.error_not_permitted else old_vm_opaqueref = vm_get_ref(old_vm_uuid)['Value'] # Get /dev/xvda VDI Reference Code First. vbd_sets = vm_get_vbds(old_vm_uuid, false)['Value'] xvda_id = '' vbd_sets.each do |vbd_opaqueref| record = vbd_get_detail(vbd_opaqueref)['Value'] next if record['type'] != 'Disk' || record['device'] != 'xvda' xvda_id = record['VDI'] end # Delete VM task_token = @connect.call('Async.VM.destroy', @session, old_vm_opaqueref) result = async_task_manager(task_token, false) if result['Status'] == 'Success' # Delete VM OK => Cleanup residue: /dev/xvda VDI vdi_destroy2(xvda_id) else # Prompt Error on Deleting VM result end end end
Get Various Runtime Detail about the VM
vm_uuid
-
VM Reference
# File xenapi.rb, line 173 def vm_get_guest_metrics(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] ref = @connect.call('VM.get_guest_metrics', @session, vm_opaqueref)['Value'] dat = @connect.call('VM_guest_metrics.get_record', @session, ref) # convert mess stuffs to Human-readable begin dat['Value']['last_updated'] = dat['last_updated'].to_time.to_s rescue NoMethodError dat['Value']['last_updated'] = nil end # Output. return is redundant in Ruby World. dat end end
Get VM Network IPs discussions.citrix.com/topic/244784-how-to-get-ip-address-of-vm-network-adapters/
vm_uuid
-
VM Reference
# File xenapi.rb, line 195 def vm_get_guest_metrics_network(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] ref = @connect.call('VM.get_guest_metrics', @session, vm_opaqueref)['Value'] @connect.call('VM_guest_metrics.get_networks', @session, ref) end end
Get Various Physical Details about the VM
vm_uuid
-
VM Reference
# File xenapi.rb, line 147 def vm_get_metrics(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] ref = @connect.call('VM.get_metrics', @session, vm_opaqueref)['Value'] dat = @connect.call('VM_metrics.get_record', @session, ref) # convert mess stuffs to Human-readable begin dat['Value']['start_time'] = dat['Value']['last_updated'].to_time.to_s dat['Value']['install_time'] = dat['Value']['last_updated'].to_time.to_s dat['Value']['last_updated'] = dat['Value']['last_updated'].to_time.to_s rescue NoMethodError dat['Value']['start_time'] = nil dat['Value']['install_time'] = nil dat['Value']['last_updated'] = nil end # Output. return is redundant in Ruby World. dat end end
Get other_config field.
vm_uuid
-
VM UUID
# File xenapi.rb, line 452 def vm_get_other_config(vm_uuid) vm_opaqueref = vm_get_ref(vm_uuid) vm_opaqueref.key?('Value') ? @connect.call('VM.get_other_config', @session, vm_opaqueref['Value']) : vm_opaqueref end
Get Virtual Machines Detail by OpaqueRef
vm_uuid
-
VM Reference
# File xenapi.rb, line 102 def vm_get_record(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] record = @connect.call('VM.get_record', @session, vm_opaqueref)['Value'] # Post processing # 1. Decode Time Object to Human-readable begin record['snapshot_time'] = record['snapshot_time'].to_time.to_s rescue NoMethodError true end # 2. Last Boot Record is JSON, decode to Ruby Hash so that it won't clash # the JSON generator record['last_booted_record'] = parse_last_boot_record(record['last_booted_record']) # Output. return is redundant in Ruby World. Messages.success_nodesc_with_payload(record) end end
Get VDI OpaqueRef
# File xenapi.rb, line 564 def vm_get_ref(vm_uuid) @connect.call('VM.get_by_uuid', @session, vm_uuid) end
Get Virtual Machines Detail by OpaqueRef
vm_uuid
-
VM Reference
# File xenapi.rb, line 127 def vm_get_template_record(vm_uuid) if check_vm_template_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] record = @connect.call('VM.get_record', @session, vm_opaqueref)['Value'] begin record['snapshot_time'] = record['snapshot_time'].to_time.to_s rescue NoMethodError record['snapshot_time'] = nil end # Output. return is redundant in Ruby World. Messages.success_nodesc_with_payload(record) end end
Get VM uuid
# File xenapi.rb, line 558 def vm_get_uuid(vm_opaqueref) @connect.call('VM.get_uuid', @session, vm_opaqueref) end
Get Block Devices of the specified VM
vm_uuid
-
VM Reference
uuid_mode
-
Use UUID Mode?
# File xenapi.rb, line 210 def vm_get_vbds(vm_uuid, uuid_mode) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] vbds = @connect.call('VM.get_VBDs', @session, vm_opaqueref) end if uuid_mode == true && vbds['Status'] == 'Success' && vbds['Value'].empty? == false vbds['Value'].map! do |ref| vbd_get_uuid(ref)['Value'] end end vbds end
Get Virtual Network Interfaces (VIFs) of the specified VM
vm_uuid
-
VM Reference
uuid_mode
-
Use UUID Mode?
# File xenapi.rb, line 230 def vm_get_vifs(vm_uuid, uuid_mode) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] vifs = @connect.call('VM.get_VIFs', @session, vm_opaqueref) end if uuid_mode == true && vifs['Status'] == 'Success' && vifs['Value'].empty? == false vifs['Value'].map! do |ref| vif_get_uuid(ref)['Value'] end end vifs end
Get All Virtual Machines Using list instead to circumvent RuboCop
# File xenapi.rb, line 65 def vm_list_all all_records = @connect.call('VM.get_all', @session) # Filter Away Control Domain all_records['Value'].select! do |vm_opaqueref| !check_vm_entity_is_dom0(vm_opaqueref) end # Filter Away Template all_records['Value'].select! do |vm_opaqueref| !check_vm_entity_is_template(vm_opaqueref) end all_records['Value'].map! do |ref| vm_get_uuid(ref)['Value'] end all_records end
Get all Templates
# File xenapi.rb, line 83 def vm_list_all_templates all_records = @connect.call('VM.get_all', @session) # Filter Away non-template (VM Instances + dom0) all_records['Value'].select! do |tpl_opaqueref| check_vm_entity_is_template(tpl_opaqueref) end all_records['Value'].select! do |tpl_opaqueref| check_vm_entity_is_paravirtual(tpl_opaqueref) end all_records['Value'].map! do |ref| vm_get_uuid(ref)['Value'] end all_records end
Power OFF the specified Virtual Machine
vm_uuid
-
VM Reference
# File xenapi.rb, line 263 def vm_power_off(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] task_token = @connect.call('Async.VM.shutdown', @session, vm_opaqueref) async_task_manager(task_token, false) end end
Power ON the specified Virtual Machine
vm_uuid
-
VM Reference
# File xenapi.rb, line 249 def vm_power_on(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] task_token = @connect.call('Async.VM.start', @session, vm_opaqueref, false, false) async_task_manager(task_token, false) end end
Suspend the specified Virtual Machine
vm_uuid
-
VM Reference
# File xenapi.rb, line 291 def vm_power_pause(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] # API Manual P116 # void suspend (session_id s, VM ref vm) task_token = @connect.call('Async.VM.suspend', @session, vm_opaqueref) async_task_manager(task_token, false) end end
Reboot the specified Virtual Machines
vm_uuid
-
VM Reference
# File xenapi.rb, line 277 def vm_power_reboot(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] task_token = @connect.call('Async.VM.hard_reboot', @session, vm_opaqueref) async_task_manager(task_token, false) end end
Wake up the specified Virtual Machine
vm_uuid
-
VM Reference
# File xenapi.rb, line 307 def vm_power_unpause(vm_uuid) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] # API Manual P116-117 # void resume (session_id s, VM ref vm, bool start_paused, bool force) task_token = @connect.call('Async.VM.resume', @session, vm_opaqueref, false, false) async_task_manager(task_token, false) end end
Unset 'other config', useful for official PV Instance template
vm_uuid
-
VM UUID
key
-
Config Key
# File xenapi.rb, line 443 def vm_rm_other_config(vm_uuid, key) vm_opaqueref = vm_get_ref(vm_uuid) vm_opaqueref.key?('Value') ? @connect.call('VM.remove_from_other_config', @session, vm_opaqueref['Value'], key) : vm_opaqueref end
Unset VM tags
vm_uuid
-
VM UUID
tag
-
Tag
# File xenapi.rb, line 487 def vm_rm_tag(vm_uuid, tag) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] @connect.call('VM.remove_tags', @session, vm_opaqueref, tag) end end
search VM by tag
tag
-
Tag
Returns Matched VM
# File xenapi.rb, line 516 def vm_search_by_tag(tag) all_vm = vm_list_all all_vm['Value'].select do |vm_uuid| vm_opaqueref = vm_get_ref(vm_uuid)['Value'] vm_get_tags(vm_opaqueref)['Value'].include?(tag) end end
Set VM Memory Size
vm_uuid
-
VM OpaqueRef
max_size
-
Memory Capacity
# File xenapi.rb, line 542 def vm_set_max_ram(vm_uuid, max_size) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] begin @connect.call('VM.set_memory_static_max', @session, vm_opaqueref, max_size.to_i) @connect.call('VM.set_memory_dynamic_max', @session, vm_opaqueref, max_size.to_i) rescue RuntimeError Messages.error_unsupported end end end
Set VM Name
vm_uuid
-
VM OpaqueRef
vm_name
-
VM Name
# File xenapi.rb, line 529 def vm_set_name(vm_uuid, vm_name) if check_vm_entity_validity(vm_uuid) Messages.error_not_permitted else vm_opaqueref = vm_get_ref(vm_uuid)['Value'] @connect.call('VM.set_name_label', @session, vm_opaqueref, vm_name) end end
Set 'other config', useful for official PV Instance template
vm_uuid
-
VM UUID
key
-
Config Key
value
-
Config Value
# File xenapi.rb, line 463 def vm_set_other_config(vm_uuid, key, value) s = vm_rm_other_config(vm_uuid, key) s.key?('Value') ? vm_add_other_config(vm_uuid, key, value) : s end
Private Instance Methods
Refactor: AsyncTask Task Manager It would poll the server continuously for task status
# File xenapi.rb, line 1310 def async_task_manager(task_opaqueref, has_payload) if task_opaqueref['Status'] != 'Success' Messages.error_switch(task_opaqueref['ErrorDescription'][0][0]) else task_status = task_get_status(task_opaqueref['Value'])['Value'] while task_status == 'pending' task_status = task_get_status(task_opaqueref['Value'])['Value'] sleep(5) end if task_status == 'success' && has_payload == false task_destroy(task_opaqueref['Value']) Messages.success_nodesc elsif task_status == 'success' && has_payload == true result = task_get_result(task_opaqueref['Value']) task_destroy(task_opaqueref['Value']) result['Value'] = xml_parse(result['Value']) result['Value'] else error_info = task_get_error(task_opaqueref['Value'])['Value'] task_destroy(task_opaqueref['Value']) Messages.error_unknown(error_info) end end end
Check if a SR is ISO
# File xenapi.rb, line 1233 def check_sr_is_iso(sr_opaqueref) type = sr_get_type2(sr_opaqueref)['Value'] type != 'iso' ? true : false end
Check if a SR is udes
# File xenapi.rb, line 1240 def check_sr_is_udev(sr_opaqueref) type = sr_get_type2(sr_opaqueref)['Value'] type != 'udev' ? true : false end
Filter away non-VM SR
# File xenapi.rb, line 1284 def check_sr_is_vdisr_by_ref(sr_ref) if sr_ref.nil? || sr_ref == '' false else !(check_sr_is_iso(sr_ref) && check_sr_is_udev(sr_ref)) end end
Filter away non-VM SR
# File xenapi.rb, line 1294 def check_sr_is_vdisr_by_uuid(sr_uuid) if sr_uuid.nil? || sr_uuid == '' false else sr_ref = sr_get_ref(sr_uuid) sr_ref.key?('Value') ? !(check_sr_is_iso(sr_ref['Value']) && check_sr_is_udev(sr_ref['Value'])) : false end end
Refactor: Aggregated VDI Validity Check
# File xenapi.rb, line 1273 def check_vdi_entity_validity(vdi_uuid) if vdi_uuid == '' || vdi_uuid.nil? true else vdi_opaqueref = vdi_get_ref(vdi_uuid) vdi_opaqueref['Status'] == 'Success' ? check_vdi_is_a_snapshot(vdi_opaqueref['Value']) || check_vdi_is_xs_iso(vdi_opaqueref['Value']) : true end end
Filter: Ignore Snapshots
# File xenapi.rb, line 1227 def check_vdi_is_a_snapshot(vdi_opaqueref) @connect.call('VDI.get_is_a_snapshot', @session, vdi_opaqueref)['Value'] end
Filter: Ignore ISO and Hypervisor host CD drive Only HDD Image can clone. resize is OK but it will not show running disk.
# File xenapi.rb, line 1219 def check_vdi_is_iso(vdi_opaqueref) result = @connect.call('VDI.get_allowed_operations', @session, vdi_opaqueref)['Value'] read_only = @connect.call('VDI.get_read_only', @session, vdi_opaqueref)['Value'] result.include?('clone') && read_only == false ? false : true end
Filter: Ignore XS-Tools ISO
# File xenapi.rb, line 1212 def check_vdi_is_xs_iso(vdi_opaqueref) @connect.call('VDI.get_is_tools_iso', @session, vdi_opaqueref)['Value'] end
Filter: Check the requested VM entity is the dom0 or not.
# File xenapi.rb, line 1183 def check_vm_entity_is_dom0(vm_opaqueref) @connect.call('VM.get_is_control_domain', @session, vm_opaqueref)['Value'] end
Filter: Check VM Existency
# File xenapi.rb, line 1195 def check_vm_entity_is_nonexist(vm_opaqueref) result = @connect.call('VM.get_uuid', @session, vm_opaqueref)['Status'] result == 'Success' ? false : true end
Filter: Check VM IS PV
# File xenapi.rb, line 1202 def check_vm_entity_is_paravirtual(vm_opaqueref) result = @connect.call('VM.get_PV_bootloader', @session, vm_opaqueref)['Value'] # PV Templates always have pygrub in PV_bootloader field # https://wiki.xenproject.org/wiki/XCP_PV_templates_start # pygrub will be used after install finished result == 'eliloader' ? true : false end
Filter: Check the requested VM entity is an Template or not.
# File xenapi.rb, line 1189 def check_vm_entity_is_template(vm_opaqueref) @connect.call('VM.get_is_a_template', @session, vm_opaqueref)['Value'] end
Refactor: Aggregated Validity Check
# File xenapi.rb, line 1251 def check_vm_entity_validity(vm_uuid) if vm_uuid == '' || vm_uuid.nil? true else vm_opaqueref = vm_get_ref(vm_uuid) vm_opaqueref['Status'] == 'Success' ? check_vm_entity_is_dom0(vm_opaqueref['Value']) || check_vm_entity_is_template(vm_opaqueref['Value']) : true end end
Refactor: Check Template Validity
# File xenapi.rb, line 1262 def check_vm_template_validity(vm_tpl_uuid) if vm_tpl_uuid == '' || vm_tpl_uuid.nil? true else vm_tpl_opaqueref = vm_get_ref(vm_tpl_uuid) vm_tpl_opaqueref['Status'] == 'Success' ? !check_vm_entity_is_template(vm_tpl_opaqueref['Value']) || !check_vm_entity_is_paravirtual(vm_tpl_opaqueref['Value']) : true end end
stackoverflow.com/questions/5661466/test-if-string-is-a-number-in-ruby-on-rails
# File xenapi.rb, line 1364 def number?(string) true if Integer(string) rescue Integer::ArgumentError false end
Parse the last boot record to Hash.
This parser is adapted from gist.github.com/ascendbruce/7070951
# File xenapi.rb, line 1339 def parse_last_boot_record(raw_last_boot_record) parsed = JSON.parse(raw_last_boot_record) # Also need to convert mess stuffs to Human-readable begin parsed['last_start_time'] = Time.at(parsed['last_start_time']).to_s rescue NoMethodError parsed['last_start_time'] = '' end parsed rescue JSON::ParserError # Ruby rescue is catch in other languages # Parsing struct is farrrrrrr too difficult Messages.error_unsupported end
XML Parser, important github.com/savonrb/nori
# File xenapi.rb, line 1357 def xml_parse(raw_xml) xml_parser = Nori.new(convert_tags_to: ->(tag) { tag.snakecase }) xml_parser.parse(raw_xml) end