class SSHKit::Backend::Abstract

Attributes

host[R]

Public Class Methods

config() click to toggle source
# File lib/sshkit/backends/abstract.rb, line 119
def config
  @config ||= OpenStruct.new
end
configure() { |config| ... } click to toggle source
# File lib/sshkit/backends/abstract.rb, line 123
def configure
  yield config
end
new(host, &block) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 34
def initialize(host, &block)
  raise "Must pass a Host object" unless host.is_a? Host
  @host  = host
  @block = block

  @pwd   = nil
  @env   = nil
  @user  = nil
  @group = nil
end

Public Instance Methods

as(who) { || ... } click to toggle source
# File lib/sshkit/backends/abstract.rb, line 98
      def as(who, &_block)
        if who.is_a? Hash
          @user  = who[:user]  || who["user"]
          @group = who[:group] || who["group"]
        else
          @user  = who
          @group = nil
        end
        execute "          if ! sudo -u #{@user} whoami > /dev/null
            then echo "You cannot switch to user '#{@user}' using sudo, please check the sudoers file" 1>&2
            false
          fi
", verbosity: Logger::DEBUG
        yield
      ensure
        remove_instance_variable(:@user)
        remove_instance_variable(:@group)
      end
background(*args) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 64
def background(*args)
  SSHKit.config.deprecation_logger.log(
    'The background method is deprecated. Blame badly behaved pseudo-daemons!'
  )
  options = args.extract_options!.merge(run_in_background: true)
  create_command_and_execute(args, options).success?
end
capture(*args) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 58
def capture(*args)
  options = { verbosity: Logger::DEBUG, strip: true }.merge(args.extract_options!)
  result = create_command_and_execute(args, options).full_stdout
  options[:strip] ? result.strip : result
end
download!(_remote, _local=nil, _options = {}) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 130
def download!(_remote, _local=nil, _options = {}) raise MethodUnavailableError end
execute(*args) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 72
def execute(*args)
  options = args.extract_options!
  create_command_and_execute(args, options).success?
end
make(commands=[]) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 45
def make(commands=[])
  execute :make, commands
end
rake(commands=[]) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 49
def rake(commands=[])
  execute :rake, commands
end
run() click to toggle source
# File lib/sshkit/backends/abstract.rb, line 27
def run
  Thread.current["sshkit_backend"] = self
  instance_exec(@host, &@block)
ensure
  Thread.current["sshkit_backend"] = nil
end
test(*args) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 53
def test(*args)
  options = args.extract_options!.merge(raise_on_non_zero_exit: false, verbosity: Logger::DEBUG)
  create_command_and_execute(args, options).success?
end
upload!(_local, _remote, _options = {}) click to toggle source

Backends which extend the Abstract backend should implement the following methods:

# File lib/sshkit/backends/abstract.rb, line 129
def upload!(_local, _remote, _options = {}) raise MethodUnavailableError end
with(environment) { || ... } click to toggle source
# File lib/sshkit/backends/abstract.rb, line 90
def with(environment, &_block)
  env_old = (@env ||= {})
  @env = env_old.merge environment
  yield
ensure
  @env = env_old
end
within(directory) { || ... } click to toggle source
# File lib/sshkit/backends/abstract.rb, line 77
      def within(directory, &_block)
        (@pwd ||= []).push directory.to_s
        execute "          if test ! -d #{File.join(@pwd)}
            then echo "Directory does not exist '#{File.join(@pwd)}'" 1>&2
            false
          fi
", verbosity: Logger::DEBUG
          yield
      ensure
        @pwd.pop
      end

Private Instance Methods

command(args, options) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 152
def command(args, options)
  SSHKit::Command.new(*[*args, options.merge({in: pwd_path, env: @env, host: @host, user: @user, group: @group})])
end
create_command_and_execute(args, options) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 140
def create_command_and_execute(args, options)
  command(args, options).tap { |cmd| execute_command(cmd) }
end
execute_command(_cmd) click to toggle source
# File lib/sshkit/backends/abstract.rb, line 131
def execute_command(_cmd) raise MethodUnavailableError end
output() click to toggle source
# File lib/sshkit/backends/abstract.rb, line 136
def output
  SSHKit.config.output
end
pwd_path() click to toggle source
# File lib/sshkit/backends/abstract.rb, line 144
def pwd_path
  if @pwd.nil? || @pwd.empty?
    nil
  else
    File.join(@pwd)
  end
end