vagrant/plugins/commands/package/command.rb

84 lines
2.3 KiB
Ruby
Raw Normal View History

2011-12-18 00:58:54 +00:00
require 'optparse'
2012-04-19 20:59:48 +00:00
module VagrantPlugins
module CommandPackage
2012-11-07 05:05:14 +00:00
class Command < Vagrant.plugin("2", :command)
2010-08-25 06:25:01 +00:00
def execute
2011-12-18 00:58:54 +00:00
options = {}
2012-08-16 04:04:37 +00:00
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant package [vm-name] [--base name] [--output name.box]"
o.separator " [--include one,two,three] [--vagrantfile file]"
2011-12-18 00:58:54 +00:00
2012-08-16 04:04:37 +00:00
o.separator ""
2011-12-18 00:58:54 +00:00
2012-08-16 04:04:37 +00:00
o.on("--base NAME", "Name of a VM in virtualbox to package as a base box") do |b|
2011-12-18 00:58:54 +00:00
options[:base] = b
end
2012-08-16 04:04:37 +00:00
o.on("--output NAME", "Name of the file to output") do |output|
options[:output] = output
2011-12-18 00:58:54 +00:00
end
2012-08-16 04:04:37 +00:00
o.on("--include x,y,z", Array, "Additional files to package with the box.") do |i|
2011-12-18 00:58:54 +00:00
options[:include] = i
end
2012-08-16 04:04:37 +00:00
o.on("--vagrantfile file", "Vagrantfile to package with the box.") do |v|
2011-12-18 00:58:54 +00:00
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
# Success, exit status 0
0
2012-08-16 04:04:37 +00:00
end
2010-08-25 06:25:01 +00:00
protected
2011-12-18 00:58:54 +00:00
def package_base(options)
# XXX: This whole thing is hardcoded and very temporary. The whole
# `vagrant package --base` process is deprecated for something much
# better in the future. We just hardcode this to keep VirtualBox working
# for now.
2013-02-21 19:14:39 +00:00
provider = Vagrant.plugin("2").manager.providers[:virtualbox]
vm = Vagrant::Machine.new(
options[:base],
:virtualbox, provider,
nil, @env.config_global,
nil, nil,
@env, true)
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)
with_target_vms(name, :single_target => true) do |vm|
2011-12-18 00:58:54 +00:00
@logger.debug("Packaging VM: #{vm.name}")
package_vm(vm, options)
end
end
2011-12-18 00:58:54 +00:00
def package_vm(vm, options)
opts = options.inject({}) do |acc, data|
k,v = data
acc["package.#{k}"] = v
acc
end
2012-08-16 04:04:37 +00:00
vm.action(:package, opts)
2010-08-25 06:25:01 +00:00
end
end
end
end