Class: Vagrant::Box

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

Overview

Represents a "box," which is simply a packaged vagrant environment. Boxes are simply tar files which contain an exported VirtualBox virtual machine, at the least. They are created with vagrant package and may contain additional files if specified by the creator. This class serves to help manage these boxes, although most of the logic is kicked out to middlewares.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil, name = nil) ⇒ Box

Creates a new box instance. Given an optional name parameter, newly created instance will have that name, otherwise it defaults to nil.

Note: This method does not actually create the box, but merely returns a new, abstract representation of it. To add a box, see #add.



40
41
42
43
# File 'lib/vagrant/box.rb', line 40

def initialize(env=nil, name=nil)
  @name = name
  @env = env
end

Instance Attribute Details

#envObject (readonly)

The environment which this box belongs to. Although this could actually be many environments, this points to the environment of a specific instance.



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

def env
  @env
end

#nameObject

The name of the box.



10
11
12
# File 'lib/vagrant/box.rb', line 10

def name
  @name
end

#uriObject

The URI for a new box. This is not available for existing boxes.



13
14
15
# File 'lib/vagrant/box.rb', line 13

def uri
  @uri
end

Class Method Details

.add(env, name, uri) ⇒ Object

Adds a new box with given name from the given URI. This method begins the process of adding a box from a given URI by setting up the Vagrant::Box instance and calling #add.

Parameters:

  • name (String)

    The name of the box

  • uri (String)

    URI to the box file



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

def add(env, name, uri)
  box = new(env, name)
  box.uri = uri
  box.add
end

Instance Method Details

#<=>(other) ⇒ Object

Implemented for comparison with other boxes. Comparison is implemented by simply comparing name.



85
86
87
88
# File 'lib/vagrant/box.rb', line 85

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

#addObject

Begins the process of adding a box to the vagrant installation. This method requires that name and uri be set. The logic of this method is kicked out to the box_add registered middleware.



59
60
61
62
# File 'lib/vagrant/box.rb', line 59

def add
  raise Errors::BoxAlreadyExists, :name => name if File.directory?(directory)
  env.actions.run(:box_add, { "box" => self, "validate" => false })
end

#destroyObject

Begins the process of destroying this box. This cannot be undone!



65
66
67
# File 'lib/vagrant/box.rb', line 65

def destroy
  env.actions.run(:box_remove, { "box" => self, "validate" => false })
end

#directoryString

Returns the directory to the location of this boxes content in the local filesystem. Note that if the box isn't imported yet, then the path may not yet exist, but still represents where the box will be imported to.

Returns:

  • (String)


79
80
81
# File 'lib/vagrant/box.rb', line 79

def directory
  env.boxes_path.join(name)
end

#ovf_fileString

Returns path to the OVF file of the box. The OVF file is an open virtual machine file which contains specifications of the exported virtual machine this box contains.

This will only be valid once the box is imported.

Returns:

  • (String)


52
53
54
# File 'lib/vagrant/box.rb', line 52

def ovf_file
  directory.join(env.config.vm.box_ovf)
end

#repackage(options = nil) ⇒ Object

Begins sequence to repackage this box.



70
71
72
# File 'lib/vagrant/box.rb', line 70

def repackage(options=nil)
  env.actions.run(:box_repackage, { "box" => self, "validate" => false }.merge(options || {}))
end