lab 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ require 'vm_driver'
2
+
3
+ ##
4
+ ## $Id$
5
+ ##
6
+
7
+ # This driver was built against:
8
+ # VMware ESX Host Agent 4.1.0 build-348481
9
+
10
+ module Lab
11
+ module Drivers
12
+
13
+ class RemoteEsxDriver < VmDriver
14
+
15
+ def initialize(config)
16
+ unless config['user'] then raise ArgumentError, "Must provide a username" end
17
+ unless config['host'] then raise ArgumentError, "Must provide a hostname" end
18
+
19
+ super(config)
20
+
21
+ @user = filter_command(config['user'])
22
+ @host = filter_command(config['host'])
23
+ @port = config['port']
24
+ end
25
+
26
+ def start
27
+ remote_system_command("vim-cmd vmsvc/power.on #{@vmid}")
28
+ end
29
+
30
+ def stop
31
+ remote_system_command("vim-cmd vmsvc/power.off #{@vmid}")
32
+ end
33
+
34
+ def suspend
35
+ remote_system_command("vim-cmd vmsvc/power.suspend #{@vmid}")
36
+ end
37
+
38
+ def pause
39
+ remote_system_command("vim-cmd vmsvc/power.suspend #{@vmid}")
40
+ end
41
+
42
+ def resume
43
+ remote_system_command("vim-cmd vmsvc/power.suspendResume #{@vmid}")
44
+ end
45
+
46
+ def reset
47
+ remote_system_command("vim-cmd vmsvc/power.reset #{@vmid}")
48
+ end
49
+
50
+ def create_snapshot(snapshot)
51
+ snapshot = filter_input(snapshot)
52
+
53
+ remote_system_command("vim-cmd vmsvc/snapshot.create #{@vmid} #{snapshot} \'lab created snapshot\' 1 true")
54
+ end
55
+
56
+ def revert_snapshot(snapshot)
57
+
58
+ snapshots = get_snapshots
59
+
60
+ # Look through our snapshot list, choose the right one based on display_name
61
+ snapshots.each do |snapshot_obj|
62
+
63
+ #puts "DEBUG: checking #{snapshot_obj}"
64
+
65
+ if snapshot_obj[:display_name].downcase == snapshot.downcase
66
+ snapshot_identifier = snapshot_obj[:name].join(" ")
67
+
68
+ #puts "DEBUG: I would revert to #{snapshot_obj}"
69
+ remote_system_command("vim-cmd vmsvc/snapshot.revert #{@vmid} 0 #{snapshot_identifier}")
70
+ return true
71
+ end
72
+ end
73
+
74
+ # If we got here, the snapshot didn't exist
75
+ raise "Invalid Snapshot Name"
76
+ end
77
+
78
+ def delete_snapshot(snapshot, remove_children=false)
79
+ snapshots = get_snapshots
80
+
81
+ # Look through our snapshot list, choose the right one based on display_name
82
+ snapshots.each do |snapshot_obj|
83
+
84
+ #puts "DEBUG: checking #{snapshot_obj}"
85
+
86
+ if snapshot_obj[:display_name].downcase == snapshot.downcase
87
+ snapshot_identifier = snapshot_obj[:name].join(" ")
88
+ remote_system_command("vim-cmd vmsvc/snapshot.remove #{@vmid} #{remove_children} #{snapshot_identifier}")
89
+ return true
90
+ end
91
+ end
92
+
93
+ # If we got here, the snapshot didn't exist
94
+ raise "Invalid Snapshot Name"
95
+ end
96
+
97
+ def delete_all_snapshots
98
+ remote_system_command("vim-cmd vmsvc/snapshot.removeall #{@vmid}")
99
+ end
100
+
101
+ def run_command(command)
102
+ raise "Not Implemented"
103
+ end
104
+
105
+ def copy_from(from, to)
106
+ if @os == "linux"
107
+ scp_from(from, to)
108
+ else
109
+ raise "Unimplemented"
110
+ end
111
+ end
112
+
113
+ def copy_to(from, to)
114
+ if @os == "linux"
115
+ scp_to(from, to)
116
+ else
117
+ raise "Unimplemented"
118
+ end
119
+ end
120
+
121
+ def check_file_exists(file)
122
+ raise "Not Implemented"
123
+ end
124
+
125
+ def create_directory(directory)
126
+ raise "Not Implemented"
127
+ end
128
+
129
+ def cleanup
130
+
131
+ end
132
+
133
+ def running?
134
+ power_status_string = `ssh #{@user}@#{@host} \"vim-cmd vmsvc/power.getstate #{@vmid}\"`
135
+ return true if power_status_string =~ /Powered on/
136
+ false
137
+ end
138
+
139
+ def get_snapshots
140
+ # Command take the format:
141
+ # vmware-vim-cmd vmsvc/snapshot.revert [vmid: int] [snapshotlevel: int] [snapshotindex: int]
142
+ output = `ssh #{@user}@#{@host} \"vim-cmd vmsvc/snapshot.get #{@vmid}\"`
143
+
144
+ # this keeps track of the snapshots, takes the form:
145
+ #[ {:name => [0,0], :display_name => "String containing the snapshotname},
146
+ # {:name => [0,1], :display_name => "String containing the snapshotname}, ]
147
+ # ...
148
+ snapshots = []
149
+
150
+ # Use these to keep track of the parsing...
151
+ current_tree = -1
152
+ current_num = 0
153
+ count = 0
154
+
155
+ # Do the parsing & stick the snapshots in the snapshots array
156
+ output_lines = output.split("\n")
157
+ output_lines.each do |line|
158
+ if line.include?("|") # this is a new snapshot
159
+ if line.include?("ROOT") # it's a root
160
+ current_num = 0
161
+ current_tree = current_tree + 1 # new tree
162
+ snapshots << { :name => [current_num, current_tree], :display_name => output_lines[count+1].split(":").last.strip }
163
+ else
164
+ current_num = current_num + 1 # new snapshot in current tree
165
+ snapshots << { :name => [current_num, current_tree], :display_name => output_lines[count+1].split(":").last.strip }
166
+ end
167
+ end
168
+ count = count+1
169
+ end
170
+
171
+ snapshots
172
+ end
173
+
174
+ end
175
+
176
+ end
177
+ end
@@ -0,0 +1,198 @@
1
+ require 'vm_driver'
2
+
3
+ ##
4
+ ## $Id$
5
+ ##
6
+
7
+ module Lab
8
+ module Drivers
9
+
10
+ class RemoteWorkstationDriver < VmDriver
11
+
12
+ attr_accessor :location # among other things
13
+
14
+ def initialize(config)
15
+
16
+ unless config['user'] then raise ArgumentError, "Must provide a username" end
17
+ unless config['host'] then raise ArgumentError, "Must provide a hostname" end
18
+
19
+ super(config)
20
+
21
+ @user = filter_command(config['user'])
22
+ @host = filter_command(config['host'])
23
+ end
24
+
25
+ def start
26
+ remote_system_command("vmrun -T ws start \'#{@location}\' nogui")
27
+ end
28
+
29
+ def stop
30
+ remote_system_command("vmrun -T ws stop \'#{@location}\' nogui")
31
+ end
32
+
33
+ def suspend
34
+ remote_system_command("vmrun -T ws suspend \'#{@location}\' nogui")
35
+ end
36
+
37
+ def pause
38
+ remote_system_command("vmrun -T ws pause \'#{@location}\' nogui")
39
+ end
40
+
41
+ def reset
42
+ remote_system_command("vmrun -T ws reset \'#{@location}\' nogui")
43
+ end
44
+
45
+ def create_snapshot(snapshot)
46
+ snapshot = filter_input(snapshot)
47
+ remote_system_command("vmrun -T ws snapshot \'#{@location}\' #{snapshot} nogui")
48
+ end
49
+
50
+ def revert_snapshot(snapshot)
51
+ snapshot = filter_input(snapshot)
52
+ remote_system_command("vmrun -T ws revertToSnapshot \'#{@location}\' #{snapshot} nogui")
53
+ end
54
+
55
+ def delete_snapshot(snapshot)
56
+ snapshot = filter_input(snapshot)
57
+ remote_system_command("vmrun -T ws deleteSnapshot \'#{@location}\' #{snapshot} nogui" )
58
+ end
59
+
60
+ def run_command(command)
61
+ # generate local & remote script paths
62
+ script_rand_name = rand(10000)
63
+
64
+ if @os == "windows"
65
+ local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.bat"
66
+ remote_tempfile_path = "C:\\\\lab_script_#{script_rand_name}.bat"
67
+ remote_run_command = remote_tempfile_path
68
+ else
69
+ local_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
70
+ remote_tempfile_path = "/tmp/lab_script_#{script_rand_name}.sh"
71
+ remote_run_command = "/bin/sh #{remote_tempfile_path}"
72
+ end
73
+
74
+ # write out our script locally
75
+ File.open(local_tempfile_path, 'w') {|f| f.write(command) }
76
+
77
+ # we really can't filter command, so we're gonna stick it in a script
78
+ if @tools
79
+ # copy it to the vm host - this is because we're a remote driver
80
+ remote_copy_command = "scp #{local_tempfile_path} #{@user}@#{@host}:#{local_tempfile_path}"
81
+ system_command(remote_copy_command)
82
+
83
+ # we have it on the vm host, copy it to the vm guest
84
+ vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
85
+ "copyFileFromHostToGuest \'#{@location}\' \'#{local_tempfile_path}\' " +
86
+ "\'#{remote_tempfile_path}\' nogui\""
87
+ system_command(vmrunstr)
88
+
89
+ # now run it on the guest
90
+ vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
91
+ "runProgramInGuest \'#{@location}\' -noWait -activeWindow \'#{remote_run_command}\'"
92
+ system_command(vmrunstr)
93
+
94
+ ## CLEANUP
95
+ # delete it on the guest
96
+ vmrunstr = "ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
97
+ "deleteFileInGuest \'#{@location}\' \'#{remote_tempfile_path}\'"
98
+ system_command(vmrunstr)
99
+
100
+ # and delete it on the vm host
101
+ vmhost_delete_command = "ssh #{@user}@#{@host} rm #{local_tempfile_path}"
102
+ system_command(vmhost_delete_command)
103
+
104
+ # delete it locally
105
+ local_delete_command = "rm #{local_tempfile_path}"
106
+ system_command(local_delete_command)
107
+ else
108
+ # since we can't copy easily w/o tools, let's just run it directly :/
109
+ if @os == "linux"
110
+ scp_to(local_tempfile_path, remote_tempfile_path)
111
+ ssh_exec(remote_run_command)
112
+ ssh_exec("rm #{remote_tempfile_path}")
113
+ else
114
+ raise "Not Implemented - Install VmWare Tools"
115
+ end
116
+ end
117
+ end
118
+
119
+ def copy_from(from, to)
120
+ from = filter_input(from)
121
+ to = filter_input(to)
122
+
123
+ # copy it to the vm host - this is because we're a remote driver
124
+ remote_copy_command = "scp #{from} #{@user}@#{@host}:#{from}"
125
+ system_command(remote_copy_command)
126
+
127
+ if @tools
128
+
129
+ remote_system_command("ssh #{@user}@#{@host} \"vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
130
+ "copyFileFromGuestToHost \'#{@location}\' \'#{from}\' \'#{to}\' nogui")
131
+ else
132
+ scp_to(from,to)
133
+ end
134
+ end
135
+
136
+ def copy_to(from, to)
137
+
138
+ from = filter_input(from)
139
+ to = filter_input(to)
140
+
141
+ # copy it to the vm host - this is because we're a remote driver
142
+ remote_copy_command = "scp #{from} #{@user}@#{@host}:#{from}"
143
+ system_command(remote_copy_command)
144
+
145
+ if @tools
146
+ remote_system_command("vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
147
+ "copyFileFromHostToGuest \'#{@location}\' \'#{from}\' \'#{to}\' nogui")
148
+ else
149
+ scp_to(from,to)
150
+ end
151
+ end
152
+
153
+ def check_file_exists(file)
154
+
155
+ if @tools
156
+ file = filter_input(file)
157
+ remote_system_command("vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
158
+ "fileExistsInGuest \'#{@location}\' \'{file}\' nogui")
159
+ else
160
+ raise "Not Implemented - Install VmWare Tools"
161
+ end
162
+ end
163
+
164
+ def create_directory(directory)
165
+ directory = filter_input(directory)
166
+
167
+ if @tools
168
+ emote_system_command("ssh #{@user}@#{@host} vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " +
169
+ "createDirectoryInGuest \'#{@location}\' \'#{directory}\' nogui")
170
+ system_command(vmrunstr)
171
+ else
172
+ raise "Not Implemented - Install VmWare Tools"
173
+ end
174
+ end
175
+
176
+ def cleanup
177
+
178
+ end
179
+
180
+ def running?
181
+ ## Get running VMs
182
+ running = `ssh #{@user}@#{@host} \"vmrun list nogui\"`
183
+ running_array = running.split("\n")
184
+ running_array.shift
185
+
186
+ running_array.each do |vmx|
187
+ if vmx.to_s == @location.to_s
188
+ return true
189
+ end
190
+ end
191
+
192
+ false
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+ end
@@ -0,0 +1,142 @@
1
+ require 'vm_driver'
2
+ require 'nokogiri'
3
+
4
+ ##
5
+ ## $Id$
6
+ ##
7
+ module Lab
8
+ module Drivers
9
+ class VirtualBoxDriver < VmDriver
10
+
11
+ attr_accessor :location
12
+
13
+ def initialize(config)
14
+
15
+ super(config)
16
+
17
+ ## Check to see if we already know this vm, if not, go on location
18
+ vmid_list = ::Lab::Controllers::VirtualBoxController::config_list
19
+ unless vmid_list.include? @vmid
20
+ raise "Error, no such vm: #{@vmid}" unless @location
21
+
22
+ if !File.exist?(@location)
23
+ raise ArgumentError,"Error, no vm at: #{@location}"
24
+ end
25
+
26
+ # Registering @location
27
+ @vmid = register_and_return_vmid
28
+ end
29
+
30
+ vmInfo = `VBoxManage showvminfo \"#{@vmid}\" --machinereadable`
31
+ @location = vmInfo.scan(/CfgFile=\"(.*?)\"/).flatten[0].to_s
32
+
33
+ if !File.exist?(@location)
34
+ raise ArgumentError,"Couldn't find: " + @location
35
+ end
36
+
37
+ end
38
+
39
+ def register_and_return_vmid
40
+
41
+ xml = Nokogiri::XML(File.new(@location))
42
+ vmid = xml.root.xpath("//Machine[@name]")
43
+
44
+ ## only register if we don't already know the vmid
45
+ if !::Lab::Controllers::VirtualBoxController::config_list.include? vmid
46
+ system_command("VBoxManage registervm \"#{@location}\"")
47
+ end
48
+
49
+ return vmid
50
+
51
+ end
52
+
53
+ def unregister
54
+ system_command("VBoxManage unregistervm \"#{@vmid}\"")
55
+ end
56
+
57
+ def start
58
+ system_command("VBoxManage startvm \"#{@vmid}\"")
59
+ end
60
+
61
+ def stop
62
+ system_command("VBoxManage controlvm \"#{@vmid}\" poweroff")
63
+ end
64
+
65
+ def suspend
66
+ system_command("VBoxManage controlvm \"#{@vmid}\" savestate")
67
+ end
68
+
69
+ def pause
70
+ system_command("VBoxManage controlvm \"#{@vmid}\" pause")
71
+ end
72
+
73
+ def reset
74
+ system_command("VBoxManage controlvm \"#{@vmid}\" reset")
75
+ end
76
+
77
+ def create_snapshot(snapshot)
78
+ snapshot = filter_input(snapshot)
79
+ system_command("VBoxManage snapshot \"#{@vmid}\" take #{snapshot}")
80
+ end
81
+
82
+ def revert_snapshot(snapshot)
83
+ snapshot = filter_input(snapshot)
84
+ system_command("VBoxManage snapshot \"#{@vmid}\" restore #{snapshot}")
85
+ end
86
+
87
+ def delete_snapshot(snapshot)
88
+ snapshot = filter_input(snapshot)
89
+ system_command("VBoxManage snapshot \"#{@vmid}\" delete #{snapshot}")
90
+ end
91
+
92
+ def run_command(command, arguments=nil)
93
+ command = filter_input(command)
94
+ arguments = filter_input(arguments)
95
+
96
+ command = "VBoxManage guestcontrol exec \"#{@vmid}\" \"#{command}\" --username \"#{@vm_user}\"" +
97
+ " --password \"#{@vm_pass}\" --arguments \"#{arguments}\""
98
+ system_command(command)
99
+ end
100
+
101
+ def copy_from(from, to)
102
+ from = filter_input(from)
103
+ to = filter_input(to)
104
+
105
+ raise "Not supported by Virtual Box"
106
+ end
107
+
108
+ def copy_to(from, to)
109
+ from = filter_input(from)
110
+ to = filter_input(to)
111
+
112
+ command = "VBoxManage guestcontrol copyto \"#{@vmid}\" \"#{from}\" \"#{to}\" " +
113
+ "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
114
+ system_command(command)
115
+ end
116
+
117
+ def check_file_exists(file)
118
+ file = filter_input(file)
119
+
120
+ raise "Not supported by Virtual Box"
121
+ end
122
+
123
+ def create_directory(directory)
124
+ directory = filter_input(directory)
125
+
126
+ command = "VBoxManage guestcontrol createdir \"#{@vmid}\" \"#{directory}\" " +
127
+ "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
128
+ system_command(command)
129
+ end
130
+
131
+ def cleanup
132
+
133
+ end
134
+
135
+ def running?
136
+ ## Get running Vms
137
+ ::Lab::Controllers::VirtualBoxController::running_list.include? @vmid
138
+ end
139
+
140
+ end
141
+ end
142
+ end