Method: CloudFlock::Remote::SSH#as_root

Defined in:
lib/cloudflock/remote/ssh.rb

#as_root(command, timeout = 30, recoverable = false) ⇒ Object

Public: Wrap query, guaranteeing that the user performing the given command is root.

command - Command to be executed. timeout - Number of seconds to allow the command to run before

terminating the channel and returning any buffer returned
so far.  A value of 0 or nil will result in no timeout.
(default: 30)

recoverable - Whether a Timeout should be considered acceptable.

(default: false)

Returns a String. Raises Timeout::Error if timeout is reached.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cloudflock/remote/ssh.rb', line 146

def as_root(command, timeout = 30, recoverable = false)
  return query(command, timeout, recoverable) if root?

  priv    = 'su -'
  priv    = 'sudo ' + priv if options[:sudo]
  uid     = ['id;logout||exit']
  command = ["#{command};logout||exit"]

  passwordless = query(priv, timeout, true, uid)

  unless /uid=0/.match(passwordless)
    command.unshift(options[:root_password] + "\n")
  end

  buffer = query(priv, timeout, recoverable, command)
  cleanup = Regexp.new("^#{Regexp::escape(command.join)}\r\n")
  buffer.gsub(cleanup, '')
end