1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-11 05:17:41 +03:00

L #-: Linting for vCenter TM

(cherry picked from commit fbde74b6f6)
This commit is contained in:
Tino Vazquez 2019-12-18 12:38:24 +01:00
parent 9cb2b90c19
commit 58f8354015
No known key found for this signature in database
GPG Key ID: 2FE9C32E94AEABBE
11 changed files with 274 additions and 299 deletions

View File

@ -62,7 +62,6 @@ AllCops:
- src/pm_mad/remotes/ec2/reset - src/pm_mad/remotes/ec2/reset
- src/pm_mad/remotes/ec2/poll - src/pm_mad/remotes/ec2/poll
- src/onegate/config.ru - src/onegate/config.ru
- src/datastore_mad/remotes/vcenter/monitor
- src/datastore_mad/remotes/vcenter/mkfs - src/datastore_mad/remotes/vcenter/mkfs
- src/datastore_mad/remotes/vcenter/stat - src/datastore_mad/remotes/vcenter/stat
- src/datastore_mad/remotes/vcenter/clone - src/datastore_mad/remotes/vcenter/clone
@ -166,14 +165,6 @@ AllCops:
- src/market_mad/remotes/s3/monitor - src/market_mad/remotes/s3/monitor
- src/market_mad/remotes/s3/delete - src/market_mad/remotes/s3/delete
- src/market_mad/remotes/s3/import - src/market_mad/remotes/s3/import
- src/tm_mad/vcenter/monitor
- src/tm_mad/vcenter/delete
- src/tm_mad/vcenter/mvds
- src/tm_mad/vcenter/mkimage
- src/tm_mad/vcenter/cpds
- src/tm_mad/vcenter/clone
- src/tm_mad/vcenter/mv
- src/tm_mad/vcenter/resize
- src/flow/config.ru - src/flow/config.ru
- src/flow/Gemfile - src/flow/Gemfile
- src/cli/oneprovision - src/cli/oneprovision

View File

