2011-01-17 05:58:16 +00:00
|
|
|
require 'vm_driver'
|
|
|
|
|
|
|
|
##
|
|
|
|
## $Id$
|
|
|
|
##
|
|
|
|
|
|
|
|
module Lab
|
|
|
|
module Drivers
|
|
|
|
|
|
|
|
class RemoteWorkstationDriver < VmDriver
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
attr_accessor :location # among other things
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def initialize(vmid, location, os=nil, tools=false, user=nil, host=nil, credentials=nil)
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
## TODO - Should proabably check file existence?
|
2011-01-17 05:58:16 +00:00
|
|
|
unless user then raise ArgumentError, "Must provide a username" end
|
|
|
|
unless host then raise ArgumentError, "Must provide a hostname" end
|
2011-05-24 15:56:32 +00:00
|
|
|
|
|
|
|
@vmid = filter_input(vmid)
|
2011-02-08 07:26:18 +00:00
|
|
|
@location = filter_input(location)
|
|
|
|
@user = filter_input(user)
|
2011-02-16 02:15:24 +00:00
|
|
|
@host = filter_input(host)
|
2011-05-24 15:56:32 +00:00
|
|
|
@credentials = credentials # individually filtered
|
|
|
|
@tools = tools # not used in command lines, no filter
|
|
|
|
@os = os # not used in command lines, no filter
|
|
|
|
|
|
|
|
# TODO - Currently only implemented for the first set
|
|
|
|
if @credentials.count > 0
|
|
|
|
@vm_user = filter_input(@credentials[0]['user'])
|
|
|
|
@vm_pass = filter_input(@credentials[0]['pass'])
|
|
|
|
@vm_keyfile = filter_input(@credentials[0]['keyfile'])
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws start \'#{@location}\' nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws stop \'#{@location}\' nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def suspend
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws suspend \'#{@location}\' nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pause
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws pause \'#{@location}\' nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws reset \'#{@location}\' nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-02-11 15:04:33 +00:00
|
|
|
def create_snapshot(snapshot)
|
2011-02-16 02:15:24 +00:00
|
|
|
snapshot = filter_input(snapshot)
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws snapshot \'#{@location}\' #{snapshot} nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-02-11 15:04:33 +00:00
|
|
|
def revert_snapshot(snapshot)
|
2011-02-16 02:15:24 +00:00
|
|
|
snapshot = filter_input(snapshot)
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws revertToSnapshot \'#{@location}\' #{snapshot} nogui\"")
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_snapshot(snapshot)
|
2011-02-16 02:15:24 +00:00
|
|
|
snapshot = filter_input(snapshot)
|
2011-05-25 07:30:22 +00:00
|
|
|
system_command("ssh #{@user}@#{@host} \"vmrun -T ws deleteSnapshot \'#{@location}\' #{snapshot} nogui\"" )
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def run_command(command)
|
|
|
|
# generate local & remote script paths
|
|
|
|
script_rand_name = rand(10000)
|
|
|
|
|
|
|
|
if @os == "windows"
|
|
|
|
local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.bat"
|
|
|
|
remote_tempfile_path = "C:\\\\lab_script_#{script_rand_name}.bat"
|
|
|
|
remote_run_command = remote_tempfile_path
|
|
|
|
else
|
|
|
|
local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
|
|
|
|
remote_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
|
|
|
|
remote_run_command = "/bin/sh #{remote_tempfile_path}"
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
# write out our script locally
|
|
|
|
File.open(local_tempfile_path, 'w') {|f| f.write(command) }
|
|
|
|
|
|
|
|
# we really can't filter command, so we're gonna stick it in a script
|
|
|
|
if @tools
|
|
|
|
# copy it to the vm host - this is because we're a remote driver
|
|
|
|
remote_copy_command = "scp #{local_tempfile_path} #{@user}@#{@host}:#{local_tempfile_path}"
|
|
|
|
system_command(remote_copy_command)
|
|
|
|
|
|
|
|
# we have it on the vm host, copy it to the vm guest
|
|
|
|
vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"copyFileFromHostToGuest \'#{@location}\' \'#{local_tempfile_path}\' " +
|
|
|
|
"\'#{remote_tempfile_path}\' nogui\""
|
|
|
|
system_command(vmrunstr)
|
|
|
|
|
|
|
|
# now run it on the guest
|
|
|
|
vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"runProgramInGuest \'#{@location}\' -noWait -activeWindow \'#{remote_run_command}\'\""
|
|
|
|
system_command(vmrunstr)
|
|
|
|
|
|
|
|
## CLEANUP
|
|
|
|
# delete it on the guest
|
|
|
|
vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"deleteFileInGuest \'#{@location}\' \'#{remote_tempfile_path}\'\""
|
2011-05-25 06:26:26 +00:00
|
|
|
#system_command(vmrunstr)
|
2011-05-24 15:56:32 +00:00
|
|
|
|
|
|
|
# and delete it on the vm host
|
|
|
|
vmhost_delete_command = "ssh #{@user}@#{@host} rm #{local_tempfile_path}"
|
|
|
|
system_command(vmhost_delete_command)
|
|
|
|
|
|
|
|
# delete it locally
|
|
|
|
local_delete_command = "rm #{local_tempfile_path}"
|
|
|
|
system_command(local_delete_command)
|
|
|
|
else
|
|
|
|
# since we can't copy easily w/o tools, let's just run it directly :/
|
|
|
|
if @os == "linux"
|
|
|
|
scp_to(local_tempfile_path, remote_tempfile_path)
|
|
|
|
ssh_exec(remote_run_command)
|
|
|
|
ssh_exec("rm #{remote_tempfile_path}")
|
|
|
|
else
|
|
|
|
raise "zomgwtfbbqnotools"
|
|
|
|
end
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def copy_from(from, to)
|
2011-02-08 07:37:06 +00:00
|
|
|
from = filter_input(from)
|
|
|
|
to = filter_input(to)
|
2011-05-24 15:56:32 +00:00
|
|
|
if @tools
|
2011-05-25 06:26:26 +00:00
|
|
|
vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"copyFileFromGuestToHost \'#{@location}\' \'#{from}\' \'#{to}\' nogui\""
|
2011-05-24 15:56:32 +00:00
|
|
|
system_command(vmrunstr)
|
|
|
|
else
|
|
|
|
scp_to(from,to)
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def copy_to(from, to)
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-02-08 07:37:06 +00:00
|
|
|
from = filter_input(from)
|
|
|
|
to = filter_input(to)
|
2011-05-24 15:56:32 +00:00
|
|
|
|
|
|
|
if @tools
|
2011-05-25 06:26:26 +00:00
|
|
|
vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"copyFileFromHostToGuest \'#{@location}\' \'#{from}\' \'#{to}\' nogui\""
|
2011-05-24 15:56:32 +00:00
|
|
|
system_command(vmrunstr)
|
|
|
|
else
|
|
|
|
scp_to(from,to)
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def check_file_exists(file)
|
|
|
|
|
|
|
|
if @tools
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
file = filter_input(file)
|
|
|
|
vmrunstr = "\"ssh #{@user}@#{@host} vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"fileExistsInGuest \'#{@location}\' \'{file}\' nogui\""
|
|
|
|
system_command(vmrunstr)
|
|
|
|
else
|
|
|
|
raise "not implemented"
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def create_directory(directory)
|
2011-02-08 07:37:06 +00:00
|
|
|
directory = filter_input(directory)
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
if @tools
|
|
|
|
vmrunstr = "\"ssh #{@user}@#{@host} vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
|
|
|
|
"createDirectoryInGuest \'#{@location}\' \'#{directory}\' nogui\""
|
|
|
|
system_command(vmrunstr)
|
|
|
|
else
|
|
|
|
ssh_exec(command)
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def running?
|
2011-05-24 15:56:32 +00:00
|
|
|
## Get running VMs
|
2011-05-25 07:30:22 +00:00
|
|
|
running = `ssh #{@user}@#{@host} \"vmrun list nogui\"`
|
2011-01-17 05:58:16 +00:00
|
|
|
running_array = running.split("\n")
|
|
|
|
running_array.shift
|
|
|
|
|
|
|
|
running_array.each do |vmx|
|
|
|
|
if vmx.to_s == @location.to_s
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-04 09:17:11 +00:00
|
|
|
false
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|