Class: Lab::Drivers::VirtualBoxDriver

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

Instance Attribute Summary collapse

Attributes inherited from VmDriver

#credentials, #os, #tools, #vmid

Instance Method Summary collapse

Methods inherited from VmDriver

#register, #resume

Constructor Details

#initialize(config) ⇒ VirtualBoxDriver

Returns a new instance of VirtualBoxDriver.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lab/driver/virtualbox_driver.rb', line 13

def initialize(config)

	super(config)

	## Check to see if we already know this vm, if not, go on location
	vmid_list = ::Lab::Controllers::VirtualBoxController::config_list
	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
		
		# Registering @location
		@vmid = register_and_return_vmid
	end
	
	vmInfo = `VBoxManage showvminfo \"#{@vmid}\" --machinereadable`
	@location = vmInfo.scan(/CfgFile=\"(.*?)\"/).flatten[0].to_s

	if !File.exist?(@location)
		raise ArgumentError,"Couldn't find: " + @location
	end

end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



11
12
13
# File 'lib/lab/driver/virtualbox_driver.rb', line 11

def location
  @location
end

Instance Method Details

#check_file_exists(file) ⇒ Object



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

def check_file_exists(file)
	file = filter_input(file)

	raise "Not supported by Virtual Box"
end

#cleanupObject



131
132
133
# File 'lib/lab/driver/virtualbox_driver.rb', line 131

def cleanup

end

#copy_from(from, to) ⇒ Object



101
102
103
104
105
106
# File 'lib/lab/driver/virtualbox_driver.rb', line 101

def copy_from(from, to)
	from = filter_input(from)
	to = filter_input(to)

	raise "Not supported by Virtual Box"
end

#copy_to(from, to) ⇒ Object



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

def copy_to(from, to)
	from = filter_input(from)
	to = filter_input(to)
	
	command = "VBoxManage guestcontrol copyto \"#{@vmid}\" \"#{from}\"  \"#{to}\" " +
			 "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
	system_command(command)
end

#create_directory(directory) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/lab/driver/virtualbox_driver.rb', line 123

def create_directory(directory)
	directory = filter_input(directory)

	command = "VBoxManage guestcontrol createdir \"#{@vmid}\" \"#{directory}\" " + 
			 "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
	system_command(command)
end

#create_snapshot(snapshot) ⇒ Object



77
78
79
80
# File 'lib/lab/driver/virtualbox_driver.rb', line 77

def create_snapshot(snapshot)
	snapshot = filter_input(snapshot)
	system_command("VBoxManage snapshot \"#{@vmid}\" take #{snapshot}")
end

#delete_snapshot(snapshot) ⇒ Object



87
88
89
90
# File 'lib/lab/driver/virtualbox_driver.rb', line 87

def delete_snapshot(snapshot)
	snapshot = filter_input(snapshot)
	system_command("VBoxManage snapshot \"#{@vmid}\" delete  #{snapshot}")
end

#pauseObject



69
70
71
# File 'lib/lab/driver/virtualbox_driver.rb', line 69

def pause
	system_command("VBoxManage controlvm \"#{@vmid}\" pause")
end

#register_and_return_vmidObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lab/driver/virtualbox_driver.rb', line 39

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
	if !::Lab::Controllers::VirtualBoxController::config_list.include? vmid
		system_command("VBoxManage registervm \"#{@location}\"")
	end
	
	return vmid
	
end

#resetObject



73
74
75
# File 'lib/lab/driver/virtualbox_driver.rb', line 73

def reset
	system_command("VBoxManage controlvm \"#{@vmid}\" reset")
end

#revert_snapshot(snapshot) ⇒ Object



82
83
84
85
# File 'lib/lab/driver/virtualbox_driver.rb', line 82

def revert_snapshot(snapshot)
	snapshot = filter_input(snapshot)
	system_command("VBoxManage snapshot \"#{@vmid}\" restore #{snapshot}")
end

#run_command(command, arguments = nil) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/lab/driver/virtualbox_driver.rb', line 92

def run_command(command, arguments=nil)
	command = filter_input(command)
	arguments = filter_input(arguments)

	command = "VBoxManage guestcontrol exec \"#{@vmid}\" \"#{command}\" --username \"#{@vm_user}\"" +
			 " --password \"#{@vm_pass}\" --arguments \"#{arguments}\""
	system_command(command)
end

#running?Boolean

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/lab/driver/virtualbox_driver.rb', line 135

def running?
	## Get running Vms
	::Lab::Controllers::VirtualBoxController::running_list.include? @vmid
end

#startObject



57
58
59
# File 'lib/lab/driver/virtualbox_driver.rb', line 57

def start
	system_command("VBoxManage startvm \"#{@vmid}\"")
end

#stopObject



61
62
63
# File 'lib/lab/driver/virtualbox_driver.rb', line 61

def stop
	system_command("VBoxManage controlvm \"#{@vmid}\" poweroff")
end

#suspendObject



65
66
67
# File 'lib/lab/driver/virtualbox_driver.rb', line 65

def suspend
	system_command("VBoxManage controlvm \"#{@vmid}\" savestate")
end

#unregisterObject



53
54
55
# File 'lib/lab/driver/virtualbox_driver.rb', line 53

def unregister
	system_command("VBoxManage unregistervm \"#{@vmid}\"")
end