From 58f83540155ef2c9dd7ad1477333213a77955751 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Wed, 18 Dec 2019 12:38:24 +0100 Subject: [PATCH] L #-: Linting for vCenter TM (cherry picked from commit fbde74b6f6be84a96f1a0b0f48fc0edac503d69b) --- share/linters/.rubocop.yml | 9 -- src/datastore_mad/remotes/vcenter/monitor | 46 +++++---- src/datastore_mad/remotes/vcenter/rm | 110 +++++++++------------- src/datastore_mad/remotes/vcenter/stat | 45 +++++---- src/tm_mad/vcenter/clone | 75 ++++++++------- src/tm_mad/vcenter/cpds | 55 ++++++----- src/tm_mad/vcenter/delete | 63 ++++++------- src/tm_mad/vcenter/mkimage | 58 ++++++------ src/tm_mad/vcenter/mv | 28 +++--- src/tm_mad/vcenter/mvds | 40 ++++---- src/tm_mad/vcenter/resize | 44 +++++---- 11 files changed, 274 insertions(+), 299 deletions(-) diff --git a/share/linters/.rubocop.yml b/share/linters/.rubocop.yml index 839a82c198..efa85a8156 100644 --- a/share/linters/.rubocop.yml +++ b/share/linters/.rubocop.yml @@ -62,7 +62,6 @@ AllCops: - src/pm_mad/remotes/ec2/reset - src/pm_mad/remotes/ec2/poll - src/onegate/config.ru - - src/datastore_mad/remotes/vcenter/monitor - src/datastore_mad/remotes/vcenter/mkfs - src/datastore_mad/remotes/vcenter/stat - src/datastore_mad/remotes/vcenter/clone @@ -166,14 +165,6 @@ AllCops: - src/market_mad/remotes/s3/monitor - src/market_mad/remotes/s3/delete - 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/Gemfile - src/cli/oneprovision diff --git a/src/datastore_mad/remotes/vcenter/monitor b/src/datastore_mad/remotes/vcenter/monitor index f6697ef669..1bfdaba45d 100755 --- a/src/datastore_mad/remotes/vcenter/monitor +++ b/src/datastore_mad/remotes/vcenter/monitor @@ -20,42 +20,41 @@ # 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' -def is_not_dsid_or_dsref_valid?(ds_id, ds_ref) - return ds_id.nil? || ds_ref.nil? +def not_dsid_or_dsref_valid?(ds_id, ds_ref) + ds_id.nil? || ds_ref.nil? end drv_action_enc = ARGV[0] id = ARGV[1] 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_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"] -check_valid ds_ref, "ds_ref" +ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID'] +ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF'] +check_valid ds_ref, 'ds_ref' -if is_not_dsid_or_dsref_valid?(ds_id, ds_ref) - STDERR.puts "Not enough information to monitor the datastore." - exit -1 +if not_dsid_or_dsref_valid?(ds_id, ds_ref) + STDERR.puts 'Not enough information to monitor the datastore.' + exit(-1) end begin @@ -63,14 +62,13 @@ begin storage = VCenterDriver::Storage.new_from_ref(ds_ref, vi_client) puts storage.monitor - -rescue Exception => e - message = "Error monitoring datastore #{id}."\ - " Reason: \"#{e.message}\"\n#{e.backtrace}" +rescue StandardError => e + message = "Error monitoring datastore #{id}. Reason: \"#{e.message}\"." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/datastore_mad/remotes/vcenter/rm b/src/datastore_mad/remotes/vcenter/rm index aa3e008248..2effbc57bc 100755 --- a/src/datastore_mad/remotes/vcenter/rm +++ b/src/datastore_mad/remotes/vcenter/rm @@ -20,54 +20,50 @@ # 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' -def was_img_imported?(imported) - return imported.nil? || imported.empty? +def img_imported?(imported) + imported.nil? || imported.empty? end drv_action_enc = ARGV[0] -id = ARGV[1] 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_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/ID"] -img_src = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/SOURCE"] -imported = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/VCENTER_IMPORTED"] +ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF'] +ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID'] +img_src = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/SOURCE'] +imported = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/TEMPLATE/VCENTER_IMPORTED'] -check_valid ds_ref, "ds_ref" -check_valid img_src, "img_src" +check_valid ds_ref, 'ds_ref' +check_valid img_src, 'img_src' -CDROM = "1" +CDROM = '1' begin - if was_img_imported?(imported) + if img_imported?(imported) vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id) - ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client) 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 ds_name = ds['name'] @@ -77,69 +73,57 @@ begin search_params = ds.get_search_params(ds_name, img_path, img_name) # 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 if img_type != CDROM # delete the disk begin - retries ||= 0 - ds.delete_virtual_disk(img_src) + retries ||= 0 + ds.delete_virtual_disk(img_src) rescue StandardError => e - - if (retries += 1) < VCenterDriver::CONFIG[:retries] message = "Delete DISK #{img_name} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the attempt \##{retries}\n#{e.backtrace.join("\n")}" - else - message = "Delete DISK #{img_name} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the final attempt\n#{e.backtrace.join("\n")}" - end - 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) + "\"#{e.message}\" on the attempt \##{retries}." + OpenNebula.log_error(message) + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + sleep VCenterDriver::CONFIG[:retry_interval].to_i + retry if retries < VCenterDriver::CONFIG[:retries] + exit(-1) end else # delete the CDROM iso begin - retries ||= 0 - ds.delete_file(img_src) + retries ||= 0 + ds.delete_file(img_src) rescue StandardError => e - - if (retries += 1) < VCenterDriver::CONFIG[:retries] message = "Delete CDROM #{img_name} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the attempt \##{retries}\n#{e.backtrace.join("\n")}" - else - message = "Delete CDROM #{img_name} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the final attempt\n#{e.backtrace.join("\n")}" - end - 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) + "\"#{e.message}\" on the attempt \##{retries}." + OpenNebula.log_error(message) + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + sleep VCenterDriver::CONFIG[:retry_interval].to_i + retry if retries < VCenterDriver::CONFIG[:retries] + exit(-1) end end - + # Erase folder if empty ds.rm_directory(img_dir) if ds.dir_empty?(img_dir) end -rescue Exception => e +rescue StandardError => e if !e.message.start_with?('FileNotFound') message = "Error deleting virtual disk #{img_src}."\ " Reason: \"#{e.message}\"\n#{e.backtrace}" OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) end ensure vi_client.close_connection if vi_client diff --git a/src/datastore_mad/remotes/vcenter/stat b/src/datastore_mad/remotes/vcenter/stat index aa6c5c6005..02518dc932 100755 --- a/src/datastore_mad/remotes/vcenter/stat +++ b/src/datastore_mad/remotes/vcenter/stat @@ -20,19 +20,17 @@ # 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -40,31 +38,32 @@ $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' drv_action_enc = ARGV[0] -id = ARGV[1] 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_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/ID"] -img_path = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"] +ds_ref = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF'] +ds_id = drv_action['/DS_DRIVER_ACTION_DATA/DATASTORE/ID'] +img_path = drv_action['/DS_DRIVER_ACTION_DATA/IMAGE/PATH'] -check_valid ds_ref, "ds_ref" -check_valid img_path, "img_path" +check_valid ds_ref, 'ds_ref' +check_valid img_path, 'img_path' -if img_path.start_with? "vcenter://" +if img_path.start_with? 'vcenter://' begin vi_client = VCenterDriver::VIClient.new_from_datastore(ds_id) - ds = VCenterDriver::Datastore.new_from_ref(ds_ref, vi_client) - puts ds.stat(img_path.sub("vcenter://","")) - - rescue Exception => e + puts ds.stat(img_path.sub('vcenter://', '')) + rescue StandardError => e message = "Error calculating image #{img_path} size."\ - " Reason: \"#{e.message}\"\n#{e.backtrace}" - STDERR.puts error_message(message) - exit -1 + " Reason: \"#{e.message}\"." + OpenNebula.log_error(message) + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/tm_mad/vcenter/clone b/src/tm_mad/vcenter/clone index 7c8e305cde..fa8b053356 100755 --- a/src/tm_mad/vcenter/clone +++ b/src/tm_mad/vcenter/clone @@ -24,19 +24,17 @@ # - vmid is the id of the VM # - 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -48,48 +46,51 @@ dst = ARGV[1] vm_id = ARGV[2] source_ds_id = ARGV[3] -check_valid src, "src" -check_valid dst, "dst" -check_valid vm_id, "vm_id" -check_valid source_ds_id, "source_ds_id" +check_valid src, 'src' +check_valid dst, 'dst' +check_valid vm_id, 'vm_id' +check_valid source_ds_id, 'source_ds_id' -target_ds_id = dst.split("/")[-3] -disk_id = dst.split(".")[-1] +target_ds_id = dst.split('/')[-3] +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) -hostname = dst.split(":").first +hostname = dst.split(':').first # Get host ID host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) host_id = host['ID'] # 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'] -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'] -check_valid source_ds_ref, "source_ds" -check_valid target_ds_ref, "target_ds" +check_valid source_ds_ref, 'source_ds' +check_valid target_ds_ref, 'target_ds' # Get VM info one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id) # calculate target path -target_path_escaped = VCenterDriver::FileHelper.get_img_name_from_path(src_path_escaped, - vm_id, - disk_id) +target_path_escaped = VCenterDriver::FileHelper + .get_img_name_from_path(src_path_escaped, vm_id, disk_id) target_path = VCenterDriver::FileHelper.unescape_path(target_path_escaped) begin 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) new_size = disk.new_size @@ -98,28 +99,34 @@ begin is_storage_drs = target_ds_ref.start_with?('group-') 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 target_ds_vc = source_ds_vc 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 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 -rescue Exception => e +rescue StandardError => e message = "Error clone virtual disk #{src_path} in "\ "datastore #{target_ds_name_vc}. "\ - "Reason: #{e.message}\n#{e.backtrace}" + "Reason: #{e.message}." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/tm_mad/vcenter/cpds b/src/tm_mad/vcenter/cpds index ce20a02343..1a0aa1e310 100755 --- a/src/tm_mad/vcenter/cpds +++ b/src/tm_mad/vcenter/cpds @@ -23,19 +23,17 @@ # - remote_system_ds is the path for the system datastore in the host # - 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -44,18 +42,17 @@ require 'vcenter_driver' src = ARGV[0] target_path = ARGV[1] -snap_id = ARGV[2] #TODO snapshots? +# snap_id = ARGV[2] vmid = ARGV[3] target_ds_id = ARGV[4] -check_valid src,"src" -check_valid target_path,"target_path" -check_valid vmid,"vmid" -check_valid target_ds_id,"target_ds_id" +check_valid src, 'src' +check_valid target_path, 'target_path' +check_valid vmid, 'vmid' +check_valid target_ds_id, 'target_ds_id' -disk_id = src.split(".")[-1] -source_ds_id = src.split("/")[-3] -hostname, src_path = src.split ":" +disk_id = src.split('.')[-1] +hostname, src_path = src.split ':' # Get host ID 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) 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." exit 1 end @@ -76,8 +73,7 @@ begin vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid) if vm.has_snapshots? - raise "'disk-saveas' operation is not supported for VMs with system snapshots." - exit 1 + raise "'disk-saveas' not supported in VMs with system snapshots." end # Get source and target ds ref @@ -85,23 +81,26 @@ begin src_path = disk.path 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 - 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_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'] source_ds_vc.copy_virtual_disk(src_path, target_ds_vc, target_path) - -rescue Exception => e +rescue StandardError => e message = "Error copying img #{src_path} to #{target_ds_name_vc} "\ - "Reason: #{e.message}\n#{e.backtrace}" + "Reason: #{e.message}." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/tm_mad/vcenter/delete b/src/tm_mad/vcenter/delete index 3d4f1d3fe3..22dd487352 100755 --- a/src/tm_mad/vcenter/delete +++ b/src/tm_mad/vcenter/delete @@ -23,36 +23,34 @@ # - 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' -VM_PREFIX_DEFAULT = "one-$i-" +VM_PREFIX_DEFAULT = 'one-$i-' path = ARGV[0] vmid = ARGV[1] dsid = ARGV[2] -check_valid path, "path" -check_valid vmid, "vmid" -check_valid dsid, "dsid" +check_valid path, 'path' +check_valid vmid, 'vmid' +check_valid dsid, 'dsid' -hostname, img_path = path.split(":") +hostname, img_path = path.split(':') # Get host ID host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) @@ -66,34 +64,33 @@ vm = nil # tm:delete INIT block: begin - @error_message = "error obtaining client and vm" + @error_message = 'error obtaining client and vm' vi_client = VCenterDriver::VIClient.new_from_host(host_id) - if vm_ref && !vm_ref.empty? - vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid) - else + unless vm_ref && !vm_ref.empty? # 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 - 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 = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid) end + vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid) vm.one_item = one_vm is_disk = path.match(/disk\.\d+$/) - if is_disk - dsid = img_path.split("/")[-3] # get dsid from path - @error_message = "error deleting disk with #{img_path} in datastore: #{dsid}" - one_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, dsid) - ds_ref = one_ds['TEMPLATE/VCENTER_DS_REF'] + dsid = img_path.split('/')[-3] # get dsid from path + @error_message = "error deleting disk with #{img_path} "\ + "in datastore #{dsid}" - # Get disk info, destroy it if is possible - disk_id = img_path.split(".")[-1] + # Get disk info, destroy it if possible + disk_id = img_path.split('.')[-1] disk = vm.disk(disk_id) vm.destroy_disk(disk) else @@ -107,12 +104,12 @@ begin vm.destroy end end - -rescue Exception => e +rescue StandardError => e 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) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) end diff --git a/src/tm_mad/vcenter/mkimage b/src/tm_mad/vcenter/mkimage index 21ed0465c8..b6f0e93529 100755 --- a/src/tm_mad/vcenter/mkimage +++ b/src/tm_mad/vcenter/mkimage @@ -24,19 +24,17 @@ # - vmid is the id of the VM # - 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -48,13 +46,13 @@ path = ARGV[2] vmid = ARGV[3] dsid = ARGV[4] -check_valid size, "size" -check_valid path, "path" -check_valid vmid, "vmid" -check_valid dsid, "dsid" +check_valid size, 'size' +check_valid path, 'path' +check_valid vmid, 'vmid' +check_valid dsid, 'dsid' -hostname, img_name = path.split(":") -disk_id = img_name.split(".")[-1] +hostname, img_name = path.split(':') +disk_id = img_name.split('.')[-1] # Get host ID 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) # Adapter and disk type from one_vm -adapter_type = one_vm["/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/VCENTER_ADAPTER_TYPE"] || - 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") +disk_xpath = "/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/" -check_valid adapter_type, "adapter_type" -check_valid disk_type, "disk_type" +adapter_type = one_vm[disk_xpath+'VCENTER_ADAPTER_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 -ds_volatile_dir = one_vm["/VM/TEMPLATE/DISK[DISK_ID=#{disk_id}]/VCENTER_DS_VOLATILE_DIR"] || - "one-volatile" +ds_volatile_dir = one_vm[disk_xpath+'VCENTER_DS_VOLATILE_DIR'] || 'one-volatile' begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) @@ -90,14 +92,14 @@ begin if ds_vc.class == VCenterDriver::Datastore ds_vc.create_virtual_disk(img_name, size, adapter_type, disk_type) end - -rescue Exception => e +rescue StandardError => e message = "Error creating virtual disk in #{ds_vc['name']}."\ - " Reason: #{e.message}\n#{e.backtrace}" + " Reason: #{e.message}}." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/tm_mad/vcenter/mv b/src/tm_mad/vcenter/mv index 52b0522ae7..896aaf9569 100755 --- a/src/tm_mad/vcenter/mv +++ b/src/tm_mad/vcenter/mv @@ -16,19 +16,17 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) +ONE_LOCATION ||= ENV['ONE_LOCATION'] if !ONE_LOCATION - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -52,20 +50,22 @@ begin vm = OpenNebula::VirtualMachine.new_with_id(vmid, one_client) 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 - VCenterDriver::VirtualMachine.migrate_routine(vmid, host_orig, host_dest) + VCenterDriver::VirtualMachine + .migrate_routine(vmid, host_orig, host_dest) else - VCenterDriver::VirtualMachine.migrate_routine(vmid, host_orig, host_dest, false, dsid) + VCenterDriver::VirtualMachine + .migrate_routine(vmid, host_orig, host_dest, false, dsid) end - rescue StandardError => e message = "Cannot migrate for VM #{vmid}. "\ 'Failed due to '\ "\"#{e.message}\"\n" 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) end diff --git a/src/tm_mad/vcenter/mvds b/src/tm_mad/vcenter/mvds index abd3573b6d..557b9adb3a 100755 --- a/src/tm_mad/vcenter/mvds +++ b/src/tm_mad/vcenter/mvds @@ -24,19 +24,17 @@ # - vmid is the id of the VM # - 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 - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -48,10 +46,10 @@ im_path = ARGV[1] vmid = ARGV[2] dsid = ARGV[3] -check_valid path, "path" -check_valid vmid, "vmid" +check_valid path, 'path' +check_valid vmid, 'vmid' -hostname, img_path = path.split(":") +hostname, img_path = path.split(':') # Get host ID 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'] # Get image path -disk_id = img_path.split(".")[-1] +disk_id = img_path.split('.')[-1] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) @@ -70,13 +68,15 @@ begin vmperst = vm.instantiated_as_persistent? 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 if disk && disk.exists? 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 if disk.cloned? @@ -84,14 +84,14 @@ begin ds.move_virtual_disk(disk, im_path, dsid, vi_client) end end - -rescue Exception => e +rescue StandardError => e message = "Error detaching virtual disk #{disk_id} from vm #{vmid}."\ - " Reason: #{e.message}\n#{e.backtrace}" + " Reason: #{e.message}." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/tm_mad/vcenter/resize b/src/tm_mad/vcenter/resize index 4ea87f5eb0..2ad239a5e3 100755 --- a/src/tm_mad/vcenter/resize +++ b/src/tm_mad/vcenter/resize @@ -18,19 +18,17 @@ # resize image size vmid -ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) +ONE_LOCATION ||= ENV['ONE_LOCATION'] if !ONE_LOCATION - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) -end +Gem.use_paths(GEMS_LOCATION) if File.directory?(GEMS_LOCATION) $LOAD_PATH << RUBY_LIB_LOCATION $LOAD_PATH << File.dirname(__FILE__) @@ -41,12 +39,12 @@ src = ARGV[0] new_size = ARGV[1] vmid = ARGV[2] -check_valid src,"src" -check_valid new_size,"new_size" -check_valid vmid,"vmid" +check_valid src, 'src' +check_valid new_size, 'new_size' +check_valid vmid, 'vmid' -disk_id = src.split(".")[-1] -hostname, = src.split ":" +disk_id = src.split('.')[-1] +hostname, = src.split ':' # Get host ID host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname) @@ -59,29 +57,29 @@ vm_ref = one_vm['DEPLOY_ID'] begin # This action is only possible when VM in ACTIVE / DISK_RESIZE_POWEROFF 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 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 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 disk = vm.disk(disk_id) disk.change_size(new_size) vm.resize_disk(disk) - -rescue Exception => e - message = "Error resizing disk #{disk_id} for VM #{one_vm["NAME"]} "\ - "Reason: #{e.message}\n#{e.backtrace}" +rescue StandardError => e + message = "Error resizing disk #{disk_id} for VM #{one_vm['NAME']} "\ + "Reason: #{e.message}." OpenNebula.log_error(message) - STDERR.puts "#{message} #{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - - exit -1 + if VCenterDriver::CONFIG[:debug_information] + STDERR.puts "#{message} #{e.backtrace}" + end + exit(-1) ensure vi_client.close_connection if vi_client end