Class: Lab::Drivers::WorkstationDriver

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

Instance Attribute Summary

Attributes inherited from VmDriver

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

Instance Method Summary collapse

Methods inherited from VmDriver

#register, #resume, #unregister

Constructor Details

#initialize(config) ⇒ WorkstationDriver

Returns a new instance of WorkstationDriver.



12
13
14
15
16
17
18
# File 'lib/lab/driver/workstation_driver.rb', line 12

def initialize(config)
  super(config)
  
  if !File.exist?(@location)
    raise ArgumentError,"Couldn't find: #{@location}"
  end
end

Instance Method Details

#check_file_exists(file) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/lab/driver/workstation_driver.rb', line 190

def check_file_exists(file)
  file = filter_input(file)
  if @tools
    vmrunstr = "vmrun -T ws -gu \'#{@vm_user}\' -gp \'#{@vm_pass}\' fileExistsInGuest " +
      "\'#{@location}\' \'#{file}\'"
    system_command(vmrunstr)
  else
    raise "Unsupported"
  end
end

#cleanupObject



212
213
214
# File 'lib/lab/driver/workstation_driver.rb', line 212

def cleanup

end

#copy_from(from, to) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/lab/driver/workstation_driver.rb', line 166

def copy_from(from, to)
  from = filter_input(from)
  to = filter_input(to)
  if @tools
    vmrunstr = "vmrun -T ws -gu \'#{@vm_user}\' -gp \'#{@vm_pass}\' copyFileFromGuestToHost " +
        "\'#{@location}\' \'#{from}\' \'#{to}\'" 
  else
    scp_from(from, to)
  end
  system_command(vmrunstr)
end

#copy_to(from, to) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/lab/driver/workstation_driver.rb', line 178

def copy_to(from, to)
  from = filter_input(from)
  to = filter_input(to)
  if @tools
    vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} copyFileFromHostToGuest " +
      "\'#{@location}\' \'#{from}\' \'#{to}\'"
    system_command(vmrunstr)
  else
    scp_to(from, to)
  end
end

#create_directory(directory) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/lab/driver/workstation_driver.rb', line 201

def create_directory(directory)
  directory = filter_input(directory)
  if @tools
    vmrunstr = "vmrun -T ws -gu \'#{@vm_user}\' -gp \'#{@vm_pass}\' createDirectoryInGuest " +
        " \'#{@location}\' \'#{directory}\' "
    system_command(vmrunstr)
  else
    raise "Unsupported"
  end
end

#create_snapshot(snapshot) ⇒ Object



40
41
42
43
# File 'lib/lab/driver/workstation_driver.rb', line 40

def create_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("vmrun -T ws snapshot " + "\'#{@location}\' \'#{snapshot}\' nogui")
end

#delete_snapshot(snapshot) ⇒ Object



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

def delete_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("vmrun -T ws deleteSnapshot " + "\'#{@location}\' \'#{snapshot}\' nogui" )
end

#pauseObject



32
33
34
# File 'lib/lab/driver/workstation_driver.rb', line 32

def pause
  system_command("vmrun -T ws pause " + "\'#{@location}\' nogui")
end

#resetObject



36
37
38
# File 'lib/lab/driver/workstation_driver.rb', line 36

def reset
  system_command("vmrun -T ws reset " + "\'#{@location}\' nogui")
end

#revert_snapshot(snapshot) ⇒ Object



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

def revert_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("vmrun -T ws revertToSnapshot " + "\'#{@location}\' \'#{snapshot}\' nogui")
end

#run_command(command) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/lab/driver/workstation_driver.rb', line 55

def run_command(command)

  #
  # Generate a script name
  #
  script_rand_name = rand(1000000)

  #
  # Configure paths for each OS - We really can't filter command, so we're gonna 
  # stick it in a script
  #
  if @os == "windows"
    local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.bat"
    remote_tempfile_path = "C:\\\\lab_script_#{script_rand_name}.bat"
    remote_output_file = "C:\\\\lab_command_output_#{script_rand_name}"
    remote_run_command = remote_tempfile_path
    File.open(local_tempfile_path, 'w') {|f| f.write(command) }
  else
    
    local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
    remote_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
    remote_output_file = "/tmp/lab_command_output_#{script_rand_name}"
    local_output_file = "/tmp/lab_command_output_#{script_rand_name}"
    
    remote_run_command = remote_tempfile_path
    
    File.open(local_tempfile_path, 'w') {|f| f.write("#!/bin/sh\n#{command}\n")}
  end
  
  if @tools

    #puts "DEBUG: Running w/ tools"

    #
    # Copy our local tempfile to the guest
    #
    vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
        "copyFileFromHostToGuest \'#{@location}\' \'#{local_tempfile_path}\'" +
        " \'#{remote_tempfile_path}\'"
    system_command(vmrunstr)

    if @os == "linux"
      #
      # Now run the command directly on the guest (linux - call w/ /bin/sh)
      #
      vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
          "runProgramInGuest \'#{@location}\' /bin/sh #{remote_tempfile_path} > #{remote_output_file}"
      system_command(vmrunstr)
    else
      #
      # Now run the command directly on the guest (windows)
      #
      vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
          "runProgramInGuest \'#{@location}\' #{remote_tempfile_path} > #{remote_output_file}" 
      system_command(vmrunstr)
    end
    
    #
    # Cleanup. Delete it on the guest
    #
    vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + 
        "deleteFileInGuest \'#{@location}\' \'#{remote_tempfile_path}\'"
    system_command(vmrunstr)

    #
    # Delete it locally
    #
    local_delete_command = "rm #{local_tempfile_path}"
    system_command(local_delete_command)
  else

    #
    # Use SCP / SSH
    #

    if @os == "linux"
      
      #
      # Copy it over
      #
      scp_to(local_tempfile_path, remote_tempfile_path)

      #
      # And ... execute it
      #
      ssh_exec("/bin/sh #{remote_tempfile_path} > #{remote_output_file}")

      #
      # Now copy the output back to us
      #
      scp_from(remote_output_file, local_output_file)

      # Now, let's look at the output of the command
      output_string = File.open(local_output_file,"r").read

      #
      # And clean up
      #
      ssh_exec("rm #{remote_output_file}")
      ssh_exec("rm #{remote_tempfile_path}")
      
      `rm #{local_output_file}`

    else
      raise "Hey, no tools, and windows? can't do nuttin for ya man."
    end
    
  end
output_string
end

#running?Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/lab/driver/workstation_driver.rb', line 216

def running?
  ## Get running Vms
  running = `vmrun list`
  running_array = running.split("\n")
  running_array.shift

  running_array.each do |vmx|
    if vmx.to_s == @location.to_s
      return true
    end
  end

  return false
end

#startObject



20
21
22
# File 'lib/lab/driver/workstation_driver.rb', line 20

def start
  system_command("vmrun -T ws start " + "\'#{@location}\' nogui")
end

#stopObject



24
25
26
# File 'lib/lab/driver/workstation_driver.rb', line 24

def stop
  system_command("vmrun -T ws stop " + "\'#{@location}\' nogui")
end

#suspendObject



28
29
30
# File 'lib/lab/driver/workstation_driver.rb', line 28

def suspend
  system_command("vmrun -T ws suspend " + "\'#{@location}\' nogui")
end