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.



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
135
136
137
138
139
# File 'lib/etna/filesystem.rb', line 109

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)


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

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

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



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/etna/filesystem.rb', line 181

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 << "-O"
  cmd << @port.to_s

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

  cmd << "-v"
  cmd << "-k"
  cmd << "0"
  cmd << "--file-manifest=text"
  cmd << "--file-manifest-path=/var/tmp"
  cmd << "--file-checksum=md5"
  cmd << "-l"
  cmd << "100m"
  cmd << "--overwrite=always"

  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

  puts "end of mkcmd: #{cmd}"
  cmd
end

#mkdir_p(dir) ⇒ Object



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

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

#mv(src, dest) ⇒ Object



177
178
179
# File 'lib/etna/filesystem.rb', line 177

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

#rm_rf(dir) ⇒ Object



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

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

#run_ascli_cmd(cmd, *opts) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/etna/filesystem.rb', line 141

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



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

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

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



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

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

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



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

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