@ -20,42 +20,41 @@
# This script is used to monitor the free and used space of a datastore # This script is used to monitor the free and used space of a datastore
############################################################################### ###############################################################################
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
require 'vcenter_driver' require 'vcenter_driver'
def is_not_dsid_or_dsref_valid?(ds_id, ds_ref) def not_dsid_or_dsref_valid?(ds_id, ds_ref)
return ds_id.nil? || ds_ref.nil? ds_id.nil? || ds_ref.nil?
end end
drv_action_enc = ARGV[0] drv_action_enc = ARGV[0]
id = ARGV[1] id = ARGV[1]
drv_action = OpenNebula::XMLElement.new drv_action = OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA') drv_action.initialize_xml(Base64.decode64(drv_action_enc),
'DS_DRIVER_ACTION_DATA')
ds_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/ID"] ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID']
ds_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"] ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF']
check_valid ds_ref, "ds_ref" check_valid ds_ref, 'ds_ref'
if is_not_dsid_or_dsref_valid?(ds_id, ds_ref) if not_dsid_or_dsref_valid?(ds_id, ds_ref)
STDERR.puts "Not enough information to monitor the datastore." STDERR.puts 'Not enough information to monitor the datastore.'
exit -1 exit(-1)
end end
begin begin
@ -63,14 +62,13 @@ begin
storage = VCenterDriver::Storage.new_from_ref(ds_ref, vi_client) storage = VCenterDriver::Storage.new_from_ref(ds_ref, vi_client)
puts storage.monitor puts storage.monitor
rescue StandardError => e
rescue Exception => e message = "Error monitoring datastore #{id}. Reason: \"#{e.message}\"."
message = "Error monitoring datastore #{id}."\
" Reason: \"#{e.message}\"\n#{e.backtrace}"
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -20,54 +20,50 @@
# This script is used retrieve the file size of a disk # This script is used retrieve the file size of a disk
############################################################################### ###############################################################################
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
require 'vcenter_driver' require 'vcenter_driver'
def was_img_imported?(imported) def img_imported?(imported)
return imported.nil? || imported.empty? imported.nil? || imported.empty?
end end
drv_action_enc = ARGV[0] drv_action_enc = ARGV[0]
id = ARGV[1]
drv_action =OpenNebula::XMLElement.new drv_action =OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA') drv_action.initialize_xml(Base64.decode64(drv_action_enc),
'DS_DRIVER_ACTION_DATA')
ds_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"] ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF']
ds_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/ID"] ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID']
img_src = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/SOURCE"] img_src = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/SOURCE']
imported = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/VCENTER_IMPORTED"] imported = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/VCENTER_IMPORTED']
check_valid ds_ref, "ds_ref" check_valid ds_ref, 'ds_ref'
check_valid img_src, "img_src" check_valid img_src, 'img_src'
CDROM = "1" CDROM = '1'
begin begin
if was_img_imported?(imported) if img_imported?(imported)
vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id) vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id)
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client) ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
img_dir = img_src.split('/')[0..-2].join('/') img_dir = img_src.split('/')[0..-2].join('/')
img_type = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/TYPE']
img_type = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/TYPE"]
# Check if file exists before trying to delete it # Check if file exists before trying to delete it
ds_name = ds['name'] ds_name = ds['name']
@ -77,69 +73,57 @@ begin
search_params = ds.get_search_params(ds_name, img_path, img_name) search_params = ds.get_search_params(ds_name, img_path, img_name)
# Perform search task and return results # Perform search task and return results
search_task = ds['browser'].SearchDatastoreSubFolders_Task(search_params) search_task = ds['browser']
.SearchDatastoreSubFolders_Task(search_params)
search_task.wait_for_completion search_task.wait_for_completion
if img_type != CDROM if img_type != CDROM
# delete the disk # delete the disk
begin begin
retries ||= 0 retries ||= 0
ds.delete_virtual_disk(img_src) ds.delete_virtual_disk(img_src)
rescue StandardError => e rescue StandardError => e
if (retries += 1) < VCenterDriver::CONFIG[:retries]
message = "Delete DISK #{img_name} failed due to "\ message = "Delete DISK #{img_name} failed due to "\
"\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ "\"#{e.message}\" on the attempt \##{retries}."
"on the attempt \##{retries}\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message)
else if VCenterDriver::CONFIG[:debug_information]
message = "Delete DISK #{img_name} failed due to "\ STDERR.puts "#{message} #{e.backtrace}"
"\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ end
"on the final attempt\n#{e.backtrace.join("\n")}" sleep VCenterDriver::CONFIG[:retry_interval].to_i
end retry if retries < VCenterDriver::CONFIG[:retries]
OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information]
sleep VCenterDriver::CONFIG[:retry_interval].to_i
retry if retries < VCenterDriver::CONFIG[:retries]
exit(-1)
exit(-1)
end end
else else
# delete the CDROM iso # delete the CDROM iso
begin begin
retries ||= 0 retries ||= 0
ds.delete_file(img_src) ds.delete_file(img_src)
rescue StandardError => e rescue StandardError => e
if (retries += 1) < VCenterDriver::CONFIG[:retries]
message = "Delete CDROM #{img_name} failed due to "\ message = "Delete CDROM #{img_name} failed due to "\
"\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ "\"#{e.message}\" on the attempt \##{retries}."
"on the attempt \##{retries}\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message)
else if VCenterDriver::CONFIG[:debug_information]
message = "Delete CDROM #{img_name} failed due to "\ STDERR.puts "#{message} #{e.backtrace}"
"\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ end
"on the final attempt\n#{e.backtrace.join("\n")}" sleep VCenterDriver::CONFIG[:retry_interval].to_i
end retry if retries < VCenterDriver::CONFIG[:retries]
OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information]
sleep VCenterDriver::CONFIG[:retry_interval].to_i
retry if retries < VCenterDriver::CONFIG[:retries]
exit(-1)
exit(-1)
end end
end end
# Erase folder if empty
ds.rm_directory(img_dir) if ds.dir_empty?(img_dir) ds.rm_directory(img_dir) if ds.dir_empty?(img_dir)
end end
rescue Exception => e rescue StandardError => e
if !e.message.start_with?('FileNotFound') if !e.message.start_with?('FileNotFound')
message = "Error deleting virtual disk #{img_src}."\ message = "Error deleting virtual disk #{img_src}."\
" Reason: \"#{e.message}\"\n#{e.backtrace}" " Reason: \"#{e.message}\"\n#{e.backtrace}"
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
end end
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client

View File

