class Makitzo::SSH::Context
Attributes
connection[R]
connection_error[RW]
host[R]
Public Class Methods
new(host, connection)
click to toggle source
# File lib/makitzo/ssh/context.rb, line 11 def initialize(host, connection) @host, @connection = host, connection end
protected_context_methods()
click to toggle source
# File lib/makitzo/ssh/context.rb, line 3 def self.protected_context_methods %w(x exec sudo host connection logger) + Migrations::Migration.protected_context_methods end
Public Instance Methods
exec(command, options = {})
click to toggle source
wrapper to connection.exec2! generates necessary sudo command if we're in a sudo block returns a status object with various useful data about command (output, status code)
# File lib/makitzo/ssh/context.rb, line 36 def exec(command, options = {}) log_command = true if @sudo password = @sudo[:password] || host.read_merged(:sudo_password) user = @sudo[:user] group = @sudo[:group] sudo = "sudo" # TODO: if user/group is spec'd as int (ID), prefix it with # sudo << " -u #{x(user)}" if user sudo << " -g #{x(group)}" if group log_sudo = sudo if password sudo = "echo #{x(password)} | #{sudo} -S --" log_sudo = "echo [PASSWORD REMOVED] | #{log_sudo} -S --" end log_command = "#{log_sudo} #{command}" command = "#{sudo} #{command}" end connection.exec2!(command, {:log => log_command}.update(options)) end
exec!(command, options = {})
click to toggle source
# File lib/makitzo/ssh/context.rb, line 64 def exec!(command, options = {}) res = exec(command, options) raise CommandFailed unless res.success? end
logger()
click to toggle source
# File lib/makitzo/ssh/context.rb, line 15 def logger @logger ||= (connection[:logger] || Logging::Blackhole.new) end
quote(arg)
click to toggle source
# File lib/makitzo/ssh/context.rb, line 29 def quote(arg) "#{x(arg)}" end
sudo(options = {}) { || ... }
click to toggle source
# File lib/makitzo/ssh/context.rb, line 69 def sudo(options = {}) raise "can't nest calls to sudo with different options" if (@sudo && (@sudo != options)) begin @sudo = options yield if block_given? # reset sudo timestamp so password will be required next time connection.exec2!("sudo -k") ensure @sudo = nil end end
x(arg)
click to toggle source
escape an argument for use in shell stackoverflow.com/questions/1306680/shellwords-shellescape-implementation-for-ruby-1-8
# File lib/makitzo/ssh/context.rb, line 21 def x(arg) arg = arg.strip return "''" if arg.empty? arg.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1") arg.gsub!(/\n/, "'\n'") arg end