2011-11-03 04:09:38 +00:00
|
|
|
require "log4r"
|
2011-11-22 05:16:51 +00:00
|
|
|
require "childprocess"
|
2011-11-03 04:09:38 +00:00
|
|
|
|
2011-12-23 05:46:43 +00:00
|
|
|
require "vagrant/util/subprocess"
|
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
require "acceptance/support/virtualbox"
|
|
|
|
require "support/isolated_environment"
|
2011-11-03 04:09:38 +00:00
|
|
|
|
|
|
|
module Acceptance
|
|
|
|
# This class manages an isolated environment for Vagrant to
|
|
|
|
# run in. It creates a temporary directory to act as the
|
|
|
|
# working directory as well as sets a custom home directory.
|
2011-12-11 23:53:11 +00:00
|
|
|
class IsolatedEnvironment < ::IsolatedEnvironment
|
2011-11-03 04:09:38 +00:00
|
|
|
def initialize(apps=nil, env=nil)
|
2011-12-11 23:53:11 +00:00
|
|
|
super()
|
|
|
|
|
2011-12-25 06:25:02 +00:00
|
|
|
@logger = Log4r::Logger.new("test::acceptance::isolated_environment")
|
2011-11-03 04:09:38 +00:00
|
|
|
|
2011-12-25 06:25:02 +00:00
|
|
|
@apps = apps.clone || {}
|
|
|
|
@env = env.clone || {}
|
2011-11-03 04:09:38 +00:00
|
|
|
|
2011-11-06 21:30:49 +00:00
|
|
|
# Set the home directory and virtualbox home directory environmental
|
|
|
|
# variables so that Vagrant and VirtualBox see the proper paths here.
|
2011-12-25 06:25:02 +00:00
|
|
|
@env["HOME"] ||= @homedir.to_s
|
|
|
|
@env["VBOX_USER_HOME"] ||= @homedir.to_s
|
2011-11-03 04:09:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Executes a command in the context of this isolated environment.
|
|
|
|
# Any command executed will therefore see our temporary directory
|
|
|
|
# as the home directory.
|
|
|
|
def execute(command, *argN)
|
2011-12-23 05:46:43 +00:00
|
|
|
# Create the command
|
2011-11-03 06:16:29 +00:00
|
|
|
command = replace_command(command)
|
2011-11-03 04:09:38 +00:00
|
|
|
|
2011-12-23 05:46:43 +00:00
|
|
|
# Determine the options
|
|
|
|
options = argN.last.is_a?(Hash) ? argN.pop : {}
|
|
|
|
options = {
|
|
|
|
:workdir => @workdir,
|
|
|
|
:env => @env
|
|
|
|
}.merge(options)
|
|
|
|
|
|
|
|
# Add the options to be passed on
|
|
|
|
argN << options
|
|
|
|
|
|
|
|
# Execute, logging out the stdout/stderr as we get it
|
2011-12-25 06:25:02 +00:00
|
|
|
@logger.info("Executing: #{[command].concat(argN).inspect}")
|
2011-12-23 05:46:43 +00:00
|
|
|
Vagrant::Util::Subprocess.execute(command, *argN) do |type, data|
|
|
|
|
@logger.debug("#{type}: #{data}") if type == :stdout || type == :stderr
|
|
|
|
yield type, data if block_given?
|
2011-11-20 18:38:41 +00:00
|
|
|
end
|
2011-11-03 04:09:38 +00:00
|
|
|
end
|
2011-11-03 04:41:41 +00:00
|
|
|
|
|
|
|
# Closes the environment, cleans up the temporary directories, etc.
|
|
|
|
def close
|
2011-11-07 03:20:14 +00:00
|
|
|
# Only delete virtual machines if VBoxSVC is running, meaning
|
|
|
|
# that something related to VirtualBox started running in this
|
|
|
|
# environment.
|
|
|
|
delete_virtual_machines if VirtualBox.find_vboxsvc
|
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
# Let the parent handle cleaning up
|
|
|
|
super
|
2011-11-07 03:20:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_virtual_machines
|
2011-11-07 02:45:49 +00:00
|
|
|
# Delete all virtual machines
|
|
|
|
@logger.debug("Finding all virtual machines")
|
|
|
|
execute("VBoxManage", "list", "vms").stdout.lines.each do |line|
|
|
|
|
data = /^"(?<name>.+?)" {(?<uuid>.+?)}$/.match(line)
|
|
|
|
|
2011-11-20 18:44:09 +00:00
|
|
|
begin
|
|
|
|
@logger.debug("Removing VM: #{data[:name]}")
|
|
|
|
|
|
|
|
# We add a timeout onto this because sometimes for seemingly no
|
|
|
|
# reason it will simply freeze, although the VM is successfully
|
|
|
|
# "aborted." The timeout gets around this strange behavior.
|
2011-11-23 03:28:21 +00:00
|
|
|
execute("VBoxManage", "controlvm", data[:uuid], "poweroff", :timeout => 5)
|
2011-12-23 05:46:43 +00:00
|
|
|
rescue Vagrant::Util::Subprocess::TimeoutExceeded => e
|
2011-11-20 18:44:09 +00:00
|
|
|
@logger.info("Failed to poweroff VM '#{data[:uuid]}'. Killing process.")
|
|
|
|
|
|
|
|
# Kill the process and wait a bit for it to disappear
|
|
|
|
Process.kill('KILL', e.pid)
|
|
|
|
Process.waitpid2(e.pid)
|
|
|
|
end
|
2011-11-07 02:45:49 +00:00
|
|
|
|
|
|
|
sleep 0.5
|
|
|
|
|
|
|
|
result = execute("VBoxManage", "unregistervm", data[:uuid], "--delete")
|
2011-12-25 01:59:10 +00:00
|
|
|
raise Exception, "VM unregistration failed!" if result.exit_code != 0
|
2011-11-07 02:45:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@logger.info("Removed all virtual machines")
|
2011-11-03 04:41:41 +00:00
|
|
|
end
|
2011-11-03 06:16:29 +00:00
|
|
|
|
|
|
|
# This replaces a command with a replacement defined when this
|
|
|
|
# isolated environment was initialized. If nothing was defined,
|
|
|
|
# then the command itself is returned.
|
|
|
|
def replace_command(command)
|
|
|
|
return @apps[command] if @apps.has_key?(command)
|
|
|
|
return command
|
|
|
|
end
|
2011-11-03 04:09:38 +00:00
|
|
|
end
|
|
|
|
end
|