@ -20,19 +20,17 @@
# This script is used retrieve the file size of a disk # This script is used retrieve the file size of a disk
############################################################################### ###############################################################################
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -40,31 +38,32 @@ $LOAD_PATH << File.dirname(__FILE__)
require 'vcenter_driver' require 'vcenter_driver'
drv_action_enc = ARGV[0] drv_action_enc = ARGV[0]
id = ARGV[1]
drv_action =OpenNebula::XMLElement.new drv_action =OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA') drv_action.initialize_xml(Base64.decode64(drv_action_enc),
'DS_DRIVER_ACTION_DATA')
ds_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"] ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF']
ds_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/ID"] ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID']
img_path = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"] img_path = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/PATH']
check_valid ds_ref, "ds_ref" check_valid ds_ref, 'ds_ref'
check_valid img_path, "img_path" check_valid img_path, 'img_path'
if img_path.start_with? "vcenter://" if img_path.start_with? 'vcenter://'
begin begin
vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id) vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id)
ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client) ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client)
puts ds.stat(img_path.sub("vcenter://","")) puts ds.stat(img_path.sub('vcenter://', ''))
rescue StandardError => e
rescue Exception => e
message = "Error calculating image #{img_path} size."\ message = "Error calculating image #{img_path} size."\
" Reason: \"#{e.message}\"\n#{e.backtrace}" " Reason: \"#{e.message}\"."
STDERR.puts error_message(message) OpenNebula.log_error(message)
exit -1 if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -24,19 +24,17 @@
# - vmid is the id of the VM # - vmid is the id of the VM
# - dsid is the target datastore (0 is the system datastore) # - dsid is the target datastore (0 is the system datastore)
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -48,48 +46,51 @@ dst = ARGV[1]
vm_id = ARGV[2] vm_id = ARGV[2]
source_ds_id = ARGV[3] source_ds_id = ARGV[3]
check_valid src, "src" check_valid src, 'src'
check_valid dst, "dst" check_valid dst, 'dst'
check_valid vm_id, "vm_id" check_valid vm_id, 'vm_id'
check_valid source_ds_id, "source_ds_id" check_valid source_ds_id, 'source_ds_id'
target_ds_id = dst.split("/")[-3] target_ds_id = dst.split('/')[-3]
disk_id = dst.split(".")[-1] disk_id = dst.split('.')[-1]
src_path_escaped = src.split(":")[-1] src_path_escaped = src.split(':')[-1]
src_path = VCenterDriver::FileHelper.unescape_path(src_path_escaped) src_path = VCenterDriver::FileHelper.unescape_path(src_path_escaped)
hostname = dst.split(":").first hostname = dst.split(':').first
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
host_id = host['ID'] host_id = host['ID']
# Get datastores refs # Get datastores refs
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, source_ds_id) source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore,
source_ds_id)
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF'] source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']
target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, target_ds_id) target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore,
target_ds_id)
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF'] target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']
check_valid source_ds_ref, "source_ds" check_valid source_ds_ref, 'source_ds'
check_valid target_ds_ref, "target_ds" check_valid target_ds_ref, 'target_ds'
# Get VM info # Get VM info
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id) one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
# calculate target path # calculate target path
target_path_escaped = VCenterDriver::FileHelper.get_img_name_from_path(src_path_escaped, target_path_escaped = VCenterDriver::FileHelper
vm_id, .get_img_name_from_path(src_path_escaped, vm_id, disk_id)
disk_id)
target_path = VCenterDriver::FileHelper.unescape_path(target_path_escaped) target_path = VCenterDriver::FileHelper.unescape_path(target_path_escaped)
begin begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id) vi_client = VCenterDriver::VIClient.new_from_host(host_id)
one_disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[SOURCE=\"#{src_path_escaped}\"]").select{|e| e['DISK_ID'] == disk_id}.first rescue nil disk_xpath ="TEMPLATE/DISK[SOURCE=\"#{src_path_escaped}\"]"
one_disk = one_vm.retrieve_xmlelements(disk_xpath) rescue nil
one_disk.select! {|e| e['DISK_ID'] == disk_id }.first if one_disk
raise "Cannot find disk element in vm template" if !one_disk raise 'Cannot find disk element in vm template' unless one_disk
disk = VCenterDriver::VirtualMachine::Disk.one_disk(disk_id, one_disk) disk = VCenterDriver::VirtualMachine::Disk.one_disk(disk_id, one_disk)
new_size = disk.new_size new_size = disk.new_size
@ -98,28 +99,34 @@ begin
is_storage_drs = target_ds_ref.start_with?('group-') is_storage_drs = target_ds_ref.start_with?('group-')
if !(!disk.managed? || (is_storage_drs && disk.volatile?)) if !(!disk.managed? || (is_storage_drs && disk.volatile?))
raise "Non persistent images not supported for StorageDRS datastores" if is_storage_drs if is_storage_drs
raise 'Non persistent images not supported in StorageDRS'
end
source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client) source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref,
vi_client)
if source_ds_ref == target_ds_ref if source_ds_ref == target_ds_ref
target_ds_vc = source_ds_vc target_ds_vc = source_ds_vc
else else
target_ds_vc = VCenterDriver::Storage.new_from_ref(target_ds_ref, vi_client) target_ds_vc = VCenterDriver::Storage.new_from_ref(target_ds_ref,
vi_client)
end end
target_ds_name_vc = target_ds_vc['name'] target_ds_name_vc = target_ds_vc['name']
source_ds_vc.copy_virtual_disk(src_path, target_ds_vc, target_path, new_size) source_ds_vc.copy_virtual_disk(src_path, target_ds_vc,
target_path, new_size)
end end
rescue Exception => e rescue StandardError => e
message = "Error clone virtual disk #{src_path} in "\ message = "Error clone virtual disk #{src_path} in "\
"datastore #{target_ds_name_vc}. "\ "datastore #{target_ds_name_vc}. "\
"Reason: #{e.message}\n#{e.backtrace}" "Reason: #{e.message}."
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -23,19 +23,17 @@
# - remote_system_ds is the path for the system datastore in the host # - remote_system_ds is the path for the system datastore in the host
# - snap_id is the snapshot id. "-1" for none # - snap_id is the snapshot id. "-1" for none
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -44,18 +42,17 @@ require 'vcenter_driver'
src = ARGV[0] src = ARGV[0]
target_path = ARGV[1] target_path = ARGV[1]
snap_id = ARGV[2] #TODO snapshots? # snap_id = ARGV[2]
vmid = ARGV[3] vmid = ARGV[3]
target_ds_id = ARGV[4] target_ds_id = ARGV[4]
check_valid src,"src" check_valid src, 'src'
check_valid target_path,"target_path" check_valid target_path, 'target_path'
check_valid vmid,"vmid" check_valid vmid, 'vmid'
check_valid target_ds_id,"target_ds_id" check_valid target_ds_id, 'target_ds_id'
disk_id = src.split(".")[-1] disk_id = src.split('.')[-1]
source_ds_id = src.split("/")[-3] hostname, src_path = src.split ':'
hostname, src_path = src.split ":"
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
@ -65,7 +62,7 @@ host_id = host['ID']
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid) one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
vm_ref = one_vm['DEPLOY_ID'] vm_ref = one_vm['DEPLOY_ID']
if one_vm['LCM_STATE'].to_i == 26 #ACTIVE / HOTPLUG_SAVEAS if one_vm['LCM_STATE'].to_i == 26 # ACTIVE / HOTPLUG_SAVEAS
STDERR.puts "'disk-saveas' operation is not supported for running VMs." STDERR.puts "'disk-saveas' operation is not supported for running VMs."
exit 1 exit 1
end end
@ -76,8 +73,7 @@ begin
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid) vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
if vm.has_snapshots? if vm.has_snapshots?
raise "'disk-saveas' operation is not supported for VMs with system snapshots." raise "'disk-saveas' not supported in VMs with system snapshots."
exit 1
end end
# Get source and target ds ref # Get source and target ds ref
@ -85,23 +81,26 @@ begin
src_path = disk.path src_path = disk.path
source_ds_ref = disk.ds._ref source_ds_ref = disk.ds._ref
source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client) source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref,
vi_client)
# Get target ds ref # Get target ds ref
target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, target_ds_id, false) target_ds = VCenterDriver::VIHelper
.one_item(OpenNebula::Datastore, target_ds_id, false)
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF'] target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']
target_ds_vc = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client) target_ds_vc = VCenterDriver::Datastore.new_from_ref(target_ds_ref,
vi_client)
target_ds_name_vc = target_ds_vc['name'] target_ds_name_vc = target_ds_vc['name']
source_ds_vc.copy_virtual_disk(src_path, target_ds_vc, target_path) source_ds_vc.copy_virtual_disk(src_path, target_ds_vc, target_path)
rescue StandardError => e
rescue Exception => e
message = "Error copying img #{src_path} to #{target_ds_name_vc} "\ message = "Error copying img #{src_path} to #{target_ds_name_vc} "\
"Reason: #{e.message}\n#{e.backtrace}" "Reason: #{e.message}."
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -23,36 +23,34 @@
# - dsid is the target datastore (0 is the system datastore) # - dsid is the target datastore (0 is the system datastore)
# ---------------------------------------------------------------------------- # # ---------------------------------------------------------------------------- #
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
require 'vcenter_driver' require 'vcenter_driver'
VM_PREFIX_DEFAULT = "one-$i-" VM_PREFIX_DEFAULT = 'one-$i-'
path = ARGV[0] path = ARGV[0]
vmid = ARGV[1] vmid = ARGV[1]
dsid = ARGV[2] dsid = ARGV[2]
check_valid path, "path" check_valid path, 'path'
check_valid vmid, "vmid" check_valid vmid, 'vmid'
check_valid dsid, "dsid" check_valid dsid, 'dsid'
hostname, img_path = path.split(":") hostname, img_path = path.split(':')
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
@ -66,34 +64,33 @@ vm = nil
# tm:delete INIT block: # tm:delete INIT block:
begin begin
@error_message = "error obtaining client and vm" @error_message = 'error obtaining client and vm'
vi_client = VCenterDriver::VIClient.new_from_host(host_id) vi_client = VCenterDriver::VIClient.new_from_host(host_id)
if vm_ref && !vm_ref.empty? unless vm_ref && !vm_ref.empty?
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
else
# we try to get vcenter item # we try to get vcenter item
vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client) vcenter_vm = VCenterDriver::VIHelper
.find_vcenter_vm_by_name(one_vm, host, vi_client)
# If no VM object retrieved, raise an exception # 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 if !vcenter_vm
raise "Could not find the undeployed VM in vCenter using it's name"
end
vm_ref = vcenter_vm._ref vm_ref = vcenter_vm._ref
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
end end
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
vm.one_item = one_vm vm.one_item = one_vm
is_disk = path.match(/disk\.\d+$/) is_disk = path.match(/disk\.\d+$/)
if is_disk if is_disk
dsid = img_path.split("/")[-3] # get dsid from path dsid = img_path.split('/')[-3] # get dsid from path
@error_message = "error deleting disk with #{img_path} in datastore: #{dsid}" @error_message = "error deleting disk with #{img_path} "\
one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid) "in datastore #{dsid}"
ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
# Get disk info, destroy it if is possible # Get disk info, destroy it if possible
disk_id = img_path.split(".")[-1] disk_id = img_path.split('.')[-1]
disk = vm.disk(disk_id) disk = vm.disk(disk_id)
vm.destroy_disk(disk) vm.destroy_disk(disk)
else else
@ -107,12 +104,12 @@ begin
vm.destroy vm.destroy
end end
end end
rescue StandardError => e
rescue Exception => e
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
message = "#{@error_message}. Reason: #{e.message}\n#{e.backtrace}" message = "#{@error_message}. Reason: #{e.message}."
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
end end

