class PrepKit::Remote

Public Class Methods

new(host, user, options, &callback) click to toggle source
# File lib/prep_kit/remote.rb, line 3
def initialize(host, user, options, &callback)
  @ssh, @scp = create_connection(host, user, options, &callback)
end

Public Instance Methods

exec!(command) click to toggle source
# File lib/prep_kit/remote.rb, line 7
def exec!(command)
  @ssh.(command)
end
test?(name, option = nil) click to toggle source
# File lib/prep_kit/remote.rb, line 17
def test?(name, option = nil)
  exec!(%([ #{option} #{name} ]))
end

Private Instance Methods

create_connection(host, user, options, &callback) click to toggle source
# File lib/prep_kit/remote.rb, line 23
def create_connection(host, user, options, &callback)
  connection = lambda do |&block|
    Net::SSH.start(host, user, options) do |ssh|
      block.(ssh)

      ssh.loop
    end
  end

  [create_ssh(connection, &callback), create_scp(connection)]
end
create_scp(connection) click to toggle source
# File lib/prep_kit/remote.rb, line 65
def create_scp(connection)
  -> (&callback) { connection.() { |ssh| callback.(ssh.scp) } }
end
create_ssh(connection, &callback) click to toggle source
# File lib/prep_kit/remote.rb, line 35
def create_ssh(connection, &callback)
  lambda do |command|
    status = nil

    connection.() do |ssh|
      channel = ssh.open_channel do |chan|
        chan.exec command do |ch, success|
          raise RuntimeError command unless success

          ch.on_data do |_, data|
            callback.(:output, data)
          end

          ch.on_extended_data do |_, type, data|
            callback.(:error, data)
          end

          ch.on_request('exit-status') do |_, data|
            status = data.read_long
          end
        end

        channel.wait
      end
    end

    status
  end
end