2011-02-08 07:26:18 +00:00
|
|
|
require 'vm_driver'
|
2011-02-16 02:15:24 +00:00
|
|
|
require 'nokogiri'
|
2011-02-08 07:26:18 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
## $Id$
|
|
|
|
##
|
|
|
|
module Lab
|
|
|
|
module Drivers
|
|
|
|
class VirtualBoxDriver < VmDriver
|
|
|
|
|
|
|
|
attr_accessor :type
|
|
|
|
attr_accessor :location
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def initialize(vmid, location=nil)
|
2011-02-08 07:26:18 +00:00
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
@vmid = filter_input(vmid)
|
2011-02-08 07:26:18 +00:00
|
|
|
@location = filter_input(location)
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
@type = "virtualbox"
|
|
|
|
|
|
|
|
## Check to see if we already know this vm, if not, go on location
|
2011-02-18 17:03:19 +00:00
|
|
|
vmid_list = ::Lab::Controllers::VirtualBoxController::config_list
|
2011-02-16 02:15:24 +00:00
|
|
|
unless vmid_list.include? @vmid
|
|
|
|
raise "Error, no such vm: #{@vmid}" unless @location
|
|
|
|
|
|
|
|
if !File.exist?(@location)
|
|
|
|
raise ArgumentError,"Error, no vm at: #{@location}"
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "Registering #{@location}"
|
|
|
|
@vmid = register_and_return_vmid
|
|
|
|
end
|
|
|
|
|
|
|
|
vmInfo = `VBoxManage showvminfo \"#{@vmid}\" --machinereadable`
|
2011-02-18 17:03:19 +00:00
|
|
|
@location = vmInfo.scan(/CfgFile=\"(.*?)\"/).flatten[0].to_s
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def register_and_return_vmid
|
|
|
|
|
|
|
|
xml = Nokogiri::XML(File.new(@location))
|
|
|
|
vmid = xml.root.xpath("//Machine[@name]")
|
|
|
|
|
|
|
|
## only register if we don't already know the vmid
|
2011-02-18 17:03:19 +00:00
|
|
|
if !::Lab::Controllers::VirtualBoxController::config_list.include? vmid
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage registervm \"#{@location}\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
return vmid
|
|
|
|
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def unregister
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage unregistervm \"#{@vmid}\"")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def start
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage startvm \"#{@vmid}\"")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage controlvm \"#{@vmid}\" poweroff")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def suspend
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage controlvm \"#{@vmid}\" savestate")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pause
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage controlvm \"#{@vmid}\" pause")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
2011-02-16 02:15:24 +00:00
|
|
|
system_command("VBoxManage controlvm \"#{@vmid}\" reset")
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def create_snapshot(snapshot)
|
|
|
|
snapshot = filter_input(snapshot)
|
|
|
|
system_command("VBoxManage snapshot \"#{@vmid}\" take " + snapshot)
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def revert_snapshot(snapshot)
|
|
|
|
snapshot = filter_input(snapshot)
|
|
|
|
system_command("VBoxManage snapshot \"#{@vmid}\" restore " + snapshot)
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def delete_snapshot(snapshot)
|
|
|
|
snapshot = filter_input(snapshot)
|
|
|
|
system_command("VBoxManage snapshot \"#{@vmid}\" delete " + snapshot)
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_command(command, arguments, user, pass)
|
2011-02-16 02:15:24 +00:00
|
|
|
command = "VBoxManage guestcontrol exec \"#{@vmid}\" \"#{command}\" --username \"#{user}\"
|
|
|
|
--password \"#{pass}\" --arguments \"#{arguments}\""
|
2011-02-08 07:26:18 +00:00
|
|
|
system_command(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_from(user, pass, from, to)
|
2011-02-16 02:15:24 +00:00
|
|
|
raise "Not supported by Virtual Box"
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def copy_to(user, pass, from, to)
|
2011-02-16 02:15:24 +00:00
|
|
|
command = "VBoxManage guestcontrol copyto \"#{@vmid}\" \"#{from}\" \"#{to}\"
|
|
|
|
--username \"#{user}\" --password \"#{pass}\""
|
2011-02-08 07:26:18 +00:00
|
|
|
system_command(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_file_exists(user, pass, file)
|
2011-02-16 02:15:24 +00:00
|
|
|
raise "Not supported by Virtual Box"
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_directory(user, pass, directory)
|
2011-02-16 02:15:24 +00:00
|
|
|
command = "VBoxManage guestcontrol createdir \"#{@vmid}\" \"#{directory}\"
|
|
|
|
--username \"#{user}\" --password \"#{pass}\""
|
2011-02-08 07:26:18 +00:00
|
|
|
system_command(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup
|
2011-02-16 02:15:24 +00:00
|
|
|
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def running?
|
|
|
|
## Get running Vms
|
2011-02-18 17:03:19 +00:00
|
|
|
::Lab::Controllers::VirtualBoxController::running_list.include? @vmid
|
2011-02-16 02:15:24 +00:00
|
|
|
end
|
|
|
|
|
2011-02-08 07:26:18 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|