2011-12-04 03:05:50 +00:00
|
|
|
require "fileutils"
|
|
|
|
require "pathname"
|
2012-06-30 23:29:08 +00:00
|
|
|
require "tempfile"
|
2011-12-04 03:05:50 +00:00
|
|
|
|
2012-06-30 23:29:08 +00:00
|
|
|
require "archive/tar/minitar"
|
2011-12-04 03:05:50 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2012-06-30 23:29:08 +00:00
|
|
|
require "vagrant/util/platform"
|
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
require "support/isolated_environment"
|
2012-06-30 23:29:08 +00:00
|
|
|
require "support/tempdir"
|
2011-12-04 03:05:50 +00:00
|
|
|
|
|
|
|
module Unit
|
2011-12-11 23:53:11 +00:00
|
|
|
class IsolatedEnvironment < ::IsolatedEnvironment
|
2012-01-09 07:04:23 +00:00
|
|
|
def create_vagrant_env(options=nil)
|
|
|
|
options = {
|
|
|
|
:cwd => @workdir,
|
|
|
|
:home_path => @homedir
|
|
|
|
}.merge(options || {})
|
|
|
|
|
|
|
|
Vagrant::Environment.new(options)
|
2011-12-04 03:05:50 +00:00
|
|
|
end
|
|
|
|
|
2012-03-08 21:24:04 +00:00
|
|
|
# This creates a file in the isolated environment. By default this file
|
|
|
|
# will be created in the working directory of the isolated environment.
|
|
|
|
def file(name, contents)
|
|
|
|
@workdir.join(name).open("w+") do |f|
|
|
|
|
f.write(contents)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-04 03:15:53 +00:00
|
|
|
def vagrantfile(contents, root=nil)
|
|
|
|
root ||= @workdir
|
|
|
|
root.join("Vagrantfile").open("w+") do |f|
|
2011-12-04 03:05:50 +00:00
|
|
|
f.write(contents)
|
|
|
|
end
|
|
|
|
end
|
2011-12-04 03:15:53 +00:00
|
|
|
|
2011-12-04 19:39:44 +00:00
|
|
|
def box(name, vagrantfile_contents="")
|
2012-06-28 05:21:27 +00:00
|
|
|
# Create the box directory
|
2011-12-04 03:15:53 +00:00
|
|
|
box_dir = boxes_dir.join(name)
|
2011-12-04 19:39:44 +00:00
|
|
|
box_dir.mkpath
|
2012-06-28 05:21:27 +00:00
|
|
|
|
|
|
|
# Create the "box.ovf" file because that is how Vagrant heuristically
|
|
|
|
# determines a box is a V1 box.
|
|
|
|
box_dir.join("box.ovf").open("w") { |f| f.write("") }
|
|
|
|
|
|
|
|
# Populate the vagrantfile
|
2011-12-04 03:15:53 +00:00
|
|
|
vagrantfile(vagrantfile_contents, box_dir)
|
2012-06-28 05:21:27 +00:00
|
|
|
|
|
|
|
# Return the directory
|
2012-01-09 07:04:23 +00:00
|
|
|
box_dir
|
2011-12-04 03:15:53 +00:00
|
|
|
end
|
|
|
|
|
2012-06-28 05:21:27 +00:00
|
|
|
# Create an alias because "box" makes a V1 box, so "box1"
|
|
|
|
alias :box1 :box
|
|
|
|
|
2012-06-28 00:15:08 +00:00
|
|
|
# Creates a fake box to exist in this environment.
|
|
|
|
#
|
|
|
|
# @param [String] name Name of the box
|
|
|
|
# @param [Symbol] provider Provider the box was built for.
|
|
|
|
# @return [Pathname] Path to the box directory.
|
|
|
|
def box2(name, provider)
|
|
|
|
# Make the box directory
|
|
|
|
box_dir = boxes_dir.join(name, provider.to_s)
|
|
|
|
box_dir.mkpath
|
|
|
|
|
|
|
|
# Create a metadata.json file
|
|
|
|
box_metadata_file = box_dir.join("metadata.json")
|
|
|
|
box_metadata_file.open("w") do |f|
|
2012-06-30 02:29:06 +00:00
|
|
|
f.write("{}")
|
2012-06-28 00:15:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return the box directory
|
|
|
|
box_dir
|
|
|
|
end
|
|
|
|
|
2012-06-30 23:29:08 +00:00
|
|
|
# This creates a "box" file with the given name and provider.
|
|
|
|
#
|
|
|
|
# @param [String] name Name of the box.
|
|
|
|
# @param [Symbol] provider Provider for the box.
|
|
|
|
# @return [Pathname] Path to the newly created box.
|
|
|
|
def box2_file(name, provider)
|
|
|
|
# This is the metadata we want to store in our file
|
|
|
|
metadata = {
|
|
|
|
"type" => "v2_box",
|
|
|
|
"provider" => provider
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create a temporary directory to store our data we will tar up
|
|
|
|
td_source = Tempdir.new
|
|
|
|
td_dest = Tempdir.new
|
|
|
|
|
|
|
|
# Store the temporary directory so it is not deleted until
|
|
|
|
# this instance is garbage collected.
|
|
|
|
@_box2_file_temp ||= []
|
|
|
|
@_box2_file_temp << td_dest
|
|
|
|
|
|
|
|
# The source as a Pathname, which is easier to work with
|
|
|
|
source = Pathname.new(td_source.path)
|
|
|
|
|
|
|
|
# The destination file
|
|
|
|
result = Pathname.new(td_dest.path).join("temporary.box")
|
|
|
|
|
|
|
|
File.open(result, Vagrant::Util::Platform.tar_file_options) do |tar|
|
|
|
|
Archive::Tar::Minitar::Output.open(tar) do |output|
|
|
|
|
begin
|
|
|
|
# Switch to the source directory so that Archive::Tar::Minitar
|
|
|
|
# can tar things up.
|
|
|
|
current_dir = FileUtils.pwd
|
|
|
|
FileUtils.cd(source)
|
|
|
|
|
|
|
|
# Put the metadata.json in here.
|
|
|
|
source.join("metadata.json").open("w") do |f|
|
|
|
|
f.write(JSON.generate(metadata))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add all the files
|
|
|
|
Dir.glob(File.join(".", "**", "*")).each do |entry|
|
|
|
|
Archive::Tar::Minitar.pack_file(entry, output)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
FileUtils.cd(current_dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Resulting box
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2011-12-04 03:15:53 +00:00
|
|
|
def boxes_dir
|
|
|
|
dir = @homedir.join("boxes")
|
2011-12-04 19:39:44 +00:00
|
|
|
dir.mkpath
|
2011-12-04 03:15:53 +00:00
|
|
|
dir
|
|
|
|
end
|
2011-12-04 03:05:50 +00:00
|
|
|
end
|
|
|
|
end
|