Class: Spectre::FTP::Client

Inherits:
Object
  • Object
show all
Includes:
Delegate
Defined in:
lib/spectre/ftp.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Client

Returns a new instance of Client.



162
163
164
165
# File 'lib/spectre/ftp.rb', line 162

def initialize config, logger
  @config = config['ftp'] || {}
  @logger = logger
end

Instance Method Details

#ftp(name, config = {}) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/spectre/ftp.rb', line 167

def ftp(name, config = {}, &)
  cfg = @config[name] || {}

  host = config[:host] || cfg['host'] || name
  username = config[:username] || cfg['username']
  password = config[:password] || cfg['password']

  opts = {}
  opts[:ssl] = config[:ssl]
  opts[:port] = config[:port] || cfg['port'] || 21

  ftp_conn = FTPConnection.new(host, username, password, opts, @logger)

  begin
    ftp_conn.instance_eval(&)
  ensure
    ftp_conn.close
  end
end

#sftp(name, config = {}) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/spectre/ftp.rb', line 187

def sftp(name, config = {}, &)
  cfg = @config[name] || {}

  host = config[:host] || cfg['host'] || name
  username = config[:username] || cfg['username']
  password = config[:password] || cfg['password']

  opts = {}
  opts[:password] = password
  opts[:port] = config[:port] || cfg['port'] || 22
  opts[:keys] = [cfg['key']] if cfg.key? 'key'
  opts[:passphrase] = cfg['passphrase'] if cfg.key? 'passphrase'

  opts[:auth_methods] = []
  opts[:auth_methods].push 'publickey' if opts[:keys]
  opts[:auth_methods].push 'password' if opts[:password]

  opts[:non_interactive] = true

  sftp_con = SFTPConnection.new(host, username, opts, @logger)

  begin
    sftp_con.instance_eval(&)
  ensure
    sftp_con.close
  end
end