Class: Vagrant::Box

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/vagrant/box.rb

Overview

Represents a “box,” which is a package Vagrant environment that is used as a base image when creating a new guest machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, provider, directory) ⇒ Box

This is used to initialize a box.

Parameters:

  • name (String)

    Logical name of the box.

  • provider (Symbol)

    The provider that this box implements.

  • directory (Pathname)

    The directory where this box exists on disk.

Raises:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant/box.rb', line 42

def initialize(name, provider, directory)
  @name      = name
  @provider  = provider
  @directory = directory

   = directory.join("metadata.json")
  raise Errors::BoxMetadataFileNotFound, :name => @name if !.file?
  @metadata = JSON.parse(directory.join("metadata.json").read)

  @logger = Log4r::Logger.new("vagrant::box")
end

Instance Attribute Details

#directoryPathname (readonly)

This is the directory on disk where this box exists.

Returns:

  • (Pathname)


28
29
30
# File 'lib/vagrant/box.rb', line 28

def directory
  @directory
end

#metadataHash (readonly)

This is the metadata for the box. This is read from the “metadata.json” file that all boxes require.

Returns:

  • (Hash)


34
35
36
# File 'lib/vagrant/box.rb', line 34

def 
  @metadata
end

#nameString (readonly)

The box name. This is the logical name used when adding the box.

Returns:

  • (String)


18
19
20
# File 'lib/vagrant/box.rb', line 18

def name
  @name
end

#providerSymbol (readonly)

This is the provider that this box is built for.

Returns:

  • (Symbol)


23
24
25
# File 'lib/vagrant/box.rb', line 23

def provider
  @provider
end

Instance Method Details

#<=>(other) ⇒ Object

Implemented for comparison with other boxes. Comparison is implemented by comparing names and providers.



89
90
91
92
93
94
# File 'lib/vagrant/box.rb', line 89

def <=>(other)
  return super if !other.is_a?(self.class)

  # Comparison is done by composing the name and provider
  "#{@name}-#{@provider}" <=> "#{other.name}-#{other.provider}"
end

#destroy!Object

This deletes the box. This is NOT undoable.



55
56
57
58
59
60
61
62
63
64
# File 'lib/vagrant/box.rb', line 55

def destroy!
  # Delete the directory to delete the box.
  FileUtils.rm_r(@directory)

  # Just return true always
  true
rescue Errno::ENOENT
  # This means the directory didn't exist. Not a problem.
  return true
end

#repackage(path) ⇒ Boolean

This repackages this box and outputs it to the given path.

Parameters:

  • path (Pathname)

    The full path (filename included) of where to output this box.

Returns:

  • (Boolean)

    true if this succeeds.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/vagrant/box.rb', line 71

def repackage(path)
  @logger.debug("Repackaging box '#{@name}' to: #{path}")

  Dir.chdir(@directory) do
    # Find all the files in our current directory and tar it up!
    files = Dir.glob(File.join(".", "**", "*"))

    # Package!
    Util::Subprocess.execute("bsdtar", "-czf", path.to_s, *files)
  end

  @logger.info("Repackaged box '#{@name}' successfully: #{path}")

  true
end