Class: Lab::Controllers::VmController

Inherits:
Object
  • Object
show all
Includes:
Enumerable, DynagenController, FogController, RemoteEsxController, RemoteWorkstationController, VirtualBoxController, WorkstationController
Defined in:
lib/lab/vm_controller.rb

Constant Summary

Constants included from RemoteEsxController

RemoteEsxController::VIM_CMD

Instance Method Summary collapse

Methods included from RemoteWorkstationController

dir_list, running_list

Methods included from RemoteEsxController

dir_list, running_list

Methods included from DynagenController

dir_list, running_list

Methods included from VirtualBoxController

config_list, config_list_uuid, dir_list, running_list

Methods included from WorkstationController

dir_list, running_list

Constructor Details

#initialize(labdef = nil) ⇒ VmController

include Lab::Controllers::QemuController include Lab::Controllers::QemudoController



40
41
42
43
44
45
46
47
# File 'lib/lab/vm_controller.rb', line 40

def initialize (labdef=nil)
	
	# Start with an empty array of vm objects
	@vms = [] 

	# labdef is a just a big array of hashes
	load_vms(labdef) if labdef
end

Instance Method Details

#[](x) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/lab/vm_controller.rb', line 53

def [](x)
	# Support indexing by both names and number
	if x.class == String
		find_by_vmid(x)
	else
		return @vms[x]
	end
end

#add_vm(vmid, location = nil, os = nil, tools = nil, credentials = nil, user = nil, host = nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/lab/vm_controller.rb', line 71

def add_vm(vmid, location=nil, os=nil, tools=nil, credentials=nil, user=nil, host=nil)			
	@vms << Vm.new( {	'vmid' => vmid, 
				'driver' => type, 
				'location' => location, 
				'credentials' => credentials,
				'user' => user,
				'host' => host} )
end

#build_from_config(driver_type = nil, user = nil, host = nil, clear = false) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/lab/vm_controller.rb', line 209

def build_from_config(driver_type=nil, user=nil, host=nil, clear=false)
	if clear
		@vms = []
	end

	case driver_type.intern
		when :virtualbox
			vm_list = ::Lab::Controllers::VirtualBoxController::config_list
			
			vm_list.each do |item|
				## Add it to the vm list
				@vms << Vm.new( {	'vmid' => "#{item}",
							'driver' => driver_type, 
							'location' => nil, 
							'user' => user,
							'host' => host } )
			end
				
		else
			raise TypeError, "Unsupported VM Type"
		end

end

#build_from_dir(driver_type, dir, clear = false) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/lab/vm_controller.rb', line 114

def build_from_dir(driver_type, dir, clear=false)

	if clear
		@vms = []
	end

	if driver_type.downcase == "workstation"
		vm_list = ::Lab::Controllers::WorkstationController::dir_list(dir)
	elsif driver_type.downcase == "virtualbox"	
		vm_list = ::Lab::Controllers::VirtualBoxController::dir_list(dir)
	elsif driver_type.downcase == "fog"
		vm_list = ::Lab::Controllers::FogController::dir_list(dir)
	elsif driver_type.downcase == "Dynagen"	
		vm_list = ::Lab::Controllers::DynagenController::dir_list(dir)
	elsif driver_type.downcase == "remote_workstation"	
		vm_list = ::Lab::Controllers::RemoteWorkstationController::dir_list(dir)
	elsif driver_type.downcase == "remote_esx"
		vm_list =::Lab::Controllers::RemoteEsxController::dir_list(dir)
	else
		raise TypeError, "Unsupported VM Type"
	end
	
	vm_list.each_index do |index|
		@vms << Vm.new( {'vmid' => "vm_#{index}", 'driver' => driver_type, 'location' => vm_list[index]} )
	end
end

#build_from_running(driver_type = nil, user = nil, host = nil, clear = false) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/lab/vm_controller.rb', line 141

