diff --git a/lib/lab/dynagen_controller.rb b/lib/lab/controller/dynagen_controller.rb similarity index 100% rename from lib/lab/dynagen_controller.rb rename to lib/lab/controller/dynagen_controller.rb diff --git a/lib/lab/remote_esx_controller.rb b/lib/lab/controller/remote_esx_controller.rb similarity index 100% rename from lib/lab/remote_esx_controller.rb rename to lib/lab/controller/remote_esx_controller.rb diff --git a/lib/lab/remote_workstation_controller.rb b/lib/lab/controller/remote_workstation_controller.rb similarity index 100% rename from lib/lab/remote_workstation_controller.rb rename to lib/lab/controller/remote_workstation_controller.rb diff --git a/lib/lab/virtualbox_controller.rb b/lib/lab/controller/virtualbox_controller.rb similarity index 100% rename from lib/lab/virtualbox_controller.rb rename to lib/lab/controller/virtualbox_controller.rb diff --git a/lib/lab/workstation_controller.rb b/lib/lab/controller/workstation_controller.rb similarity index 100% rename from lib/lab/workstation_controller.rb rename to lib/lab/controller/workstation_controller.rb diff --git a/lib/lab/workstation_vixr_controller.rb b/lib/lab/controller/workstation_vixr_controller.rb similarity index 100% rename from lib/lab/workstation_vixr_controller.rb rename to lib/lab/controller/workstation_vixr_controller.rb diff --git a/lib/lab/controllers.rb b/lib/lab/controllers.rb new file mode 100644 index 0000000000..36cb1596e4 --- /dev/null +++ b/lib/lab/controllers.rb @@ -0,0 +1,10 @@ +require 'controller/workstation_controller' +require 'controller/workstation_vixr_controller' +require 'controller/remote_workstation_controller' +require 'controller/virtualbox_controller' +require 'controller/dynagen_controller' +require 'controller/remote_esx_controller' +#require 'controller/qemu_controller' +#require 'controller/qemudo_controller' +#require 'controller/amazon_controller' +#require 'controller/fog_controller' diff --git a/lib/lab/dynagen_driver.rb b/lib/lab/driver/dynagen_driver.rb similarity index 100% rename from lib/lab/dynagen_driver.rb rename to lib/lab/driver/dynagen_driver.rb diff --git a/lib/lab/remote_esx_driver.rb b/lib/lab/driver/remote_esx_driver.rb similarity index 91% rename from lib/lab/remote_esx_driver.rb rename to lib/lab/driver/remote_esx_driver.rb index 6201574d35..93775ad530 100644 --- a/lib/lab/remote_esx_driver.rb +++ b/lib/lab/driver/remote_esx_driver.rb @@ -1,27 +1,23 @@ require 'vm_driver' ## -## $Id: remote_esx_driver.rb 12713 2011-05-25 07:30:22Z jcran $ +## $Id$ ## # This driver was built against: # VMware ESX Host Agent 4.1.0 build-348481 - module Lab module Drivers class RemoteEsxDriver < VmDriver - attr_accessor :location # among other things - - def initialize(vmid, location, os=nil, tools=false, user=nil, host=nil, credentials=nil) + def initialize(vmid,os=nil, tools=false, user=nil, host=nil, credentials=nil) unless user then raise ArgumentError, "Must provide a username" end unless host then raise ArgumentError, "Must provide a hostname" end - @vmid = filter_command(vmid) - @location = filter_command(location) + @vmid = vmid @user = filter_command(user) @host = filter_command(host) @credentials = credentials # individually filtered diff --git a/lib/lab/remote_workstation_driver.rb b/lib/lab/driver/remote_workstation_driver.rb similarity index 100% rename from lib/lab/remote_workstation_driver.rb rename to lib/lab/driver/remote_workstation_driver.rb diff --git a/lib/lab/virtualbox_driver.rb b/lib/lab/driver/virtualbox_driver.rb similarity index 100% rename from lib/lab/virtualbox_driver.rb rename to lib/lab/driver/virtualbox_driver.rb diff --git a/lib/lab/vm_driver.rb b/lib/lab/driver/vm_driver.rb similarity index 80% rename from lib/lab/vm_driver.rb rename to lib/lab/driver/vm_driver.rb index 1e27714bd3..b58921d91f 100644 --- a/lib/lab/vm_driver.rb +++ b/lib/lab/driver/vm_driver.rb @@ -9,7 +9,6 @@ # functionality. # - module Lab module Drivers class VmDriver @@ -95,14 +94,14 @@ private gem 'net-scp' require 'net/scp' - begin + #begin # upload a file to a remote server - Net::SCP.start(@vmid, @vm_user, :password => @vm_pass) do |scp| - scp.upload!(from,to) - end - rescue Exception => e - return false - end + Net::SCP.start(@vmid, @vm_user, :password => @vm_pass) do |scp| + scp.upload!(from,to) + end + #rescue Exception => e + # return false + #end end def scp_from(from,to) @@ -112,14 +111,14 @@ private gem 'net-scp' require 'net/scp' - begin - # download a file from a remote server - Net::SCP.start(@vmid, @vm_user, :password => @vm_pass) do |scp| - scp.download!(from,to) - end - rescue Exception => e - return false - end + #begin + # download a file from a remote server + Net::SCP.start(@vmid, @vm_user, :password => @vm_pass) do |scp| + scp.download!(from,to) + end + #rescue Exception => e + # return false + #end end @@ -127,20 +126,20 @@ private gem 'net-ssh' require 'net/ssh' - begin - Net::SSH.start(@vmid, @vm_user, :password => @vm_pass) do |ssh| - result = ssh.exec!(command) - end - rescue Exception => e - return false + #begin + Net::SSH.start(@vmid, @vm_user, :password => @vm_pass) do |ssh| + result = ssh.exec!(command) end + #rescue Exception => e + # return false + #end end def filter_input(string) return "" unless string - if !(string =~ /^[\w\s\[\]\{\}\/\\\.\-\"\(\):!]*$/) + if !(string =~ /^[0-9\w\s\[\]\{\}\/\\\.\-\"\(\):!]*$/) raise "WARNING! Invalid character in: #{string}" end @@ -150,7 +149,7 @@ private def filter_command(string) return "" unless string - if !(string =~ /^[\w\s\[\]\{\}\/\\\.\-\"\(\)]*$/) + if !(string =~ /^[0-9\w\s\[\]\{\}\/\\\.\-\"\(\)]*$/) raise "WARNING! Invalid character in: #{string}" end @@ -158,11 +157,11 @@ private end def system_command(command) - begin - system(command) - rescue Exception => e - return false - end + #begin + system(command) + #rescue Exception => e + # return false + # end end end diff --git a/lib/lab/workstation_driver.rb b/lib/lab/driver/workstation_driver.rb similarity index 92% rename from lib/lab/workstation_driver.rb rename to lib/lab/driver/workstation_driver.rb index 93c35cef69..35c13a40bb 100644 --- a/lib/lab/workstation_driver.rb +++ b/lib/lab/driver/workstation_driver.rb @@ -100,21 +100,33 @@ class WorkstationDriver < VmDriver # delete it on the guest vmrunstr = "vmrun -T ws -gu #{@vm_user} -gp #{@vm_pass} " + "deleteFileInGuest \'#{@location}\' \'#{remote_tempfile_path}\'" - #system_command(vmrunstr) + system_command(vmrunstr) # delete it locally local_delete_command = "rm #{local_tempfile_path}" - #system_command(local_delete_command) + system_command(local_delete_command) else # since we can't copy easily w/o tools, let's just run it directly :/ if @os == "linux" + + output_file = "/tmp/lab_command_output_#{rand(1000000)}" + scp_to(local_tempfile_path, remote_tempfile_path) - ssh_exec(remote_run_command) + ssh_exec(remote_run_command + "> #{output_file}") + scp_from(output_file, output_file) + + ssh_exec("rm #{output_file}") ssh_exec("rm #{remote_tempfile_path}") + + # Ghettohack! + string = File.open(output_file,"r").read + `rm #{output_file}` + else raise "zomgwtfbbqnotools" end end + return string end def copy_from(from, to) diff --git a/lib/lab/workstation_vixr_driver.rb b/lib/lab/driver/workstation_vixr_driver.rb similarity index 87% rename from lib/lab/workstation_vixr_driver.rb rename to lib/lab/driver/workstation_vixr_driver.rb index d5dc73c516..34248886e3 100644 --- a/lib/lab/workstation_vixr_driver.rb +++ b/lib/lab/driver/workstation_vixr_driver.rb @@ -16,10 +16,14 @@ class WorkstationVixrDriver < VmDriver def initialize(vmid, location, os=nil, tools=false, credentials=nil) - begin + # We have to treat this differently, as it's not in the same tree + begin require 'vixr' - rescue - raise "Oops, no vixr installed. Consider using the regular workstation driver." + rescue LoadError + puts "WARNING: Library pro_vixr not found. To resolve this error, please\n" + + " install the vixr gem. Latest is available here:\n" + + "https://github.com/rhythmx/vixr ." + raise "Unable to create vixr driver" end @vmid = filter_command(vmid) diff --git a/lib/lab/drivers.rb b/lib/lab/drivers.rb new file mode 100644 index 0000000000..39a8bdd30f --- /dev/null +++ b/lib/lab/drivers.rb @@ -0,0 +1,10 @@ +require 'driver/workstation_driver' +require 'driver/workstation_vixr_driver' +require 'driver/remote_workstation_driver' +require 'driver/virtualbox_driver' +require 'driver/dynagen_driver' +require 'driver/remote_esx_driver' +#require 'driver/qemu_driver' +#require 'driver/qemudo_driver' +#require 'driver/amazon_driver' +#require 'driver/fog_driver' diff --git a/lib/lab/modifiers.rb b/lib/lab/modifiers.rb new file mode 100644 index 0000000000..e1128f242f --- /dev/null +++ b/lib/lab/modifiers.rb @@ -0,0 +1 @@ +require 'modifier/test_modifier' diff --git a/lib/lab/vm.rb b/lib/lab/vm.rb index ddd224d494..2d5722f834 100644 --- a/lib/lab/vm.rb +++ b/lib/lab/vm.rb @@ -2,16 +2,6 @@ ## $Id$ ## -require 'workstation_driver' -require 'workstation_vixr_driver' -require 'remote_workstation_driver' -require 'virtualbox_driver' -require 'dynagen_driver' -require 'remote_esx_driver' -#require 'qemu_driver' -#require 'qemudo_driver' -#require 'amazon_driver' - module Lab class Vm @@ -38,6 +28,7 @@ class Vm ## credentials (of the form [ {'user'=>"user",'pass'=>"pass", 'admin' => false}, ... ]) ## os (currently only linux / windows) ## arch (currently only 32 / 64 + def initialize(config = {}) # Mandatory @@ -53,9 +44,14 @@ class Vm @tools = config['tools'] || false # don't filter this, not used in cmdlines @os = config['os'] || nil @arch = config['arch'] || nil - @type = filter_input(config['type']) || "unspecified" @credentials = config['credentials'] || [] + + # Load in a list of modifiers. These provide additional methods + # TODO - 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'] # Optional for virtualbox @location = filter_input(config['location']) @@ -70,6 +66,7 @@ class Vm #Only dynagen @platform = config['platform'] + # Process the correct driver if @driver_type == "workstation" @driver = Lab::Drivers::WorkstationDriver.new(@vmid, @location, @os, @tools, @credentials) elsif @driver_type == "workstation_vixr" @@ -81,16 +78,20 @@ class Vm elsif @driver_type == "dynagen" @driver = Lab::Drivers::DynagenDriver.new(@vmid, @location,@platform) elsif @driver_type == "remote_esx" - @driver = Lab::Drivers::RemoteEsxDriver.new(@vmid, @location, @os, @tools, @user, @host, @credentials) + @driver = Lab::Drivers::RemoteEsxDriver.new(@vmid, @os, @tools, @user, @host, @credentials) #elsif @driver_type == "qemu" # @driver = Lab::Drivers::QemuDriver.new #elsif @driver_type == "qemudo" # @driver = Lab::Drivers::QemudoDriver.new - #elsif @driver_type == "amazon" - # @driver = Lab::Drivers::AmazonDriver.new + #elsif @driver_type == "fog" + # @driver = Lab::Drivers::FogDriver.new else raise "Unknown Driver Type" end + + # Now handle the modifiers - for now, just eval'm + @modifiers.each { |modifier| self.class.send(:include, eval("Lab::Modifier::#{modifier}"))} + end def running? diff --git a/lib/lab/vm_controller.rb b/lib/lab/vm_controller.rb index 06a3e047fa..a25e113efd 100644 --- a/lib/lab/vm_controller.rb +++ b/lib/lab/vm_controller.rb @@ -1,25 +1,25 @@ ## ## $Id$ ## -## This is the main lab controller. +## This is the main lab controller. Require this controller to get all +## lab functionality. ## ## $:.unshift(File.expand_path(File.dirname(__FILE__))) +$:.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'))) require 'find' -require 'enumerator' -require 'vm' require 'yaml' -require 'workstation_controller' -require 'workstation_vixr_controller' -require 'remote_workstation_controller' -require 'virtualbox_controller' -require 'dynagen_controller' -require 'remote_esx_controller' -#require 'qemu_controller' -#require 'qemudo_controller' -#require 'amazon_controller' +require 'enumerator' +require 'fileutils' + +require 'vm' +require 'controllers' +require 'drivers' +require 'modifiers' module Lab module Controllers @@ -34,7 +34,8 @@ module Controllers include Lab::Controllers::RemoteEsxController #include Lab::Controllers::QemuController #include Lab::Controllers::QemudoController - #include Lab::Controllers::AmazonController + #include Lab::Controllers::AmazonController + #include Lab::Controllers::FogController def initialize (labdef=nil) @@ -65,6 +66,7 @@ module Controllers end def add_vm(vmid, type,location,credentials=nil,user=nil,host=nil) + @vms << Vm.new( { 'vmid' => vmid, 'driver' => type, 'location' => location, @@ -132,8 +134,8 @@ module Controllers vm_list =::Lab::Controllers::RemoteEsxController::dir_list(dir) #elsif driver_type.downcase == "esxi_vixr" # vm_list =::Lab::Controllers::EsxiVixrController::dir_list(dir) - #elsif driver_type.downcase == "fog_amazon" - # vm_list = ::Lab::Controllers::FogAmazonController::dir_list(dir) + #elsif driver_type.downcase == "fog" + # vm_list = ::Lab::Controllers::FogController::dir_list(dir) else raise TypeError, "Unsupported VM Type" end