File: command.rb

package info (click to toggle)
vagrant 2.2.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,072 kB
  • sloc: ruby: 80,731; sh: 369; makefile: 9; lisp: 1
file content (97 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'optparse'
require 'securerandom'

module VagrantPlugins
  module CommandPackage
    class Command < Vagrant.plugin("2", :command)
      def self.synopsis
        "packages a running vagrant environment into a box"
      end

      def execute
        options = {}

        opts = OptionParser.new do |o|
          o.banner = "Usage: vagrant package [options] [name|id]"
          o.separator ""
          o.separator "Options:"
          o.separator ""

          o.on("--base NAME", "Name of a VM in VirtualBox to package as a base box (VirtualBox Only)") do |b|
            options[:base] = b
          end

          o.on("--output NAME", "Name of the file to output") do |output|
            options[:output] = output
          end

          o.on("--include FILE,FILE..", Array, "Comma separated additional files to package with the box") do |i|
            options[:include] = i
          end

          o.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

        # Success, exit status 0
        0
      end

      protected

      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.
        provider = Vagrant.plugin("2").manager.providers[:virtualbox]
        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
      end

      def package_target(name, options)
        with_target_vms(name, single_target: true) do |vm|
          @logger.debug("Packaging VM: #{vm.name}")
          package_vm(vm, options)
        end
      end

      def package_vm(vm, options)
        opts = options.inject({}) do |acc, data|
          k,v = data
          acc["package.#{k}"] = v
          acc
        end

        env = vm.action(:package, opts)
        temp_dir = env["export.temp_dir"]
      ensure
        FileUtils.rm_rf(temp_dir) if temp_dir
      end
    end
  end
end