Class: ThorSsh::RemoteFile
- Inherits:
-
Object
- Object
- ThorSsh::RemoteFile
- Defined in:
- lib/thor-ssh/remote_file.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #binread(path) ⇒ Object
-
#binwrite(path, data) ⇒ Object
TODO: we should just move this to a more standard thing.
- #chmod(mode, file_name) ⇒ Object
-
#close_sftp! ⇒ Object
This is a workaround for bug: github.com/net-ssh/net-sftp/issues/13.
- #exists?(path) ⇒ Boolean
-
#identical?(file1, file2) ⇒ Boolean
See if these paths point to the same inode.
-
#initialize(base, connection) ⇒ RemoteFile
constructor
A new instance of RemoteFile.
- #inode(file_name) ⇒ Object
- #link(old_name, new_name) ⇒ Object
-
#mkdir_p(path) ⇒ Object
Creates the directory at the path on the remote server.
-
#rm_rf(path) ⇒ Object
(also: #unlink)
Remote the file/folder on the remote server.
- #run(command) ⇒ Object
- #symlink(old_name, new_name) ⇒ Object
Constructor Details
#initialize(base, connection) ⇒ RemoteFile
Returns a new instance of RemoteFile.
10 11 12 13 |
# File 'lib/thor-ssh/remote_file.rb', line 10 def initialize(base, connection) @base = base @connection = connection end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
8 9 10 |
# File 'lib/thor-ssh/remote_file.rb', line 8 def base @base end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/thor-ssh/remote_file.rb', line 7 def connection @connection end |
Instance Method Details
#binread(path) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/thor-ssh/remote_file.rb', line 57 def binread(path) # If we first logged in as the running user if base.destination_server.running_as_current_user? data = nil connection.sftp.file.open(path, "rb") do |f| data = f.read end close_sftp! else # We just run this as root, when reading we don't need to go back # down to the user data = @base.destination_server.run("cat \"#{path}\"") end return data end |
#binwrite(path, data) ⇒ Object
TODO: we should just move this to a more standard thing
75 76 77 78 79 |
# File 'lib/thor-ssh/remote_file.rb', line 75 def binwrite(path, data) io = StringIO.new(data) connection.sftp.upload!(io, path) close_sftp! end |
#chmod(mode, file_name) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/thor-ssh/remote_file.rb', line 81 def chmod(mode, file_name) if mode.is_a?(Integer) # Mode is an integer, convert to octal mode = '%04d' % mode.to_s(8) end return run("chmod #{mode} \"#{file_name}\"") end |
#close_sftp! ⇒ Object
This is a workaround for bug: github.com/net-ssh/net-sftp/issues/13
17 18 19 20 |
# File 'lib/thor-ssh/remote_file.rb', line 17 def close_sftp! connection.sftp.close_channel() connection.instance_variable_set('@sftp', nil) end |
#exists?(path) ⇒ Boolean
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/thor-ssh/remote_file.rb', line 22 def exists?(path) begin res = connection.sftp.stat!(path) rescue Net::SFTP::StatusException close_sftp! return false end close_sftp! return true end |
#identical?(file1, file2) ⇒ Boolean
See if these paths point to the same inode
95 96 97 |
# File 'lib/thor-ssh/remote_file.rb', line 95 def identical?(file1, file2) inode(file1) == inode(file2) end |
#inode(file_name) ⇒ Object
90 91 92 |
# File 'lib/thor-ssh/remote_file.rb', line 90 def inode(file_name) return run("ls -i \"#{file_name}\"").strip.split(/ /).first end |
#link(old_name, new_name) ⇒ Object
53 54 55 |
# File 'lib/thor-ssh/remote_file.rb', line 53 def link(old_name, new_name) run("ln \"#{old_name}\" \"#{new_name}\"") end |
#mkdir_p(path) ⇒ Object
Creates the directory at the path on the remote server
39 40 41 |
# File 'lib/thor-ssh/remote_file.rb', line 39 def mkdir_p(path) run "mkdir -p \"#{path}\"" end |
#rm_rf(path) ⇒ Object Also known as: unlink
Remote the file/folder on the remote server
44 45 46 |
# File 'lib/thor-ssh/remote_file.rb', line 44 def rm_rf(path) run "rm -rf \"#{path}\"" end |
#run(command) ⇒ Object
34 35 36 |
# File 'lib/thor-ssh/remote_file.rb', line 34 def run(command) return base.exec(command) end |
#symlink(old_name, new_name) ⇒ Object
49 50 51 |
# File 'lib/thor-ssh/remote_file.rb', line 49 def symlink(old_name, new_name) run("ln -s \"#{old_name}\" \"#{new_name}\"") end |