Class: Aspera::Plugins::Server::AsCmd

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/plugins/server/ascmd.rb

Overview

Run ascmd commands using specified executor (usually, remotely on transfer node) Equivalent of SDK "command client" execute: "ascmd -h" to get syntax

Protocol (see ascmdtypes.h):

  • Input: one command per line (as_<operation> <arguments>), then as_exit.
  • Output: one reply per command, ascmd continues after a failed command. The first reply is the system information: sent at start (protocol 1), or for as_session_init (protocol 2).
  • Replies and their fields are Type-Length-Value: type (1 byte), length (4 bytes), value. Numbers are big endian.

Defined Under Namespace

Classes: AccessDenied, Error, NotFound

Constant Summary collapse

OPERATIONS =

list of supported actions

OPERATION_SPECS.keys.freeze

Instance Method Summary collapse

Constructor Details

#initialize(command_executor) ⇒ AsCmd

Returns a new instance of AsCmd.

Parameters:

  • command_executor (Object) —

    provides the "execute" method, taking a command to execute, and stdin to feed to it, typically: ssh or local



136
137
138
# File 'lib/aspera/plugins/server/ascmd.rb', line 136

def initialize(command_executor)
  @command_executor = command_executor
end

Instance Method Details

#execute_single(operation, arguments, version: 2, host: nil) ⇒ true, ...

Execute an operation in one session of ascmd: one command per group of arguments. All commands are executed, even after one failed.

Parameters:

  • operation (Symbol) —

    One of OPERATIONS

  • arguments (Array<String>) —

    Paths (mv, cp: source and destination pairs)

  • version (Integer) (defaults to: 2) —

    Protocol: 1 or 2

  • host (String, nil) (defaults to: nil) —

    Sent with as_session_init (protocol 2)

Returns:

  • (true, Array<Hash>, Hash) —

    depending on operation: rm, mv, cp, mkdir: true; ls: entries of all paths; du, md5sum: one Hash per path, with key path; df: mount points; info: system information.

Raises:

  • (Error) —

    if one or more commands failed: NotFound or AccessDenied when all failed for this reason

  • (ParameterError) —

    if the arguments do not fit the operation, or cannot be sent to ascmd



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
195
196
197
# File 'lib/aspera/plugins/server/ascmd.rb', line 154

def execute_single(operation, arguments, version: 2, host: nil)
  arguments = [] if arguments.nil?
  Log.log.debug { "execute_single:#{operation}:#{arguments}" }
  Aspera.assert_values(operation, OPERATIONS)
  Aspera.assert_values(version, [1, 2]) { 'protocol' }
  Aspera.assert_array_all(arguments, String) { 'arguments' }
  commands = command_arguments(operation, arguments)
  remote_cmd = 'ascmd'
  lines = []
  if version.eql?(2)
    lines.push(['as_session_init', "--protocol=#{version}", (quote("--host=#{host}") unless host.nil?)].compact.join(' '))
    remote_cmd += ' -V2'
  end
  commands.each { |args| lines.push(["as_#{operation}", *args.map { |arg| quote(arg) }].join(' ')) }
  lines.each do |line|
    Aspera.assert(line.bytesize <= MAX_LINE_BYTES, type: ParameterError) { "ascmd: command longer than #{MAX_LINE_BYTES} bytes: #{line[0, 80]}..." }
  end
  lines.push('as_exit', '')
  stdin_input = lines.join("\n")
  Log.log.trace1 { "execute_single:#{stdin_input}" }
  output = @command_executor.execute(remote_cmd, input: stdin_input)
  Aspera.assert(!output.empty?, 'empty answer from server')
  replies = each_tlv(output).map { |id, value| decode_reply(id, value) }
  Log.log.trace1 { "execute_single:replies=#{replies}" }
  system_info = replies.shift
  # `ascmd` failed to start, e.g. invalid docroot
  raise error_for(operation, [failure(system_info.value, [])], nil) if system_info.type.eql?(:error)
  Aspera.assert(system_info.type.eql?(:info)) { "ascmd: expecting system information first, but have: #{system_info.type}" }
  Aspera.assert(replies.length <= commands.length) { "ascmd: #{replies.length} replies for #{commands.length} commands" }
  failures = []
  commands.each_with_index do |args, index|
    reply = replies[index]
    if reply.nil?
      # `ascmd` stopped before this command
      failures.push(Error::Failure.new(errno: nil, errstr: 'no reply', arguments: args))
    elsif reply.type.eql?(:error)
      failures.push(failure(reply.value, args))
    else
      Aspera.assert_values(reply.type, OPERATION_SPECS[operation][:reply]) { "ascmd: reply to #{operation}" }
    end
  end
  raise error_for(operation, failures, system_info.value) unless failures.empty?
  return result_of(operation, commands, replies)
end