View File

@ -24,19 +24,17 @@
# - vmid is the id of the VM # - vmid is the id of the VM
# - dsid is the target datastore (0 is the system datastore) # - dsid is the target datastore (0 is the system datastore)
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -48,13 +46,13 @@ path = ARGV[2]
vmid = ARGV[3] vmid = ARGV[3]
dsid = ARGV[4] dsid = ARGV[4]
check_valid size, "size" check_valid size, 'size'
check_valid path, "path" check_valid path, 'path'
check_valid vmid, "vmid" check_valid vmid, 'vmid'
check_valid dsid, "dsid" check_valid dsid, 'dsid'
hostname, img_name = path.split(":") hostname, img_name = path.split(':')
disk_id = img_name.split(".")[-1] disk_id = img_name.split('.')[-1]
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
@ -68,17 +66,21 @@ ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF']
one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid) one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
# Adapter and disk type from one_vm # Adapter and disk type from one_vm
adapter_type = one_vm["/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/VCENTER_ADAPTER_TYPE"] || disk_xpath = "/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/"
VCenterDriver::VIHelper.get_default("IMAGE/TEMPLATE/VCENTER_ADAPTER_TYPE")
disk_type = one_vm["/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/VCENTER_DISK_TYPE"] ||
VCenterDriver::VIHelper.get_default("IMAGE/TEMPLATE/VCENTER_DISK_TYPE")
check_valid adapter_type, "adapter_type" adapter_type = one_vm[disk_xpath+'VCENTER_ADAPTER_TYPE'] ||
check_valid disk_type, "disk_type" VCenterDriver::VIHelper
.get_default('IMAGE/TEMPLATE/VCENTER_ADAPTER_TYPE')
disk_type = one_vm[disk_xpath+'VCENTER_DISK_TYPE'] ||
VCenterDriver::VIHelper
.get_default('IMAGE/TEMPLATE/VCENTER_DISK_TYPE')
check_valid adapter_type, 'adapter_type'
check_valid disk_type, 'disk_type'
# Volatile images dir from one_vm # Volatile images dir from one_vm
ds_volatile_dir = one_vm["/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/VCENTER_DS_VOLATILE_DIR"] || ds_volatile_dir = one_vm[disk_xpath+'VCENTER_DS_VOLATILE_DIR'] || 'one-volatile'
"one-volatile"
begin begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id) vi_client = VCenterDriver::VIClient.new_from_host(host_id)
@ -90,14 +92,14 @@ begin
if ds_vc.class == VCenterDriver::Datastore if ds_vc.class == VCenterDriver::Datastore
ds_vc.create_virtual_disk(img_name, size, adapter_type, disk_type) ds_vc.create_virtual_disk(img_name, size, adapter_type, disk_type)
end end
rescue StandardError => e
rescue Exception => e
message = "Error creating virtual disk in #{ds_vc['name']}."\ message = "Error creating virtual disk in #{ds_vc['name']}."\
" Reason: #{e.message}\n#{e.backtrace}" " Reason: #{e.message}}."
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -16,19 +16,17 @@
# limitations under the License. # # limitations under the License. #
#--------------------------------------------------------------------------- # #--------------------------------------------------------------------------- #
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -52,20 +50,22 @@ begin
vm = OpenNebula::VirtualMachine.new_with_id(vmid, one_client) vm = OpenNebula::VirtualMachine.new_with_id(vmid, one_client)
vm.info vm.info
src_ds = vm.retrieve_elements("HISTORY_RECORDS/HISTORY/DS_ID")[-2] src_ds = vm.retrieve_elements('HISTORY_RECORDS/HISTORY/DS_ID')[-2]
if src_ds == dsid if src_ds == dsid
VCenterDriver::VirtualMachine.migrate_routine(vmid, host_orig, host_dest) VCenterDriver::VirtualMachine
.migrate_routine(vmid, host_orig, host_dest)
else else
VCenterDriver::VirtualMachine.migrate_routine(vmid, host_orig, host_dest, false, dsid) VCenterDriver::VirtualMachine
.migrate_routine(vmid, host_orig, host_dest, false, dsid)
end end
rescue StandardError => e rescue StandardError => e
message = "Cannot migrate for VM #{vmid}. "\ message = "Cannot migrate for VM #{vmid}. "\
'Failed due to '\ 'Failed due to '\
"\"#{e.message}\"\n" "\"#{e.message}\"\n"
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
end
exit(-1) exit(-1)
end end

