Sha256: a1d5626beacd0fda4351d6a4607ddd8325235b84d81f0a4c0dea5b36a1a62731

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 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)
          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.5 lib/oas/log_collector/source.rb