Class: Lab::Drivers::RemoteEsxiDriver

Inherits:
VmDriver
  • Object
show all
Defined in:
lib/lab/driver/remote_esxi_driver.rb

Instance Attribute Summary

Attributes inherited from VmDriver

#credentials, #location, #os, #tools, #vmid

Instance Method Summary collapse

Methods inherited from VmDriver

#register, #unregister

Constructor Details

#initialize(config) ⇒ RemoteEsxiDriver

Returns a new instance of RemoteEsxiDriver.



15
16
17
18
19
20
21
22
23
24
# File 'lib/lab/driver/remote_esxi_driver.rb', line 15

def initialize(config)
  unless config['user'] then raise ArgumentError, "Must provide a username" end
  unless config['host'] then raise ArgumentError, "Must provide a hostname" end

  super(config)
  
  @user = filter_command(config['user'])
  @host = filter_command(config['host'])
  @port = config['port']
end

Instance Method Details

#check_file_exists(file) ⇒ Object



125
126
127
# File 'lib/lab/driver/remote_esxi_driver.rb', line 125

def check_file_exists(file)
  raise "Not Implemented"
end

#cleanupObject



133
134
135
# File 'lib/lab/driver/remote_esxi_driver.rb', line 133

def cleanup

end

#copy_from(from, to) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/lab/driver/remote_esxi_driver.rb', line 109

def copy_from(from, to)
  if @os == "linux"
    scp_from(from, to)
  else
    raise "Unimplemented"
  end
end

#copy_to(from, to) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/lab/driver/remote_esxi_driver.rb', line 117

def copy_to(from, to)
  if @os == "linux"
    scp_to(from, to)
  else
    raise "Unimplemented"
  end
end

#create_directory(directory) ⇒ Object



129
130
131
# File 'lib/lab/driver/remote_esxi_driver.rb', line 129

def create_directory(directory)
  raise "Not Implemented"
end

#create_snapshot(snapshot) ⇒ Object



54
55
56
57
58
# File 'lib/lab/driver/remote_esxi_driver.rb', line 54

def create_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  
  remote_system_command("vim-cmd vmsvc/snapshot.create #{@vmid} #{snapshot} \'lab created snapshot\' 1 true")
end

#delete_all_snapshotsObject



101
102
103
# File 'lib/lab/driver/remote_esxi_driver.rb', line 101

def delete_all_snapshots
  remote_system_command("vim-cmd vmsvc/snapshot.removeall #{@vmid}")
end

#delete_snapshot(snapshot, remove_children = false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lab/driver/remote_esxi_driver.rb', line 82

def delete_snapshot(snapshot, remove_children=false)
  snapshots = get_snapshots
  
  # Look through our snapshot list, choose the right one based on display_name    
  snapshots.each do |snapshot_obj|
  
    #puts "DEBUG: checking #{snapshot_obj}"
  
    if snapshot_obj[:display_name].downcase == snapshot.downcase
      snapshot_identifier = snapshot_obj[:name].join(" ")
      remote_system_command("vim-cmd vmsvc/snapshot.remove #{@vmid} #{remove_children} #{snapshot_identifier}")
      return true
    end
  end

  # If we got here, the snapshot didn't exist
  raise "Invalid Snapshot Name"
end

#get_snapshotsObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/lab/driver/remote_esxi_driver.rb', line 143

def get_snapshots
  # Command take the format: 
  # vmware-vim-cmd vmsvc/snapshot.revert [vmid: int] [snapshotlevel: int] [snapshotindex: int]
  output = `ssh #{@user}@#{@host} \"vim-cmd vmsvc/snapshot.get #{@vmid}\"`

  # this keeps track of the snapshots, takes the form:
  #[ {:name => [0,0], :display_name => "String containing the snapshotname}, 
  #  {:name => [0,1], :display_name => "String containing the snapshotname}, ]
  #  ... 
  snapshots = []
  
  # Use these to keep track of the parsing...    
  current_tree = -1
  current_num = 0
  count = 0
  
  # Do the parsing & stick the snapshots in the snapshots array
  output_lines = output.split("\n")
  output_lines.each do |line|
    if line.include?("|") # this is a new snapshot
      if line.include?("ROOT") # it's a root
        current_num = 0
        current_tree = current_tree + 1 # new tree
        snapshots << { :name => [current_num, current_tree], :display_name => output_lines[count+1].split(":").last.strip }
      else
        current_num = current_num + 1 # new snapshot in current tree
        snapshots << { :name => [current_num, current_tree], :display_name => output_lines[count+1].split(":").last.strip }
      end
    end
    count = count+1
  end

snapshots
end

#pauseObject



38
39
40
# File 'lib/lab/driver/remote_esxi_driver.rb', line 38

def pause
  remote_system_command("vim-cmd vmsvc/power.suspend #{@vmid}")
end

#query_snapshotsObject



50
51
52
# File 'lib/lab/driver/remote_esxi_driver.rb', line 50

def query_snapshots
  get_snapshots
end

#resetObject



46
47
48
# File 'lib/lab/driver/remote_esxi_driver.rb', line 46

def reset
  remote_system_command("vim-cmd vmsvc/power.reset #{@vmid}")
end

#resumeObject



42
43
44
# File 'lib/lab/driver/remote_esxi_driver.rb', line 42

def resume
  remote_system_command("vim-cmd vmsvc/power.suspendResume #{@vmid}")
end

#revert_snapshot(snapshot) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lab/driver/remote_esxi_driver.rb', line 60

def revert_snapshot(snapshot)

  snapshots = get_snapshots
  
  # Look through our snapshot list, choose the right one based on display_name    
  snapshots.each do |snapshot_obj|
  
    #puts "DEBUG: checking #{snapshot_obj}"
  
    if snapshot_obj[:display_name].downcase == snapshot.downcase
      snapshot_identifier = snapshot_obj[:name].join(" ")
      
      #puts "DEBUG: I would revert to #{snapshot_obj}"
      remote_system_command("vim-cmd vmsvc/snapshot.revert #{@vmid} 0 #{snapshot_identifier}")
      return true
    end
  end

  # If we got here, we couldn't make it happen
  raise "Unable to revert"
end

#run_command(command) ⇒ Object



105
106
107
# File 'lib/lab/driver/remote_esxi_driver.rb', line 105

def run_command(command)
  raise "Not Implemented"
end

#running?Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/lab/driver/remote_esxi_driver.rb', line 137

def running?
  power_status_string = `ssh #{@user}@#{@host} \"vim-cmd vmsvc/power.getstate #{@vmid}\"`
  return true if power_status_string =~ /Powered on/
false
end

#startObject



26
27
28
# File 'lib/lab/driver/remote_esxi_driver.rb', line 26

def start
  remote_system_command("vim-cmd vmsvc/power.on #{@vmid}")
end

#stopObject



30
31
32
# File 'lib/lab/driver/remote_esxi_driver.rb', line 30

def stop
  remote_system_command("vim-cmd vmsvc/power.off #{@vmid}")
end

#suspendObject



34
35
36
# File 'lib/lab/driver/remote_esxi_driver.rb', line 34

def suspend
  remote_system_command("vim-cmd vmsvc/power.suspend #{@vmid}")
end