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

#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


90
91
92
93
# File 'lib/derelict/instance.rb', line 90

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.



98
99
100
# File 'lib/derelict/instance.rb', line 98

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)
* block:      Passed through to Shell.execute (shell-executer)


62
63
64
65
66
# File 'lib/derelict/instance.rb', line 62

def execute(subcommand, *arguments, &block)
  command = command(subcommand, *arguments)
  logger.debug "Executing #{command} using #{description}"
  Shell.execute command, &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 Shell.execute (shell-executer)

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/derelict/instance.rb', line 75

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