Class: Etna::Filesystem::AsperaCliFilesystem

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

Direct Known Subclasses

GeneAsperaCliFilesystem

Instance Method Summary collapse

Methods included from WithPipeConsumer

#mkio

Methods inherited from Etna::Filesystem

#ls, #stat

Constructor Details

#initialize(ascli_bin:, ascp_bin:, host:, username:, password: nil, key_file: nil, port: 33001) ⇒ AsperaCliFilesystem

Returns a new instance of AsperaCliFilesystem.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/etna/filesystem.rb', line 104

def initialize(ascli_bin:, ascp_bin:, host:, username:, password: nil, key_file: nil, port: 33001)
  @ascli_bin = ascli_bin
  @ascp_bin = ascp_bin
  @username = username
  @password = password
  @key_file = key_file
  @host = host
  @port = port

  @config_file = File.join(Dir.mktmpdir, "config.yml")
  config = {}
  config["config"] = {"version" => `#{ascli_bin} --version`.chomp}
  config["default"] = {"server" => "clifilesystem"}
  server_config = config["clifilesystem"] = {
      "url" => "ssh://#{host}:#{port}",
      "username" => username,
      "ssh_options" => {append_all_supported_algorithms: true},
  }

  if password
    server_config["password"] = password
  elsif key_file
    server_config["ssh_keys"] = key_file
  else
    raise "One of password or key_file must be provided"
  end

  ::File.open(@config_file, "w") do |file|
    file.write(config.to_yaml)
  end
end

Instance Method Details

#exist?(src) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/etna/filesystem.rb', line 168

def exist?(src)
  !run_ascli_cmd("ls", src).nil?
end

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



176
177
178
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
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/etna/filesystem.rb', line 176

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

  if @password
    env['ASPERA_SCP_PASS'] = @password
  else
    cmd << "-i"
    cmd << @key_file
  end

  cmd << "-P"
  cmd << @port.to_s

  remote_path = file
  # https://download.asperasoft.com/download/docs/entsrv/3.9.1/es_admin_linux/webhelp/index.html#dita/stdio_2.html
  local_path = "stdio://"
  if size_hint
    local_path += "/?#{size_hint}"
  end

  if opts.include?('r')
    cmd << '--mode=recv'
    cmd << "--host=#{@host}"
    cmd << "--user=#{@username}"
    cmd << remote_path
    cmd << local_path

    cmd << {out: wd}
  elsif opts.include?('w')
    cmd << '--mode=send'
    cmd << "--host=#{@host}"
    cmd << "--user=#{@username}"
    cmd << local_path
    cmd << remote_path

    cmd << {in: rd}
  end

  cmd
end

#mkdir_p(dir) ⇒ Object



154
155
156
# File 'lib/etna/filesystem.rb', line 154

def mkdir_p(dir)
  raise "Failed to mkdir #{dir}" unless run_ascli_cmd("mkdir", dir)
end

#mv(src, dest) ⇒ Object



172
173
174
# File 'lib/etna/filesystem.rb', line 172

def mv(src, dest)
  raise "Failed to mv #{src} to #{dest}" unless run_ascli_cmd("mv", src, dest)
end

#rm_rf(dir) ⇒ Object



158
159
160
# File 'lib/etna/filesystem.rb', line 158

def rm_rf(dir)
  raise "Failed to rm_rf #{dir}" unless run_ascli_cmd("rm", dir)
end

#run_ascli_cmd(cmd, *opts) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/etna/filesystem.rb', line 136

def run_ascli_cmd(cmd, *opts)
  output, status = Open3.capture2(@ascli_bin, "server", cmd, *opts, "--format=json", "--config=#{@config_file}")

  if status.success?
    return JSON.parse(output)
  end

  nil
end

#tmpdirObject



162
163
164
165
166
# File 'lib/etna/filesystem.rb', line 162

def tmpdir
  tmpdir = "/Upload/Temp/#{SecureRandom.hex}"
  mkdir_p(tmpdir)
  tmpdir
end

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



150
151
152
# File 'lib/etna/filesystem.rb', line 150

def with_readable(src, opts = 'r', &block)
  mkio(src, opts, &block)
end

#with_writeable(dest, opts = 'w', size_hint: nil, &block) ⇒ Object



146
147
148
# File 'lib/etna/filesystem.rb', line 146

def with_writeable(dest, opts = 'w', size_hint: nil, &block)
  mkio(dest, opts, size_hint: size_hint, &block)
end