class SSHKit::Backend::Netssh

Attributes

pool[RW]

Public Class Methods

config() click to toggle source
# File lib/sshkit/backends/netssh.rb, line 94
def config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source
# File lib/sshkit/backends/netssh.rb, line 90
def configure
  yield config
end

Public Instance Methods

download!(remote, local=nil, options = {}) click to toggle source
# File lib/sshkit/backends/netssh.rb, line 72
def download!(remote, local=nil, options = {})
  summarizer = transfer_summarizer('Downloading', options)
  remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") || pwd_path.nil?
  with_ssh do |ssh|
    ssh.scp.download!(remote, local, options, &summarizer)
  end
end
upload!(local, remote, options = {}) click to toggle source
# File lib/sshkit/backends/netssh.rb, line 64
def upload!(local, remote, options = {})
  summarizer = transfer_summarizer('Uploading', options)
  remote = File.join(pwd_path, remote) unless remote.to_s.start_with?("/") || pwd_path.nil?
  with_ssh do |ssh|
    ssh.scp.upload!(local, remote, options, &summarizer)
  end
end

Private Instance Methods

execute_command(cmd) click to toggle source
# File lib/sshkit/backends/netssh.rb, line 126
def execute_command(cmd)
  output.log_command_start(cmd.with_redaction)
  cmd.started = true
  exit_status = nil
  with_ssh do |ssh|
    ssh.open_channel do |chan|
      chan.request_pty if Netssh.config.pty
      chan.exec cmd.to_command do |_ch, _success|
        chan.on_data do |ch, data|
          cmd.on_stdout(ch, data)
          output.log_command_data(cmd, :stdout, data)
        end
        chan.on_extended_data do |ch, _type, data|
          cmd.on_stderr(ch, data)
          output.log_command_data(cmd, :stderr, data)
        end
        chan.on_request("exit-status") do |_ch, data|
          exit_status = data.read_long
        end
        #chan.on_request("exit-signal") do |ch, data|
        #  # TODO: This gets called if the program is killed by a signal
        #  # might also be a worthwhile thing to report
        #  exit_signal = data.read_string.to_i
        #  warn ">>> " + exit_signal.inspect
        #  output.log_command_killed(cmd, exit_signal)
        #end
        chan.on_open_failed do |_ch|
          # TODO: What do do here?
          # I think we should raise something
        end
        chan.on_process do |_ch|
          # TODO: I don't know if this is useful
        end
        chan.on_eof do |_ch|
          # TODO: chan sends EOF before the exit status has been
          # writtend
        end
      end
      chan.wait
    end
    ssh.loop
  end
  # Set exit_status and log the result upon completion
  if exit_status
    cmd.exit_status = exit_status
    output.log_command_exit(cmd)
  end
end
transfer_summarizer(action, options = {}) click to toggle source
# File lib/sshkit/backends/netssh.rb, line 101
def transfer_summarizer(action, options = {})
  log_percent = options[:log_percent] || 10
  log_percent = 100 if log_percent <= 0
  last_name = nil
  last_percentage = nil
  proc do |_ch, name, transferred, total|
    percentage = (transferred.to_f * 100 / total.to_f)
    unless percentage.nan?
      message = "#{action} #{name} #{percentage.round(2)}%"
      percentage_r = (percentage / log_percent).truncate * log_percent
      if percentage_r > 0 && (last_name != name || last_percentage != percentage_r)
        verbosity = (options[:verbosity] || :INFO).downcase # TODO: ideally reuse command.rb logic
        public_send verbosity, message
        last_name = name
        last_percentage = percentage_r
      else
        debug message
      end
    else
      warn "Error calculating percentage #{transferred}/#{total}, " <<
        "is #{name} empty?"
    end
  end
end
with_ssh(&block) click to toggle source
# File lib/sshkit/backends/netssh.rb, line 175
def with_ssh(&block)
  host.ssh_options = self.class.config.ssh_options.merge(host.ssh_options || {})
  self.class.pool.with(
    Net::SSH.method(:start),
    String(host.hostname),
    host.username,
    host.netssh_options,
    &block
  )
end