Class: Etna::Filesystem::SftpFilesystem

Inherits:
Filesystem
  • Object
show all
Includes:
WithPipeConsumer
Defined in:
lib/etna/filesystem.rb

Defined Under Namespace

Classes: SftpFile

Instance Method Summary collapse

Methods included from WithPipeConsumer

#mkio

Constructor Details

#initialize(host:, username:, password: nil, port: 22, **args) ⇒ SftpFilesystem

Returns a new instance of SftpFilesystem.



246
247
248
249
250
251
252
253
# File 'lib/etna/filesystem.rb', line 246

def initialize(host:, username:, password: nil, port: 22, **args)
  @username = username
  @password = password
  @host = host
  @port = port

  @dir_listings = {}
end

Instance Method Details

#authnObject



259
260
261
# File 'lib/etna/filesystem.rb', line 259

def authn
  "#{@username}:#{@password}"
end

#curl_cmd(path, opts = []) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/etna/filesystem.rb', line 263

def curl_cmd(path, opts=[])
  connection = Curl::Easy.new(url(path))
  connection.http_auth_types = :basic
  connection.username = @username
  connection.password = @password

  connection
end

#exist?(src) ⇒ Boolean

Returns:

  • (Boolean)


313
314
315
316
# File 'lib/etna/filesystem.rb', line 313

def exist?(src)
  files = ls(::File.dirname(src))
  files.include?(::File.basename(src))
end

#ls(dir) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/etna/filesystem.rb', line 318

def ls(dir)
  dir = dir + "/" unless "/" == dir[-1]  # Trailing / makes curl list directory

  return @dir_listings[dir] if @dir_listings.has_key?(dir)

  listing = ''
  connection = curl_cmd(dir)
  connection.on_body { |data| listing << data; data.size }
  connection.perform

  @dir_listings[dir] = listing

  listing
end

#mkcommand(rd, wd, file, opts, size_hint: nil) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/etna/filesystem.rb', line 286

def mkcommand(rd, wd, file, opts, size_hint: nil)
  env = {}
  cmd = [env, "curl"]

  cmd << "-u"
  cmd << authn
  cmd << "-o"
  cmd << "-"
  cmd << "-s"
  cmd << "-N"
  cmd << url(file)

  if opts.include?('r')
    cmd << {out: wd}
  end

  cmd
end

#sftp_file_from_path(src) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/etna/filesystem.rb', line 272

def sftp_file_from_path(src)
  files = ls(::File.dirname(src)).split("\n").map do |listing|
    SftpFile.new(listing)
  end

  file = files.select do |file|
    file.name == ::File.basename(src)
  end

  raise "#{src} not found" if file.empty?

  file.first
end

#stat(src) ⇒ Object



333
334
335
# File 'lib/etna/filesystem.rb', line 333

def stat(src)
  sftp_file_from_path(src)
end

#url(src) ⇒ Object



255
256
257
# File 'lib/etna/filesystem.rb', line 255

def url(src)
  "sftp://#{@host}/#{src}"
end

#with_readable(src, opts = 'r', &block) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/etna/filesystem.rb', line 305

def with_readable(src, opts = 'r', &block)
  raise "#{src} does not exist" unless exist?(src)

  sftp_file = sftp_file_from_path(src)

  mkio(src, opts, size_hint: sftp_file.size, &block)
end