Sha256: 53a5433d1d8a9531dcec96158526ca60b0f7978693cd02a1f8487ca7301cc6ee

Contents?: true

Size: 1.88 KB

Versions: 73

Compression:

Stored size: 1.88 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!"

        file_tempfile = Tempfile.new("file")
        remote_tempfile = ::File.join("", "tmp", ::File.basename(file_tempfile.path.dup))
        file_tempfile.close!

        local_tempfile  = Tempfile.new("tempfile-local")

        !block.nil? and block.call(local_tempfile)
        local_tempfile.respond_to?(:flush) and local_tempfile.flush

        self.upload(local_tempfile.path, remote_tempfile)

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

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

        local_tempfile.close!

        true
      end

    end

  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
ztk-3.3.2 lib/ztk/ssh/file.rb
ztk-3.3.1 lib/ztk/ssh/file.rb
ztk-3.3.0 lib/ztk/ssh/file.rb
ztk-3.2.6 lib/ztk/ssh/file.rb
ztk-3.2.5 lib/ztk/ssh/file.rb
ztk-3.2.4 lib/ztk/ssh/file.rb
ztk-3.2.3 lib/ztk/ssh/file.rb
ztk-3.2.2 lib/ztk/ssh/file.rb
ztk-3.2.1 lib/ztk/ssh/file.rb
ztk-3.2.0 lib/ztk/ssh/file.rb
ztk-3.1.0 lib/ztk/ssh/file.rb
ztk-3.0.4 lib/ztk/ssh/file.rb
ztk-3.0.3 lib/ztk/ssh/file.rb
ztk-3.0.2 lib/ztk/ssh/file.rb
ztk-3.0.1 lib/ztk/ssh/file.rb
ztk-3.0.0 lib/ztk/ssh/file.rb
ztk-2.4.2 lib/ztk/ssh/file.rb
ztk-2.4.1 lib/ztk/ssh/file.rb
ztk-2.4.0 lib/ztk/ssh/file.rb
ztk-2.3.1 lib/ztk/ssh/file.rb