def build_from_running(driver_type=nil, user=nil, host=nil, clear=false)

	if clear
		@vms = []
	end

	case driver_type.intern
		when :workstation
			vm_list = ::Lab::Controllers::WorkstationController::running_list
			
			vm_list.each do |item|
	
				## Name the VM
				index = @vms.count + 1
	
				## Add it to the vm list
				@vms << Vm.new( {	'vmid' => "vm_#{index}",
							'driver' => driver_type, 
							'location' => item, 
							'user' => user,
							'host' => host } )
			end
			
			
		when :virtualbox
			vm_list = ::Lab::Controllers::VirtualBoxController::running_list
			vm_list.each do |item|
				## Add it to the vm list
				@vms << Vm.new( {	'vmid' => "#{item}",
							'driver' => driver_type,
							'location' => nil })
			end
		when :fog
			raise "Unsupported" # TODO - figure out a way to allow this
		when :dynagen
			raise "Unsupported"
		when :remote_workstation
			vm_list = ::Lab::Controllers::RemoteWorkstationController::running_list(user, host)
			
			vm_list.each do |item|
	
				## Name the VM
				index = @vms.count + 1
	
				## Add it to the vm list
				@vms << Vm.new( {	'vmid' => "vm_#{index}",
							'driver' => driver_type, 
							'location' => item, 
							'user' => user,
							'host' => host } )
			end
		when :remote_esx
			vm_list = ::Lab::Controllers::RemoteEsxController::running_list(user,host)
			
			vm_list.each do |item|
				@vms << Vm.new( {	'vmid' => "#{item[:id]}",
							'name' => "#{item[:name]}",
							'driver' => driver_type, 
							'user' => user,
							'host' => host } )
			end
				
		else
			raise TypeError, "Unsupported VM Type"
		end

end

#clear!Object



49
50
51
# File 'lib/lab/vm_controller.rb', line 49

def clear!
	@vms = []
end

#each(&block) ⇒ Object



99
100
101
# File 'lib/lab/vm_controller.rb', line 99

def each &block
	@vms.each { |vm| yield vm }
end

#find_by_vmid(vmid) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/lab/vm_controller.rb', line 62

def find_by_vmid(vmid)
	@vms.each do |vm|
		if (vm.hostname.to_s.downcase == vmid.to_s.downcase)
			return vm
		end
	end
	return nil
end

#from_file(file) ⇒ Object



84
85
86
# File 'lib/lab/vm_controller.rb', line 84

def from_file(file)
	load_vms(YAML::load_file(file))
end

#includes?(specified_vm) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/lab/vm_controller.rb', line 103

def includes?(specified_vm)
	@vms.each { |vm| if (vm == specified_vm) then return true end  }
end

#includes_vmid?(vmid) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'lib/lab/vm_controller.rb', line 107

def includes_vmid?(vmid)
	@vms.each do |vm|
		return true if (vm.vmid == vmid)
	end
false
end

#load_vms(vms) ⇒ Object



88
89
90
91
92
93
# File 'lib/lab/vm_controller.rb', line 88

def load_vms(vms)
	vms.each do |item|
		vm = Vm.new(item)
		@vms << vm unless includes_vmid? vm.vmid
	end
end

#remove_by_vmid(vmid) ⇒ Object



80
81
82
# File 'lib/lab/vm_controller.rb', line 80

def remove_by_vmid(vmid)
	@vms.delete(self.find_by_vmid(vmid))
end

#running?(vmid) ⇒ Boolean

Returns:

  • (Boolean)


233
234
235
236
237
238
# File 'lib/lab/vm_controller.rb', line 233

def running?(vmid)
	if includes_vmid?(vmid)
		return self.find_by_vmid(vmid).running?
	end
	return false 
end

#to_file(file) ⇒ Object



95
96
97
# File 'lib/lab/vm_controller.rb', line 95

def to_file(file)
	File.open(file, 'w') { |f| @vms.each { |vm| f.puts vm.to_yaml } } 
end