Class: Aspera::AsCmd

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/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 Note: “ls” can take filters: as_ls -f *.txt -f *.bin /

Defined Under Namespace

Classes: Error

Constant Summary collapse

OPERATIONS =

list of supported actions

[:ls,:rm,:mv,:du,:info,:mkdir,:cp,:df,:md5sum]

Instance Method Summary collapse

Constructor Details

#initialize(command_executor) ⇒ AsCmd

@param command_executor [Object] provides the “execute” method, taking a command to execute, and stdin to feed to it, typically: ssh or local



13
14
15
# File 'lib/aspera/ascmd.rb', line 13

def initialize(command_executor)
  @command_executor = command_executor
end

Instance Method Details

#execute_single(action_sym, args = nil) ⇒ Object

execute an “as” command on a remote server

Parameters:

  • one (Symbol)

    of OPERATIONS

  • parameters (Array)

    for “as” command

Returns:

  • result of command, type depends on command (bool, array, hash)

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aspera/ascmd.rb', line 21

def execute_single(action_sym,args=nil)
  # concatenate arguments, enclose in double quotes, protect backslash and double quotes, add "as_" command and as_exit
  stdin_input=(args||[]).map{|v| '"' + v.gsub(/["\\]/n) {|s| '\\' + s } + '"'}.unshift('as_'+action_sym.to_s).join(' ')+"\nas_exit\n"
  # execute, get binary output
  bytebuffer=@command_executor.execute('ascmd',stdin_input).unpack('C*')
  # get hash or table result
  result=self.class.parse(bytebuffer,:result)
  raise "ERROR: unparsed bytes remaining" unless bytebuffer.empty?
  # get and delete info,always present in results
  system_info=result[:info]
  result.delete(:info)
  # make single file result like a folder
  if result.has_key?(:file);result[:dir]=[result[:file]];result.delete(:file);end
  # add type field for stats
  if result.has_key?(:dir)
    result[:dir].each do |file|
      if file.has_key?(:smode)
        # Converts the first character of the file mode (see 'man ls') into a type.
        file[:type]=case file[:smode][0,1];when'd';:directory;when'-';:file;when'l';:link;else;:other;end
      end
    end
  end
  # for info, second overrides first, so restore it
  case result.keys.length;when 0;result=system_info;when 1;result=result[result.keys.first];else raise "error";end
  # raise error as exception
  raise Error.new(result[:errno],result[:errstr],action_sym,args) if result.is_a?(Hash) and result.keys.sort == TYPES_DESCR[:error][:fields].map{|i|i[:name]}.sort
  return result
end