View File

@ -24,19 +24,17 @@
# - vmid is the id of the VM # - vmid is the id of the VM
# - dsid is the target datastore (0 is the system datastore) # - dsid is the target datastore (0 is the system datastore)
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -48,10 +46,10 @@ im_path = ARGV[1]
vmid = ARGV[2] vmid = ARGV[2]
dsid = ARGV[3] dsid = ARGV[3]
check_valid path, "path" check_valid path, 'path'
check_valid vmid, "vmid" check_valid vmid, 'vmid'
hostname, img_path = path.split(":") hostname, img_path = path.split(':')
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
@ -62,7 +60,7 @@ one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vmid)
vm_ref = one_vm['DEPLOY_ID'] vm_ref = one_vm['DEPLOY_ID']
# Get image path # Get image path
disk_id = img_path.split(".")[-1] disk_id = img_path.split('.')[-1]
begin begin
vi_client = VCenterDriver::VIClient.new_from_host(host_id) vi_client = VCenterDriver::VIClient.new_from_host(host_id)
@ -70,13 +68,15 @@ begin
vmperst = vm.instantiated_as_persistent? vmperst = vm.instantiated_as_persistent?
vm.remove_all_snapshots if vm.has_snapshots? vm.remove_all_snapshots if vm.has_snapshots?
disk = vm.disk(disk_id) disk = vm.disk(disk_id)
# Don't detach persistent disks if the VM has snapshots # Don't detach persistent disks if the VM has snapshots
if disk && disk.exists? if disk && disk.exists?
vm.one_item = one_vm vm.one_item = one_vm
# Do not detach persistent unmanaged disk, we need them for mark as a template # Do not detach persistent unmanaged disk
# if we need them to create a VM Template
# (instantiate to persistent feature)
vm.detach_disk(disk) if disk.managed? || !vmperst vm.detach_disk(disk) if disk.managed? || !vmperst
if disk.cloned? if disk.cloned?
@ -84,14 +84,14 @@ begin
ds.move_virtual_disk(disk, im_path, dsid, vi_client) ds.move_virtual_disk(disk, im_path, dsid, vi_client)
end end
end end
rescue StandardError => e
rescue Exception => e
message = "Error detaching virtual disk #{disk_id} from vm #{vmid}."\ message = "Error detaching virtual disk #{disk_id} from vm #{vmid}."\
" Reason: #{e.message}\n#{e.backtrace}" " Reason: #{e.message}."
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end

