Module: ZTK::SSH::File

Included in:
ZTK::SSH
Defined in:
lib/ztk/ssh/file.rb

Overview

SSH Remote File Functionality

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) file(options = {}, &block)

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.

Parameters:

  • options (Hash) (defaults to: {})

    The options hash.

Options Hash (options):

  • :target (String)

    The target file on the remote host.

  • :chown (String)

    A user:group representation of who to change ownership of the target file to (i.e. 'root:root').

  • :chmod (String)

    An octal file mode which to set the target file to (i.e. '0755').

Returns:

  • (Boolean)

    Returns true if successful.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ztk/ssh/file.rb', line 22

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