Class: Vagrant::Action::General::Package

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/vagrant/action/general/package.rb

Overview

A general packaging (tar) middleware. Given the following options, it will do the right thing:

  • package.output - The filename of the outputted package.
  • package.include - An array of files to include in the package.
  • package.directory - The directory which contains the contents to compress into the package.

This middleware always produces the final file in the current working directory (FileUtils.pwd)

Direct Known Subclasses

Box::Package, VM::Package

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Package

Returns a new instance of Package.



20
21
22
23
24
25
26
# File 'lib/vagrant/action/general/package.rb', line 20

def initialize(app, env)
  @app = app
  @env = env
  @env["package.output"] ||= env["config"].package.name
  @env["package.include"] ||= []
  @env["package.vagrantfile"] ||= nil
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vagrant/action/general/package.rb', line 28

def call(env)
  @env = env

  raise Errors::PackageOutputExists if File.exist?(tar_path)
  raise Errors::PackageRequiresDirectory if !@env["package.directory"] || !File.directory?(@env["package.directory"])

  verify_files_to_copy
  compress

  @app.call(env)
end

#compressObject

Compress the exported file into a package



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/vagrant/action/general/package.rb', line 82

def compress
  @env.ui.info I18n.t("vagrant.actions.general.package.compressing", :tar_path => tar_path)
  File.open(tar_path, Platform.tar_file_options) do |tar|
    Archive::Tar::Minitar::Output.open(tar) do |output|
      begin
        current_dir = FileUtils.pwd

        copy_include_files

        FileUtils.cd(@env["package.directory"])
        Dir.glob(File.join(".", "**", "*")).each do |entry|
          Archive::Tar::Minitar.pack_file(entry, output)
        end
      ensure
        FileUtils.cd(current_dir)
      end
    end
  end
end

#copy_include_filesObject

This method copies the include files (passed in via command line) to the temporary directory so they are included in a sub-folder within the actual box



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vagrant/action/general/package.rb', line 67

def copy_include_files
  files_to_copy.each do |from, to|
    @env.ui.info I18n.t("vagrant.actions.general.package.packaging", :file => from)
    FileUtils.mkdir_p(to.parent)

    # Copy direcotry contents recursively.
    if File.directory?(from)
      FileUtils.cp_r(Dir.glob(from), to.parent)
    else
      FileUtils.cp(from, to)
    end
  end
end

#files_to_copyObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant/action/general/package.rb', line 45

def files_to_copy
  package_dir = Pathname.new(@env["package.directory"]).join("include")

  files = @env["package.include"].inject({}) do |acc, file|
    source = Pathname.new(file)
    acc[file] = source.relative? ? package_dir.join(source) : package_dir.join(source.basename)
    acc
  end

  files[@env["package.vagrantfile"]] = package_dir.join("_Vagrantfile") if @env["package.vagrantfile"]
  files
end

#recover(env) ⇒ Object



40
41
42
43
# File 'lib/vagrant/action/general/package.rb', line 40

def recover(env)
  # Cleanup any packaged files if the packaging failed at some point.
  File.delete(tar_path) if File.exist?(tar_path)
end

#tar_pathObject

Path to the final box output file



103
104
105
# File 'lib/vagrant/action/general/package.rb', line 103

def tar_path
  File.join(FileUtils.pwd, @env["package.output"])
end

#verify_files_to_copyObject



58
59
60
61
62
# File 'lib/vagrant/action/general/package.rb', line 58

def verify_files_to_copy
  files_to_copy.each do |file, _|
    raise Errors::PackageIncludeMissing, :file => file if !File.exist?(file)
  end
end