Class: Dorothy::Doro_VSM::ESX

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

Overview

ESX vSphere5 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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dorothy2/VSM.rb', line 13

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}
  r = 0
  begin
    @auth=RbVmomi::VIM::NamePasswordAuthentication(guestauth)
    abort if am.ValidateCredentialsInGuest(:vm => @vm, :auth => @auth) != nil
  rescue RbVmomi::Fault => e
    if e.inspect =~ /InvalidPowerState/
      if r <= 5
        r = r+1
        LOGGER.debug "VSM", "VM busy (maybe still revertig, retrying.."
        sleep 2
        retry
      end
      LOGGER.error "VSM", "Error, can't connect to VM #{@vm[:name]}"
      LOGGER.debug "VSM", e
      raise "VSM Error"
    end
  end

end

Instance Method Details

#check_internetObject



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

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

#copy_file(filename, file) ⇒ Object



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

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, program) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/dorothy2/VSM.rb', line 73

def exec_file(filename, program)
  program["prog_args"].nil? ? args = "" : args = program["prog_args"]
  args += " #{filename}"
  cmd = { :programPath => program["prog_path"], :arguments => args }
  pid = @pm.StartProgramInGuest(:vm => @vm , :auth => @auth, :spec => cmd )
  pid.to_i
end

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



81
82
83
84
85
86
# File 'lib/dorothy2/VSM.rb', line 81

def exec_file_raw(filename, arguments="")
  filepath = "C:\\#{filename}"
  cmd = { :programPath => filepath, :arguments => arguments }
  pid = @pm.StartProgramInGuest(:vm => @vm , :auth => @auth, :spec => cmd )
  pid.to_i
end

#get_files(path) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/dorothy2/VSM.rb', line 118

def get_files(path)
  fm_files = @fm.ListFilesInGuest(:vm => @vm, :auth=> @auth, :filePath=> path).files
  @files = Hash.new
  fm_files.each {|file|
    @files.merge!(Hash[file.path, Hash[:size, file.size, :type, file.type, :attrs, file.attributes]])
  }
  @files
end

#get_new_procs(current_procs, original_procs_file) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/dorothy2/VSM.rb', line 109

def get_new_procs(current_procs, original_procs_file)
  original_procs = YAML.load_file(original_procs_file)
  @new_procs = Hash.new
  current_procs.each_key {|pid|
    @new_procs.merge!(Hash[pid, current_procs[pid]]) unless original_procs.has_key?(pid)
  }
  @new_procs
end

#get_running_procs(pid = nil, save_tofile = false, filename = "#{DoroSettings.env[:home]}/etc/baseline_processes.yml") ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dorothy2/VSM.rb', line 97

def get_running_procs(pid=nil, save_tofile=false, filename="#{DoroSettings.env[:home]}/etc/baseline_processes.yml")
  pid = Array(pid) unless pid.nil?
  @pp2 = Hash.new
  procs = @pm.ListProcessesInGuest(:vm => @vm , :auth => @auth, :pids => pid )
  procs.each {|pp2| @pp2.merge! Hash[pp2.pid, Hash["pname", pp2.name, "owner", pp2.owner, "cmdLine", pp2.cmdLine, "startTime", pp2.startTime, "endTime", pp2.endTime, "exitCode", pp2.exitCode]]}
  if save_tofile
    Util.write(filename, @pp2.to_yaml)
    LOGGER.info "VSM", "Current running processes saved to #{filename}"
  end
  @pp2
end

#get_status(pid) ⇒ Object



92
93
94
95
# File 'lib/dorothy2/VSM.rb', line 92

def get_status(pid)
  p = get_running_procs(pid)
  p["exitCode"]
end

#revert_vmObject



54
55
56
# File 'lib/dorothy2/VSM.rb', line 54

def revert_vm
  @vm.RevertToCurrentSnapshot_Task
end

#screenshotObject



127
128
129
130
131
# File 'lib/dorothy2/VSM.rb', line 127

def screenshot
  a = @vm.CreateScreenshot_Task.wait_for_completion.split(" ")
  screenpath = "/vmfs/volumes/" + a[0].delete("[]") + "/" + a[1]
  return screenpath
end