Class: SSHKit::Backend::SudoNetssh

Inherits:
Netssh
  • Object
show all
Defined in:
lib/sshkit/sudo/sudo_netssh.rb

Overview

Values that are static, like the filtered logging patterns, or the name of the env variables, can be implemented, if needed, as Configuration attributes.

Constant Summary collapse

PASSWORD_PROMPT_REGEX =
/\[sudo\] password for \S+\:/
SKIP_STDOUT_LOGGING_PATTERNS =
[
  /^\r\n$/,
  PASSWORD_PROMPT_REGEX, # Skipping this removes context from the wrong password prompt, but it's
                         # still understandable.
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ SudoNetssh

Returns a new instance of SudoNetssh.



38
39
40
41
42
# File 'lib/sshkit/sudo/sudo_netssh.rb', line 38

def initialize(*args, &block)
  super

  @interaction_handler = SSHKit::Sudo::PasswordSendingInteractionHandler.new(self.class.config.servers_password + "\n")
end

Class Method Details

.configObject

It’s not possible to send a custom configuration class, so we need to create a custom one. The :owner could be an instance variable, but it’s a bit ugly to use a different setting strategy.



31
32
33
34
35
# File 'lib/sshkit/sudo/sudo_netssh.rb', line 31

def config
  @config ||= Class.new(Netssh::Configuration) do
    attr_accessor :owner, :servers_password, :commands_log
  end.new
end

.poolObject

pool is a class-level instance variable, so we can’t use the superclass’ attr_accessor. The attribute is only read though, so we don’t need to handle assignments.



23
24
25
# File 'lib/sshkit/sudo/sudo_netssh.rb', line 23

def pool
  self.superclass.pool
end

Instance Method Details

#capture(*args) ⇒ Object



44
45
46
47
48
49
# File 'lib/sshkit/sudo/sudo_netssh.rb', line 44

def capture(*args)
  # To ensure that we clean out the sudo part when the results are returned,
  # otherwise the commands will be corrupt.
  #
  super.gsub(PASSWORD_PROMPT_REGEX, '')
end

#upload!(local, remote, options = {}) ⇒ Object

Required because the uploaded file is owned by the SSH user, not the owner.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sshkit/sudo/sudo_netssh.rb', line 53

def upload!(local, remote, options = {})
  super

  # We can't check the user inside the :as block, because @user is root.
  #
  target_user = @user || self.class.config.owner

  as :root do
    execute :chown, target_user, remote
  end
end