Class: Dorothy::Doro_VSM::ESX

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

Overview

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ESX.



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

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



71
72
73
# File 'lib/dorothy2/MAM.rb', line 71

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dorothy2/MAM.rb', line 41

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dorothy2/MAM.rb', line 56

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



76
77
78
79
80
# File 'lib/dorothy2/MAM.rb', line 76

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



37
38
39
# File 'lib/dorothy2/MAM.rb', line 37

def revert_vm
  @vm.RevertToCurrentSnapshot_Task
end

#screenshotObject



83
84
85
86
87
88
# File 'lib/dorothy2/MAM.rb', line 83

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