2011-12-18 00:58:54 +00:00
|
|
|
require 'optparse'
|
|
|
|
|
2012-04-19 20:59:48 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommandPackage
|
2012-06-26 23:18:02 +00:00
|
|
|
class Command < Vagrant.plugin("1", :command)
|
2010-08-25 06:25:01 +00:00
|
|
|
def execute
|
2011-12-18 00:58:54 +00:00
|
|
|
options = {}
|
|
|
|
|
|
|
|
opts = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: vagrant package [vm-name] [--base name] [--output name.box]"
|
|
|
|
opts.separator " [--include one,two,three] [--vagrantfile file]"
|
|
|
|
|
|
|
|
opts.separator ""
|
|
|
|
|
|
|
|
opts.on("--base NAME", "Name of a VM in virtualbox to package as a base box") do |b|
|
|
|
|
options[:base] = b
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on("--output NAME", "Name of the file to output") do |o|
|
|
|
|
options[:output] = o
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on("--include x,y,z", Array, "Additional files to package with the box.") do |i|
|
|
|
|
options[:include] = i
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on("--vagrantfile file", "Vagrantfile to package with the box.") do |v|
|
|
|
|
options[:vagrantfile] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Parse the options
|
|
|
|
argv = parse_options(opts)
|
|
|
|
return if !argv
|
|
|
|
|
|
|
|
@logger.debug("package options: #{options.inspect}")
|
|
|
|
if options[:base]
|
|
|
|
package_base(options)
|
|
|
|
else
|
|
|
|
package_target(argv[0], options)
|
|
|
|
end
|
2012-03-23 15:07:35 +00:00
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
0
|
|
|
|
end
|
2010-08-25 06:25:01 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-12-18 00:58:54 +00:00
|
|
|
def package_base(options)
|
2012-04-19 20:59:48 +00:00
|
|
|
vm = Vagrant::VM.new(options[:base], @env, @env.config.global, :base => true)
|
|
|
|
raise Vagrant::Errors::BaseVMNotFound, :name => options[:base] if !vm.created?
|
2011-12-18 00:58:54 +00:00
|
|
|
@logger.debug("Packaging base VM: #{vm.name}")
|
|
|
|
package_vm(vm, options)
|
2010-08-25 06:25:01 +00:00
|
|
|
end
|
|
|
|
|
2011-12-18 00:58:54 +00:00
|
|
|
def package_target(name, options)
|
2012-03-23 15:29:10 +00:00
|
|
|
with_target_vms(name, :single_target => true) do |vm|
|
2012-04-19 20:59:48 +00:00
|
|
|
raise Vagrant::Errors::VMNotCreatedError if !vm.created?
|
2011-12-18 00:58:54 +00:00
|
|
|
@logger.debug("Packaging VM: #{vm.name}")
|
|
|
|
package_vm(vm, options)
|
|
|
|
end
|
2010-08-25 07:11:59 +00:00
|
|
|
end
|
|
|
|
|
2011-12-18 00:58:54 +00:00
|
|
|
def package_vm(vm, options)
|
2010-08-25 07:11:59 +00:00
|
|
|
opts = options.inject({}) do |acc, data|
|
|
|
|
k,v = data
|
|
|
|
acc["package.#{k}"] = v
|
|
|
|
acc
|
|
|
|
end
|
|
|
|
|
|
|
|
vm.package(opts)
|
2010-08-25 06:25:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|