diff --git a/src/vmm_mad/remotes/vcenter/attach_disk b/src/vmm_mad/remotes/vcenter/attach_disk index c9ffbf9ded..8b1c603232 100755 --- a/src/vmm_mad/remotes/vcenter/attach_disk +++ b/src/vmm_mad/remotes/vcenter/attach_disk @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -35,7 +35,7 @@ drv_action_enc = ARGV[4] drv_action = OpenNebula::XMLElement.new drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'VMM_DRIVER_ACTION_DATA') -vc_cluster_name = drv_action["/VMM_DRIVER_ACTION_DATA/HOST"] +vc_cluster_name = drv_action['VMM_DRIVER_ACTION_DATA/HOST'] host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] @@ -43,16 +43,15 @@ begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) # Setting one_item with info with the vm_xml including DISK to be added - one_item = drv_action.retrieve_xmlelements("VM").first + one_item = drv_action.retrieve_xmlelements('VM').first vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item) vm.attach_disk - -rescue Exception => e +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) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/attach_nic b/src/vmm_mad/remotes/vcenter/attach_nic index 93ca75ae1a..20672052ba 100755 --- a/src/vmm_mad/remotes/vcenter/attach_nic +++ b/src/vmm_mad/remotes/vcenter/attach_nic @@ -16,16 +16,16 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -42,17 +42,16 @@ begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) # Setting one_item with info with the vm_xml including NIC to be added - one_item = drv_action.retrieve_xmlelements("VM").first + one_item = drv_action.retrieve_xmlelements('VM').first vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item) vm.attach_nic - -rescue Exception => e +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) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/cancel b/src/vmm_mad/remotes/vcenter/cancel index 65f89d84c3..fa0e7b3fa6 100755 --- a/src/vmm_mad/remotes/vcenter/cancel +++ b/src/vmm_mad/remotes/vcenter/cancel @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -47,12 +47,11 @@ begin vm.one_item = drv_action.retrieve_xmlelements('VM').first vm.poweroff_hard - -rescue Exception => e +rescue StandardError => e message = "Cancel VM #{vm_ref} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/deploy b/src/vmm_mad/remotes/vcenter/deploy index 4f67bdb428..060a27713f 100755 --- a/src/vmm_mad/remotes/vcenter/deploy +++ b/src/vmm_mad/remotes/vcenter/deploy @@ -36,7 +36,7 @@ vm_id = ARGV[2] ## Helpers def deploy_id_valid?(deploy_id) - deploy_id && !deploy_id.empty? + deploy_id && !deploy_id.empty? end drv_action = OpenNebula::XMLElement.new @@ -46,8 +46,8 @@ deploy_id = drv_action['DEPLOY_ID'] host_id = drv_action['HISTORY_RECORDS/HISTORY/HID'] begin - vi_client = VCenterDriver::VIClient.new_from_host(host_id) - one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id) + vi_client = VCenterDriver::VIClient.new_from_host(host_id) + one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id) if deploy_id_valid?(deploy_id) # VM is not new, we just need to reconfigure it and to power it on diff --git a/src/vmm_mad/remotes/vcenter/detach_disk b/src/vmm_mad/remotes/vcenter/detach_disk index cdd0d4069e..325587ec8d 100755 --- a/src/vmm_mad/remotes/vcenter/detach_disk +++ b/src/vmm_mad/remotes/vcenter/detach_disk @@ -16,16 +16,16 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -43,14 +43,15 @@ begin vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) - raise "vCenter doesn't allow to remove a virtual disk if it's part of a "\ - "snapshot of the virtual machine." if vm.has_snapshots? - -rescue Exception => e + if vm.has_snapshots? + raise 'vCenter doesn\'t allow to remove a virtual disk if it\'s part of a '\ + 'snapshot of the virtual machine.' + end +rescue StandardError => e message = "Detach DISK for VM #{vm_ref} on vCenter cluster #{vc_cluster_name} "\ "failed due to \"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/detach_nic b/src/vmm_mad/remotes/vcenter/detach_nic index 7e5559a88c..50f537d0cd 100755 --- a/src/vmm_mad/remotes/vcenter/detach_nic +++ b/src/vmm_mad/remotes/vcenter/detach_nic @@ -16,16 +16,16 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -42,16 +42,15 @@ begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) # Setting one_item with info with the vm_xml including NIC to be added - one_item = drv_action.retrieve_xmlelements("VM").first + one_item = drv_action.retrieve_xmlelements('VM').first vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item) vm.detach_nic - -rescue Exception => e +rescue StandardError => e message = "Detach NIC for VM #{vm_ref} on vCenter cluster #{vc_cluster_name} "\ "failed due to \"#{e.message}\"\n" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/migrate b/src/vmm_mad/remotes/vcenter/migrate index 269774df71..8e1bcc2259 100755 --- a/src/vmm_mad/remotes/vcenter/migrate +++ b/src/vmm_mad/remotes/vcenter/migrate @@ -15,17 +15,17 @@ # See the License for the specific language governing permissions and # # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) + +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) - +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -34,7 +34,6 @@ src_host = ARGV[-3] dst_host = ARGV[-4] begin - one_client = OpenNebula::Client.new pool = OpenNebula::HostPool.new(one_client) @@ -43,7 +42,7 @@ begin src_id = pool["/HOST_POOL/HOST[NAME='#{src_host}']/ID"].to_i dst_id = pool["/HOST_POOL/HOST[NAME='#{dst_host}']/ID"].to_i - vi_client = VCenterDriver::VIClient.new_from_host(src_id) + vi_client = VCenterDriver::VIClient.new_from_host(src_id) # required one objects vm = OpenNebula::VirtualMachine.new_with_id(vm_id, one_client) @@ -54,21 +53,21 @@ begin dst_host.info # required vcenter objects - vc_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm["/VM/DEPLOY_ID"]) - vc_host = VCenterDriver::ClusterComputeResource.new_from_ref(dst_host["/HOST/TEMPLATE/VCENTER_CCR_REF"],vi_client).item + vc_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm['/VM/DEPLOY_ID']) + ccr_ref = dst_host['/HOST/TEMPLATE/VCENTER_CCR_REF'] + esx_host = dst_host['HOST/TEMPLATE/HOST/HOSTNAME'] + vc_host = VCenterDriver::ClusterComputeResource.new_from_ref(ccr_ref, vi_client).item - config = {:cluster => vc_host } + config = { :cluster => vc_host } vc_vm.migrate(config) - vm.replace({"VCENTER_CCR_REF" => dst_host["/HOST/TEMPLATE/VCENTER_CCR_REF"], "VCENTER_ESX_HOST" => dst_host["/HOST/TEMPLATE/HOST/HOSTNAME"]}) -rescue Exception => e - + vm.replace({ 'VCENTER_CCR_REF' => ccr_ref, 'VCENTER_ESX_HOST' => esx_host }) +rescue StandardError => e message = "Cannot migrate for VM #{vm_id}"\ - "failed due to "\ + 'failed due to '\ "\"#{e.message}\"\n" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client -end -# raise "Cannot migrate VM #{e.message}\n#{e.backtrace.join("\n")}" \ No newline at end of file +end \ No newline at end of file diff --git a/src/vmm_mad/remotes/vcenter/poll b/src/vmm_mad/remotes/vcenter/poll index f9a45a7ff8..bcfff7795e 100755 --- a/src/vmm_mad/remotes/vcenter/poll +++ b/src/vmm_mad/remotes/vcenter/poll @@ -16,17 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # - -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -34,24 +33,22 @@ vm_ref = ARGV[0] vc_cluster_name = ARGV[1] vm_id = ARGV[2] -host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) +host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vi_client) + vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id) vm.monitor_poll_vm puts vm.info - -rescue Exception => e - message = "Cannot poll info for VM #{vm_ref} on vCenter cluster "\ +rescue StandardError => e + message = "Cannot poll info for VM #{vm_ref} with OpenNebula id #{vm_id} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/prereconfigure b/src/vmm_mad/remotes/vcenter/prereconfigure index 86bcbf2a65..78f0007093 100755 --- a/src/vmm_mad/remotes/vcenter/prereconfigure +++ b/src/vmm_mad/remotes/vcenter/prereconfigure @@ -15,6 +15,7 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # + STDIN=`cat -` exit 0 diff --git a/src/vmm_mad/remotes/vcenter/reboot b/src/vmm_mad/remotes/vcenter/reboot index 57ddb7a79c..78f90ce1d4 100755 --- a/src/vmm_mad/remotes/vcenter/reboot +++ b/src/vmm_mad/remotes/vcenter/reboot @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -41,13 +41,12 @@ begin vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) vm.reboot - -rescue Exception => e +rescue StandardError => e message = "Guest reboot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/reconfigure b/src/vmm_mad/remotes/vcenter/reconfigure index c144a53684..c6ebddd375 100755 --- a/src/vmm_mad/remotes/vcenter/reconfigure +++ b/src/vmm_mad/remotes/vcenter/reconfigure @@ -16,18 +16,19 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -input = STDIN.read +# Avoid pipeline errors by reading and discarding stdin input +STDIN.read -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -35,22 +36,20 @@ vm_ref = ARGV[0] vc_cluster_name = ARGV[-1] vm_id = ARGV[-2] -host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) +host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id) + vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id) vm.regenerate_context - -rescue Exception => e +rescue StandardErrord => e message = "Reconfiguration of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace}" STDERR.puts error_message(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/reset b/src/vmm_mad/remotes/vcenter/reset index 35d9bd8fe7..b2848e793b 100755 --- a/src/vmm_mad/remotes/vcenter/reset +++ b/src/vmm_mad/remotes/vcenter/reset @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -37,17 +37,15 @@ host_id = host['ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) + vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) vm.reset - -rescue Exception => e +rescue StandardError => e message = "Reset of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/restore b/src/vmm_mad/remotes/vcenter/restore index 565c8b238c..f7af2ceb06 100755 --- a/src/vmm_mad/remotes/vcenter/restore +++ b/src/vmm_mad/remotes/vcenter/restore @@ -16,40 +16,37 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' vm_ref = ARGV[2] vc_cluster_name = ARGV[-1] -host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) +host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) + vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) vm.poweron - vm.set_running(true) - -rescue Exception => e +rescue StandardError => e message = "Restore of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/save b/src/vmm_mad/remotes/vcenter/save index 47ee4e7af2..d8c766a49f 100755 --- a/src/vmm_mad/remotes/vcenter/save +++ b/src/vmm_mad/remotes/vcenter/save @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -35,23 +35,23 @@ vc_cluster_name = ARGV[-1] host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] -drv_action_enc = STDIN.read.gsub("\n","") +drv_action_enc = STDIN.read.delete("\n") drv_action = OpenNebula::XMLElement.new drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'VMM_DRIVER_ACTION_DATA') -lcm_state = drv_action["/VMM_DRIVER_ACTION_DATA/VM/LCM_STATE"] -check_valid(lcm_state, "lcm_state") +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 lcm_state_str == "SAVE_MIGRATE" - STDERR.puts "Migration between vCenters cluster not supported" - exit -1 +if lcm_state_str == 'SAVE_MIGRATE' + STDERR.puts 'Migration between vCenters cluster not supported' + exit(-1) end -if !["SAVE_SUSPEND", "SAVE_STOP"].include?(lcm_state_str) - STDERR.puts "Wrong lcm state when saving VM" - exit -1 +if !(%{'SAVE_SUSPEND', 'SAVE_STOP'}).include?(lcm_state_str) + STDERR.puts "Wrong lcm state #{lcm_state_str} }when saving VM" + exit(-1) end begin @@ -61,12 +61,12 @@ begin vm.suspend -rescue Exception => e +rescue StandardError => e message = "Save of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/shutdown b/src/vmm_mad/remotes/vcenter/shutdown index 81bfca70ae..1c074546c6 100755 --- a/src/vmm_mad/remotes/vcenter/shutdown +++ b/src/vmm_mad/remotes/vcenter/shutdown @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -36,22 +36,20 @@ vm_id = ARGV[2] host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name) host_id = host['ID'] -drv_action_enc = STDIN.read.gsub("\n","") +drv_action_enc = STDIN.read.delete("\n") drv_action = OpenNebula::XMLElement.new drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'VMM_DRIVER_ACTION_DATA') -lcm_state = drv_action["/VMM_DRIVER_ACTION_DATA/VM/LCM_STATE"] -check_valid(lcm_state, "lcm_state") +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 !["SHUTDOWN", "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY"].include?(lcm_state_str) - STDERR.puts "Wrong lcm state when shutting down VM" - exit -1 +if !(%{'SHUTDOWN', 'SHUTDOWN_POWEROFF', 'SHUTDOWN_UNDEPLOY'}).include?(lcm_state_str) + STDERR.puts "Wrong lcm state #{lcm_state_str} when shutting down VM" + exit(-1) end -## TODO copy_template - begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) @@ -64,14 +62,13 @@ begin vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) - vm.shutdown #Undeploy, Poweroff or Terminate - -rescue Exception => e + vm.shutdown # Undeploy, Poweroff or Terminate +rescue StandardError => e message = "Shutdown of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} failed due to "\ "\"#{e.message}\"\n#{e.backtrace.join("\n")}" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/snapshot_create b/src/vmm_mad/remotes/vcenter/snapshot_create index 2584320648..b7c4c59bf6 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_create +++ b/src/vmm_mad/remotes/vcenter/snapshot_create @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -44,27 +44,26 @@ snap_name = drv_action["VM/TEMPLATE/SNAPSHOT[ACTIVE='YES']/NAME"] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - one_vm = drv_action.retrieve_xmlelements("VM").first + 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]") + persistent_disks = one_vm.retrieve_xmlelements('TEMPLATE/DISK[PERSISTENT=YES]') if !persistent_disks.empty? STDERR.puts "Snapshot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} cannot be performed if "\ - "it contains persistent disks" + 'it contains persistent disks.' exit 1 end puts vm.create_snapshot(snap_id, snap_name) -rescue Exception => e +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) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/snapshot_delete b/src/vmm_mad/remotes/vcenter/snapshot_delete index f332490907..50cfc1ba3e 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_delete +++ b/src/vmm_mad/remotes/vcenter/snapshot_delete @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -38,17 +38,15 @@ host_id = host['ID'] begin vi_client = VCenterDriver::VIClient.new_from_host(host_id) - - vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) + vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) vm.delete_snapshot(snap_id) - -rescue Exception => e +rescue StandardError => e message = "Snapshot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} could not be deleted due to "\ "\"#{e.message}\"\n" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/snapshot_revert b/src/vmm_mad/remotes/vcenter/snapshot_revert index 3ab077d119..f351ba8994 100755 --- a/src/vmm_mad/remotes/vcenter/snapshot_revert +++ b/src/vmm_mad/remotes/vcenter/snapshot_revert @@ -16,16 +16,16 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = '/usr/lib/one/ruby' unless defined?(RUBY_LIB_LOCATION) else - RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) + RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' unless defined?(RUBY_LIB_LOCATION) end -$: << RUBY_LIB_LOCATION -$: << File.dirname(__FILE__) +$LOAD_PATH << RUBY_LIB_LOCATION +$LOAD_PATH << File.dirname(__FILE__) require 'vcenter_driver' @@ -42,13 +42,12 @@ begin vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref) vm.revert_snapshot(snap_id) - -rescue Exception => e +rescue StandardError => e message = "Snapshot of VM #{vm_ref} on vCenter cluster "\ "#{vc_cluster_name} could not be reverted due "\ "to \"#{e.message}\"\n" OpenNebula.log_error(message) - exit -1 + exit(-1) ensure vi_client.close_connection if vi_client end diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index ec906aed10..e799669d94 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -18,25 +18,25 @@ # Set up the environment for the driver # # ---------------------------------------------------------------------------- # -ONE_LOCATION = ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION) if !ONE_LOCATION - BIN_LOCATION = "/usr/bin" if !defined?(BIN_LOCATION) - LIB_LOCATION = "/usr/lib/one" if !defined?(LIB_LOCATION) - ETC_LOCATION = "/etc/one/" if !defined?(ETC_LOCATION) - VAR_LOCATION = "/var/lib/one" if !defined?(VAR_LOCATION) + BIN_LOCATION = '/usr/bin' unless defined?(BIN_LOCATION) + LIB_LOCATION = '/usr/lib/one' unless defined?(LIB_LOCATION) + ETC_LOCATION = '/etc/one/' unless defined?(ETC_LOCATION) + VAR_LOCATION = '/var/lib/one' unless defined?(VAR_LOCATION) else - BIN_LOCATION = ONE_LOCATION + "/bin" if !defined?(BIN_LOCATION) - LIB_LOCATION = ONE_LOCATION + "/lib" if !defined?(LIB_LOCATION) - ETC_LOCATION = ONE_LOCATION + "/etc/" if !defined?(ETC_LOCATION) - VAR_LOCATION = ONE_LOCATION + "/var/" if !defined?(VAR_LOCATION) + BIN_LOCATION = ONE_LOCATION + '/bin' unless defined?(BIN_LOCATION) + LIB_LOCATION = ONE_LOCATION + '/lib' unless defined?(LIB_LOCATION) + ETC_LOCATION = ONE_LOCATION + '/etc/' unless defined?(ETC_LOCATION) + VAR_LOCATION = ONE_LOCATION + '/var/' unless defined?(VAR_LOCATION) end ENV['LANG'] = 'C' -$: << LIB_LOCATION + '/ruby/vendors/rbvmomi/lib' -$: << LIB_LOCATION + '/ruby' -$: << LIB_LOCATION + '/ruby/vcenter_driver' +$LOAD_PATH << LIB_LOCATION + '/ruby/vendors/rbvmomi/lib' +$LOAD_PATH << LIB_LOCATION + '/ruby' +$LOAD_PATH << LIB_LOCATION + '/ruby/vcenter_driver' require 'rbvmomi' require 'yaml' @@ -70,13 +70,13 @@ def error_message(message) error_str << message error_str << "\nERROR MESSAGE ------>8--" - return error_str + error_str end def check_valid(parameter, label) if parameter.nil? || parameter.empty? STDERR.puts error_message("The parameter '#{label}' is required for this action.") - exit -1 + exit(-1) end end @@ -85,12 +85,12 @@ def check_item(item, target_class) item.name if CHECK_REFS if target_class if !item.instance_of?(target_class) - raise "Expecting type 'RbVmomi::VIM::#{target_class}'. " << + raise "Expecting type 'RbVmomi::VIM::#{target_class}'. " \ "Got '#{item.class} instead." end end rescue RbVmomi::Fault => e - raise "Reference \"#{item._ref}\" error. The reference does not exist" + raise "Reference \"#{item._ref}\" error [#{e.message}]. The reference does not exist" end end @@ -98,9 +98,8 @@ def wait_deploy_timeout(vm, timeout_deploy = 300) time_start = Time.now begin time_running = Time.now - time_start - if time_running.to_i >= timeout_deploy - raise "reached deploy timeout" - end + raise 'reached deploy timeout' if time_running.to_i >= timeout_deploy + sleep(2) state = vm.item.summary.runtime.powerState end until(state == "poweredOn")