Sha256: 99b1f99381c946880658a3a0b5b9f23ff049eb4abd734132562507c5b62b1204

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'net/ssh'
require 'net/scp'
require 'fileutils'

module OAS
  module LogCollector
    class Source
      class Error < StandardError; end

      attr_reader :host, :user, :port

      def initialize(host, user, port = 22)
        @host, @user, @port = host, user, port
      end

      def find(path, options = {})
        out = ''
        ssh = Net::SSH.start(host, user, :port => port)
        ssh.exec!(build_find(path, options)) do |channel, stream, data|
          raise Error.new(data) if stream == :stderr
          out << data
        end
        ssh.close
        out.split("\n")
      end

      def download(files)
        Net::SCP.start(host, user, :port => port) do |scp|
          files.each do |remote_path, dest_path|
            FileUtils.mkdir_p(File.dirname(dest_path))
            scp.download!(remote_path, dest_path, :preserve => true)
          end
        end
      end

      private
      def build_find(path, options)
        str = "find #{path}"
        options.each do |k, v|
          str += " -#{k} '#{v}'"
        end
        str
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
oas-log-collector-0.1.6 lib/oas/log_collector/source.rb