Class: Dorothy::Doro_VSM::ESX

Inherits:
Object
  • Object
show all
Defined in:
lib/dorothy2/VSM.rb

Overview

ESX5 interface

Instance Method Summary collapse

Constructor Details

#initialize(server, user, pass, vmname, guestuser, guestpass) ⇒ ESX

Creates a new instance for communicating with ESX through the vSpere5’s API



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dorothy2/VSM.rb', line 14

def initialize(server,user,pass,vmname,guestuser,guestpass)

  begin
    vim = RbVmomi::VIM.connect(:host => server , :user => user, :password=> pass, :insecure => true)
  rescue Timeout::Error
    raise "Fail to connect to the ESXi server #{server} - TimeOut (Are you sure that is the right address?)"
  end

  @server = server
  dc = vim.serviceInstance.find_datacenter
  @vm = dc.find_vm(vmname)

  raise "Virtual Machine #{vmname} not present within ESX!!" if @vm.nil?

  om = vim.serviceContent.guestOperationsManager
  am = om.authManager
  @pm = om.processManager
  @fm = om.fileManager

  #AUTHENTICATION
  guestauth = {:interactiveSession => false, :username => guestuser, :password => guestpass}
  @auth=RbVmomi::VIM::NamePasswordAuthentication(guestauth)
  abort if am.ValidateCredentialsInGuest(:vm => @vm, :auth => @auth) != nil
end

Instance Method Details

#check_internetObject



73
74
75
# File 'lib/dorothy2/VSM.rb', line 73

def check_internet
   exec_file("windows\\system32\\ping.exe", "-n 1 www.google.com")  #make www.google.com customizable, move to doroconf
end

#copy_file(filename, file) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dorothy2/VSM.rb', line 43

def copy_file(filename,file)
  filepath = "C:\\#{filename}" #put md5 hash

  begin
    url = @fm.InitiateFileTransferToGuest(:vm => @vm, :auth=> @auth, :guestFilePath=> filepath, :fileSize => file.size, :fileAttributes => '', :overwrite => true).sub('*:443', @server)

    RestClient.put(url, file)

  rescue RbVmomi::Fault
    LOGGER.error "VSM", "Fail to copy the file #{file} to #{@vm}: #{$!}"
    abort
  end

end

#exec_file(filename, arguments = "") ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dorothy2/VSM.rb', line 58

def exec_file(filename, arguments="")
  filepath = "C:\\#{filename}"

  if File.extname(filename) == ".dll"
    cmd = { :programPath => "C:\\windows\\system32\\rundll32.exe", :arguments => filepath}
    LOGGER.info "VSM", ".:: Executing dll #{filename}"

  else
    cmd = { :programPath => filepath, :arguments => arguments }
  end

  pid = @pm.StartProgramInGuest(:vm => @vm , :auth => @auth, :spec => cmd )
  pid.to_i
end

#get_status(pid) ⇒ Object



78
79
80
81
82
# File 'lib/dorothy2/VSM.rb', line 78

def get_status(pid)
  p = @pm.ListProcessesInGuest(:vm => @vm , :auth => @auth, :pids => Array(pid) ).inspect
  status = (p =~ /exitCode=>([0-9])/ ? $1.to_i : nil )
  return status
end

#revert_vmObject



39
40
41
# File 'lib/dorothy2/VSM.rb', line 39

def revert_vm
  @vm.RevertToCurrentSnapshot_Task
end

#screenshotObject



85
86
87
88
89
90
# File 'lib/dorothy2/VSM.rb', line 85

def screenshot
  a = @vm.CreateScreenshot_Task.wait_for_completion.split(" ")
  ds = @vm.datastore.find { |ds| ds.name  == a[0].delete("[]")}
  screenpath = "/vmfs/volumes/" + a[0].delete("[]") + "/" + a[1]
  return screenpath
end