Class: BFS::Bucket::SCP

Inherits:
Abstract
  • Object
show all
Defined in:
lib/bfs/bucket/scp.rb

Overview

SCP buckets are operating on SCP/SSH connections.

Defined Under Namespace

Classes: CommandError

Instance Method Summary collapse

Constructor Details

#initialize(host, prefix: nil, **opts) ⇒ SCP

Initializes a new bucket

Parameters:

  • host (String)

    the host name

  • opts (Hash)

    options

Options Hash (**opts):

  • :port (Integer)

    custom port. Default: 22.

  • :user (String)

    user name for login.

  • :password (String)

    password for login.

  • :prefix (String)

    optional prefix.

  • :compression (Boolean)

    use compression.

  • :keepalive (Boolean)

    use keepalive.

  • :keepalive_interval (Integer)

    interval if keepalive enabled. Default: 300.

  • :keys (Array<String>)

    an array of file names of private keys to use for publickey and hostbased authentication.

  • :verify_host_key (Boolean|Symbol)

    specifying how strict host-key verification should be, either false, true, :very, or :secure.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bfs/bucket/scp.rb', line 31

def initialize(host, prefix: nil, **opts)
  super(**opts)

  @prefix = prefix
  @client = Net::SCP.start(host, nil, **opts.slice(*Net::SSH::VALID_OPTIONS))

  if @prefix # rubocop:disable Style/GuardClause
    @prefix = norm_path(@prefix) + '/'
    mkdir_p abs_path(@prefix)
  end
end

Instance Method Details

#closeObject

Closes the underlying connection



135
136
137
# File 'lib/bfs/bucket/scp.rb', line 135

def close
  @client.session.close unless @client.session.closed?
end

#cp(src, dst, **_opts) ⇒ Object

Copies src to dst

Parameters:

  • src (String)

    The source path.

  • dst (String)

    The destination path.



110
111
112
113
114
115
116
117
118
# File 'lib/bfs/bucket/scp.rb', line 110

def cp(src, dst, **_opts)
  full_src = full_path(src)
  full_dst = full_path(dst)

  mkdir_p File.dirname(full_dst)
  sh! 'cp', '-a', '-f', full_src, full_dst
rescue CommandError => e
  e.status == 1 ? raise(BFS::FileNotFound, src) : raise
end

#create(path, encoding: self.encoding, perm: self.perm, **opts, &block) ⇒ Object

Creates a new file and opens it for writing

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :encoding (String|Encoding)

    Custom file encoding.

  • :perm (Integer)

    Custom file permission, default: 0600.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bfs/bucket/scp.rb', line 71

def create(path, encoding: self.encoding, perm: self.perm, **opts, &block)
  full = full_path(path)

  opts[:preserve] = true if perm && !opts.key?(:preserve)
  temp = BFS::TempWriter.new(path, encoding: encoding, perm: perm) do |temp_path|
    mkdir_p File.dirname(full)
    @client.upload!(temp_path, full, **opts)
  end
  return temp unless block

  begin
    yield temp
  ensure
    temp.close
  end
end

#info(path, **_opts) ⇒ Object

Info returns the object info



57
58
59
60
61
62
63
64
65
66
# File 'lib/bfs/bucket/scp.rb', line 57

def info(path, **_opts)
  full = full_path(path)
  path = norm_path(path)
  out  = sh! 'stat', '-c', '%s;%Z;%a', full

  size, epoch, mode = out.strip.split(';', 3)
  BFS::FileInfo.new(path: path, size: size.to_i, mtime: Time.at(epoch.to_i), mode: BFS.norm_mode(mode))
rescue CommandError => e
  e.status == 1 ? raise(BFS::FileNotFound, path) : raise
end

#ls(pattern = '**/*', **_opts) ⇒ Object

Lists the contents of a bucket using a glob pattern



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bfs/bucket/scp.rb', line 44

def ls(pattern = '**/*', **_opts)
  prefix = @prefix ? abs_path(@prefix) : '/'
  Enumerator.new do |y|
    sh! 'find', prefix, '-type', 'f' do |out|
      out.each_line do |line|
        path = trim_prefix(norm_path(line.strip))
        y << path if File.fnmatch?(pattern, path, File::FNM_PATHNAME)
      end
    end
  end
end

#mv(src, dst, **_opts) ⇒ Object

Moves src to dst

Parameters:

  • src (String)

    The source path.

  • dst (String)

    The destination path.



124
125
126
127
128
129
130
131
132
# File 'lib/bfs/bucket/scp.rb', line 124

def mv(src, dst, **_opts)
  full_src = full_path(src)
  full_dst = full_path(dst)

  mkdir_p File.dirname(full_dst)
  sh! 'mv', '-f', full_src, full_dst
rescue CommandError => e
  e.status == 1 ? raise(BFS::FileNotFound, src) : raise
end

#open(path, encoding: self.encoding, tempdir: nil, **_opts, &block) ⇒ Object

Opens an existing file for reading



89
90
91
92
93
94
95
96
97
98
# File 'lib/bfs/bucket/scp.rb', line 89

def open(path, encoding: self.encoding, tempdir: nil, **_opts, &block)
  full = full_path(path)
  temp = Tempfile.new(File.basename(path), tempdir, encoding: encoding)
  temp.close

  @client.download!(full, temp.path)
  File.open(temp.path, encoding: encoding, &block)
rescue Net::SCP::Error
  raise BFS::FileNotFound, path
end

#rm(path, **_opts) ⇒ Object

Deletes a file.



101
102
103
104
# File 'lib/bfs/bucket/scp.rb', line 101

def rm(path, **_opts)
  path = full_path(path)
  sh! 'rm', '-f', path
end