View File

@ -18,19 +18,17 @@
# resize image size vmid # resize image size vmid
ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) ONE_LOCATION ||= ENV['ONE_LOCATION']
if !ONE_LOCATION if !ONE_LOCATION
RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= '/usr/share/one/gems'
else else
RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) GEMS_LOCATION ||= ONE_LOCATION + '/share/gems'
end end
if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION)
Gem.use_paths(GEMS_LOCATION)
end
$LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << File.dirname(__FILE__) $LOAD_PATH << File.dirname(__FILE__)
@ -41,12 +39,12 @@ src = ARGV[0]
new_size = ARGV[1] new_size = ARGV[1]
vmid = ARGV[2] vmid = ARGV[2]
check_valid src,"src" check_valid src, 'src'
check_valid new_size,"new_size" check_valid new_size, 'new_size'
check_valid vmid,"vmid" check_valid vmid, 'vmid'
disk_id = src.split(".")[-1] disk_id = src.split('.')[-1]
hostname, = src.split ":" hostname, = src.split ':'
# Get host ID # Get host ID
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
@ -59,29 +57,29 @@ vm_ref = one_vm['DEPLOY_ID']
begin begin
# This action is only possible when VM in ACTIVE / DISK_RESIZE_POWEROFF # This action is only possible when VM in ACTIVE / DISK_RESIZE_POWEROFF
if one_vm['LCM_STATE'].to_i != 63 if one_vm['LCM_STATE'].to_i != 63
raise "'disk-resize' operation is not supported for vCenter running VMs." raise "'disk-resize' operation not supported for vCenter running VMs."
end end
vi_client = VCenterDriver::VIClient.new_from_host(host_id) vi_client = VCenterDriver::VIClient.new_from_host(host_id)
vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid ) vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
# Cannot resize if VM has snapshots # Cannot resize if VM has snapshots
if vm.has_snapshots? if vm.has_snapshots?
raise "'disk-resize' operation is not supported for VMs with system snapshots." raise "'disk-resize' operation not supported for VMs with snapshots."
end end
disk = vm.disk(disk_id) disk = vm.disk(disk_id)
disk.change_size(new_size) disk.change_size(new_size)
vm.resize_disk(disk) vm.resize_disk(disk)
rescue StandardError => e
rescue Exception => e message = "Error resizing disk #{disk_id} for VM #{one_vm['NAME']} "\
message = "Error resizing disk #{disk_id} for VM #{one_vm["NAME"]} "\ "Reason: #{e.message}."
"Reason: #{e.message}\n#{e.backtrace}"
OpenNebula.log_error(message) OpenNebula.log_error(message)
STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] if VCenterDriver::CONFIG[:debug_information]
STDERR.puts "#{message} #{e.backtrace}"
exit -1 end
exit(-1)
ensure ensure
vi_client.close_connection if vi_client vi_client.close_connection if vi_client
end end