2011-12-11 23:53:11 +00:00
|
|
|
require "fileutils"
|
|
|
|
require "pathname"
|
2014-01-15 18:48:19 +00:00
|
|
|
require "tmpdir"
|
2011-12-11 23:53:11 +00:00
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
2015-11-20 23:09:17 +00:00
|
|
|
require "vagrant/util/platform"
|
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# This class also provides various helpers to create Vagrantfiles,
|
|
|
|
# boxes, etc.
|
|
|
|
class IsolatedEnvironment
|
|
|
|
attr_reader :homedir
|
|
|
|
attr_reader :workdir
|
|
|
|
|
|
|
|
# Initializes an isolated environment. You can pass in some
|
|
|
|
# options here to configure runing custom applications in place
|
|
|
|
# of others as well as specifying environmental variables.
|
|
|
|
#
|
|
|
|
# @param [Hash] apps A mapping of application name (such as "vagrant")
|
|
|
|
# to an alternate full path to the binary to run.
|
|
|
|
# @param [Hash] env Additional environmental variables to inject
|
|
|
|
# into the execution environments.
|
|
|
|
def initialize
|
2011-12-25 06:25:02 +00:00
|
|
|
@logger = Log4r::Logger.new("test::isolated_environment")
|
2011-12-11 23:53:11 +00:00
|
|
|
|
|
|
|
# Create a temporary directory for our work
|
2016-05-29 03:04:38 +00:00
|
|
|
@tempdir = Vagrant::Util::Platform.fs_real_path(Dir.mktmpdir("vagrant-iso-env"))
|
2014-01-15 18:48:19 +00:00
|
|
|
@logger.info("Initialize isolated environment: #{@tempdir}")
|
2011-12-11 23:53:11 +00:00
|
|
|
|
|
|
|
# Setup the home and working directories
|
2014-01-15 18:48:19 +00:00
|
|
|
@homedir = Pathname.new(File.join(@tempdir, "home"))
|
|
|
|
@workdir = Pathname.new(File.join(@tempdir, "work"))
|
2011-12-11 23:53:11 +00:00
|
|
|
|
|
|
|
@homedir.mkdir
|
|
|
|
@workdir.mkdir
|
|
|
|
end
|
|
|
|
|
|
|
|
# This closes the environment by cleaning it up.
|
|
|
|
def close
|
2014-01-15 18:48:19 +00:00
|
|
|
@logger.info("Removing isolated environment: #{@tempdir}")
|
|
|
|
FileUtils.rm_rf(@tempdir)
|
2011-12-11 23:53:11 +00:00
|
|
|
end
|
|
|
|
end
|