2010-12-28 19:47:33 +00:00
|
|
|
##
|
|
|
|
## $Id$
|
|
|
|
##
|
2011-05-24 15:56:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# !!WARNING!! - All drivers are expected to filter input before running
|
|
|
|
# anything based on it. This is particularly important in the case
|
2011-08-29 16:03:28 +00:00
|
|
|
# of the drivers which wrap a command line to provide functionality.
|
2011-05-24 15:56:32 +00:00
|
|
|
#
|
|
|
|
|
2011-01-17 05:58:16 +00:00
|
|
|
module Lab
|
|
|
|
module Drivers
|
2010-12-28 19:47:33 +00:00
|
|
|
class VmDriver
|
2011-08-29 16:03:28 +00:00
|
|
|
|
|
|
|
attr_accessor :vmid
|
|
|
|
attr_accessor :location
|
|
|
|
attr_accessor :os
|
|
|
|
attr_accessor :tools
|
|
|
|
attr_accessor :credentials
|
|
|
|
|
2011-09-17 03:00:57 +00:00
|
|
|
def initialize(config)
|
|
|
|
|
|
|
|
@vmid = filter_command(config["vmid"].to_s)
|
|
|
|
@location = filter_command(config["location"])
|
|
|
|
@credentials = config["credentials"] || []
|
|
|
|
@tools = filter_input(config["tools"])
|
|
|
|
@os = filter_input(config["os"])
|
2011-08-29 16:03:28 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def register # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def unregister # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def start # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def stop # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def suspend # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def pause # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
|
|
|
end
|
|
|
|
|
|
|
|
def resume # Must be implemented in a child *_driver class
|
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def reset # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def create_snapshot(snapshot) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def revert_snapshot(snapshot) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def delete_snapshot(snapshot) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def run_command(command) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def copy_from(from, to) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def copy_to(from, to) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def check_file_exists(file) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def create_directory(directory) # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def cleanup # Must be implemented in a child *_driver class
|
2011-07-11 05:52:51 +00:00
|
|
|
raise "Command not Implemented"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
private
|
2011-07-11 05:52:51 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def scp_to(from,to)
|
|
|
|
require 'net/scp'
|
2011-08-29 16:03:28 +00:00
|
|
|
|
2011-09-17 03:00:57 +00:00
|
|
|
Net::SCP.start(@hostname, @vm_user, :password => @vm_pass) do |scp|
|
2011-07-29 18:20:02 +00:00
|
|
|
scp.upload!(from,to)
|
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def scp_from(from,to)
|
|
|
|
require 'net/scp'
|
2011-08-29 16:03:28 +00:00
|
|
|
|
2011-07-29 18:20:02 +00:00
|
|
|
# download a file from a remote server
|
2011-09-17 03:00:57 +00:00
|
|
|
Net::SCP.start(@hostname, @vm_user, :password => @vm_pass) do |scp|
|
2011-07-29 18:20:02 +00:00
|
|
|
scp.download!(from,to)
|
2011-08-29 16:03:28 +00:00
|
|
|
end
|
2011-05-24 15:56:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ssh_exec(command)
|
2011-09-17 03:00:57 +00:00
|
|
|
Net::SSH.start(@hostname, @vm_user, :password => @vm_pass) do |ssh|
|
2011-07-29 18:20:02 +00:00
|
|
|
result = ssh.exec!(command)
|
2011-01-17 05:58:16 +00:00
|
|
|
end
|
2011-05-24 15:56:32 +00:00
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
def filter_input(string)
|
2011-08-29 16:03:28 +00:00
|
|
|
return "" unless string # nil becomes empty string
|
|
|
|
return unless string.class == String # Allow other types unmodified
|
|
|
|
|
2011-09-17 03:00:57 +00:00
|
|
|
unless /^[\w\s\[\]\{\}\/\\\.\-\"\(\):!]*$/.match string
|
2011-05-24 15:56:32 +00:00
|
|
|
raise "WARNING! Invalid character in: #{string}"
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
|
|
|
|
2011-05-24 15:56:32 +00:00
|
|
|
string
|
|
|
|
end
|
2011-07-18 20:26:26 +00:00
|
|
|
|
|
|
|
def filter_command(string)
|
2011-08-29 16:03:28 +00:00
|
|
|
return "" unless string # nil becomes empty string
|
|
|
|
return unless string.class == String # Allow other types unmodified
|
|
|
|
|
2011-09-17 03:00:57 +00:00
|
|
|
unless /^[\w\s\[\]\{\}\/\\\.\-\"\(\)]*$/.match string
|
2011-07-18 20:26:26 +00:00
|
|
|
raise "WARNING! Invalid character in: #{string}"
|
|
|
|
end
|
|
|
|
|
|
|
|
string
|
|
|
|
end
|
2011-05-24 15:56:32 +00:00
|
|
|
|
2011-08-29 16:03:28 +00:00
|
|
|
# The only reason we don't filter here is because we need
|
|
|
|
# the ability to still run clean (controlled entirely by us)
|
|
|
|
# command lines.
|
2011-05-24 15:56:32 +00:00
|
|
|
def system_command(command)
|
2011-07-29 18:20:02 +00:00
|
|
|
system(command)
|
2011-05-24 15:56:32 +00:00
|
|
|
end
|
2010-12-28 19:47:33 +00:00
|
|
|
end
|
2011-01-17 05:58:16 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|