Class: Spectre::FTP::SFTPConnection
- Inherits:
-
Object
- Object
- Spectre::FTP::SFTPConnection
- Includes:
- Delegate
- Defined in:
- lib/spectre/ftp.rb
Instance Method Summary collapse
- #can_connect? ⇒ Boolean
- #close ⇒ Object
- #connect! ⇒ Object
- #download(remotefile, to: File.basename(remotefile)) ⇒ Object
- #exists(path) ⇒ Object
-
#initialize(host, username, opts, logger) ⇒ SFTPConnection
constructor
A new instance of SFTPConnection.
- #passphrase(phrase) ⇒ Object
- #password(pass) ⇒ Object
- #private_key(file_path) ⇒ Object
- #stat(path) ⇒ Object
- #upload(localfile, to: File.basename(localfile)) ⇒ Object
- #username(user) ⇒ Object
Constructor Details
#initialize(host, username, opts, logger) ⇒ SFTPConnection
Returns a new instance of SFTPConnection.
80 81 82 83 84 85 86 |
# File 'lib/spectre/ftp.rb', line 80 def initialize host, username, opts, logger @__logger = logger @__session = nil @__host = host @__username = username @__opts = opts end |
Instance Method Details
#can_connect? ⇒ Boolean
120 121 122 123 124 125 |
# File 'lib/spectre/ftp.rb', line 120 def can_connect? connect! true rescue StandardError false end |
#close ⇒ Object
114 115 116 117 118 |
# File 'lib/spectre/ftp.rb', line 114 def close return if @__session.nil? or @__session.closed? @__session.close_channel end |
#connect! ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/spectre/ftp.rb', line 106 def connect! return unless @__session.nil? or @__session.closed? @__logger.info("Connecting to '#{@__host}' with user '#{@__username}'") @__session = Net::SFTP.start(@__host, @__username, @__opts) @__session.connect! end |
#download(remotefile, to: File.basename(remotefile)) ⇒ Object
127 128 129 130 131 |
# File 'lib/spectre/ftp.rb', line 127 def download remotefile, to: File.basename(remotefile) connect! @__logger.info "Downloading '#{@__username}@#{@__host}:#{remotefile}' to '#{File. to}'" @__session.download!(remotefile, to) end |
#exists(path) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/spectre/ftp.rb', line 146 def exists path begin @__session.stat! path rescue Net::SFTP::StatusException => e return false if e.description == 'no such file' raise e end true end |
#passphrase(phrase) ⇒ Object
102 103 104 |
# File 'lib/spectre/ftp.rb', line 102 def passphrase phrase @__opts[:passphrase] = phrase end |
#password(pass) ⇒ Object
92 93 94 95 |
# File 'lib/spectre/ftp.rb', line 92 def password pass @__opts[:password] = pass @__opts[:auth_methods].push 'password' unless @__opts[:auth_methods].include? 'password' end |
#private_key(file_path) ⇒ Object
97 98 99 100 |
# File 'lib/spectre/ftp.rb', line 97 def private_key file_path @__opts[:keys] = [file_path] @__opts[:auth_methods].push 'publickey' unless @__opts[:auth_methods].include? 'publickey' end |
#stat(path) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/spectre/ftp.rb', line 139 def stat path connect! file_info = @__session.stat! path @__logger.info "Stat '#{path}'\n#{JSON.pretty_generate file_info.attributes}" file_info.attributes end |
#upload(localfile, to: File.basename(localfile)) ⇒ Object
133 134 135 136 137 |
# File 'lib/spectre/ftp.rb', line 133 def upload localfile, to: File.basename(localfile) connect! @__logger.info "Uploading '#{File. localfile}' to '#{@__username}@#{@__host}:#{to}'" @__session.upload!(localfile, to) end |
#username(user) ⇒ Object
88 89 90 |
# File 'lib/spectre/ftp.rb', line 88 def username user @__username = user end |