Advertisement
Guest User

Untitled

a guest
Feb 5th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.40 KB | None | 0 0
  1. #
  2. # This script runs a command inside the virtual machine
  3. #
  4. # The command is executed by utilizing the VMware Tools running on the VM
  5. # If the tools are not running, this will fail.
  6. # One advantage of using this method to execute a command within the VM,
  7. # is that it does not a require a network connection between CF and the VM.
  8. #
  9.  
  10. begin
  11.   @method = 'vmexec'
  12.   $evm.log("info", "#{@method} - EVM Automate Method Started")
  13.  
  14.   require 'rbvmomi'
  15.  
  16.   def exec_command(vim, vm_ref, guest_user, guest_pass, command, arguments, workdir)
  17.     auth = RbVmomi::VIM::NamePasswordAuthentication({:username => guest_user, :password => guest_pass, :interactiveSession => false})
  18.  
  19.     guest_op_managers = vim.serviceContent.guestOperationsManager
  20.  
  21.     vm = vim.searchIndex.FindByUuid(uuid: vm_ref, vmSearch: true)
  22.  
  23.     prog_spec = RbVmomi::VIM::GuestProgramSpec(
  24.     :programPath => command,
  25.     :arguments => arguments,
  26.     :workingDirectory => workdir)
  27.  
  28.     res = guest_op_managers.processManager.StartProgramInGuest(
  29.     :vm => vm,
  30.     :auth => auth,
  31.     :spec => prog_spec)
  32.  
  33.   end
  34.  
  35.   guest_user = nil
  36.   guest_pass = nil
  37.   guest_user ||= $evm.object['guest_user']
  38.   guest_pass ||= $evm.object.decrypt('guest_pass')
  39.   command    ||= $evm.object['command']
  40.   arguments  ||= $evm.object['arguments']
  41.   workdir    ||= $evm.object['workdir']
  42.  
  43.   vm = $evm.root['vm']
  44.   $evm.log("error", "#{@method} - Missing $evm.root['vm'] object") if vm.nil?
  45.   vm_ref = vm.uid_ems
  46.  
  47.   VIM = RbVmomi::VIM
  48.  
  49.   # Check to ensure that the VM in question is vmware
  50.   vendor = vm.vendor.downcase rescue nil
  51.   $evm.log("info", "#{@method} - Invalid vendor detected: #{vendor}" unless vendor == 'vmware'
  52.  
  53.   # retrieve Provider URL and credentials from the VMDB
  54.   servername = vm.ext_management_system.hostname
  55.   username = vm.ext_management_system.authentication_userid
  56.   password = vm.ext_management_system.authentication_password
  57.  
  58.   # open connection to vCenter
  59.   vim = RbVmomi::VIM.connect host: servername, user: username, password: password, insecure: true
  60.  
  61.   # execute command inside the VM
  62.   exec_command(vim, vm_ref, guest_user, guest_pass, command, arguments, workdir)
  63.  
  64.   #
  65.   # Exit method
  66.   #
  67.   $evm.log("info", "#{@method} - EVM Automate Method Ended")
  68.   exit MIQ_OK
  69.  
  70.   #
  71.   # Set Ruby rescue behavior
  72.   #
  73. rescue => err
  74.   $evm.log("error", "#{@method} - [#{err}]\n#{err.backtrace.join("\n")}")
  75.   exit MIQ_ABORT
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement