Class: Trooper::Host
- Inherits:
-
Object
- Object
- Trooper::Host
- Defined in:
- lib/trooper/host.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#close ⇒ Object
Public: Close net/ssh connection.
-
#execute(command, options = {}) ⇒ Object
Public: Execute a set of commands via net/ssh.
-
#initialize(host, user, options = { :forward_agent => true, :paranoid => Net::SSH::Verifiers::Null.new }) ⇒ Host
constructor
Public: Initialize a new Host object.
-
#to_s ⇒ Object
Public: Friendly version of the object.
Constructor Details
#initialize(host, user, options = { :forward_agent => true, :paranoid => Net::SSH::Verifiers::Null.new }) ⇒ Host
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.
23 24 25 26 27 |
# File 'lib/trooper/host.rb', line 23 def initialize(host, user, = { :forward_agent => true, :paranoid => Net::SSH::Verifiers::Null.new }) @host = host @user = user @connection = Net::SSH.start(host, user, ) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/trooper/host.rb', line 9 def connection @connection end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/trooper/host.rb', line 9 def host @host end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
9 10 11 |
# File 'lib/trooper/host.rb', line 9 def user @user end |
Instance Method Details
#close ⇒ Object
Public: Close net/ssh connection.
Examples
@host.close # => true
Returns boolean.
90 91 92 |
# File 'lib/trooper/host.rb', line 90 def close connection.close end |
#execute(command, options = {}) ⇒ Object
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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/trooper/host.rb', line 53 def execute(command, = {}) = {} if == nil commands = parse command Trooper.logger.debug commands if ![: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.}\n[ERROR INFO] #{commands}" end end end end |
#to_s ⇒ Object
Public: Friendly version of the object.
Examples
@host.to_s # => '[email protected]'
Returns user@host as a String.
36 37 38 |
# File 'lib/trooper/host.rb', line 36 def to_s "#{user}@#{host}" end |