add workstation-over-ssh control into the lab vm-control plugin
git-svn-id: file:///home/svn/framework3/trunk@11584 4d416f70-5f16-0410-b530-b9f4589650daunstable
parent
e8bf4118b1
commit
4384618950
|
@ -0,0 +1,19 @@
|
||||||
|
module Lab
|
||||||
|
module Controllers
|
||||||
|
module RemoteWorkstationController
|
||||||
|
|
||||||
|
def workstation_running_list(user,host)
|
||||||
|
vm_list = `ssh #{user}@#{host} vmrun list nogui`.split("\n")
|
||||||
|
vm_list.shift
|
||||||
|
|
||||||
|
return vm_list
|
||||||
|
end
|
||||||
|
|
||||||
|
def workstation_dir_list(basepath=nil)
|
||||||
|
vm_list = Find.find(basepath).select { |f| f =~ /\.vmx$/ }
|
||||||
|
|
||||||
|
return vm_list
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,162 @@
|
||||||
|
require 'vm_driver'
|
||||||
|
|
||||||
|
##
|
||||||
|
## $Id$
|
||||||
|
##
|
||||||
|
|
||||||
|
module Lab
|
||||||
|
module Drivers
|
||||||
|
|
||||||
|
class RemoteWorkstationDriver < VmDriver
|
||||||
|
|
||||||
|
attr_accessor :type
|
||||||
|
attr_accessor :location
|
||||||
|
|
||||||
|
def initialize(location, user=nil, host=nil, credentials=nil)
|
||||||
|
|
||||||
|
## Can we check file existence?
|
||||||
|
|
||||||
|
#if !File.exist?(location)
|
||||||
|
# raise ArgumentError,"Couldn't find: " + location
|
||||||
|
#end
|
||||||
|
|
||||||
|
unless user then raise ArgumentError, "Must provide a username" end
|
||||||
|
unless host then raise ArgumentError, "Must provide a hostname" end
|
||||||
|
|
||||||
|
@location = location
|
||||||
|
@host = host
|
||||||
|
@user = user
|
||||||
|
@credentials = credentials
|
||||||
|
@type = "RemoteWorkstation"
|
||||||
|
end
|
||||||
|
|
||||||
|
def start
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws start \\\'#{@location}\\\' nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws stop \\\'#{@location}\\\' nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def suspend
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws suspend \\\'#{@location}\\\' nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def pause
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws pause \\\'#{@location}\\\' nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def reset
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws reset \\\'#{@location}\\\' nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def create(snapshot)
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws snapshot \\\'#{@location}\\\' #{snapshot} nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def revert(snapshot)
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws revertToSnapshot \\\'#{@location}\\\' #{snapshot} nogui")
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_snapshot(snapshot)
|
||||||
|
system_command("ssh #{@user}@#{@host} vmrun -T ws deleteSnapshot \\\'#{@location}\\\' #{snapshot} nogui" )
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def run_command(command, named_user=nil)
|
||||||
|
|
||||||
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "ssh #{@user}@#{@host} vmrun -T ws -gu \\\'{user}\\\' -gp \\\'{pass}\\\' runProgramInGuest \\\'#{@location}\\\' \\\'{command}\\\' -noWait -activeWindow nogui"
|
||||||
|
|
||||||
|
system_command(vmrunstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_from(from, to, named_user=nil)
|
||||||
|
|
||||||
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "ssh #{@user}@#{@host} vmrun -T ws -gu {user} -gp {pass} copyFileFromGuestToHost \\\'#{@location}\\\' \\\'{from}\\\' \\\'{to}\\\' nogui"
|
||||||
|
system_command(vmrunstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
def copy_to(from, to, named_user=nil)
|
||||||
|
|
||||||
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
|
||||||
|
vmrunstr = "ssh #{@user}@#{@host} vmrun -T ws -gu {user} -gp {pass} copyFileFromHostToGuest \\\'#{@location}\\\' \\\'{from}\\\' \\\'{to}\\\' nogui"
|
||||||
|
system_command(vmrunstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_file_exists(file, named_user=nil)
|
||||||
|
|
||||||
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
|
||||||
|
vmrunstr = "ssh #{@user}@#{@host} vmrun -T ws -gu {user} -gp {pass} fileExistsInGuest \\\'#{@location}\\\' \\\'{file}\\\' nogui"
|
||||||
|
system_command(vmrunstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_directory(directory, named_user=nil)
|
||||||
|
|
||||||
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "ssh #{@user}@#{@host} vmrun -T ws -gu {user} -gp {pass} createDirectoryInGuest \\\'#{@location}\\\' \\\'#{directory}\\\' nogui"
|
||||||
|
system_command(vmrunstr)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def running?
|
||||||
|
## Get running Vms
|
||||||
|
running = `ssh #{@user}@#{@host} vmrun list nogui`
|
||||||
|
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
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,12 +3,15 @@
|
||||||
##
|
##
|
||||||
|
|
||||||
require 'workstation_driver'
|
require 'workstation_driver'
|
||||||
|
require 'remote_workstation_driver'
|
||||||
#require 'server_driver'
|
#require 'server_driver'
|
||||||
#require 'qemu_driver'
|
#require 'qemu_driver'
|
||||||
#require 'vbox_driver'
|
#require 'virtualbox_driver'
|
||||||
#require 'ec2_driver'
|
#require 'ec2_driver'
|
||||||
#require 'azure_driver'
|
#require 'azure_driver'
|
||||||
|
|
||||||
|
module Lab
|
||||||
|
|
||||||
class Vm
|
class Vm
|
||||||
|
|
||||||
attr_accessor :vmid
|
attr_accessor :vmid
|
||||||
|
@ -18,10 +21,12 @@ class Vm
|
||||||
attr_accessor :tools
|
attr_accessor :tools
|
||||||
|
|
||||||
## Initialize takes a vm configuration hash of the form
|
## Initialize takes a vm configuration hash of the form
|
||||||
## - driver (vm technology)
|
|
||||||
## - vmid (unique identifier)
|
## - vmid (unique identifier)
|
||||||
## - location (file / uri)
|
## driver (vm technology)
|
||||||
## - credentials (of the form [ {'user'=>"user",'pass'=>"pass", 'admin' => false}, ... ])
|
## user (if applicable)
|
||||||
|
## host (if applicable)
|
||||||
|
## location (file / uri)
|
||||||
|
## credentials (of the form [ {'user'=>"user",'pass'=>"pass", 'admin' => false}, ... ])
|
||||||
def initialize(config = {})
|
def initialize(config = {})
|
||||||
@driver = nil
|
@driver = nil
|
||||||
driver_type = config['driver']
|
driver_type = config['driver']
|
||||||
|
@ -37,8 +42,13 @@ class Vm
|
||||||
@ports = nil ## TODO
|
@ports = nil ## TODO
|
||||||
@vulns = nil ## TODO
|
@vulns = nil ## TODO
|
||||||
|
|
||||||
|
@user = config['user'] || nil
|
||||||
|
@host = config['host'] || nil
|
||||||
|
|
||||||
if driver_type == "workstation"
|
if driver_type == "workstation"
|
||||||
@driver = WorkstationDriver.new(@location)
|
@driver = Lab::Drivers::WorkstationDriver.new(@location, @credentials)
|
||||||
|
elsif driver_type == "remote_workstation"
|
||||||
|
@driver = Lab::Drivers::RemoteWorkstationDriver.new(@location,@user, @host, @credentials)
|
||||||
#elsif driver_type == "server"
|
#elsif driver_type == "server"
|
||||||
# @driver = ServerDriver.new
|
# @driver = ServerDriver.new
|
||||||
#elsif driver_type == "virtualbox"
|
#elsif driver_type == "virtualbox"
|
||||||
|
@ -75,6 +85,10 @@ class Vm
|
||||||
@driver.suspend
|
@driver.suspend
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def reset
|
||||||
|
@driver.reset
|
||||||
|
end
|
||||||
|
|
||||||
def resume
|
def resume
|
||||||
@driver.resume
|
@driver.resume
|
||||||
end
|
end
|
||||||
|
@ -83,8 +97,20 @@ class Vm
|
||||||
@driver.snapshot(name)
|
@driver.snapshot(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
## revert needs to restore the current state of the machine
|
||||||
|
## meaning, if it's running when revert is called
|
||||||
|
## we'll want to restart it after reverting it.
|
||||||
def revert(name)
|
def revert(name)
|
||||||
|
start = false
|
||||||
|
if running?
|
||||||
|
start = true
|
||||||
|
end
|
||||||
|
|
||||||
@driver.revert(name)
|
@driver.revert(name)
|
||||||
|
|
||||||
|
if start
|
||||||
|
@driver.start
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_to(from_file,to_file)
|
def copy_to(from_file,to_file)
|
||||||
|
@ -108,10 +134,10 @@ class Vm
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_yaml
|
def to_yaml
|
||||||
out = " - vmid: #{vmid}\n"
|
out = " - vmid: #{@vmid}\n"
|
||||||
out += " driver: #{driver.type}\n"
|
out += " driver: #{@driver.type}\n"
|
||||||
out += " location: #{location}\n"
|
out += " location: #{@location}\n"
|
||||||
out += " tools: #{tools}\n"
|
out += " tools: #{@tools}\n"
|
||||||
out += " credentials:\n"
|
out += " credentials:\n"
|
||||||
@credentials.each do |credential|
|
@credentials.each do |credential|
|
||||||
out += " - user: #{credential['user']}\n"
|
out += " - user: #{credential['user']}\n"
|
||||||
|
@ -119,6 +145,13 @@ class Vm
|
||||||
out += " admin: #{credential['admin']}\n"
|
out += " admin: #{credential['admin']}\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if @server_user or @server_host
|
||||||
|
out += " server_user: #{@server_user}\n"
|
||||||
|
out += " server_host: #{@server_host}\n"
|
||||||
|
end
|
||||||
|
|
||||||
return out
|
return out
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
##
|
##
|
||||||
## $Id$
|
## $Id$
|
||||||
##
|
##
|
||||||
## This is the main lab controller which will call out to other controller
|
## This is the main lab controller. Require this file to create a lab of vms
|
||||||
## libraries. Requiring this file and specifying the type of VM at initialization
|
|
||||||
## will allow you to start/stop/snapshot/revert & run commands on VMs
|
|
||||||
##
|
##
|
||||||
## $Revision$
|
|
||||||
##
|
##
|
||||||
|
|
||||||
$:.unshift(File.expand_path(File.dirname(__FILE__))) ## Msf Test libraries
|
$:.unshift(File.expand_path(File.dirname(__FILE__))) ## Msf Test libraries
|
||||||
|
@ -14,14 +11,25 @@ require 'find'
|
||||||
require 'enumerator'
|
require 'enumerator'
|
||||||
require 'vm'
|
require 'vm'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
|
require 'workstation_controller'
|
||||||
|
require 'remote_workstation_controller'
|
||||||
|
|
||||||
#
|
#require 'amazon_controller'
|
||||||
# ~Higher-level lab methods which are generic to the types of things we want to do with a lab of machines
|
#require 'virtualbox_controller'
|
||||||
# Note that any generic vm functionality should be pushed down into the controller class.
|
#require 'dynagen_controller'
|
||||||
|
|
||||||
|
module Lab
|
||||||
|
module Controllers
|
||||||
class VmController
|
class VmController
|
||||||
|
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
include Lab::Controllers::WorkstationController ## gives access to workstation-specific controller methods
|
||||||
|
include Lab::Controllers::RemoteWorkstationController ## gives access to workstation-specific controller methods
|
||||||
|
|
||||||
|
# include Lab::AmazonController ## gives access to amazon-specific controller methods
|
||||||
|
# include Lab::VirtualBoxController ## gives access to virtualbox-specific controller methods
|
||||||
|
# include Lab::DynagenController ## gives access to dynagen-specific controller methods
|
||||||
|
|
||||||
|
|
||||||
def initialize (labdef = nil)
|
def initialize (labdef = nil)
|
||||||
|
|
||||||
|
@ -63,7 +71,7 @@ class VmController
|
||||||
labdef = YAML::load_file(file)
|
labdef = YAML::load_file(file)
|
||||||
|
|
||||||
labdef.each do |item|
|
labdef.each do |item|
|
||||||
puts "Lab item: " + item.inspect
|
#puts "Lab item: " + item.inspect
|
||||||
@vms << Vm.new(item)
|
@vms << Vm.new(item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -78,48 +86,74 @@ class VmController
|
||||||
@vms.each { |vm| yield vm }
|
@vms.each { |vm| yield vm }
|
||||||
end
|
end
|
||||||
|
|
||||||
# def includes?(vm)
|
def includes?(specified_vm)
|
||||||
# @vms.each { |vm| if (vm.vmid.to_ == vmid.to_s) then return true end }
|
@vms.each { |vm| if (vm == specified_vm) then return true end }
|
||||||
# end
|
end
|
||||||
|
|
||||||
def includes_vmid?(vmid)
|
def includes_vmid?(vmid)
|
||||||
@vms.each { |vm| if (vm.vmid.to_s == vmid.to_s) then return true end }
|
@vms.each { |vm| if (vm.vmid.to_s == vmid.to_s) then return true end }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def build_from_dir(dir, type, clear=false)
|
||||||
|
|
||||||
|
if clear
|
||||||
|
@vms = []
|
||||||
|
end
|
||||||
|
|
||||||
|
if type.downcase == "workstation"
|
||||||
|
vm_list = WorkstationController::workstation_dir_list(dir)
|
||||||
|
elsif type.downcase == "remote_workstation"
|
||||||
|
vm_list = RemoteWorkstationController::workstation_dir_list(dir)
|
||||||
|
else
|
||||||
|
raise TypeError, "Unsupported VM Type"
|
||||||
|
end
|
||||||
|
|
||||||
|
vm_list.each do |item|
|
||||||
|
index = @vms.count + 1
|
||||||
|
@vms << Vm.new( {"vmid" => index, "driver" => type, "location" => item} )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_from_running(type, user=nil, host=nil, clear=false)
|
||||||
|
|
||||||
|
if clear
|
||||||
|
@vms = []
|
||||||
|
end
|
||||||
|
|
||||||
|
if type.downcase == "workstation"
|
||||||
|
vm_list = WorkstationController::workstation_running_list
|
||||||
|
elsif type.downcase == "remote_workstation"
|
||||||
|
vm_list = RemoteWorkstationController::workstation_running_list(user, host)
|
||||||
|
else
|
||||||
|
raise TypeError, "Unsupported VM Type"
|
||||||
|
end
|
||||||
|
|
||||||
|
vm_list.each do |item|
|
||||||
|
index = @vms.count + 1
|
||||||
|
@vms << Vm.new( {"vmid" => index, "driver" => type, "location" => item} )
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_vm(vmid, type,location,credentials=nil,user=nil,host=nil)
|
||||||
|
@vms << Vm.new( { "vmid" => vmid,
|
||||||
|
"driver" => type,
|
||||||
|
"location" => location,
|
||||||
|
"credentials" => credentials,
|
||||||
|
"user" => user,
|
||||||
|
"host" => host
|
||||||
|
} )
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_by_vmid(vmid)
|
||||||
|
@vms.delete(self.find_by_vmid(vmid))
|
||||||
|
end
|
||||||
|
|
||||||
def running?(vmid)
|
def running?(vmid)
|
||||||
if exists?(vmid)
|
if exists?(vmid)
|
||||||
return self.find_by_vmid(vmid).running?
|
return self.find_by_vmid(vmid).running?
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
## Might want to mix this (workstation) functionality in?
|
|
||||||
def build_from_running_workstation(clear=false)
|
|
||||||
|
|
||||||
if clear
|
|
||||||
@vms = []
|
|
||||||
end
|
|
||||||
|
|
||||||
vm_list = `vmrun list`.split("\n")
|
|
||||||
vm_list.shift
|
|
||||||
vm_list.each do |vmx|
|
|
||||||
index = @vms.count + 1 ## give us a vmid!
|
|
||||||
@vms << Vm.new( {"vmid" => index, "driver" => "workstation",
|
|
||||||
"location" => vmx})
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_from_dir_workstation(basepath=nil, clear=false)
|
|
||||||
|
|
||||||
if clear
|
|
||||||
@vms = []
|
|
||||||
end
|
|
||||||
|
|
||||||
vm_list = Find.find(basepath).select { |f| f =~ /\.vmx$/ }
|
|
||||||
vm_list.each do |vmx|
|
|
||||||
index = @vms.count + 1 ## give us a vmid!
|
|
||||||
@vms << Vm.new( {"vmid" => index, "driver" => "workstation",
|
|
||||||
"location" => vmx})
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,13 +1,27 @@
|
||||||
##
|
##
|
||||||
## $Id$
|
## $Id$
|
||||||
##
|
##
|
||||||
|
module Lab
|
||||||
|
module Drivers
|
||||||
class VmDriver
|
class VmDriver
|
||||||
|
|
||||||
attr_accessor :type
|
attr_accessor :type
|
||||||
attr_accessor :location
|
attr_accessor :location
|
||||||
|
|
||||||
def initialize(location)
|
def initialize(location)
|
||||||
|
|
||||||
|
@location = location
|
||||||
|
@host = host
|
||||||
|
@user = user
|
||||||
|
@credentials = credentials
|
||||||
|
@type = ""
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def register
|
||||||
|
end
|
||||||
|
|
||||||
|
def unregister
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
@ -34,41 +48,75 @@ class VmDriver
|
||||||
def delete_snapshot(snapshot)
|
def delete_snapshot(snapshot)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_command(command, user, pass)
|
def run_command(command, named_user=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_from(user, pass, from, to)
|
def copy_from(from, to, named_user=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_to(user, pass, from, to)
|
def copy_to(from, to, named_user=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_file_exists(user, pass, file)
|
def check_file_exists(file, named_user=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_directory(user, pass, directory)
|
def create_directory(directory, named_user=nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
=begin
|
||||||
def ssh_exec(host, command, user)
|
def ssh_exec(host, command, user)
|
||||||
ssh_command = "ssh " + user + "@" + host + " " + command
|
ssh_command = "ssh " + user + "@" + host + " " + command
|
||||||
system_command(ssh_command)
|
system_command(ssh_command)
|
||||||
end
|
end
|
||||||
|
|
||||||
def scp_from(host, user, from, to)
|
def scp_from(host, user, from, to)
|
||||||
vmrunstr = "scp -r \"" + user + "@" + host + ":" + from + "\" \"" + to + "\"" ## TODO - setup keys
|
vmrunstr = "scp -r \"" + user + "@" + host + ":" + from + "\" \"" + to + "\""
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def scp_to(host, user, from, to)
|
def scp_to(host, user, from, to)
|
||||||
vmrunstr = "scp -r \"" + from + "\" \"" + user + "@" + host + ":" + to + "\"" ## TODO - setup keys
|
vmrunstr = "scp -r \"" + from + "\" \"" + user + "@" + host + ":" + to + "\""
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
=end
|
||||||
|
|
||||||
|
def cleanup
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|
||||||
|
## Takes a username in the form of a string
|
||||||
|
## and returns a credentials hash
|
||||||
|
def get_best_creds(named_user)
|
||||||
|
if !@credentials.empty?
|
||||||
|
return get_named_user_creds(named_user) || @credentials[0]
|
||||||
|
else
|
||||||
|
raise Exception, "No credentials for this VM ):"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
## Checks the array of credentials to see if we have one
|
||||||
|
## with this user's username. returns the first.
|
||||||
|
def get_named_user_creds(user)
|
||||||
|
cretdentials.each do |credential|
|
||||||
|
if credential['user'].downcase == user.downcase
|
||||||
|
return credential
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
def system_command(command)
|
def system_command(command)
|
||||||
## TODO - filter here
|
## TODO - filter here
|
||||||
|
|
||||||
|
puts "DEBUG: running system command: #{command}"
|
||||||
|
|
||||||
system(command)
|
system(command)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -4,83 +4,134 @@ require 'vm_driver'
|
||||||
## $Id$
|
## $Id$
|
||||||
##
|
##
|
||||||
|
|
||||||
|
module Lab
|
||||||
|
module Drivers
|
||||||
|
|
||||||
class WorkstationDriver < VmDriver
|
class WorkstationDriver < VmDriver
|
||||||
|
|
||||||
attr_accessor :type
|
attr_accessor :type
|
||||||
attr_accessor :location
|
attr_accessor :location
|
||||||
|
|
||||||
def initialize(location)
|
def initialize(location, credentials=nil)
|
||||||
if !File.exist?(location)
|
if !File.exist?(location)
|
||||||
raise ArgumentError,"Couldn't find: " + location
|
raise ArgumentError,"Couldn't find: " + location
|
||||||
end
|
end
|
||||||
|
|
||||||
@location = location
|
@location = location
|
||||||
|
@credentials = credentials
|
||||||
@type = "Workstation"
|
@type = "Workstation"
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
system_command("vmrun -T ws start " + "\"" + @location + "\"")
|
system_command("vmrun -T ws start " + "\"#{@location}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop
|
def stop
|
||||||
system_command("vmrun -T ws stop " + "\"" + @location + "\"")
|
system_command("vmrun -T ws stop " + "\"#{@location}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def suspend
|
def suspend
|
||||||
system_command("vmrun -T ws suspend " + "\"" + @location + "\"")
|
system_command("vmrun -T ws suspend " + "\"#{@location}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def pause
|
def pause
|
||||||
system_command("vmrun -T ws pause " + "\"" + @location + "\"")
|
system_command("vmrun -T ws pause " + "\"#{@location}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset
|
def reset
|
||||||
system_command("vmrun -T ws reset " + "\"" + @location + "\"")
|
system_command("vmrun -T ws reset " + "\"#{@location}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(snapshot)
|
def create(snapshot)
|
||||||
system_command("vmrun -T ws snapshot " + "\"" + @location + "\" " + snapshot)
|
system_command("vmrun -T ws snapshot " + "\"#{@location}\" \"#{@snapshot}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def revert(snapshot)
|
def revert(snapshot)
|
||||||
system_command("vmrun -T ws revertToSnapshot " + "\"" + @location + "\" " + snapshot)
|
system_command("vmrun -T ws revertToSnapshot " + "\"#{@location}\" \"#{@snapshot}\"")
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_snapshot(snapshot)
|
def delete_snapshot(snapshot)
|
||||||
system_command("vmrun -T ws deleteSnapshot " + "\"" + @location + "\" " + snapshot )
|
system_command("vmrun -T ws deleteSnapshot " + "\"#{@location}\" \"#{@snapshot}\"" )
|
||||||
end
|
end
|
||||||
|
|
||||||
def run_command(command, user, pass)
|
def run_command(command, named_user=nil)
|
||||||
vmrunstr = "vmrun -T ws -gu \"" + user + "\" -gp \"" + pass + "\" runProgramInGuest \"" +
|
|
||||||
@location + "\" " + "\"" + command + "\" -noWait -activeWindow"
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "vmrun -T ws -gu \"{user}\" -gp \"{pass}\" runProgramInGuest \"#{@location}\" \"{command}\" -noWait -activeWindow"
|
||||||
|
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_from(user, pass, from, to)
|
def copy_from(from, to, named_user=nil)
|
||||||
vmrunstr = "vmrun -T ws -gu " + user + " -gp " + pass + " copyFileFromGuestToHost \"" +
|
|
||||||
@location + "\" \"" + from + "\" \"" + to + "\""
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "vmrun -T ws -gu {user} -gp {pass} copyFileFromGuestToHost \"#{@location}\" \"{from}\" \"{to}\""
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def copy_to(user, pass, from, to)
|
def copy_to(from, to, named_user=nil)
|
||||||
vmrunstr = "vmrun -T ws -gu " + user + " -gp " + pass + " copyFileFromHostToGuest \"" +
|
|
||||||
@location + "\" \"" + from + "\" \"" + to + "\""
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
|
||||||
|
vmrunstr = "vmrun -T ws -gu {user} -gp {pass} copyFileFromHostToGuest \"#{@location}\" \"{from}\" \"{to}\""
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_file_exists(user, pass, file)
|
def check_file_exists(file, named_user=nil)
|
||||||
vmrunstr = "vmrun -T ws -gu " + user + " -gp " + pass + " fileExistsInGuest \"" +
|
|
||||||
@location + "\" \"" + file + "\" "
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
|
||||||
|
vmrunstr = "vmrun -T ws -gu {user} -gp {pass} fileExistsInGuest \"#{@location}\" \"{file}\" "
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_directory(user, pass, directory)
|
def create_directory(directory, named_user=nil)
|
||||||
vmrunstr = "vmrun -T ws -gu " + user + " -gp " + pass + " createDirectoryInGuest \"" +
|
|
||||||
@location + "\" \"" + directory + "\" "
|
## this will return the first user if named_user doesn't exist
|
||||||
|
## -- that may not be entirely obvious...
|
||||||
|
cred = get_best_credentials(named_user)
|
||||||
|
|
||||||
|
user = cred['user']
|
||||||
|
pass = cred['pass']
|
||||||
|
admin = cred['admin']
|
||||||
|
|
||||||
|
vmrunstr = "vmrun -T ws -gu {user} -gp {pass} createDirectoryInGuest \"#{@location}\" \"#{directory}\" "
|
||||||
system_command(vmrunstr)
|
system_command(vmrunstr)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cleanup
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
def running?
|
def running?
|
||||||
## Get running Vms
|
## Get running Vms
|
||||||
running = `vmrun list`
|
running = `vmrun list`
|
||||||
|
@ -97,3 +148,6 @@ class WorkstationDriver < VmDriver
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue