Sha256: a4609461372bdef7309e14c56eb4c2e73d0f7791e9e492205551ef66d0db1375

Contents?: true

Size: 1.92 KB

Versions: 16

Compression:

Stored size: 1.92 KB

Contents

module ZTK
  class SSH

    # SSH Upload Functionality
    module Upload

      # Uploads a local file to a remote host.
      #
      # @example Upload a file:
      #   $logger = ZTK::Logger.new(STDOUT)
      #   ssh = ZTK::SSH.new
      #   ssh.config do |config|
      #     config.user = ENV["USER"]
      #     config.host_name = "127.0.0.1"
      #   end
      #   local = File.expand_path(File.join(ENV['HOME'], ".ssh", "id_rsa.pub"))
      #   remote = File.expand_path(File.join(ZTK::Locator.root, "tmp", "id_rsa.pub"))
      #   ssh.upload(local, remote)
      #
      # @param [String] local The local file/path you wish to upload from.
      # @param [String] remote The remote file/path you with to upload to.
      # @return [Boolean] True if successful.
      def upload(local, remote, options={})
        options = OpenStruct.new(config.send(:table).merge(options))

        options.ui.logger.debug { "config=#{config.send(:table).inspect}" }
        options.ui.logger.debug { "options=#{options.send(:table).inspect}" }
        config.ui.logger.info { "upload(#{local.inspect}, #{remote.inspect})" }

        ZTK::RescueRetry.try(:tries => 3, :on => EOFError, :on_retry => method(:on_retry)) do
          sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
            case event
            when :open
              options.ui.logger.debug { "upload(#{args[0].local} -> #{args[0].remote})" }
            when :close
              options.ui.logger.debug { "close(#{args[0].remote})" }
            when :mkdir
              options.ui.logger.debug { "mkdir(#{args[0]})" }
            when :put
              options.ui.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
              options.on_progress.nil? or options.on_progress.call
            when :finish
              options.ui.logger.debug { "finish" }
            end
          end
        end

        true
      end

    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ztk-1.6.20 lib/ztk/ssh/upload.rb
ztk-1.6.19 lib/ztk/ssh/upload.rb
ztk-1.6.18 lib/ztk/ssh/upload.rb
ztk-1.6.17 lib/ztk/ssh/upload.rb
ztk-1.6.16 lib/ztk/ssh/upload.rb
ztk-1.6.15 lib/ztk/ssh/upload.rb
ztk-1.6.14 lib/ztk/ssh/upload.rb
ztk-1.6.12 lib/ztk/ssh/upload.rb
ztk-1.6.11 lib/ztk/ssh/upload.rb
ztk-1.6.10 lib/ztk/ssh/upload.rb
ztk-1.6.9 lib/ztk/ssh/upload.rb
ztk-1.6.8 lib/ztk/ssh/upload.rb
ztk-1.6.7 lib/ztk/ssh/upload.rb
ztk-1.6.6 lib/ztk/ssh/upload.rb
ztk-1.6.5 lib/ztk/ssh/upload.rb
ztk-1.6.4 lib/ztk/ssh/upload.rb