class Trooper::Host

Attributes

connection[R]
host[R]
user[R]

Public Class Methods

new(host, user, options = { :forward_agent => true, :paranoid => Net::SSH::Verifiers::Null.new }) click to toggle source

Public: Initialize a new Host object.

host - The String of the host location user - The String of the user name. options - The Hash of options to pass into Net::SSH

see Net::SSH for details (default: { :forward_agent => true })

Examples

Host.new('my.example.com', 'admin', :forward_agent => false)

Returns a Host object.

# File lib/trooper/host.rb, line 23
def initialize(host, user, options = { :forward_agent => true, :paranoid => Net::SSH::Verifiers::Null.new })
  @host = host
  @user = user
  @connection = Net::SSH.start(host, user, options)
end

Public Instance Methods

close() click to toggle source

Public: Close net/ssh connection.

Examples

@host.close # => true

Returns boolean.

# File lib/trooper/host.rb, line 90
def close
  connection.close
end
execute(command, options = {}) click to toggle source

Public: Execute a set of commands via net/ssh.

command - A String or Array of command to run on a remote server options - The Hash options used to refine the selection (default: {}):

:local - Run the commands on the local machine (optional).

Examples

runner.execute(['cd to/path', 'touch file']) # => ['cd to/path && touch file', :stdout, '']
runner.execute('cat file') # => ['cat file', :stdout, 'file content']
runner.execute('cat file', :local => true) # => ['cat file', :stdout, 'file content']

Returns an array or raises an exception.

# File lib/trooper/host.rb, line 53
def execute(command, options = {})
  options = {} if options == nil
  
  commands = parse command
  Trooper.logger.debug commands
  
  if !options[:local]

    connection.exec! commands do |ch, stream, data|
      raise Trooper::StdError, "#{data}\n[ERROR INFO] #{commands}" if stream == :stderr
      ch.wait
      return [commands, stream, data]
    end
    
  else

    if commands != ''
      begin
        stdin, stdout, stderr = Open3.popen3(commands)
        raise Trooper::StdError, "#{stderr.read}\n[ERROR INFO] #{commands}" if stderr.read != ''

        return [commands, :stdout, stdout.read]
      rescue Exception => e
        raise Trooper::StdError, "#{e.message}\n[ERROR INFO] #{commands}"
      end
    end

  end
end
to_s() click to toggle source

Public: Friendly version of the object.

Examples

@host.to_s # => 'foo@bar.com'

Returns user@host as a String.

# File lib/trooper/host.rb, line 36
def to_s
  "#{user}@#{host}"
end

Private Instance Methods

parse(command) click to toggle source

parse command, expects a string or array

parse(['cd to/path', 'touch file']) # => 'cd to/path && touch file'
# File lib/trooper/host.rb, line 98
def parse(command)
  case command.class.name.downcase.to_sym #Array => :array
  when :array
    command.compact.join(' && ')
  when :string
    command.chomp
  else
    raise Trooper::MalformedCommandError, "Command Not a String or Array: #{command.inspect}"
  end
end