Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Description: run VMware guest command via the vSphere API
- #
- begin
- require 'rbvmomi'
- rescue LoadError
- `gem install rbvmomi`
- sleep 1.minute
- require 'rbvmomi'
- end
- # basic retry logic
- def retry_method(retry_time=1.minute)
- $evm.log(:info, "Sleeping #{retry_time}")
- $evm.root['ae_result'] = 'retry'
- $evm.root['ae_retry_interval'] = retry_time
- exit MIQ_OK
- end
- def exec_command(vim, vm_ref, cmd, args)
- working_dir = '/'
- vim = RbVmomi::VIM.connect(host: @esx, user: @esx_userid, password: @esx_password, insecure: true)
- $evm.log(:info, "#{cmd} #{args}")
- guest_auth = RbVmomi::VIM::NamePasswordAuthentication({
- :username => guest_username,
- :password => guest_password,
- :interactiveSession => false
- })
- gom = vim.serviceContent.guestOperationsManager
- vm = vim.searchIndex.FindByUuid(uuid: vm_ref, vmSearch: true)
- prog_spec = RbVmomi::VIM::GuestProgramSpec(
- :programPath => cmd,
- :arguments => args,
- :workingDirectory => working_dir
- )
- res = gom.processManager.StartProgramInGuest(
- :vm => vm,
- :auth => guest_auth,
- :spec => prog_spec
- )
- end
- # Grab the VM object
- $evm.log(:info, "vmdb_object_type: #{$evm.root['vmdb_object_type']}")
- case $evm.root['vmdb_object_type']
- when 'miq_provision'
- prov = $evm.root['miq_provision']
- vm = prov.vm unless prov.nil?
- else
- vm = $evm.root['vm']
- end
- raise 'VM object is empty' if vm.nil?
- unless vm.vendor.downcase == 'vmware'
- $evm.log(:warn, "Only VMware supported currently, exiting gracefully")
- exit MIQ_OK
- end
- vm_ref = vm.uid_ems
- @esx = vm.ext_management_system.hostname
- @esx_userid = vm.ext_management_system.authentication_userid
- @esx_password = vm.ext_management_system.authentication_password
- guest_username = $evm.root['dialog_guest_username']
- guest_password = $evm.root.decrypt('dialog_guest_password')
- # $evm.log(:info, "guest_username: #{guest_username}")
- # $evm.log(:info, "guest_password: #{guest_password}")
- $evm.log(:info, "VM: #{vm.name}")
- $evm.log(:info, "Tags: #{vm.tags}")
- if guest_username.nil? || guest_password.nil?
- # Catch provisioning without Chef
- 10.times { $evm.log(:warn, "Required parameters missing, exiting gracefully") }
- exit MIQ_OK
- end
- $evm.log(:info, "VM power state: #{vm.power_state}")
- if vm.power_state == 'off'
- $evm.log(:info, "Starting VM...")
- vm.start
- end
- ###################################
- # Wait until VM is on the network
- ###################################
- unless vm.ipaddresses.empty?
- non_zeroconf = false
- vm.ipaddresses.each do |ipaddr|
- non_zeroconf = true unless ipaddr.match(/^(169.254|0)/)
- $evm.log(:info, "VM:<#{vm.name}> IP Address found #{ipaddr} (#{non_zeroconf})")
- end
- if non_zeroconf
- $evm.log(:info, "VM:<#{vm.name}> IP addresses:<#{vm.ipaddresses.inspect}> present.")
- $evm.root['ae_result'] = 'ok'
- else
- $evm.log(:warn, "VM:<#{vm.name}> IP addresses:<#{vm.ipaddresses.inspect}> not present.")
- retry_method("15.seconds")
- end
- else
- $evm.log(:warn, "VM:<#{vm.name}> IP addresses:<#{vm.ipaddresses.inspect}> not present.")
- vm.refresh
- retry_method("15.seconds")
- end
- if vm.hostnames.empty? || vm.hostnames.first.blank?
- $evm.log(:info, "Waiting for vm hostname to populate")
- vm.refresh
- retry_method("15.seconds")
- end
- ###################################
- # Execute Run Once
- ###################################
- begin
- # http://www.rubydoc.info/github/rlane/rbvmomi/RbVmomi/VIM
- #vim = RbVmomi::VIM.connect(host: @esx, user: @esx_userid, password: @esx_password, insecure: true)
- vim = nil
- # Bail unless linux and vmware
- unless vm.operating_system[:product_name].downcase.include?('linux') && vm.vendor.downcase == 'vmware'
- log(:warn, "Invalid vendor or product found")
- exit MIQ_STOP
- end
- $evm.log(:info, "===> Executing RUNONCE! <===")
- cmd, args = '/cfme/runonce.sh', ""
- $evm.log(:info, "CMD: #{cmd}, ARGS: #{args}")
- exec_command(vim, vm_ref, cmd, args)
- $evm.log(:info, "===> We're done!!!!!!!! <===")
- rescue => err
- $evm.log(:error, "[#{err}]\n#{err.backtrace.join("\n")}")
- # [InvalidGuestLogin: Failed to authenticate with the guest operating system using the supplied credentials.]
- # [GuestOperationsUnavailable: The guest operations agent could not be contacted.]
- if err.to_s.match(/InvalidGuestLogin|GuestOperationsUnavailable/i)
- $evm.log(:info, "Waiting for SysPrep to complete")
- retry_method("15.seconds")
- else
- exit MIQ_STOP
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement