Class: Derelict::Box::Manager

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Utils::Logger
Defined in:
lib/derelict/box/manager.rb

Overview

A class that handles managing boxes for a Vagrant instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Logger

#logger

Constructor Details

#initialize(instance) ⇒ Manager

Initializes a Manager for use with a particular instance

* instance: The Derelict::Instance which will have its
            boxes managed by this Manager


17
18
19
20
# File 'lib/derelict/box/manager.rb', line 17

def initialize(instance)
  @instance = instance
  logger.debug "Successfully initialized #{description}"
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



11
12
13
# File 'lib/derelict/box/manager.rb', line 11

def instance
  @instance
end

Instance Method Details

#add(box_name, source, options = {}) ⇒ Object

Adds a box from a file or URL

The provider will be automatically determined from the box file’s manifest.

* box_name: The name of the box to add (e.g. "precise64")
* source:   The URL or path to the box file
* options:  Hash of options. Valid keys:
   * log:   Whether to log the output (optional, defaults to
            false)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/derelict/box/manager.rb', line 50

def add(box_name, source, options = {})
  options = {:log => false}.merge(options)
  logger.info <<-END.gsub(/ {10}|\n\Z/, '')
    Adding box '#{box_name}' from '#{source}' using #{description}
  END

  command = [:box, "add", box_name, source]
  command << "--force" if options[:force]

  log_block = options[:log] ? shell_log_block : nil
  instance.execute!(*command, &log_block).tap do
    flush_cache # flush memoized method return values
  end
end

#descriptionObject

Provides a description of this Connection

Mainly used for log messages.



106
107
108
# File 'lib/derelict/box/manager.rb', line 106

def description
  "Derelict::Box::Manager for #{instance.description}"
end

#fetch(box_name, provider) ⇒ Object

Retrieves a box with a particular name

* box_name: Name of the box to look for (as a string)


94
95
96
97
98
99
100
101
# File 'lib/derelict/box/manager.rb', line 94

def fetch(box_name, provider)
  box = list.find do |box|
    box.name == box_name && box.provider == provider
  end

  raise Box::NotFound.new box_name, provider if box.nil?
  box
end

#listObject

Retrieves the Set of currently installed boxes



23
24
25
26
27
# File 'lib/derelict/box/manager.rb', line 23

def list
  logger.info "Retrieving Vagrant box list for #{description}"
  output = instance.execute!(:box, "list").stdout
  Derelict::Parser::BoxList.new(output).boxes
end

#present?(box_name, provider) ⇒ Boolean

Determines whether a particular box is installed

* box_name: Name of the box to look for (as a string)

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/derelict/box/manager.rb', line 33

def present?(box_name, provider)
  fetch(box_name, provider)
  true
rescue Box::NotFound
  false
end

#remove(box_name, options = {}) ⇒ Object

Removes an installed box for a particular provider

* box_name:    Name of the box to remove (e.g. "precise64")
* options:     Hash of options. Valid keys:
   * log:      Whether to log the output (optional, defaults
               to false)
   * provider: If specified, only the box for a particular
               provider is removed; otherwise (by default),
               the box is removed for all providers


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/derelict/box/manager.rb', line 74

def remove(box_name, options = {})
  options = {:log => false, :provider => nil}.merge(options)

  provider = options[:provider]
  command = [:box, "remove", box_name]
  command << provider unless provider.nil?

  logger.info <<-END.gsub(/ {10}|\n\Z/, '')
    Removing box '#{box_name}' for '#{provider}' using #{description}
  END

  log_block = options[:log] ? shell_log_block : nil
  instance.execute!(*command, &log_block).tap do
    flush_cache # flush memoized method return values
  end
end