Class: Aspera::Plugins::Server::AsCmd
- Inherits:
-
Object
- Object
- Aspera::Plugins::Server::AsCmd
- 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>), thenas_exit. - Output: one reply per command,
ascmdcontinues after a failed command. The first reply is the system information: sent at start (protocol 1), or foras_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
-
#execute_single(operation, arguments, version: 2, host: nil) ⇒ true, ...
Execute an operation in one session of
ascmd: one command per group of arguments. -
#initialize(command_executor) ⇒ AsCmd
constructor
A new instance of AsCmd.
Constructor Details
#initialize(command_executor) ⇒ AsCmd
Returns a new instance of AsCmd.
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.
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 |