Class: Vagrant::Actions::VM::Package

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/actions/vm/package.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#runner

Instance Method Summary collapse

Methods inherited from Base

#cleanup, #follows, #precedes, #rescue

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

#initialize(vm, out_path = nil, include_files = nil, *args) ⇒ Package

Returns a new instance of Package.



9
10
11
12
13
14
# File 'lib/vagrant/actions/vm/package.rb', line 9

def initialize(vm, out_path = nil, include_files = nil, *args)
  super
  @out_path = out_path || "package"
  @include_files = include_files || []
  @temp_path = nil
end

Instance Attribute Details

#export_actionObject (readonly)

Returns the value of attribute export_action.



7
8
9
# File 'lib/vagrant/actions/vm/package.rb', line 7

def export_action
  @export_action
end

#include_filesObject

Returns the value of attribute include_files.



6
7
8
# File 'lib/vagrant/actions/vm/package.rb', line 6

def include_files
  @include_files
end

#out_pathObject

Returns the value of attribute out_path.



5
6
7
# File 'lib/vagrant/actions/vm/package.rb', line 5

def out_path
  @out_path
end

Instance Method Details

#compressObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant/actions/vm/package.rb', line 39

def compress
  logger.info "Packaging VM into #{tar_path} ..."
  File.open(tar_path, File::CREAT | File::WRONLY, 0644) do |tar|
    Archive::Tar::Minitar::Output.open(tar) do |output|
      begin
        current_dir = FileUtils.pwd

        include_files.each do |f|
          logger.info "Packaging additional file: #{f}"
          Archive::Tar::Minitar.pack_file(f, output)
        end

        FileUtils.cd(temp_path)

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

#execute!Object



27
28
29
# File 'lib/vagrant/actions/vm/package.rb', line 27

def execute!
  compress
end

#prepareObject

Raises:



16
17
18
19
20
21
22
23
24
25
# File 'lib/vagrant/actions/vm/package.rb', line 16

def prepare
  # Verify the existance of all the additional files, if any
  @include_files.each do |file|
    raise ActionException.new(:package_include_file_doesnt_exist, :filename => file) unless File.exists?(file)
  end

  # Get the export action and store a reference to it
  @export_action = @runner.find_action(Export)
  raise ActionException.new(:packaged_requires_export) unless @export_action
end

#tar_pathObject



31
32
33
# File 'lib/vagrant/actions/vm/package.rb', line 31

def tar_path
  File.join(FileUtils.pwd, "#{out_path}#{@runner.env.config.package.extension}")
end

#temp_pathObject



35
36
37
# File 'lib/vagrant/actions/vm/package.rb', line 35

def temp_path
  export_action.temp_dir
end