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

Constructor Details

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

Returns a new instance of AsperaCliFilesystem.



98
99
100
101
102
103
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
# File 'lib/etna/filesystem.rb', line 98

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)


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

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

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



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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/etna/filesystem.rb', line 170

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



148
149
150
# File 'lib/etna/filesystem.rb', line 148

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

#mv(src, dest) ⇒ Object



166
167
168
# File 'lib/etna/filesystem.rb', line 166

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

#rm_rf(dir) ⇒ Object



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

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

#run_ascli_cmd(cmd, *opts) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/etna/filesystem.rb', line 130

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



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

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

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



144
145
146
# File 'lib/etna/filesystem.rb', line 144

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

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



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

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