File: store_box_metadata.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 (37 lines) | stat: -rw-r--r-- 1,195 bytes parent folder | download | duplicates (5)
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
require "json"

module VagrantPlugins
  module CommandUp
    # Stores metadata information about the box used
    # for the current guest. This allows Vagrant to
    # determine the box currently in use when the
    # Vagrantfile is modified with a new box name or
    # version while the guest still exists.
    class StoreBoxMetadata
      def initialize(app, env)
        @app = app
        @logger = Log4r::Logger.new("vagrant::up::storeboxmetadata")
      end

      def call(env)
        if env[:machine].box
          box = env[:machine].box
          box_meta = {
            name: box.name,
            version: box.version,
            provider: box.provider,
            directory: box.directory.sub(Vagrant.user_data_path.to_s + "/", "")
          }
          meta_file = env[:machine].data_dir.join("box_meta")
          @logger.debug("Writing box metadata file to #{meta_file}")
          File.open(meta_file.to_s, "w+") do |file|
            file.write(JSON.dump(box_meta))
          end
        else
          @logger.debug("No box data found for #{env[:machine].name} with provider #{env[:machine].provider_name}")
        end
        @app.call(env)
      end
    end
  end
end