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

Constructor Details

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

Returns a new instance of AsperaCliFilesystem.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/etna/filesystem.rb', line 82

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)


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

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

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/etna/filesystem.rb', line 154

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



132
133
134
# File 'lib/etna/filesystem.rb', line 132

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

#mv(src, dest) ⇒ Object



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

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

#rm_rf(dir) ⇒ Object



136
137
138
# File 'lib/etna/filesystem.rb', line 136

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

#run_ascli_cmd(cmd, *opts) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/etna/filesystem.rb', line 114

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



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

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

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



128
129
130
# File 'lib/etna/filesystem.rb', line 128

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

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



124
125
126
# File 'lib/etna/filesystem.rb', line 124

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