feature #4186: Add vCenter attach/detach functionality

This commit is contained in:
Tino Vazquez committed 2016-01-15 17:59:43 +01:00
1 parent c08f6e5b80
commit fc57b2b69c
5 files changed
+303 -38

No files matched your search

+1 -1
View File
@@ -94,7 +94,7 @@ class OpenNebulaDriver < ActionManager
command = action_command_line(aname, params, options[:script_name])
if action_is_local?(aname)
execution = LocalCommand.run(command, log_method(id))
execution = LocalCommand.run(command, log_method(id), Base64::encode64(options[:stdin].to_s.gsub("\n","")))
elsif options[:ssh_stream]
if options[:stdin]
cmdin = "cat << EOT | #{command}"
+1
View File
@@ -26,5 +26,6 @@ LOCAL_ACTIONS="deploy,shutdown,reboot,cancel,save,restore,migrate,poll,pre"
LOCAL_ACTIONS="${LOCAL_ACTIONS},post,clean"
LOCAL_ACTIONS="${LOCAL_ACTIONS},snapshotcreate,snapshotrevert,snapshotdelete"
LOCAL_ACTIONS="${LOCAL_ACTIONS},attach_nic,detach_nic,reset"
LOCAL_ACTIONS="${LOCAL_ACTIONS},attach_disk,detach_disk"
exec $MAD_LOCATION/one_vmm_exec -l $LOCAL_ACTIONS $*
+65 -18
View File
@@ -1,20 +1,67 @@
#!/bin/bash
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ---------------------------------------------------------------------------- #
SCRIPT_NAME=$(basename $0)
echo "Action $SCRIPT_NAME not supported" 1>&2
###############################################################################
# This script is used retrieve the file size of a disk
###############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
end
$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)
require 'vcenter_driver'
require 'opennebula'
deploy_id = ARGV[0]
source = ARGV[1]
target = ARGV[2]
target_index = ARGV[3]
drv_action_enc = ARGV[4]
drv_action = OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'VMM_DRIVER_ACTION_DATA')
hostname = drv_action["/VMM_DRIVER_ACTION_DATA/HOST"]
vm_id = drv_action["/VMM_DRIVER_ACTION_DATA/VM/ID"]
ds_name = drv_action["/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']/DATASTORE"]
img_path = drv_action["/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']/SOURCE"]
size_kb = 0 # No volatile disk support at the moment
if ds_name.nil? || hostname.nil? || img_path.nil?
STDERR.puts "Not enough information to clone the image, missing datastore"\
" name or vcenter cluster name or image path."
exit -1
end
begin
VCenterDriver::VCenterVm.attach_disk(hostname,
deploy_id,
ds_name,
img_path,
size_kb)
rescue Exception => e
STDERR.puts "Error attach image #{img_path} size. Reason: #{e.message}"
exit -1
end
+65 -18
View File
@@ -1,20 +1,67 @@
#!/bin/bash
#!/usr/bin/env ruby
# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# ---------------------------------------------------------------------------- #
SCRIPT_NAME=$(basename $0)
echo "Action $SCRIPT_NAME not supported" 1>&2
###############################################################################
# This script is used retrieve the file size of a disk
###############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
if !ONE_LOCATION
RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION)
else
RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION)
end
$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)
require 'vcenter_driver'
require 'opennebula'
deploy_id = ARGV[0]
source = ARGV[1]
target = ARGV[2]
target_index = ARGV[3]
drv_action_enc = STDIN.read.gsub("\n","")
drv_action = OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc),
'VMM_DRIVER_ACTION_DATA')
hostname = drv_action["/VMM_DRIVER_ACTION_DATA/HOST"]
vm_id = drv_action["/VMM_DRIVER_ACTION_DATA/VM/ID"]
img_path = drv_action["/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']/SOURCE"]
ds_name = drv_action["/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']/DATASTORE"]
if ds_name.nil? || hostname.nil? || vm_id.nil? | img_path.nil?
STDERR.puts "Not enough information to perform detach_disk."
exit -1
end
begin
VCenterDriver::VCenterVm.detach_disk(hostname,
deploy_id,
ds_name,
img_path)
rescue Exception => e
STDERR.puts "Error detaching image #{img_path} size. Reason: #{e.message}"
exit -1
end
+171 -1
View File
@@ -52,7 +52,7 @@ module VCenterDriver
# For the VCenter driver each OpenNebula host represents a VCenter cluster
################################################################################
class VIClient
attr_reader :vim, :one, :root, :cluster, :user, :pass, :host
attr_reader :vim, :one, :root, :cluster, :user, :pass, :host, :dc
def get_entities(folder, type, entities=[])
return nil if folder == []
@@ -1730,6 +1730,176 @@ private
return vm_uuid
end
############################################################################
# Attach disk to a VM
# @params adapter_type[String] type of bus where the disk will be plugged
# @params disk_type[String] vmdk type
# @params size_kb[String] size in kb of the disk
############################################################################
def self.attach_disk(hostname, deploy_id, ds_name, img_name, size_kb)
hid = VIClient::translate_hostname(hostname)
connection = VIClient.new(hid)
vm = connection.find_vm_template(deploy_id)
# Find datastore within datacenter
ds=connection.dc.datastoreFolder.childEntity.select{|ds|
ds.name == ds_name}[0]
controller, new_number = find_free_controller(vm)
vmdk_backing = RbVmomi::VIM::VirtualDiskFlatVer2BackingInfo(
:datastore => ds,
:diskMode => 'persistent',
:fileName => "[#{ds_name}] #{img_name}"
)
device = RbVmomi::VIM::VirtualDisk(
:backing => vmdk_backing,
:capacityInKB => size_kb,
:controllerKey => controller.key,
:key => -1,
:unitNumber => new_number
)
device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
:device => device,
:operation => RbVmomi::VIM::VirtualDeviceConfigSpecOperation('add')
)
vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
deviceChange: [device_config_spec]
)
vm.ReconfigVM_Task(spec: vm_config_spec).wait_for_completion
end
def self.find_free_controller(vm)
free_scsi_controllers = Array.new
available_controller = nil
scsi_schema = Hash.new
vm.config.hardware.device.each{ |dev|
if dev.is_a? RbVmomi::VIM::VirtualSCSIController
if scsi_schema[dev.controllerKey].nil?
scsi_schema[dev.key] = Hash.new
scsi_schema[dev.key][:lower] = Array.new
end
scsi_schema[dev.key][:device] = dev
end
next if dev.class != RbVmomi::VIM::VirtualDisk
if scsi_schema[dev.controllerKey].nil?
scsi_schema[dev.controllerKey] = Hash.new
scsi_schema[dev.controllerKey][:lower] = Array.new
end
scsi_schema[dev.controllerKey][:lower] << dev
}
scsi_schema.keys.each{|controller|
if scsi_schema[controller][:lower].length < 15
free_scsi_controllers << scsi_schema[controller][:device].deviceInfo.label
end
}
available_controller_label = free_scsi_controllers.length > 0 ? free_scsi_controllers[0] : add_new_scsi(vm, scsi_schema)
# Get the controller resource from the label
controller = nil
vm.config.hardware.device.each { |device|
(controller = device ; break) if device.deviceInfo.label == available_controller_label
}
new_unit_number = find_new_unit_number(scsi_schema, controller)
return controller, new_unit_number
end
def self.find_new_unit_number(scsi_schema, controller)
used_numbers = Array.new
available_numbers = Array.new
scsi_schema.keys.each { |c|
next if controller.key != scsi_schema[c][:device].key
used_numbers << scsi_schema[c][:device].scsiCtlrUnitNumber
scsi_schema[c][:lower].each { |disk|
used_numbers << disk.unitNumber
}
}
15.times{ |scsi_id|
available_numbers << scsi_id if used_numbers.grep(scsi_id).length <= 0
}
return available_numbers.sort[0]
end
def self.add_new_scsi(vm, scsi_schema)
controller = nil
if scsi_schema.keys.length < 4
scsi_key = scsi_schema.keys.sort[-1] + 1
scsi_number = scsi_tree[scsi_tree.keys.sort[-1]][:device].busNumber + 1
controller_device = RbVmomi::VIM::VirtualLsiLogicController(
:key => scsi_key,
:busNumber => scsi_number,
:sharedBus => :noSharing
)
device_config_spec = RbVmomi::VIM::VirtualDeviceConfigSpec(
:device => controller_device,
:operation => RbVmomi::VIM::VirtualDeviceConfigSpecOperation('add')
)
vm_config_spec = RbVmomi::VIM::VirtualMachineConfigSpec(
:deviceChange => [device_config_spec]
)
vm.ReconfigVM_Task(:spec => vm_config_spec).wait_for_completion
else
raise "Cannot add a new controller, maximum is 4."
end
vm.config.hardware.device.each { |device|
if device.class == RbVmomi::VIM::VirtualLsiLogicController &&
device.key == scsi_key
controller = device.deviceInfo.label
end
}
return controller
end
############################################################################
# Detach all disks from a VM
############################################################################
def self.detach_disk(hostname, deploy_id, ds_name, img_path)
hid = VIClient::translate_hostname(hostname)
connection = VIClient.new(hid)
vm = connection.find_vm_template(deploy_id)
ds_and_img_name = "[#{ds_name}] #{img_path}"
disk = vm.config.hardware.device.select { |d| is_disk?(d) &&
d.backing.fileName == ds_and_img_name }
raise "Disk #{img_path} not found." if disk.nil?
spec = { :deviceChange => [{
:operation => :remove,
:device => disk[0]
}]}
vm.ReconfigVM_Task(:spec => spec).wait_for_completion
end
############################################################################
# Detach all disks from a VM
############################################################################