Class: ThorSsh::RemoteServer
- Inherits:
-
Object
- Object
- ThorSsh::RemoteServer
- Defined in:
- lib/thor-ssh/remote_server.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
-
#initialize(base, connection) ⇒ RemoteServer
constructor
A new instance of RemoteServer.
- #run(command, with_codes = false) ⇒ Object
- #run_with_codes(command) ⇒ Object
- #running_as_current_user? ⇒ Boolean
Constructor Details
#initialize(base, connection) ⇒ RemoteServer
Returns a new instance of RemoteServer.
9 10 11 12 |
# File 'lib/thor-ssh/remote_server.rb', line 9 def initialize(base, connection) @base = base @connection = connection end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
7 8 9 |
# File 'lib/thor-ssh/remote_server.rb', line 7 def base @base end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/thor-ssh/remote_server.rb', line 6 def connection @connection end |
Instance Method Details
#run(command, with_codes = false) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/thor-ssh/remote_server.rb', line 58 def run(command, with_codes=false) if running_as_current_user? # We need to change to a different user if base.run_as_user == 'root' # We need to go up to root command = "sudo #{command}" else # We need to go up to root, then down to this user # This involves running sudo (to go up to root), then running # sudo again as the new user, then running the command command = "sudo sudo -u #{base.run_as_user} #{command}" end end results = run_with_codes(command) if with_codes return results else return results.first end end |
#run_with_codes(command) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/thor-ssh/remote_server.rb', line 14 def run_with_codes(command) stdout_data = "" stderr_data = "" exit_code = nil exit_signal = nil channel = connection.open_channel do |cha| cha.exec(command) do |ch, success| unless success abort "FAILED: couldn't execute command (connection.channel.exec)" end channel.on_data do |ch,data| stdout_data += data end channel.on_extended_data do |ch,type,data| stderr_data += data end channel.on_request("exit-status") do |ch,data| exit_code = data.read_long end channel.on_request("exit-signal") do |ch, data| exit_signal = data.read_long end # channel.on_close do |ch| # puts "Channel is Closing! #{connection.closed?}" # channel.close # end end # channel.wait # puts "Done Loop" # channel.close end connection.loop return stdout_data, stderr_data, exit_code, exit_signal end |
#running_as_current_user? ⇒ Boolean
54 55 56 |
# File 'lib/thor-ssh/remote_server.rb', line 54 def running_as_current_user? base.run_as_user && connection.[:user] != base.run_as_user end |