vagrant/test/acceptance/support/isolated_environment.rb

103 lines
3.5 KiB
Ruby
Raw Normal View History

require "log4r"
require "childprocess"
require "vagrant/util/subprocess"
2011-12-11 23:53:11 +00:00
require "acceptance/support/virtualbox"
require "support/isolated_environment"
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
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-12-25 06:25:02 +00:00
@apps = apps.clone || {}
@env = env.clone || {}
# 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
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)
# Create the command
command = replace_command(command)
# 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}")
Vagrant::Util::Subprocess.execute(command, *argN) do |type, data|
@logger.debug("#{type}: #{data}") if type == :stdout || type == :stderr
yield type, data if block_given?
end
end
# Closes the environment, cleans up the temporary directories, etc.
def close
# 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
end
def delete_virtual_machines
# Delete all virtual machines
@logger.debug("Finding all virtual machines")
execute("VBoxManage", "list", "vms").stdout.lines.each do |line|
data = /^"(?<name>.+?)" {(?<uuid>.+?)}$/.match(line)
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.
execute("VBoxManage", "controlvm", data[:uuid], "poweroff", :timeout => 5)
rescue Vagrant::Util::Subprocess::TimeoutExceeded => e
@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
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
end
@logger.info("Removed all virtual machines")
end
# 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
end
end