Class: Lab::Drivers::RemoteWorkstationDriver

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

Instance Attribute Summary collapse

Attributes inherited from VmDriver

#credentials, #os, #tools, #vmid

Instance Method Summary collapse

Methods inherited from VmDriver

#register, #resume, #unregister

Constructor Details

#initialize(config) ⇒ RemoteWorkstationDriver

Returns a new instance of RemoteWorkstationDriver.



14
15
16
17
18
19
20
21
22
23
# File 'lib/lab/driver/remote_workstation_driver.rb', line 14

def initialize(config)

	unless config['user'] then raise ArgumentError, "Must provide a username" end
	unless config['host'] then raise ArgumentError, "Must provide a hostname" end

	super(config)

	@user = filter_command(config['user'])
	@host = filter_command(config['host'])
end

Instance Attribute Details

#locationObject

among other things



12
13
14
# File 'lib/lab/driver/remote_workstation_driver.rb', line 12

def location
  @location
end

Instance Method Details

#check_file_exists(file) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/lab/driver/remote_workstation_driver.rb', line 152

def check_file_exists(file)
	
	if @tools
		file = filter_input(file)
		remote_system_command("vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
				"fileExistsInGuest \'#{@location}\' \'{file}\' nogui")
	else
		raise "Not Implemented - Install VmWare Tools"
	end
end

#cleanupObject



175
176
177
# File 'lib/lab/driver/remote_workstation_driver.rb', line 175

def cleanup

end

#copy_from(from, to) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lab/driver/remote_workstation_driver.rb', line 119

def copy_from(from, to)
	from = filter_input(from)
	to = filter_input(to)
	
	# copy it to the vm host - this is because we're a remote driver
	remote_copy_command = "scp #{from} #{@user}@#{@host}:#{from}"
	system_command(remote_copy_command)

	if @tools 
		remote_system_command("ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
				"copyFileFromGuestToHost \'#{@location}\' \'#{from}\' \'#{to}\' nogui")
	else
		scp_from(to,from)
	end
end

#copy_to(from, to) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lab/driver/remote_workstation_driver.rb', line 135

def copy_to(from, to)

	from = filter_input(from)
	to = filter_input(to)
	
	# copy it to the vm host - this is because we're a remote driver
	remote_copy_command = "scp #{from} #{@user}@#{@host}:#{from}"
	system_command(remote_copy_command)
	
	if @tools
		remote_system_command("vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
				"copyFileFromHostToGuest \'#{@location}\' \'#{from}\' \'#{to}\' nogui")
	else
		scp_to(from,to)
	end
end

#create_directory(directory) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/lab/driver/remote_workstation_driver.rb', line 163

def create_directory(directory)
	directory = filter_input(directory)

	if @tools
		emote_system_command("ssh #{@user}@#{@host} vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
				"createDirectoryInGuest \'#{@location}\' \'#{directory}\' nogui")
		system_command(vmrunstr)
	else
		raise "Not Implemented - Install VmWare Tools"
	end
end

#create_snapshot(snapshot) ⇒ Object



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

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

#delete_snapshot(snapshot) ⇒ Object



55
56
57
58
# File 'lib/lab/driver/remote_workstation_driver.rb', line 55

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

#pauseObject



37
38
39
# File 'lib/lab/driver/remote_workstation_driver.rb', line 37

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

#resetObject



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

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

#revert_snapshot(snapshot) ⇒ Object



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

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

#run_command(command) ⇒ Object



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
# File 'lib/lab/driver/remote_workstation_driver.rb', line 60

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

	# 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}\'"
		system_command(vmrunstr)

		# 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 "Not Implemented - Install VmWare Tools"
		end	
	end
end

#running?Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lab/driver/remote_workstation_driver.rb', line 179

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

	false
end

#startObject



25
26
27
# File 'lib/lab/driver/remote_workstation_driver.rb', line 25

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

#stopObject



29
30
31
# File 'lib/lab/driver/remote_workstation_driver.rb', line 29

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

#suspendObject



33
34
35
# File 'lib/lab/driver/remote_workstation_driver.rb', line 33

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