Class: Aspera::Plugins::Node::Agent

Inherits:
Agent::Base show all
Defined in:
lib/aspera/plugins/node/agent.rb

Overview

this singleton class is used by the CLI to provide a common interface to start a transfer before using it, the use must set the node_api member.

Instance Attribute Summary

Attributes inherited from Agent::Base

#transfer_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Agent::Base

#async_params, #in_process?, #last_job_id, required_security_level, transfer_cancel, #wait_for_completion

Constructor Details

#initialize(url: nil, username: nil, password: nil, root_id: nil, **base_options) ⇒ Agent

Base options used: progress, preset_lookup (when url is not given)

Parameters:

  • url (String) (defaults to: nil) —

    the base url of the node api, else from the default preset of plugin node

  • username (String) (defaults to: nil) —

    the username to use for the node api

  • password (String) (defaults to: nil) —

    the password to use for the node api

  • root_id (String) (defaults to: nil) —

    root file id if the node is an access key

  • base_options (Hash) —

    base options (see Base#initialize)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/aspera/plugins/node/agent.rb', line 60

def initialize(
  url:      nil,
  username: nil,
  password: nil,
  root_id:  nil,
  **base_options
)
  super(**base_options)
  node_params = {url: url, username: username, password: password, root_id: root_id}.compact
  # No URL: parameters of the default preset of plugin `node` (override the given ones)
  if url.nil?
    preset = preset_values(:node)
    node_params.merge!(preset.symbolize_keys) unless preset.nil?
  end
  setup_api(**node_params)
  # TODO: currently only supports one transfer. This is bad shortcut. but ok for CLI.
  @transfer_id = nil
end

Class Method Details

.normalize_status(data) ⇒ Object

Normalize a node API transfer hash into the standard async-transfer fields.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/aspera/plugins/node/agent.rb', line 36

def normalize_status(data)
  result = {'bytes_transferred' => data['bytes_transferred'].to_i}
  case data['status']
  when 'completed'
    result['status'] = 'completed'
    result['ended_at'] = Time.now.utc.iso8601
  when 'failed'
    result.merge!('status' => 'failed', 'ended_at' => Time.now.utc.iso8601, 'error' => data['error_desc'].to_s)
  when 'canceled'
    result['status'] = 'cancelled'
    result['ended_at'] = Time.now.utc.iso8601
  else
    result['status'] = 'running'
  end
  result
end

.transfer_status(transfer_id, agent_params) ⇒ Hash

Re-query a previously started transfer via the Node REST API. agent_params must contain 'url', 'username', 'password' (and optionally 'root_id').

Returns:

  • (Hash) —

    normalized status hash



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aspera/plugins/node/agent.rb', line 23

def transfer_status(transfer_id, agent_params)
  rest_params = {base_url: agent_params['url']}
  if OAuth::Factory.bearer_auth?(agent_params['password'])
    rest_params[:headers] = Api.bearer_headers(agent_params['password'], access_key: agent_params['username'])
  else
    rest_params[:auth] = {type: :basic, username: agent_params['username'], password: agent_params['password']}
  end
  node_api = Rest::Client.new(**rest_params)
  data = node_api.read("ops/transfers/#{transfer_id}") || {'status' => 'unknown'}
  normalize_status(data)
end

Instance Method Details

#local_destination? ⇒ Boolean

Received files are written on the node, not on the local system

Returns:

  • (Boolean) —

    false



81
82
83
# File 'lib/aspera/plugins/node/agent.rb', line 81

def local_destination?
  false
end

#start_transfer(transfer_spec, token_regenerator: nil) ⇒ Object

generic method :reek:UnusedParameters token_regenerator



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aspera/plugins/node/agent.rb', line 87

def start_transfer(transfer_spec, token_regenerator: nil)
  # add root id if access key
  if !@root_id.nil?
    case transfer_spec['direction']
    when Aspera::Transfer::Spec::DIRECTION_SEND then transfer_spec['source_root_id'] = @root_id
    when Aspera::Transfer::Spec::DIRECTION_RECEIVE then transfer_spec['destination_root_id'] = @root_id
    else Aspera.error_unexpected_value(transfer_spec['direction'])
    end
  end
  # add mandatory retry parameter for node api
  ts_tags = transfer_spec['tags']
  ts_tags[Aspera::Transfer::Spec::TAG_RESERVED]['xfer_retry'] ||= 150 if ts_tags.is_a?(Hash) && ts_tags[Aspera::Transfer::Spec::TAG_RESERVED].is_a?(Hash)
  # Optimization in case of sending to the same node
  # TODO: probably remove this, as /etc/hosts shall be used for that on server side.
  transfer_spec['remote_host'] = '127.0.0.1' if !transfer_spec['wss_enabled'] && transfer_spec['remote_host'].eql?(URI.parse(node_api_.base_url).host)
  resp = node_api_.create('ops/transfers', transfer_spec)
  @transfer_id = resp['id']
  Log.log.debug { "tr_id=#{@transfer_id}" }
  return @transfer_id
end

#wait_for_transfers_completion ⇒ Object

generic method



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/aspera/plugins/node/agent.rb', line 109

def wait_for_transfers_completion
  # set to true when we know the total size of the transfer
  session_started = false
  bytes_expected = nil
  # lets emulate management events to display progress bar
  loop do
    # status is empty sometimes with status 200...
    transfer_data = node_api_.read("ops/transfers/#{@transfer_id}") || {'status' => 'unknown'} rescue {'status' => 'waiting(api error)'}
    case transfer_data['status']
    when 'waiting', 'partially_completed', 'paused', 'unknown', 'waiting(read error)', 'waiting(api error)'
      notify_progress(:sessions_init, info: transfer_data['status'])
    when 'running'
      if !session_started
        notify_progress(:session_start, session_id: @transfer_id)
        session_started = true
      end
      message = transfer_data['status']
      message = "#{message} (#{transfer_data['error_desc']})" if !transfer_data['error_desc']&.empty?
      notify_progress(:sessions_init, info: message)
      if bytes_expected.nil? &&
          transfer_data['precalc'].is_a?(Hash) &&
          transfer_data['precalc']['status'].eql?('ready')
        bytes_expected = transfer_data['precalc']['bytes_expected']
        notify_progress(:session_size, session_id: @transfer_id, info: bytes_expected)
      end
      notify_progress(:transfer, session_id: @transfer_id, info: transfer_data['bytes_transferred'])
    when 'completed'
      notify_progress(:transfer, session_id: @transfer_id, info: bytes_expected) if bytes_expected
      notify_progress(:session_end, session_id: @transfer_id)
      break
    when 'failed', 'canceled'
      notify_progress(:session_end, session_id: @transfer_id)
      # Bug in HSTS ? transfer is marked failed, but there is no reason
      break if transfer_data['status'].eql?('failed') && transfer_data['error_code'].eql?(0) && transfer_data['error_desc'].empty?
      raise Aspera::Transfer::Error, "status: #{transfer_data['status']}. code: #{transfer_data['error_code']}. description: #{transfer_data['error_desc']}"
    else Aspera.error_unexpected_value(transfer_data['status']) { "transfer_data -> #{transfer_data}" }
    end
    sleep(1.0)
  end
  # TODO: get status of sessions
  return []
end