From 9128a58ede9ab173e9b9625d44b12145977041a1 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Wed, 4 Dec 2019 19:43:16 +0100 Subject: [PATCH] L #-: Changes for rubocop linting In all vcenter VMM driver scripts (cherry picked from commit d0e8d214c61533f1937f59021d2c2a725a43d377) --- share/linters/.rubocop.yml | 15 ------- src/vmm_mad/remotes/vcenter/attach_disk | 21 +++++----- src/vmm_mad/remotes/vcenter/attach_nic | 19 +++++---- src/vmm_mad/remotes/vcenter/cancel | 34 ++++++---------- src/vmm_mad/remotes/vcenter/deploy | 34 ++++++---------- src/vmm_mad/remotes/vcenter/detach_disk | 22 +++++------ src/vmm_mad/remotes/vcenter/detach_nic | 21 +++++----- src/vmm_mad/remotes/vcenter/migrate | 10 ++--- src/vmm_mad/remotes/vcenter/poll | 19 +++++---- src/vmm_mad/remotes/vcenter/reboot | 19 +++++---- src/vmm_mad/remotes/vcenter/reconfigure | 23 ++++++----- src/vmm_mad/remotes/vcenter/reset | 19 +++++---- src/vmm_mad/remotes/vcenter/restore | 19 +++++---- src/vmm_mad/remotes/vcenter/save | 22 +++++------ src/vmm_mad/remotes/vcenter/shutdown | 44 ++++++++++----------- src/vmm_mad/remotes/vcenter/snapshot_create | 24 +++++------ src/vmm_mad/remotes/vcenter/snapshot_delete | 19 +++++---- src/vmm_mad/remotes/vcenter/snapshot_revert | 19 +++++---- 18 files changed, 170 insertions(+), 233 deletions(-) diff --git a/share/linters/.rubocop.yml b/share/linters/.rubocop.yml index 68dc8cea02..49683e7f46 100644 --- a/share/linters/.rubocop.yml +++ b/share/linters/.rubocop.yml @@ -102,21 +102,6 @@ AllCops: - src/vnm_mad/remotes/ovswitch_vxlan/post - src/vnm_mad/remotes/ovswitch_vxlan/clean - src/vnm_mad/remotes/ovswitch_vxlan/pre - - src/vmm_mad/remotes/vcenter/attach_nic - - src/vmm_mad/remotes/vcenter/cancel - - src/vmm_mad/remotes/vcenter/snapshot_revert - - src/vmm_mad/remotes/vcenter/detach_nic - - src/vmm_mad/remotes/vcenter/snapshot_delete - - src/vmm_mad/remotes/vcenter/detach_disk - - src/vmm_mad/remotes/vcenter/shutdown - - src/vmm_mad/remotes/vcenter/attach_disk - - src/vmm_mad/remotes/vcenter/reboot - - src/vmm_mad/remotes/vcenter/reset - - src/vmm_mad/remotes/vcenter/reconfigure - - src/vmm_mad/remotes/vcenter/save - - src/vmm_mad/remotes/vcenter/restore - - src/vmm_mad/remotes/vcenter/snapshot_create - - src/vmm_mad/remotes/vcenter/poll - src/vmm_mad/remotes/lxd/attach_nic - src/vmm_mad/remotes/lxd/detach_nic - src/vmm_mad/remotes/lxd/detach_disk diff --git a/src/vmm_mad/remotes/vcenter/attach_disk b/src/vmm_mad/remotes/vcenter/attach_disk index 99ad6fb9ba..634319328b 100755 --- a/src/vmm_mad/remotes/vcenter/attach_disk +++ b/src/vmm_mad/remotes/vcenter/attach_disk @@ -16,26 +16,24 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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' -vm_ref = ARGV[0] +vm_ref = ARGV[0] drv_action = OpenNebula::XMLElement.new @@ -59,8 +57,9 @@ rescue StandardError => e message = "Attach image for VM #{vm_ref} on vCenter cluster" \ "#{vc_cluster_name} 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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/attach_nic b/src/vmm_mad/remotes/vcenter/attach_nic index 5da4d21787..2ebb4e2a77 100755 --- a/src/vmm_mad/remotes/vcenter/attach_nic +++ b/src/vmm_mad/remotes/vcenter/attach_nic @@ -16,19 +16,17 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -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__) @@ -59,8 +57,9 @@ rescue StandardError => e message = "Attach NIC for VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} 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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/cancel b/src/vmm_mad/remotes/vcenter/cancel index 600df35493..1781a9bc69 100755 --- a/src/vmm_mad/remotes/vcenter/cancel +++ b/src/vmm_mad/remotes/vcenter/cancel @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -38,7 +36,6 @@ require 'vcenter_driver' CONFIG = VCenterConf.new vm_ref = ARGV[0] -host = ARGV[1] vm_id = ARGV[-2] drv_action = OpenNebula::XMLElement.new @@ -46,10 +43,6 @@ drv_action.initialize_xml(Base64.decode64(STDIN.read), 'VMM_DRIVER_ACTION_DATA') host_id = drv_action['VM/HISTORY_RECORDS/HISTORY/HID'] -lcm_state = drv_action['/VMM_DRIVER_ACTION_DATA/VM/LCM_STATE'] -check_valid(lcm_state, 'lcm_state') -lcm_state_str = OpenNebula::VirtualMachine::LCM_STATE[lcm_state.to_i] - begin retries ||= 0 vi_client = VCenterDriver::VIClient.new_from_host(host_id) @@ -58,17 +51,12 @@ begin vm.poweroff_hard rescue StandardError => e - if (retries += 1) < CONFIG[:retries] - message = "Cancel VM #{vm_ref} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the attempt \##{retries}\n#{e.backtrace.join("\n")}" - else - message = "Cancel VM #{vm_ref} failed due to "\ - "\"#{e.message}\"\n#{e.backtrace.join("\n")} "\ - "on the final attempt\n#{e.backtrace.join("\n")}" - end + message = "Cancel VM #{vm_ref} failed due to \"#{e.message}\""\ + "on attempt \##{retries}" 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 sleep CONFIG[:retry_interval].to_i retry if retries < CONFIG[:retries] diff --git a/src/vmm_mad/remotes/vcenter/deploy b/src/vmm_mad/remotes/vcenter/deploy index 8402d153a2..3504edc6f3 100755 --- a/src/vmm_mad/remotes/vcenter/deploy +++ b/src/vmm_mad/remotes/vcenter/deploy @@ -16,25 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) +ONE_LOCATION ||= ENV['ONE_LOCATION'] if !ONE_LOCATION - unless defined?(RUBY_LIB_LOCATION) - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' - end - - GEMS_LOCATION = '/usr/share/one/gems' unless defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - unless defined?(RUBY_LIB_LOCATION) - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' - end - - 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__) @@ -84,14 +76,10 @@ begin puts vm['_ref'] rescue StandardError => e - if (retries += 1) < CONFIG[:retries] - message = "Deploy of VM #{vm_id} on vCenter cluster #{cluster_name} " \ - "with #{dfile} failed due to \"#{e.message}\"" \ - "on the attempt \##{retries}." - else - message = "Deploy of VM #{vm_id} on vCenter cluster #{cluster_name} " \ - "with #{dfile} failed due to \"#{e.message}\"" \ - 'on the final attempt.' + message = "Deploy of VM #{vm_id} on vCenter cluster #{cluster_name} " \ + "with #{dfile} failed due to \"#{e.message}\"" \ + "on attempt \##{retries}." + unless (retries += 1) < CONFIG[:retries] if e.message.include?('ManagedObjectNotFound') template_id = drv_action['TEMPLATE/TEMPLATE_ID'].to_i @@ -105,7 +93,7 @@ rescue StandardError => e template_name = resource.name message << "\nVerify inside vCenter cluster #{cluster_name} " \ - "that the template #{template_name} currently " \ + "that template #{template_name} currently " \ 'exists and has not been deleted' end end diff --git a/src/vmm_mad/remotes/vcenter/detach_disk b/src/vmm_mad/remotes/vcenter/detach_disk index 8233a57836..abca3c90ae 100755 --- a/src/vmm_mad/remotes/vcenter/detach_disk +++ b/src/vmm_mad/remotes/vcenter/detach_disk @@ -16,18 +16,14 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -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) -end - -if File.directory?(GEMS_LOCATION) - Gem.use_paths(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end $LOAD_PATH << RUBY_LIB_LOCATION @@ -46,14 +42,14 @@ host_id = drv_action['VM/HISTORY_RECORDS/HISTORY/HID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) - + VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) rescue StandardError => e message = "Detach DISK for VM #{vm_ref} on vCenter cluster " \ "#{vc_cluster_name} failed due to \"#{e.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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/detach_nic b/src/vmm_mad/remotes/vcenter/detach_nic index 5c8a44d61d..1f351eda6e 100755 --- a/src/vmm_mad/remotes/vcenter/detach_nic +++ b/src/vmm_mad/remotes/vcenter/detach_nic @@ -16,19 +16,17 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -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__) @@ -52,15 +50,16 @@ begin # Extract nic from driver action one_nic = one_item.retrieve_xmlelements("TEMPLATE/NIC[ATTACH='YES']").first - mac = one_nic["MAC"] + mac = one_nic['MAC'] vm.detach_nic(mac) rescue StandardError => e message = "Detach NIC for VM #{vm_ref} on vCenter cluster " \ "#{vc_cluster_name} failed due to \"#{e.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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/migrate b/src/vmm_mad/remotes/vcenter/migrate index dee11831e7..37dde17a93 100755 --- a/src/vmm_mad/remotes/vcenter/migrate +++ b/src/vmm_mad/remotes/vcenter/migrate @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) +ONE_LOCATION ||= ENV['ONE_LOCATION'] if !ONE_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 RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' - GEMS_LOCATION = ONE_LOCATION + '/share/gems' unless defined?(GEMS_LOCATION) + 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__) diff --git a/src/vmm_mad/remotes/vcenter/poll b/src/vmm_mad/remotes/vcenter/poll index 662976a717..45cebfd8a1 100755 --- a/src/vmm_mad/remotes/vcenter/poll +++ b/src/vmm_mad/remotes/vcenter/poll @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -56,8 +54,9 @@ rescue StandardError => e " on vCenter cluster #{vc_cluster_name} failed due to " \ "\"#{e.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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/reboot b/src/vmm_mad/remotes/vcenter/reboot index eb0fb4d738..0f5852b0e4 100755 --- a/src/vmm_mad/remotes/vcenter/reboot +++ b/src/vmm_mad/remotes/vcenter/reboot @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -55,8 +53,9 @@ rescue StandardError => e "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/reconfigure b/src/vmm_mad/remotes/vcenter/reconfigure index e46c3c0304..28fdf7b328 100755 --- a/src/vmm_mad/remotes/vcenter/reconfigure +++ b/src/vmm_mad/remotes/vcenter/reconfigure @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -42,7 +40,7 @@ vm_id = ARGV[-2] drv_action = OpenNebula::XMLElement.new drv_action.initialize_xml(Base64.decode64(STDIN.read), 'VMM_DRIVER_ACTION_DATA') -host_id = drv_action['VM/HISTORY_RECORDS/HISTORY[last()]/HID'] +host_id = drv_action['VM/HISTORY_RECORDS/HISTORY[last()]/HID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) @@ -53,9 +51,10 @@ rescue StandardErrord => e message = "Reconfiguration of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"." - STDERR.puts error_message(message) - STDERR.puts "#{e.backtrace}" if VCenterDriver::CONFIG[:debug_information] - + 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 diff --git a/src/vmm_mad/remotes/vcenter/reset b/src/vmm_mad/remotes/vcenter/reset index 3be0d2ba90..40460f263c 100755 --- a/src/vmm_mad/remotes/vcenter/reset +++ b/src/vmm_mad/remotes/vcenter/reset @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -54,8 +52,9 @@ rescue StandardError => e "#{vc_cluster_name} failed due to "\ "\"#{e.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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/restore b/src/vmm_mad/remotes/vcenter/restore index 3798b60a4d..1e040d81f4 100755 --- a/src/vmm_mad/remotes/vcenter/restore +++ b/src/vmm_mad/remotes/vcenter/restore @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -54,8 +52,9 @@ rescue StandardError => e "#{vc_cluster_name} failed due to "\ "\"#{e.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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/save b/src/vmm_mad/remotes/vcenter/save index ddc64d82ba..24083d31bb 100755 --- a/src/vmm_mad/remotes/vcenter/save +++ b/src/vmm_mad/remotes/vcenter/save @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -36,7 +34,6 @@ $LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' vm_ref = ARGV[0] -vm_id = ARGV[3] vc_cluster_name = ARGV[-1] drv_action_enc = STDIN.read.delete("\n") @@ -49,7 +46,7 @@ lcm_state = drv_action['/VMM_DRIVER_ACTION_DATA/VM/LCM_STATE'] check_valid(lcm_state, 'lcm_state') lcm_state_str = OpenNebula::VirtualMachine::LCM_STATE[lcm_state.to_i] -if !(%{'SAVE_SUSPEND', 'SAVE_STOP', 'SAVE_MIGRATE'}).include?(lcm_state_str) +if !%('SAVE_SUSPEND', 'SAVE_STOP', 'SAVE_MIGRATE').include?(lcm_state_str) STDERR.puts "Wrong lcm state #{lcm_state_str} when saving VM" exit(-1) end @@ -65,8 +62,9 @@ rescue StandardError => e "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/shutdown b/src/vmm_mad/remotes/vcenter/shutdown index 4a7d38b2b3..5888821f64 100755 --- a/src/vmm_mad/remotes/vcenter/shutdown +++ b/src/vmm_mad/remotes/vcenter/shutdown @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -52,7 +50,8 @@ lcm_state = drv_action['/VMM_DRIVER_ACTION_DATA/VM/LCM_STATE'] check_valid(lcm_state, 'lcm_state') lcm_state_str = OpenNebula::VirtualMachine::LCM_STATE[lcm_state.to_i] -if !(%{'SAVE_MIGRATE', 'SHUTDOWN', 'SHUTDOWN_POWEROFF', 'SHUTDOWN_UNDEPLOY'}).include?(lcm_state_str) +if !%('SAVE_MIGRATE', 'SHUTDOWN', 'SHUTDOWN_POWEROFF', \ + 'SHUTDOWN_UNDEPLOY').include?(lcm_state_str) STDERR.puts "Wrong lcm state #{lcm_state_str} when shutting down VM" exit(-1) end @@ -62,10 +61,14 @@ begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) if vm_ref.empty? - one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id) - host = OpenNebula::Host.new_with_id(host_id, OpenNebula::Client.new()) - vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client) - raise "Could not find the undeployed VM in vCenter's inventory using it's name" if !vcenter_vm + one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, + vm_id) + host = OpenNebula::Host.new_with_id(host_id, OpenNebula::Client.new) + vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, + host, + vi_client) + raise "Couldn't find the VM in vCenter by it's name" unless vcenter_vm + vm_ref = vcenter_vm._ref end @@ -73,17 +76,12 @@ begin vm.shutdown # Undeploy, Poweroff or Terminate rescue StandardError => e - if (retries += 1) < CONFIG[:retries] - message = "Shutdown of VM #{vm_ref} on vCenter cluster "\ - "#{vc_cluster_name} failed due to "\ - "\"#{e.message}\" on the attempt \##{retries}\n#{e.backtrace.join("\n")}" - else - message = "Shutdown of VM #{vm_ref} on vCenter cluster "\ - "#{vc_cluster_name} failed due to "\ - "\"#{e.message}\" on the final attempt\n#{e.backtrace.join("\n")}" - end + message = "Shutdown of VM #{vm_ref} on vCenter cluster #{vc_cluster_name} "\ + "failed due to \"#{e.message}\" on attempt \##{retries}" 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 sleep CONFIG[:retry_interval].to_i retry if retries < CONFIG[:retries] diff --git a/src/vmm_mad/remotes/vcenter/snapshot_create b/src/vmm_mad/remotes/vcenter/snapshot_create index d9e90536b0..53389955b9 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_create +++ b/src/vmm_mad/remotes/vcenter/snapshot_create @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -52,9 +50,7 @@ begin one_vm = drv_action.retrieve_xmlelements('VM').first vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_vm) - persistent_disks = one_vm.retrieve_xmlelements('TEMPLATE/DISK[PERSISTENT=YES]') - - if !persistent_disks.empty? + unless one_vm.retrieve_xmlelements('TEMPLATE/DISK[PERSISTENT=YES]').empty? STDERR.puts "Snapshot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} cannot be performed if "\ 'it contains persistent disks.' @@ -62,14 +58,14 @@ begin end puts vm.create_snapshot(snap_id, snap_name) - rescue StandardError => e message = "Snapshot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} 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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/snapshot_delete b/src/vmm_mad/remotes/vcenter/snapshot_delete index b279572a0f..f4ed77f384 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_delete +++ b/src/vmm_mad/remotes/vcenter/snapshot_delete @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -54,8 +52,9 @@ rescue StandardError => e "#{vc_cluster_name} could not be deleted 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) ensure vi_client.close_connection if vi_client diff --git a/src/vmm_mad/remotes/vcenter/snapshot_revert b/src/vmm_mad/remotes/vcenter/snapshot_revert index 9257cb9283..6c9680b250 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_revert +++ b/src/vmm_mad/remotes/vcenter/snapshot_revert @@ -16,19 +16,17 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -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__) @@ -55,8 +53,9 @@ rescue StandardError => e "#{vc_cluster_name} could not be reverted 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) ensure vi_client.close_connection if vi_client