Class: Vagrant::Box
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
-
#directory ⇒ Pathname
readonly
This is the directory on disk where this box exists.
-
#metadata ⇒ Hash
readonly
This is the metadata for the box.
-
#name ⇒ String
readonly
The box name.
-
#provider ⇒ Symbol
readonly
This is the provider that this box is built for.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Implemented for comparison with other boxes.
-
#destroy! ⇒ Object
This deletes the box.
-
#initialize(name, provider, directory) ⇒ Box
constructor
This is used to initialize a box.
-
#repackage(path) ⇒ Boolean
This repackages this box and outputs it to the given path.
Constructor Details
#initialize(name, provider, directory) ⇒ Box
This is used to initialize a box.
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
#directory ⇒ Pathname (readonly)
This is the directory on disk where this box exists.
28 29 30 |
# File 'lib/vagrant/box.rb', line 28 def directory @directory end |
#metadata ⇒ Hash (readonly)
This is the metadata for the box. This is read from the “metadata.json” file that all boxes require.
34 35 36 |
# File 'lib/vagrant/box.rb', line 34 def @metadata end |
#name ⇒ String (readonly)
The box name. This is the logical name used when adding the box.
18 19 20 |
# File 'lib/vagrant/box.rb', line 18 def name @name end |
#provider ⇒ Symbol (readonly)
This is the provider that this box is built for.
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.
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 |