2010-12-28 19:47:33 +00:00
|
|
|
##
|
|
|
|
## $Id$
|
|
|
|
##
|
2011-01-17 05:58:16 +00:00
|
|
|
## This is the main lab controller. Require this file to create a lab of vms
|
2010-12-28 19:47:33 +00:00
|
|
|
##
|
|
|
|
##
|
|
|
|
|
|
|
|
$:.unshift(File.expand_path(File.dirname(__FILE__))) ## Msf Test libraries
|
|
|
|
|
|
|
|
require 'find'
|
|
|
|
require 'enumerator'
|
|
|
|
require 'vm'
|
|
|
|
require 'yaml'
|
2011-01-17 05:58:16 +00:00
|
|
|
require 'workstation_controller'
|
|
|
|
require 'remote_workstation_controller'
|
2011-02-11 15:04:33 +00:00
|
|
|
#require 'qemu_controller'
|
2011-01-17 05:58:16 +00:00
|
|
|
#require 'amazon_controller'
|
2011-02-16 02:15:24 +00:00
|
|
|
require 'virtualbox_controller'
|
2011-01-17 05:58:16 +00:00
|
|
|
#require 'dynagen_controller'
|
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
|
|
|
|
include Lab::Controllers::WorkstationController ## gives access to workstation-specific controller methods
|
2011-02-11 15:04:33 +00:00
|
|
|
include Lab::Controllers::RemoteWorkstationController ## gives access to workstation-specific controller methods
|
|
|
|
#include Lab::Controllers::QemuController ## gives access to qemu-specific controller methods
|
|
|
|
#include Lab::Controllers::AmazonController ## gives access to amazon-specific controller methods
|
2011-02-16 02:15:24 +00:00
|
|
|
include Lab::Controllers::VirtualBoxController ## gives access to virtualbox-specific controller methods
|
2011-02-11 15:04:33 +00:00
|
|
|
#include Lab::Controllers::DynagenController ## gives access to dynagen-specific controller methods
|
2011-01-17 05:58:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def initialize (labdef = nil)
|
2010-12-28 19:47:33 +00:00
|
|
|
|
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-01-17 05:58:16 +00:00
|
|
|
## labdef is a big array of hashes (vms) - generally loaded from yaml
|
|
|
|
if !labdef
|
|
|
|
labdef = [] ## Just use a blank lab to start
|
|
|
|
else
|
|
|
|
labdef = labdef
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
## Create vm objects from the lab definition
|
|
|
|
labdef.each do |item|
|
|
|
|
begin
|
|
|
|
@vms << Vm.new(item)
|
|
|
|
rescue Exception => e
|
2011-02-16 02:15:24 +00:00
|
|
|
puts "Invalid VM definition"
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
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)
|
|
|
|
@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)
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
labdef.each do |item|
|
|
|
|
#puts "Lab item: " + item.inspect
|
|
|
|
@vms << Vm.new(item)
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
def to_file(file)
|
|
|
|
File.open(file, 'w') do |f|
|
|
|
|
@vms.each { |vm| f.puts vm.to_yaml }
|
|
|
|
end
|
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)
|
|
|
|
@vms.each { |vm| if (vm.vmid.to_s == vmid.to_s) then return true end }
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-02-16 02:15:24 +00:00
|
|
|
def build_from_dir(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-01-17 05:58:16 +00:00
|
|
|
if type.downcase == "workstation"
|
2011-02-11 15:04:33 +00:00
|
|
|
vm_list = ::Lab::Controllers::WorkstationController::dir_list(dir)
|
2011-01-17 05:58:16 +00:00
|
|
|
elsif type.downcase == "remote_workstation"
|
2011-02-11 15:04:33 +00:00
|
|
|
vm_list = ::Lab::Controllers::RemoteWorkstationController::dir_list(dir)
|
2011-02-16 02:15:24 +00:00
|
|
|
elsif type.downcase == "virtualbox"
|
|
|
|
vm_list = ::Lab::Controllers::VirtualBoxController::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|
|
|
|
|
puts "Creating VM object for: " + vm_list[index]
|
|
|
|
@vms << Vm.new( {'vmid' => index.to_s, '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-02-04 21:29:24 +00:00
|
|
|
def build_from_running(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-02-04 21:29:24 +00:00
|
|
|
case 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
|
|
|
|
@vms << Vm.new( { 'vmid' => index.to_s,
|
|
|
|
'driver' => type,
|
|
|
|
'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
|
|
|
|
@vms << Vm.new( { 'vmid' => "#{index}",
|
|
|
|
'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' => type,
|
|
|
|
'location' => nil,
|
|
|
|
'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
|
|
|
|
|
|
|
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
|