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 (Symbol)

    host-key verification should be, either :never, :accept_new_or_local_tunnel, :accept_new, or :always.



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

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

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

  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



130
131
132
# File 'lib/bfs/bucket/scp.rb', line 130

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.



105
106
107
108
109
110
111
112
113
# File 'lib/bfs/bucket/scp.rb', line 105

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 #{Shellwords.escape(full_src)} #{Shellwords.escape(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.



73
74
75
76
77
78
79
80
81
# File 'lib/bfs/bucket/scp.rb', line 73

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

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

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

Iterates over the contents of a bucket using a glob pattern



50
51
52
53
54
# File 'lib/bfs/bucket/scp.rb', line 50

def glob(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern, with_stat: true) {|info| acc << info }
  end
end

#info(path, **_opts) ⇒ Object

Info returns the object info



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

def info(path, **_opts)
  full = full_path(path)
  path = norm_path(path)
  out  = sh! %(stat -c '%F;%s;%Z;%a' #{Shellwords.escape full})

  type, size, epoch, mode = out.strip.split(';', 4)
  raise BFS::FileNotFound, path unless type.include?('file')

  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



43
44
45
46
47
# File 'lib/bfs/bucket/scp.rb', line 43

def ls(pattern = '**/*', **_opts)
  Enumerator.new do |acc|
    walk(pattern) {|path| acc << path }
  end
end

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

Moves src to dst

Parameters:

  • src (String)

    The source path.

  • dst (String)

    The destination path.



119
120
121
122
123
124
125
126
127
# File 'lib/bfs/bucket/scp.rb', line 119

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

  mkdir_p File.dirname(full_dst)
  sh! %(mv -f #{Shellwords.escape(full_src)} #{Shellwords.escape(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



84
85
86
87
88
89
90
91
92
93
# File 'lib/bfs/bucket/scp.rb', line 84

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.



96
97
98
99
# File 'lib/bfs/bucket/scp.rb', line 96

def rm(path, **_opts)
  path = full_path(path)
  sh! %(rm -f #{Shellwords.escape(path)})
end