class Derelict::Instance
Represents a Vagrant instance installed via the Installer package
Constants
- DEFAULT_PATH
The default path to the Vagrant installation folder
Attributes
Public Class Methods
Initialize an instance for a particular directory
* path: The path to the Vagrant installation folder (optional, defaults to DEFAULT_PATH)
# File lib/derelict/instance.rb, line 25 def initialize(path = DEFAULT_PATH) @path = path logger.debug "Successfully initialized #{description}" end
Public Instance Methods
Initializes a box manager for use with this instance
# File lib/derelict/instance.rb, line 112 def boxes logger.info "Creating box manager for #{description}" Derelict::Box::Manager.new(self) end
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
# 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
Provides a description of this Instance
Mainly used for log messages.
# File lib/derelict/instance.rb, line 120 def description "Derelict::Instance at '#{path}'" end
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
# 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
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.
# 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
Initializes a plugin manager for use with this instance
# File lib/derelict/instance.rb, line 106 def plugins logger.info "Creating plugin manager for #{description}" Derelict::Plugin::Manager.new(self) end
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
# 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
Determines the version of this Vagrant instance
# 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
Private Instance Methods
Constructs the command to execute a Vagrant subcommand
* subcommand: Vagrant subcommand to run (:up, :status, etc.) * arguments: Arguments to pass to the subcommand (optional)
# File lib/derelict/instance.rb, line 138 def command(subcommand, *arguments) args = [vagrant, subcommand.to_s, arguments].flatten args.map {|a| Shellwords.escape a }.join(' ').tap do |command| logger.debug "Generated command '#{command}' from " + "subcommand '#{subcommand.to_s}' with arguments " + arguments.inspect end end
Retrieves the path to the vagrant binary for this instance
# File lib/derelict/instance.rb, line 126 def vagrant File.join(@path, "bin", "vagrant").tap do |vagrant| logger.debug "Vagrant binary for #{description} is '#{vagrant}'" end end