2023-08-10 16:53:25 -04:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
|
# SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2011-12-17 19:58:54 -05:00
|
|
|
require 'optparse'
|
2016-12-28 18:02:13 -05:00
|
|
|
require 'securerandom'
|
2011-12-17 19:58:54 -05:00
|
|
|
|
2012-04-19 16:59:48 -04:00
|
|
|
module VagrantPlugins
|
|
|
|
|
module CommandPackage
|
2012-11-07 00:05:14 -05:00
|
|
|
class Command < Vagrant.plugin("2", :command)
|
2013-11-23 17:00:42 -05:00
|
|
|
def self.synopsis
|
|
|
|
|
"packages a running vagrant environment into a box"
|
|
|
|
|
end
|
|
|
|
|
|
2010-08-25 02:25:01 -04:00
|
|
|
def execute
|
2011-12-17 19:58:54 -05:00
|
|
|
options = {}
|
|
|
|
|
|
2012-08-16 00:04:37 -04:00
|
|
|
opts = OptionParser.new do |o|
|
2016-05-29 01:06:51 -04:00
|
|
|
o.banner = "Usage: vagrant package [options] [name|id]"
|
2014-02-08 03:20:50 -05:00
|
|
|
o.separator ""
|
|
|
|
|
o.separator "Options:"
|
2012-08-16 00:04:37 -04:00
|
|
|
o.separator ""
|
2011-12-17 19:58:54 -05:00
|
|
|
|
2016-12-08 15:22:24 -05:00
|
|
|
o.on("--base NAME", "Name of a VM in VirtualBox to package as a base box (VirtualBox Only)") do |b|
|
2011-12-17 19:58:54 -05:00
|
|
|
options[:base] = b
|
|
|
|
|
end
|
|
|
|
|
|
2012-08-16 00:04:37 -04:00
|
|
|
o.on("--output NAME", "Name of the file to output") do |output|
|
|
|
|
|
options[:output] = output
|
2011-12-17 19:58:54 -05:00
|
|
|
end
|
|
|
|
|
|
2016-12-10 15:56:21 -05:00
|
|
|
o.on("--include FILE,FILE..", Array, "Comma separated additional files to package with the box") do |i|
|
2011-12-17 19:58:54 -05:00
|
|
|
options[:include] = i
|
|
|
|
|
end
|
|
|
|
|
|
2021-04-15 01:28:41 -04:00
|
|
|
o.on("--info FILE", "Path to a custom info.json file containing additional box information") do |info|
|
|
|
|
|
options[:info] = info
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-08 03:20:50 -05:00
|
|
|
o.on("--vagrantfile FILE", "Vagrantfile to package with the box") do |v|
|
2011-12-17 19:58:54 -05: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
|
2012-03-23 11:07:35 -04:00
|
|
|
|
|
|
|
|
# Success, exit status 0
|
|
|
|
|
0
|
2012-08-16 00:04:37 -04:00
|
|
|
end
|
2010-08-25 02:25:01 -04:00
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
2011-12-17 19:58:54 -05:00
|
|
|
def package_base(options)
|
2012-08-19 21:51:36 -04:00
|
|
|
# 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 14:14:39 -05:00
|
|
|
provider = Vagrant.plugin("2").manager.providers[:virtualbox]
|
2016-08-16 14:39:32 -04:00
|
|
|
tmp_data_directory = File.join(@env.tmp_path, SecureRandom.uuid)
|
|
|
|
|
FileUtils.mkdir_p(tmp_data_directory)
|
|
|
|
|
begin
|
|
|
|
|
vm = Vagrant::Machine.new(
|
|
|
|
|
options[:base],
|
|
|
|
|
:virtualbox, provider[0], nil, provider[1],
|
|
|
|
|
@env.vagrantfile.config,
|
|
|
|
|
Pathname.new(tmp_data_directory), nil,
|
|
|
|
|
@env, @env.vagrantfile, true)
|
|
|
|
|
@logger.debug("Packaging base VM: #{vm.name}")
|
|
|
|
|
package_vm(vm, options)
|
|
|
|
|
ensure
|
|
|
|
|
FileUtils.rm_rf(tmp_data_directory)
|
|
|
|
|
end
|
2010-08-25 02:25:01 -04:00
|
|
|
end
|
|
|
|
|
|
2011-12-17 19:58:54 -05:00
|
|
|
def package_target(name, options)
|
2014-05-22 12:35:12 -04:00
|
|
|
with_target_vms(name, single_target: true) do |vm|
|
2011-12-17 19:58:54 -05:00
|
|
|
@logger.debug("Packaging VM: #{vm.name}")
|
|
|
|
|
package_vm(vm, options)
|
|
|
|
|
end
|
2010-08-25 03:11:59 -04:00
|
|
|
end
|
|
|
|
|
|
2011-12-17 19:58:54 -05:00
|
|
|
def package_vm(vm, options)
|
2010-08-25 03:11:59 -04:00
|
|
|
opts = options.inject({}) do |acc, data|
|
|
|
|
|
k,v = data
|
|
|
|
|
acc["package.#{k}"] = v
|
|
|
|
|
acc
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-04 14:07:53 -05:00
|
|
|
env = vm.action(:package, opts)
|
|
|
|
|
temp_dir = env["export.temp_dir"]
|
|
|
|
|
ensure
|
|
|
|
|
FileUtils.rm_rf(temp_dir) if temp_dir
|
2010-08-25 02:25:01 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|