Class: Derelict::Instance

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Utils::Logger
Defined in:
lib/derelict/instance.rb,
lib/derelict/instance/invalid.rb,
lib/derelict/instance/not_found.rb,
lib/derelict/instance/non_directory.rb,
lib/derelict/instance/command_failed.rb,
lib/derelict/instance/missing_binary.rb

Overview

Represents a Vagrant instance installed via the Installer package

Defined Under Namespace

Classes: CommandFailed, Invalid, MissingBinary, NonDirectory, NotFound

Constant Summary collapse

DEFAULT_PATH =

The default path to the Vagrant installation folder

"/Applications/Vagrant"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Logger

#logger

Constructor Details

#initialize(path = DEFAULT_PATH) ⇒ Instance

Initialize an instance for a particular directory

* path: The path to the Vagrant installation folder (optional,
        defaults to DEFAULT_PATH)


25
26
27
28
# File 'lib/derelict/instance.rb', line 25

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



19
20
21
# File 'lib/derelict/instance.rb', line 19

def path
  @path
end

Instance Method Details

#boxesObject

Initializes a box manager for use with this instance



112
113
114
115
# File 'lib/derelict/instance.rb', line 112

def boxes
  logger.info "Creating box manager for #{description}"
  Derelict::Box::Manager.new(self)
end

#connect(path) ⇒ Object

Initializes a Connection for use in a particular directory

* instance: The Derelict::Instance to use to control Vagrant
* path:     The project path, which contains the Vagrantfile


100
101
102
103
# File 'lib/derelict/instance.rb', line 100

def connect(path)
  logger.info "Creating connection for '#{path}' by #{description}"
  Derelict::Connection.new(self, path).validate!
end

#descriptionObject

Provides a description of this Instance

Mainly used for log messages.



120
121
122
# File 'lib/derelict/instance.rb', line 120

def description
  "Derelict::Instance at '#{path}'"
end

#execute(subcommand, *arguments, &block) ⇒ Object

Executes a Vagrant subcommand using this instance

* subcommand: Vagrant subcommand to run (:up, :status, etc.)
* arguments:  Arguments to pass to the subcommand (optional)
* options:    If the last argument is a Hash, it will be used
              as a hash of options. A list of valid options is
              below. Any options provided that aren't in the
              list of valid options will get passed through to
              Derelict::Executer.execute.
              Valid option keys:
   * sudo:    Whether to run the command as root, or not
              (defaults to false)
* block:      Passed through to Derelict::Executer.execute


70
71
72
73
74
75
76
# File 'lib/derelict/instance.rb', line 70

def execute(subcommand, *arguments, &block)
  options = arguments.last.is_a?(Hash) ? arguments.pop : Hash.new
  command = command(subcommand, *arguments)
  command = "sudo -- #{command}" if options.delete(:sudo)
  logger.debug "Executing #{command} using #{description}"
  Executer.execute command, options, &block
end

#execute!(subcommand, *arguments, &block) ⇒ Object

Executes a Vagrant subcommand, raising an exception on failure

* subcommand: Vagrant subcommand to run (:up, :status, etc.)
* arguments:  Arguments to pass to the subcommand (optional)
* block:      Passed through to Derelict::Executer.execute

Raises Derelict::Instance::CommandFailed if the command fails.



85
86
87
88
89
90
91
92
93
94
# File 'lib/derelict/instance.rb', line 85

def execute!(subcommand, *arguments, &block)
  execute(subcommand, *arguments, &block).tap do |result|
    unless result.success?
      command = command(subcommand, *arguments)
      exception = CommandFailed.new command, result
      logger.warn "Command #{command} failed: #{exception.message}"
      raise exception
    end
  end
end

#pluginsObject

Initializes a plugin manager for use with this instance



106
107
108
109
# File 'lib/derelict/instance.rb', line 106

def plugins
  logger.info "Creating plugin manager for #{description}"
  Derelict::Plugin::Manager.new(self)
end

#validate!Object

Validates the data used for this instance

Raises exceptions on failure:

* +Derelict::Instance::NotFound+ if the instance is not found
* +Derelict::Instance::NonDirectory+ if the path is a file,
  instead of a directory as expected
* +Derelict::Instance::MissingBinary+ if the "vagrant" binary
  isn't in the expected location or is not executable


39
40
41
42
43
44
45
46
47
# File 'lib/derelict/instance.rb', line 39

def validate!
  logger.debug "Starting validation for #{description}"
  raise NotFound.new path unless File.exists? path
  raise NonDirectory.new path unless File.directory? path
  raise MissingBinary.new vagrant unless File.exists? vagrant
  raise MissingBinary.new vagrant unless File.executable? vagrant
  logger.info "Successfully validated #{description}"
  self
end

#versionObject

Determines the version of this Vagrant instance



50
51
52
53
54
# File 'lib/derelict/instance.rb', line 50

def version
  logger.info "Determining Vagrant version for #{description}"
  output = execute!("--version").stdout
  Derelict::Parser::Version.new(output).version
end