Sha256: 62aa544fadf319c0f099cd662b8c4f441db5c004b830671c6a8222e2eb80ca70

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

module ZTK
  class SSH

    # SSH Remote File Functionality
    module File
      require 'tempfile'

      # Opens a temporary local file, yielding this to the supplied block.  Once
      # the block returns the temporary file is uploaded to the remote host and
      # installed as the supplied target.
      #
      # If the optional 'chown' or 'chmod' options are supplied then their
      # respective actions will be taken on the target file on the remote host.
      #
      # @param [Hash] options The options hash.
      # @option options [String] :target The target file on the remote host.
      # @option options [String] :chown A user:group representation of who
      #   to change ownership of the target file to (i.e. 'root:root').
      # @option options [String] :chmod An octal file mode which to set the
      #   target file to (i.e. '0755').
      # @return [Boolean] Returns true if successful.
      def file(options={}, &block)
        target = options[:target]
        chown  = options[:chown]
        chmod  = options[:chmod]

        target.nil? and raise SSHError, "You must supply a target file!"
        !block_given? and raise SSHError, "You must supply a block!"

        tempfile = Tempfile.new("tempfile")

        local_tempfile = tempfile.path
        remote_tempfile = ::File.join("/tmp", ::File.basename(local_tempfile))

        ::File.open(local_tempfile, 'w') do |file|
          yield(file)
        end

        ZTK::RescueRetry.try(:tries => 2, :on_retry => method(:on_retry)) do
          upload(local_tempfile, remote_tempfile)

          exec(%(sudo mv -v #{remote_tempfile} #{target}), :silence => true)

          exec(%(sudo chown -v #{chown} #{target}), :silence => true) if !chown.nil?
          exec(%(sudo chmod -v #{chmod} #{target}), :silence => true) if !chmod.nil?
        end

        true
      end

    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ztk-1.6.0 lib/ztk/ssh/file.rb
ztk-1.5.5 lib/ztk/ssh/file.rb
ztk-1.5.4 lib/ztk/ssh/file.rb
ztk-1.5.3 lib/ztk/ssh/file.rb
ztk-1.5.2 lib/ztk/ssh/file.rb
ztk-1.5.1 lib/ztk/ssh/file.rb
ztk-1.5.0 lib/ztk/ssh/file.rb
ztk-1.4.22 lib/ztk/ssh/file.rb
ztk-1.4.21 lib/ztk/ssh/file.rb
ztk-1.4.20 lib/ztk/ssh/file.rb
ztk-1.4.19 lib/ztk/ssh/file.rb
ztk-1.4.18 lib/ztk/ssh/file.rb
ztk-1.4.17 lib/ztk/ssh/file.rb
ztk-1.4.16 lib/ztk/ssh/file.rb
ztk-1.4.15 lib/ztk/ssh/file.rb