Module: Spectre::FTP

Defined in:
lib/spectre/ftp.rb

Defined Under Namespace

Classes: FTPConnection, SFTPConnection

Constant Summary collapse

@@cfg =
{}

Class Method Summary collapse

Class Method Details

.ftp(name, config = {}, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/spectre/ftp.rb', line 159

def ftp name, config={}, &block
  cfg = @@cfg[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 &block
  ensure
    ftp_conn.close
  end
end

.sftp(name, config = {}, &block) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/spectre/ftp.rb', line 179

def sftp name, config={}, &block
  cfg = @@cfg[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]

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

  begin
    sftp_con.instance_eval &block
  ensure
    sftp_con.close
  end
end