Class: Aspera::Plugins::Desktop::Agent

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

Overview

Client: Aspera for Desktop

Instance Attribute Summary collapse

Attributes inherited from Agent::Base

#transfer_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Agent::Base

#in_process?, #last_job_id, #local_destination?, transfer_cancel, #wait_for_completion

Constructor Details

#initialize(**base_options) ⇒ Agent

Returns a new instance of Agent.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/aspera/plugins/desktop/agent.rb', line 72

def initialize(**base_options)
  @application_id = SecureRandom.uuid
  @transfer_id = nil
  super
  Aspera.assert(Environment.instance.graphical?, type: EnvironmentError) { 'Using client requires a graphical environment' }
  method_index = 0
  begin
    # curl 'http://127.0.0.1:33024/' -X POST -H 'content-type: application/json' --data-raw '{"jsonrpc":"2.0","params":[],"id":999999,"method":"rpc.discover"}'
    # https://playground.open-rpc.org/?schemaUrl=http://127.0.0.1:33024
    @client_app_api = Aspera::JsonRpc::Client.new(Aspera::Rest::Client.new(base_url: aspera_client_api_url))
    client_info = @client_app_api.get_info
    Log.dump(:client_version, client_info)
    Log.log.debug('Client was reached') if method_index > 0
  rescue Errno::ECONNREFUSED => e
    start_url = START_URIS[method_index]
    method_index += 1
    Aspera.assert(!start_url.nil?) { "Unable to start #{Aspera::Products::Desktop::APP_NAME} #{method_index} times" }
    Log.log.warn { "#{Aspera::Products::Desktop::APP_NAME} is not started (#{e}). Trying to start it ##{method_index}..." }
    Environment.instance.open_uri_graphical(start_url)
    sleep(SLEEP_SEC_BETWEEN_RETRY)
    retry
  end
end

Instance Attribute Details

#application_id ⇒ Object (readonly)

Returns the application_id so callers can persist it in agent_params.



97
98
99
# File 'lib/aspera/plugins/desktop/agent.rb', line 97

def application_id
  @application_id
end

Class Method Details

.desktop_api_url ⇒ Object

Auto-discover the JSON-RPC URL from the Desktop log file (same logic as instance method).



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aspera/plugins/desktop/agent.rb', line 59

def desktop_api_url
  log_file = File.join(Aspera::Products.find(Aspera::Products::Desktop.locations).first[:log_root], Aspera::Products::Desktop::LOG_FILENAME)
  url = 'http://127.0.0.1:33024'
  File.open(log_file, 'r') do |file|
    file.each_line do |line|
      line = line.chomp
      url = "http://#{Regexp.last_match(1)}" if line =~ /JSON-RPC server listening on (.*)/
    end
  end if File.exist?(log_file)
  url
end

.normalize_status(raw_status, bytes: 0, error: nil) ⇒ Object

Normalize a raw status string into the standard async-transfer hash fields.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/aspera/plugins/desktop/agent.rb', line 41

def normalize_status(raw_status, bytes: 0, error: nil)
  result = {'bytes_transferred' => bytes}
  case raw_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' => error.to_s)
  when 'cancelled'
    result['status'] = 'cancelled'
    result['ended_at'] = Time.now.utc.iso8601
  else
    result['status'] = 'running'
  end
  result
end

.required_security_level(_options) ⇒ Object

Uses the local Aspera for Desktop application, on the local host: refused at level high (see Cli::Security).



28
# File 'lib/aspera/plugins/desktop/agent.rb', line 28

def required_security_level(_options) = :medium

.transfer_status(transfer_id, agent_params) ⇒ Hash

Re-query a previously started transfer from the Desktop JSON-RPC daemon. agent_params must contain 'application_id'; the JSON-RPC URL is auto-discovered.

Returns:

  • (Hash) —

    normalized status hash



33
34
35
36
37
38
# File 'lib/aspera/plugins/desktop/agent.rb', line 33

def transfer_status(transfer_id, agent_params)
  app_id = agent_params['application_id']
  client = Aspera::JsonRpc::Client.new(Aspera::Rest::Client.new(base_url: desktop_api_url))
  raw = client.get_transfer(app_id: app_id, transfer_id: transfer_id)
  normalize_status(raw['status'], bytes: raw['bytes_written'].to_i, error: raw['error_desc'])
end

Instance Method Details

#async_params ⇒ Hash

Returns application_id: for transfer_status.

Returns:

  • (Hash) —

    application_id: for transfer_status



100
101
102
# File 'lib/aspera/plugins/desktop/agent.rb', line 100

def async_params
  {'application_id' => @application_id}
end

#start_transfer(transfer_spec, token_regenerator: nil) ⇒ Object

:reek:UnusedParameters token_regenerator



105
106
107
108
109
110
111
112
113
# File 'lib/aspera/plugins/desktop/agent.rb', line 105

def start_transfer(transfer_spec, token_regenerator: nil)
  Aspera::Transfer::Spec.fix_transferd_resume_policy(transfer_spec)
  @request_id = SecureRandom.uuid
  # if there is a token, we ask the client app to use well known ssh private keys
  # instead of asking password
  transfer_spec['authentication'] = 'token' if transfer_spec.key?('token')
  result = @client_app_api.start_transfer(app_id: @application_id, desktop_spec: {}, transfer_spec: transfer_spec)
  @transfer_id = result['uuid']
end

#wait_for_transfers_completion ⇒ Object



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
151
152
153
154
# File 'lib/aspera/plugins/desktop/agent.rb', line 115

def wait_for_transfers_completion
  started = false
  pre_calc = false
  begin
    loop do
      transfer = @client_app_api.get_transfer(app_id: @application_id, transfer_id: @transfer_id)
      case transfer['status']
      when 'initiating', 'queued'
        notify_progress(:sessions_init, info: transfer['status'])
      when 'running'
        if !started
          notify_progress(:session_start, session_id: @transfer_id)
          started = true
        end
        if !pre_calc && (transfer['bytes_expected'] != 0)
          notify_progress(:session_size, session_id: @transfer_id, info: transfer['bytes_expected'])
          pre_calc = true
        else
          notify_progress(:transfer, session_id: @transfer_id, info: transfer['bytes_written'])
        end
      when 'completed'
        notify_progress(:session_end, session_id: @transfer_id)
        break
      when 'failed'
        notify_progress(:session_end, session_id: @transfer_id)
        raise Aspera::Transfer::Error, transfer['error_desc']
      when 'cancelled'
        notify_progress(:session_end, session_id: @transfer_id)
        raise Aspera::Transfer::Error, 'Transfer cancelled by user'
      else
        notify_progress(:session_end, session_id: @transfer_id)
        raise Aspera::Transfer::Error, "unknown status: #{transfer['status']}: #{transfer['error_desc']}"
      end
      sleep(1)
    end
  rescue StandardError => e
    return [e]
  end
  return [:success]
end