Class: Tenderloin::Box

Inherits:
Actions::Runner show all
Defined in:
lib/tenderloin/box.rb

Overview

Represents a “box,” which is simply a packaged tenderloin environment. Boxes are simply ‘tar` files which contain an exported VirtualBox virtual machine, at the least. They are created with `tenderloin 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 actions.

What can the Box class do?

  • Find boxes

  • Add existing boxes (from some URI)

  • Delete existing boxes

# Finding Boxes

Using the Box.find method, you can search for existing boxes. This method will return ‘nil` if none is found or an instance of Box otherwise.

box = Tenderloin::Box.find("base")
if box.nil?
  puts "Box not found!"
else
  puts "Box exists at #{box.directory}"
end

# Adding a Box

Boxes can be added from any URI. Some schemas aren’t supported; if this is the case, the error will output to the logger.

Tenderloin::Box.add("foo", "http://myfiles.com/foo.box")

# Destroying a box

Boxes can be deleted as well. This method is final and there is no way to undo this action once it is completed.

box = Tenderloin::Box.find("foo")
box.destroy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Actions::Runner

#action_klasses, #actions, #add_action, execute!, #execute!, #find_action, #invoke_around_callback, #invoke_callback

Methods included from Util

#error_and_exit, included, #logger, #wrap_output

Constructor Details

#initialize(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.



112
113
114
# File 'lib/tenderloin/box.rb', line 112

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

Instance Attribute Details

#nameObject

The name of the box.



45
46
47
# File 'lib/tenderloin/box.rb', line 45

def name
  @name
end

#temp_pathObject

The temporary path to the downloaded or copied box. This should only be used internally.



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

def temp_path
  @temp_path
end

#uriObject

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



48
49
50
# File 'lib/tenderloin/box.rb', line 48

def uri
  @uri
end

Class Method Details

.add(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 Tenderloin::Box instance and calling #add.

Parameters:

  • name (String)

    The name of the box

  • uri (String)

    URI to the box file



88
89
90
91
92
93
# File 'lib/tenderloin/box.rb', line 88

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

.allArray<String>

Returns an array of all created boxes, as strings.

Returns:

  • (Array<String>)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tenderloin/box.rb', line 58

def all
  results = []

  Dir.open(Env.boxes_path) do |dir|
    dir.each do |d|
      next if d == "." || d == ".." || !File.directory?(File.join(Env.boxes_path, d))
      results << d.to_s
    end
  end

  results
end

.directory(name) ⇒ String

Returns the directory to a box of the given name. The name given as a parameter is not checked for existence; this method simply returns the directory which would be used if the box did exist.

Parameters:

  • name (String)

    Name of the box whose directory you’re interested in.

Returns:

  • (String)

    Full path to the box directory.



101
102
103
# File 'lib/tenderloin/box.rb', line 101

def directory(name)
  File.join(Env.boxes_path, name)
end

.find(name) ⇒ Box

Finds a box with the given name. This method searches for a box with the given name, returning ‘nil` if none is found or returning a Tenderloin::Box instance otherwise.

Parameters:

  • name (String)

    The name of the box

Returns:



77
78
79
80
# File 'lib/tenderloin/box.rb', line 77

def find(name)
  return nil unless File.directory?(directory(name))
  new(name)
end

Instance Method Details

#addObject

Begins the process of adding a box to the tenderloin installation. This method requires that ‘name` and `uri` be set. The logic of this method is kicked out to the add box action.



126
127
128
# File 'lib/tenderloin/box.rb', line 126

def add
  execute!(Actions::Box::Add)
end

#destroyObject

Beings the process of destroying this box.



131
132
133
# File 'lib/tenderloin/box.rb', line 131

def destroy
  execute!(Actions::Box::Destroy)
end

#directoryString

Returns the directory to the location of this boxes content in the local filesystem.

Returns:

  • (String)


139
140
141
# File 'lib/tenderloin/box.rb', line 139

def directory
  self.class.directory(self.name)
end

#vmx_fileString

Returns path to the vmx file of the box. This contains the config

Returns:

  • (String)


119
120
121
# File 'lib/tenderloin/box.rb', line 119

def vmx_file
  File.join(directory, Tenderloin.config.vm.box_vmx)
end