Class: Makitzo::SSH::Context

Direct Known Subclasses

Migrations::Migration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Migrations::Commands

#upload_migration_file

Methods included from Makitzo::SSH::Commands::Unix

#killall

Methods included from Makitzo::SSH::Commands::Ruby

#require_ruby!, #ruby_version

Methods included from Makitzo::SSH::Commands::HTTP

#download, #download!, #download_with_curl, #download_with_wget

Methods included from Makitzo::SSH::Commands::FileTransfer

#scp_upload

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

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/makitzo/ssh/context.rb', line 8

def connection
  @connection
end

#connection_errorObject

Returns the value of attribute connection_error.



9
10
11
# File 'lib/makitzo/ssh/context.rb', line 9

def connection_error
  @connection_error
end

#hostObject (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_methodsObject



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, 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 = {}) ⇒ Object

Raises:



64
65
66
67
# File 'lib/makitzo/ssh/context.rb', line 64

def exec!(command, options = {})
  res = exec(command, options)
  raise CommandFailed unless res.success?
end

#loggerObject



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(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) ⇒ Object



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