Class: Makitzo::SSH::Context
- Inherits:
-
Object
- Object
- Makitzo::SSH::Context
- Includes:
- Commands::Makitzo, Migrations::Commands, Makitzo::SSH::Commands::Apple, Makitzo::SSH::Commands::FileSystem, Makitzo::SSH::Commands::FileTransfer, Makitzo::SSH::Commands::HTTP, Makitzo::SSH::Commands::Ruby, Makitzo::SSH::Commands::Unix
- Defined in:
- lib/makitzo/ssh/context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#connection_error ⇒ Object
Returns the value of attribute connection_error.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
Class Method Summary collapse
Instance Method Summary collapse
-
#exec(command, options = {}) ⇒ Object
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).
- #exec!(command, options = {}) ⇒ Object
-
#initialize(host, connection) ⇒ Context
constructor
A new instance of Context.
- #logger ⇒ Object
- #quote(arg) ⇒ Object
- #sudo(options = {}) ⇒ Object
-
#x(arg) ⇒ Object
escape an argument for use in shell stackoverflow.com/questions/1306680/shellwords-shellescape-implementation-for-ruby-1-8.
Methods included from Migrations::Commands
Methods included from Makitzo::SSH::Commands::Unix
Methods included from Makitzo::SSH::Commands::Ruby
Methods included from Makitzo::SSH::Commands::HTTP
#download, #download!, #download_with_curl, #download_with_wget
Methods included from Makitzo::SSH::Commands::FileTransfer
Methods included from Makitzo::SSH::Commands::FileSystem
#cd, #dir_exists?, #find_or_create_dir!, #mv, #require_dir!, #rm_rf!, #which?
Methods included from Makitzo::SSH::Commands::Apple
#daily_shutdown, #install_app, #install_pkg, #mount_dmg, #reboot, #serial_number, #shutdown, #shutdown_at, #unmount_dmg, #use_network_time_server
Constructor Details
#initialize(host, connection) ⇒ Context
Returns a new instance of Context.
11 12 13 |
# File 'lib/makitzo/ssh/context.rb', line 11 def initialize(host, connection) @host, @connection = host, connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/makitzo/ssh/context.rb', line 8 def connection @connection end |
#connection_error ⇒ Object
Returns the value of attribute connection_error.
9 10 11 |
# File 'lib/makitzo/ssh/context.rb', line 9 def connection_error @connection_error end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
7 8 9 |
# File 'lib/makitzo/ssh/context.rb', line 7 def host @host end |
Class Method Details
.protected_context_methods ⇒ Object
3 4 5 |
# 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 |
Instance Method Details
#exec(command, options = {}) ⇒ Object
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)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/makitzo/ssh/context.rb', line 36 def exec(command, = {}) 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()) end |
#exec!(command, options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/makitzo/ssh/context.rb', line 64 def exec!(command, = {}) res = exec(command, ) raise CommandFailed unless res.success? end |
#logger ⇒ Object
15 16 17 |
# File 'lib/makitzo/ssh/context.rb', line 15 def logger @logger ||= (connection[:logger] || Logging::Blackhole.new) end |
#quote(arg) ⇒ Object
29 30 31 |
# File 'lib/makitzo/ssh/context.rb', line 29 def quote(arg) "#{x(arg)}" end |
#sudo(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/makitzo/ssh/context.rb', line 69 def sudo( = {}) raise "can't nest calls to sudo with different options" if (@sudo && (@sudo != )) begin @sudo = 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) ⇒ Object
escape an argument for use in shell stackoverflow.com/questions/1306680/shellwords-shellescape-implementation-for-ruby-1-8
21 22 23 24 25 26 27 |
# 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 |