2011-12-04 03:05:50 +00:00
|
|
|
require "fileutils"
|
|
|
|
require "pathname"
|
2012-06-30 23:29:08 +00:00
|
|
|
require "tempfile"
|
2014-01-15 18:48:19 +00:00
|
|
|
require "tmpdir"
|
2011-12-04 03:05:50 +00:00
|
|
|
|
2012-07-11 05:16:43 +00:00
|
|
|
require "json"
|
2011-12-04 03:05:50 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2012-06-30 23:29:08 +00:00
|
|
|
require "vagrant/util/platform"
|
2013-01-28 21:28:40 +00:00
|
|
|
require "vagrant/util/subprocess"
|
2012-06-30 23:29:08 +00:00
|
|
|
|
2011-12-11 23:53:11 +00:00
|
|
|
require "support/isolated_environment"
|
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 = {
|
2014-05-22 16:35:12 +00:00
|
|
|
cwd: @workdir,
|
|
|
|
home_path: @homedir
|
2012-01-09 07:04:23 +00:00
|
|
|
}.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.
|
2012-07-10 01:31:53 +00:00
|
|
|
def box2(name, provider, options=nil)
|
|
|
|
# Default options
|
|
|
|
options = {
|
2014-05-22 16:35:12 +00:00
|
|
|
vagrantfile: ""
|
2012-07-10 01:31:53 +00:00
|
|
|
}.merge(options || {})
|
|
|
|
|
2012-06-28 00:15:08 +00:00
|
|
|
# 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-07-11 05:16:43 +00:00
|
|
|
f.write(JSON.generate({
|
2014-05-22 16:35:12 +00:00
|
|
|
provider: provider.to_s
|
2012-07-11 05:16:43 +00:00
|
|
|
}))
|
2012-06-28 00:15:08 +00:00
|
|
|
end
|
|
|
|
|
2012-07-10 01:31:53 +00:00
|
|
|
# Create a Vagrantfile
|
|
|
|
box_vagrantfile = box_dir.join("Vagrantfile")
|
|
|
|
box_vagrantfile.open("w") do |f|
|
|
|
|
f.write(options[:vagrantfile])
|
|
|
|
end
|
|
|
|
|
2012-06-28 00:15:08 +00:00
|
|
|
# Return the box directory
|
|
|
|
box_dir
|
|
|
|
end
|
|
|
|
|
2014-01-22 19:42:39 +00:00
|
|
|
# Creates a fake box to exist in this environment according
|
|
|
|
# to the "gen-3" box format.
|
|
|
|
#
|
|
|
|
# @param [String] name
|
|
|
|
# @param [String] version
|
|
|
|
# @param [String] provider
|
|
|
|
# @return [Pathname]
|
|
|
|
def box3(name, version, provider, **opts)
|
|
|
|
# Create the directory for the box
|
|
|
|
box_dir = boxes_dir.join(name, version, provider.to_s)
|
|
|
|
box_dir.mkpath
|
|
|
|
|
|
|
|
# Create the metadata.json for it
|
|
|
|
box_metadata_file = box_dir.join("metadata.json")
|
|
|
|
box_metadata_file.open("w") do |f|
|
|
|
|
f.write(JSON.generate({
|
2014-05-22 16:35:12 +00:00
|
|
|
provider: provider.to_s
|
2014-01-22 19:42:39 +00:00
|
|
|
}))
|
|
|
|
end
|
|
|
|
|
2014-01-23 00:13:24 +00:00
|
|
|
# Create a Vagrantfile
|
|
|
|
if opts[:vagrantfile]
|
|
|
|
box_vagrantfile = box_dir.join("Vagrantfile")
|
|
|
|
box_vagrantfile.open("w") do |f|
|
|
|
|
f.write(opts[:vagrantfile])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-25 18:03:33 +00:00
|
|
|
# Create the metadata URL
|
|
|
|
if opts[:metadata_url]
|
|
|
|
boxes_dir.join(name, "metadata_url").open("w") do |f|
|
|
|
|
f.write(opts[:metadata_url])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-22 19:42:39 +00:00
|
|
|
box_dir
|
|
|
|
end
|
|
|
|
|
2012-07-10 03:32:53 +00:00
|
|
|
# This creates a "box" file that is a valid V1 box.
|
|
|
|
#
|
|
|
|
# @return [Pathname] Path to the newly created box.
|
|
|
|
def box1_file
|
|
|
|
# Create a temporary directory to store our data we will tar up
|
2016-05-29 03:04:38 +00:00
|
|
|
td_source = Dir.mktmpdir("vagrant-box1-source")
|
|
|
|
td_dest = Dir.mktmpdir("vagrant-box-1-dest")
|
2012-07-10 03:32:53 +00:00
|
|
|
|
|
|
|
# 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
|
2014-01-15 19:10:38 +00:00
|
|
|
source = Pathname.new(td_source)
|
2012-07-10 03:32:53 +00:00
|
|
|
|
|
|
|
# The destination file
|
2014-01-15 19:10:38 +00:00
|
|
|
result = Pathname.new(td_dest).join("temporary.box")
|
2012-07-10 03:32:53 +00:00
|
|
|
|
2013-01-28 21:28:40 +00:00
|
|
|
# Put a "box.ovf" in there.
|
|
|
|
source.join("box.ovf").open("w") do |f|
|
|
|
|
f.write("FOO!")
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(source) do
|
|
|
|
# Find all the files in our current directory and tar it up!
|
|
|
|
files = Dir.glob(File.join(".", "**", "*"))
|
|
|
|
|
|
|
|
# Package!
|
|
|
|
Vagrant::Util::Subprocess.execute("bsdtar", "-czf", result.to_s, *files)
|
2012-07-10 03:32:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Resulting box
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2012-07-02 04:41:57 +00:00
|
|
|
# This creates a "box" file with the given provider.
|
2012-06-30 23:29:08 +00:00
|
|
|
#
|
|
|
|
# @param [Symbol] provider Provider for the box.
|
|
|
|
# @return [Pathname] Path to the newly created box.
|
2013-03-14 04:19:59 +00:00
|
|
|
def box2_file(provider, options=nil)
|
|
|
|
options ||= {}
|
|
|
|
|
2012-06-30 23:29:08 +00:00
|
|
|
# This is the metadata we want to store in our file
|
|
|
|
metadata = {
|
|
|
|
"type" => "v2_box",
|
|
|
|
"provider" => provider
|
2013-03-14 04:19:59 +00:00
|
|
|
}.merge(options[:metadata] || {})
|
2012-06-30 23:29:08 +00:00
|
|
|
|
|
|
|
# Create a temporary directory to store our data we will tar up
|
2016-05-29 03:04:38 +00:00
|
|
|
td_source = Dir.mktmpdir("vagrant-box-2-source")
|
|
|
|
td_dest = Dir.mktmpdir("vagrant-box-2-dest")
|
2012-06-30 23:29:08 +00:00
|
|
|
|
|
|
|
# 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
|
2014-01-15 19:10:38 +00:00
|
|
|
source = Pathname.new(td_source)
|
2012-06-30 23:29:08 +00:00
|
|
|
|
|
|
|
# The destination file
|
2014-01-15 19:10:38 +00:00
|
|
|
result = Pathname.new(td_dest).join("temporary.box")
|
2012-06-30 23:29:08 +00:00
|
|
|
|
2013-01-28 21:28:40 +00:00
|
|
|
# Put the metadata.json in here.
|
|
|
|
source.join("metadata.json").open("w") do |f|
|
|
|
|
f.write(JSON.generate(metadata))
|
|
|
|
end
|
|
|
|
|
|
|
|
Dir.chdir(source) do
|
|
|
|
# Find all the files in our current directory and tar it up!
|
|
|
|
files = Dir.glob(File.join(".", "**", "*"))
|
|
|
|
|
|
|
|
# Package!
|
|
|
|
Vagrant::Util::Subprocess.execute("bsdtar", "-czf", result.to_s, *files)
|
2012-06-30 23:29:08 +00:00
|
|
|
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
|