Class: Lab::Vm

Inherits:
Object
  • Object
show all
Defined in:
lib/lab/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Vm

Initialize takes a vm configuration hash of the form

- vmid (unique identifier)
  driver (vm technology)
  user (if applicable - remote system)
  host (if applicable - remote system)
  pass (if applicable - remote system)
  location (file / uri)
  credentials (of the form [ {'user'=>"user",'pass'=>"pass", 'admin' => false}, ... ])
  os (currently only linux / windows)
  arch (currently only 32 / 64


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/lab/vm.rb', line 33

def initialize(config = {})	

	# TODO - This is a mess. clean up, and pass stuff down to drivers
	# and then rework the code that uses this api. 
	@vmid = config['vmid'].to_s 
	raise "Invalid VMID" unless @vmid

	# Grab the hostname if specified, otherwise use the vmid
	# VMID will be different in the case of ESX
	@hostname = config['hostname']
	if !@hostname
		@hostname = @vmid
	end

	@driver_type = filter_input(config['driver'])
	@driver_type.downcase!

	@location = filter_input(config['location'])
	#@name = config['name'] || ""
	@description = config['description']
	@tools = config['tools']
	@os = config['os']
	@arch = config['arch']
	@type = filter_input(config['type']) || "unspecified"
	@credentials = config['credentials'] || []
	
	# TODO - Currently only implemented for the first set
	if @credentials.count > 0
		@vm_user = filter_input(@credentials[0]['user']) || "\'\'"
		@vm_pass = filter_input(@credentials[0]['pass']) || "\'\'"
		@vm_keyfile = filter_input(@credentials[0]['keyfile'])
	end

	# Only applicable to remote systems
	@user = filter_input(config['user']) || nil
	@host = filter_input(config['host']) || nil
	@port = filter_input(config['port']) || nil
	@pass = filter_input(config['pass']) || nil

	#Only dynagen systems need this
	@platform = config['platform']

	#Only fog systems need this
	@fog_config = config['fog_config']

	#puts "Passing driver config: #{config}"

	# Process the correct driver
	if @driver_type == "workstation"
		@driver = Lab::Drivers::WorkstationDriver.new(config)
	elsif @driver_type == "virtualbox"
		@driver = Lab::Drivers::VirtualBoxDriver.new(config)
	elsif @driver_type == "fog"
		@driver = Lab::Drivers::FogDriver.new(config, config['fog_config'])
	elsif @driver_type == "dynagen"
		@driver = Lab::Drivers::DynagenDriver.new(config, config['dynagen_config'])	
	elsif @driver_type == "remote_esx"
		@driver = Lab::Drivers::RemoteEsxDriver.new(config)
	elsif @driver_type == "remote_workstation"
		@driver = Lab::Drivers::RemoteWorkstationDriver.new(config)
	#elsif @driver_type == "qemu"
	#	@driver = Lab::Drivers::QemuDriver.new
	#elsif @driver_type == "qemudo"
	#	@driver = Lab::Drivers::QemudoDriver.new
	else
		raise "Unknown Driver Type"
	end
			
	# Load in a list of modifiers. These provide additional methods
	# Currently it is up to the user to verify that 
	# modifiers are properly used with the correct VM image.
	#
	# If not, the results are likely to be disasterous.
	@modifiers = config['modifiers']
	
	if @modifiers	
		begin
 			@modifiers.each { |modifier|  self.class.send(:include, eval("Lab::Modifier::#{modifier}"))}
		rescue Exception => e
			# modifier likely didn't exist
		end 		
	end
end

Instance Attribute Details

#archObject

Returns the value of attribute arch.



20
21
22
# File 'lib/lab/vm.rb', line 20

def arch
  @arch
end

#credentialsObject

Returns the value of attribute credentials.



14
15
16
# File 'lib/lab/vm.rb', line 14

def credentials
  @credentials
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#driverObject

Returns the value of attribute driver.



13
14
15
# File 'lib/lab/vm.rb', line 13

def driver
  @driver
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/lab/vm.rb', line 18

def host
  @host
end

#hostnameObject

Returns the value of attribute hostname.



9
10
11
# File 'lib/lab/vm.rb', line 9

def hostname
  @hostname
end

#locationObject

Returns the value of attribute location.



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

def location
  @location
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/lab/vm.rb', line 10

def name
  @name
end

#osObject

Returns the value of attribute os.



19
20
21
# File 'lib/lab/vm.rb', line 19

def os
  @os
end

#toolsObject

Returns the value of attribute tools.



15
16
17
# File 'lib/lab/vm.rb', line 15

def tools
  @tools
end

#typeObject

Returns the value of attribute type.



16
17
18
# File 'lib/lab/vm.rb', line 16

def type
  @type
end

#userObject

Returns the value of attribute user.



17
18
19
# File 'lib/lab/vm.rb', line 17

def user
  @user
end

#vmidObject

Returns the value of attribute vmid.



8
9
10
# File 'lib/lab/vm.rb', line 8

def vmid
  @vmid
end

Instance Method Details

#check_file_exists(file) ⇒ Object



178
179
180
# File 'lib/lab/vm.rb', line 178

def check_file_exists(file)
	@driver.check_file_exists(file)
end

#copy_from(from, to) ⇒ Object



170
171
172
# File 'lib/lab/vm.rb', line 170

def copy_from(from,to)
	@driver.copy_from(from,to)
end

#copy_to(from, to) ⇒ Object



166
167
168
# File 'lib/lab/vm.rb', line 166

def copy_to(from,to)
	@driver.copy_to(from,to)
end

#create_directory(directory) ⇒ Object



182
183
184
# File 'lib/lab/vm.rb', line 182

def create_directory(directory)
	@driver.create_directory(directory)
end

#create_snapshot(snapshot) ⇒ Object



149
150
151
# File 'lib/lab/vm.rb', line 149

def create_snapshot(snapshot)
	@driver.create_snapshot(snapshot)
end

#delete_snapshot(snapshot) ⇒ Object



157
158
159
# File 'lib/lab/vm.rb', line 157

def delete_snapshot(snapshot)
	@driver.delete_snapshot(snapshot)
end

#open_uri(uri) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lab/vm.rb', line 186

def open_uri(uri)
	# we don't filter the uri, as it's getting tossed into a script 
	# by the driver
	if @os == "windows"
		command = "\"C:\\program files\\internet explorer\\iexplore.exe\" #{uri}"
	else
		command = "firefox #{uri}"
	end

	@driver.run_command(command)
end

#pauseObject



133
134
135
# File 'lib/lab/vm.rb', line 133

def pause
	@driver.pause
end

#resetObject



141
142
143
# File 'lib/lab/vm.rb', line 141

def reset
	@driver.reset
end

#resumeObject



145
146
147
# File 'lib/lab/vm.rb', line 145

def resume
	@driver.resume
end

#revert_and_start(snapshot) ⇒ Object



161
162
163
164
# File 'lib/lab/vm.rb', line 161

def revert_and_start(snapshot)
	@driver.revert_snapshot(snapshot)
	@driver.start
end

#revert_snapshot(snapshot) ⇒ Object



153
154
155
# File 'lib/lab/vm.rb', line 153

def revert_snapshot(snapshot)
	@driver.revert_snapshot(snapshot)
end

#run_command(command) ⇒ Object



174
175
176
# File 'lib/lab/vm.rb', line 174

def run_command(command)
	@driver.run_command(command)
end

#running?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/lab/vm.rb', line 117

def running?
	@driver.running?
end

#startObject



125
126
127
# File 'lib/lab/vm.rb', line 125

def start
	@driver.start
end

#stopObject



129
130
131
# File 'lib/lab/vm.rb', line 129

def stop
	@driver.stop
end

#suspendObject



137
138
139
# File 'lib/lab/vm.rb', line 137

def suspend
	@driver.suspend
end

#to_sObject



198
199
200
# File 'lib/lab/vm.rb', line 198

def to_s
	return "#{@hostname}"
end

#to_yamlObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/lab/vm.rb', line 202

def to_yaml
	
	# TODO - push this down to the drivers.
	
	# Standard configuration options
	out =  " - vmid: #{@vmid}\n"
	out += "   driver: #{@driver_type}\n"
	out += "   location: #{@location}\n"
	out += "   type: #{@type}\n"
	out += "   tools: #{@tools}\n"
	out += "   os: #{@os}\n"
	out += "   arch: #{@arch}\n"
	
	if @user or @host # Remote vm/drivers only
		out += "   user: #{@user}\n"
		out += "   host: #{@host}\n"
	end

	if @platform
		out += "   platform: #{@platform}\n"
	end

	if @fog_config
		out += @fog_config.to_yaml
	end

	if @dynagen_config
		out += @dynagen_config.to_yaml
	end

	out += "   credentials:\n"
	@credentials.each do |credential|		
		out += "     - user: #{credential['user']}\n"
		out += "       pass: #{credential['pass']}\n"
	end

 	return out
end