Base boxes can now be packaged with `vagrant package` by specifying the `--base` parameter.

This commit is contained in:
Mitchell Hashimoto 2010-04-12 16:29:33 -07:00
parent 677b3b05cc
commit f8dee9adf9
5 changed files with 77 additions and 28 deletions

View File

@ -21,9 +21,10 @@ Package the current vagrant environment into a box.
EOS EOS
opt :base, "name of the VM to create a base package", :type => :string
opt :include, "files to include in the package", :type => :strings opt :include, "files to include in the package", :type => :strings
run do |command| run do |command|
Vagrant::Commands.execute(:package, command.argv[0], command.opts[:include]) Vagrant::Commands.execute(:package, command.argv[0], command.opts)
end end
end end

View File

@ -96,11 +96,24 @@ module Vagrant
# Export and package the current vm # Export and package the current vm
# #
# This command requires that an instance be powered off # This command requires that an instance be powered off
def package(out_path=nil, include_files=[]) def package(out_path=nil, opts={})
env.require_persisted_vm opts[:include] ||= []
error_and_exit(:vm_power_off_to_package) unless env.vm.powered_off?
env.vm.package(out_path, include_files) if !opts[:base]
# Packaging a pre-existing environment
env.require_persisted_vm
else
# Packaging a base box; that is a VM not tied to a specific
# vagrant environment
vm = VM.find(opts[:base])
vm.env = env if vm
env.vm = vm
error_and_exit(:vm_base_not_found, :name => opts[:base]) unless vm
end
error_and_exit(:vm_power_off_to_package) unless env.vm.powered_off?
env.vm.package(out_path, opts[:include])
end end
# Manages the `vagrant box` command, allowing the user to add # Manages the `vagrant box` command, allowing the user to add

View File

@ -12,7 +12,7 @@ module Vagrant
attr_reader :root_path attr_reader :root_path
attr_reader :config attr_reader :config
attr_reader :box attr_reader :box
attr_reader :vm attr_accessor :vm
attr_reader :ssh attr_reader :ssh
attr_reader :active_list attr_reader :active_list
attr_reader :commands attr_reader :commands

View File

@ -111,6 +111,8 @@
only supports VirtualBox 3.1.x. Please install the proper version to continue. only supports VirtualBox 3.1.x. Please install the proper version to continue.
:vm_failed_to_boot: |- :vm_failed_to_boot: |-
Failed to connect to VM! Failed to boot? Failed to connect to VM! Failed to boot?
:vm_base_not_found: |-
The specified base VM "<%= name %>" was not found.
:vm_not_running: |- :vm_not_running: |-
VM is not running! Nothing to shut down! VM is not running! Nothing to shut down!
:vm_not_running_for_suspend: |- :vm_not_running_for_suspend: |-

View File

@ -148,11 +148,41 @@ class CommandsTest < Test::Unit::TestCase
@persisted_vm.stubs(:powered_off?).returns(true) @persisted_vm.stubs(:powered_off?).returns(true)
end end
context "with no base specified" do
should "require a persisted vm" do should "require a persisted vm" do
@env.expects(:require_persisted_vm).once @env.expects(:require_persisted_vm).once
@commands.package @commands.package
end end
end
context "with base specified" do
setup do
@vm = mock("vm")
Vagrant::VM.stubs(:find).with(@name).returns(@vm)
@vm.stubs(:env=).with(@env)
@env.stubs(:vm=)
@name = :bar
end
should "find the given base and set it on the env" do
Vagrant::VM.expects(:find).with(@name).returns(@vm)
@vm.expects(:env=).with(@env)
@env.expects(:vm=).with(@vm)
@commands.package("foo", { :base => @name })
end
should "error if the VM is not found" do
Vagrant::VM.expects(:find).with(@name).returns(nil)
@commands.expects(:error_and_exit).with(:vm_base_not_found, :name => @name).once
@commands.package("foo", { :base => @name })
end
end
context "shared (with and without base specified)" do
should "error and exit if the VM is not powered off" do should "error and exit if the VM is not powered off" do
@persisted_vm.stubs(:powered_off?).returns(false) @persisted_vm.stubs(:powered_off?).returns(false)
@commands.expects(:error_and_exit).with(:vm_power_off_to_package).once @commands.expects(:error_and_exit).with(:vm_power_off_to_package).once
@ -169,7 +199,9 @@ class CommandsTest < Test::Unit::TestCase
out_path = mock("out_path") out_path = mock("out_path")
include_files = mock("include_files") include_files = mock("include_files")
@persisted_vm.expects(:package).with(out_path, include_files).once @persisted_vm.expects(:package).with(out_path, include_files).once
@commands.package(out_path, include_files) @commands.package(out_path, {
:include => include_files
})
end end
should "default to an empty array when not include_files are specified" do should "default to an empty array when not include_files are specified" do
@ -178,6 +210,7 @@ class CommandsTest < Test::Unit::TestCase
@commands.package(out_path) @commands.package(out_path)
end end
end end
end
context "box" do context "box" do
setup do setup do