2010-12-28 19:47:33 +00:00
|
|
|
##
|
|
|
|
## $Id$
|
|
|
|
##
|
2011-07-29 18:20:02 +00:00
|
|
|
## This is the main lab controller. Require this controller to get all
|
|
|
|
## lab functionality.
|
2010-12-28 19:47:33 +00:00
|
|
|
##
|
|
|
|
##
|
|
|
|
|
2011-07-11 05:52:51 +00:00
|
|
|
$:.unshift(File.expand_path(File.dirname(__FILE__)))
|
2011-07-29 18:20:02 +00:00
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'driver')))
|
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'controller')))
|
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'modifier')))
|
2010-12-28 19:47:33 +00:00
|
|
|
|
|
|
|
require 'find'
|
2011-07-29 18:20:02 +00:00
|
|
|
require 'yaml'
|
2010-12-28 19:47:33 +00:00
|
|
|
require 'enumerator'
|
2011-07-29 18:20:02 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2010-12-28 19:47:33 +00:00
|
|
|
require 'vm'
|
2011-07-29 18:20:02 +00:00
|
|
|
require 'controllers'
|
|
|
|
require 'drivers'
|
|
|
|
require 'modifiers'
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
module Lab
|
|
|
|
module Controllers
|
|
|
|
class VmController
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
include Enumerable
|
2011-05-25 06:26:26 +00:00
|
|
|
include Lab::Controllers::WorkstationController
|
|
|
|
include Lab::Controllers::WorkstationVixrController
|
|
|
|
include Lab::Controllers::RemoteWorkstationController
|
|
|
|
include Lab::Controllers::VirtualBoxController
|
|
|
|
include Lab::Controllers::DynagenController
|
2011-07-11 05:52:51 +00:00
|
|
|
include Lab::Controllers::RemoteEsxController
|
2011-05-25 06:26:26 +00:00
|
|
|
#include Lab::Controllers::QemuController
|
|
|
|
#include Lab::Controllers::QemudoController
|
2011-07-29 18:20:02 +00:00
|
|
|
#include Lab::Controllers::AmazonController
|
|
|
|
#include Lab::Controllers::FogController
|
2011-01-17 05:58:16 +00:00
|
|
|
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def initialize (labdef=nil)
|
2011-01-17 05:58:16 +00:00
|
|
|
@vms = [] ## Start with an empty array of vms
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-03-20 02:33:39 +00:00
|
|
|
## labdef is a big array of hashes, use yaml to store
|
|
|
|
labdef = [] unless labdef
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
## Create vm objects from the lab definition
|
2011-03-20 02:33:39 +00:00
|
|
|
load_vms(labdef)
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
2011-02-07 23:42:20 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def clear!
|
|
|
|
@vms = []
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-02-07 23:42:20 +00:00
|
|
|
def [](x)
|
|
|
|
find_by_vmid(x)
|
|
|
|
end
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def find_by_vmid(vmid)
|
|
|
|
@vms.each do |vm|
|
|
|
|
if (vm.vmid.to_s == vmid.to_s)
|
|
|
|
return vm
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
return nil
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-02-07 23:42:20 +00:00
|
|
|
def add_vm(vmid, type,location,credentials=nil,user=nil,host=nil)
|
2011-07-29 18:20:02 +00:00
|
|
|
|
2011-02-07 23:42:20 +00:00
|
|
|
@vms << Vm.new( { 'vmid' => vmid,
|
|
|
|
'driver' => type,
|
|
|
|
'location' => location,
|
|
|
|
'credentials' => credentials,
|
|
|
|
'user' => user,
|
|
|
|
'host' => host} )
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_by_vmid(vmid)
|
|
|
|
@vms.delete(self.find_by_vmid(vmid))
|
|
|
|
end
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def from_file(file)
|
|
|
|
labdef = YAML::load_file(file)
|
2011-03-20 02:33:39 +00:00
|
|
|
load_vms(labdef)
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-03-20 02:33:39 +00:00
|
|
|
def load_vms(vms)
|
|
|
|
vms.each do |item|
|
|
|
|
begin
|
|
|
|
vm = Vm.new(item)
|
|
|
|
@vms << vm unless includes_vmid? vm.vmid
|
|
|
|
rescue Exception => e
|
2011-07-11 05:52:51 +00:00
|
|
|
# TODO - this needs to go into a logfile and be raised up to an interface.
|
2011-03-20 02:33:39 +00:00
|
|
|
puts "Invalid VM definition"
|
2011-05-24 15:56:32 +00:00
|
|
|
puts "Exception: #{e.to_s}"
|
2011-03-20 02:33:39 +00:00
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def to_file(file)
|
2011-03-20 02:33:39 +00:00
|
|
|
File.open(file, 'w') { |f| @vms.each { |vm| f.puts vm.to_yaml } }
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-02-07 23:42:20 +00:00
|
|
|
def each &block
|
2011-01-17 05:58:16 +00:00
|
|
|
@vms.each { |vm| yield vm }
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def includes?(specified_vm)
|
|
|
|
@vms.each { |vm| if (vm == specified_vm) then return true end }
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def includes_vmid?(vmid)
|
2011-03-20 02:33:39 +00:00
|
|
|
@vms.each do |vm|
|
|
|
|
return true if (vm.vmid == vmid)
|
|
|
|
end
|
|
|
|
return false
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
def build_from_dir(driver_type, dir, clear=false)
|
2011-01-17 05:58:16 +00:00
|
|
|
|
|
|
|
if clear
|
|
|
|
@vms = []
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
if driver_type.downcase == "workstation"
|
2011-02-11 15:04:33 +00:00
|
|
|
vm_list = ::Lab::Controllers::WorkstationController::dir_list(dir)
|
2011-05-31 15:51:22 +00:00
|
|
|
elsif driver_type.downcase == "workstation_vixr"
|
|
|
|
vm_list = ::Lab::Controllers::WorkstationVixrController::dir_list(dir)
|
|
|
|
elsif driver_type.downcase == "remote_workstation"
|
2011-02-11 15:04:33 +00:00
|
|
|
vm_list = ::Lab::Controllers::RemoteWorkstationController::dir_list(dir)
|
2011-05-31 15:51:22 +00:00
|
|
|
elsif driver_type.downcase == "virtualbox"
|
2011-02-16 02:15:24 +00:00
|
|
|
vm_list = ::Lab::Controllers::VirtualBoxController::dir_list(dir)
|
2011-07-11 05:52:51 +00:00
|
|
|
elsif driver_type.downcase == "remote_esx"
|
|
|
|
vm_list =::Lab::Controllers::RemoteEsxController::dir_list(dir)
|
|
|
|
#elsif driver_type.downcase == "esxi_vixr"
|
|
|
|
# vm_list =::Lab::Controllers::EsxiVixrController::dir_list(dir)
|
2011-07-29 18:20:02 +00:00
|
|
|
#elsif driver_type.downcase == "fog"
|
|
|
|
# vm_list = ::Lab::Controllers::FogController::dir_list(dir)
|
2011-01-17 05:58:16 +00:00
|
|
|
else
|
|
|
|
raise TypeError, "Unsupported VM Type"
|
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
vm_list.each_index do |index|
|
2011-05-31 15:51:22 +00:00
|
|
|
@vms << Vm.new( {'vmid' => "vm_#{index}", 'driver' => driver_type, 'location' => vm_list[index]} )
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
def build_from_running(driver_type=nil, user=nil, host=nil, clear=false)
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
if clear
|
|
|
|
@vms = []
|
|
|
|
end
|
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
case driver_type.intern
|
2011-02-16 02:15:24 +00:00
|
|
|
when :workstation
|
|
|
|
vm_list = ::Lab::Controllers::WorkstationController::running_list
|
|
|
|
|
|
|
|
vm_list.each do |item|
|
2011-02-04 21:29:24 +00:00
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
## Name the VM
|
|
|
|
index = @vms.count + 1
|
2011-02-04 21:29:24 +00:00
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
## Add it to the vm list
|
2011-05-31 15:51:22 +00:00
|
|
|
@vms << Vm.new( { 'vmid' => "vm_#{index}",
|
|
|
|
'driver' => driver_type,
|
2011-02-16 02:15:24 +00:00
|
|
|
'location' => item,
|
|
|
|
'user' => user,
|
|
|
|
'host' => host } )
|
|
|
|
end
|
|
|
|
|
|
|
|
when :remote_workstation
|
|
|
|
vm_list = ::Lab::Controllers::RemoteWorkstationController::running_list(user, host)
|
|
|
|
|
|
|
|
vm_list.each do |item|
|
2011-02-04 21:29:24 +00:00
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
## Name the VM
|
|
|
|
index = @vms.count + 1
|
|
|
|
|
|
|
|
## Add it to the vm list
|
2011-05-31 15:51:22 +00:00
|
|
|
@vms << Vm.new( { 'vmid' => "vm_#{index}",
|
|
|
|
'driver' => driver_type,
|
2011-02-16 02:15:24 +00:00
|
|
|
'location' => item,
|
|
|
|
'user' => user,
|
|
|
|
'host' => host } )
|
|
|
|
end
|
|
|
|
|
|
|
|
when :virtualbox
|
|
|
|
vm_list = ::Lab::Controllers::VirtualBoxController::running_list
|
|
|
|
|
2011-07-11 05:52:51 +00:00
|
|
|
# TODO - why are user and host specified here?
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
vm_list.each do |item|
|
|
|
|
## Add it to the vm list
|
|
|
|
@vms << Vm.new( { 'vmid' => "#{item}",
|
2011-07-11 05:52:51 +00:00
|
|
|
'driver' => driver_type,
|
|
|
|
'location' => nil, # this will be filled in by the driver
|
|
|
|
'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]}",
|
2011-05-31 15:51:22 +00:00
|
|
|
'driver' => driver_type,
|
2011-02-16 02:15:24 +00:00
|
|
|
'user' => user,
|
|
|
|
'host' => host } )
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
raise TypeError, "Unsupported VM Type"
|
|
|
|
end
|
2011-02-04 21:29:24 +00:00
|
|
|
|
2011-02-07 23:42:20 +00:00
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
def build_from_config(driver_type=nil, user=nil, host=nil, clear=false)
|
2011-02-18 17:03:19 +00:00
|
|
|
|
|
|
|
if clear
|
|
|
|
@vms = []
|
|
|
|
end
|
|
|
|
|
2011-05-31 15:51:22 +00:00
|
|
|
case driver_type.intern
|
2011-02-18 17:03:19 +00:00
|
|
|
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}",
|
2011-05-31 15:51:22 +00:00
|
|
|
'driver' => driver_type,
|
2011-02-18 17:03:19 +00:00
|
|
|
'location' => nil,
|
|
|
|
'user' => user,
|
|
|
|
'host' => host } )
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
raise TypeError, "Unsupported VM Type"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def running?(vmid)
|
2011-02-07 23:42:20 +00:00
|
|
|
if includes_vmid?(vmid)
|
2011-01-17 05:58:16 +00:00
|
|
|
return self.find_by_vmid(vmid).running?
|
|
|
|
end
|
|
|
|
return false
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|