Class: ThorSsh::RemoteServer

Inherits:
Object
  • Object
show all
Defined in:
lib/thor-ssh/remote_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/thor-ssh/remote_server.rb', line 7

def base
  @base
end

#connectionObject (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, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/thor-ssh/remote_server.rb', line 60

def run(command, options={})
  options[:with_codes] ||= false
  options[:log_stderr] = true unless options.has_key?(:log_stderr)
  # Runs the command with the correct sudo's to get it to the current
  # user.  You can also do as_user(nil) do ... to get to the login
  # user.
  
  # A few notes on running commands as a different user
  # 1) we use -i to get it as if you had logged in directly
  #    as the other user
  # 2) we use bash -c to run it all in bash so things like rediects
  #    and multiple commands work
  if !running_as_current_user? && base.run_as_user != nil
    # We need to change to a different user
    if base.run_as_user == 'root'
     # We need to go up to root
     # TODO: We don't need to run in bash if its not going to pipe
     command = "sudo -i bash -c #{command.inspect}"
   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} -i bash -c #{command.inspect}"
    end
  end
  stdout_data, stderr_data, exit_code, exit_signal = run_with_codes(command, options)
  
  # if stderr_data.strip != ''
  if exit_code != 0 && options[:log_stderr] == true
    base.say "#{exit_code}>> #{command}", :red
    base.say stderr_data, :red
  end
  
  unless options[:keep_colors]
    stdout_data = stdout_data.gsub(/\e\[(\d+)m/, '')
    stderr_data = stderr_data.gsub(/\e\[(\d+)m/, '')
  end
  
  # Remove \r's
  stdout_data = stdout_data.gsub(/\r\n/, "\n").gsub(/\n\r/, "\n").gsub(/\r/, "\n") if stdout_data
  stderr_data = stderr_data.gsub(/\r\n/, "\n").gsub(/\n\r/, "\n").gsub(/\r/, "\n") if stderr_data
  
  if options[:with_codes]
    return stdout_data, stderr_data, exit_code, exit_signal
  else
    return stdout_data
  end
end

#run_with_codes(command, options) ⇒ 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
53
54
# File 'lib/thor-ssh/remote_server.rb', line 14

def run_with_codes(command, options)
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  exit_signal = nil
  channel = connection.open_channel do |cha|
    # TODO: Lets do more research on request_pty
    # It fixes the bug with "stdin: is not a tty"
    channel.request_pty unless options[:no_pty]
    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
    # puts "Done Loop"
    # channel.close
  end
  connection.loop
  
  return stdout_data, stderr_data, exit_code, exit_signal
end

#running_as_current_user?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/thor-ssh/remote_server.rb', line 56

def running_as_current_user?
  base.run_as_user && connection.options[:user] == base.run_as_user
end