Class: Aspera::Agent::Connect

Inherits:
Base
  • Object
show all
Defined in:
lib/aspera/agent/connect.rb

Constant Summary

Constants inherited from Base

Base::RUBY_EXT

Instance Method Summary collapse

Methods inherited from Base

agent_list, factory_create, to_move_options, #wait_for_completion

Constructor Details

#initialize(**base_options) ⇒ Connect

Returns a new instance of Connect.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aspera/agent/connect.rb', line 15

def initialize(**base_options)
  super(**base_options)
  @connect_settings = {
    'app_id' => SecureRandom.uuid
  }
  raise 'Using connect requires a graphical environment' if !Environment.default_gui_mode.eql?(:graphical)
  method_index = 0
  begin
    connect_url = Ascp::Products.connect_uri
    Log.log.debug{"found: #{connect_url}"}
    @connect_api = Rest.new(
      base_url: "#{connect_url}/v5/connect", # could use v6 also now
      headers: {'Origin' => Rest.user_agent})
    connect_info = @connect_api.read('info/version')[:data]
    Log.log.info('Connect was reached') if method_index > 0
    Log.log.debug{Log.dump(:connect_version, connect_info)}
  rescue StandardError => e # Errno::ECONNREFUSED
    start_url = CONNECT_START_URIS[method_index]
    method_index += 1
    raise StandardError, "Unable to start connect #{method_index} times" if start_url.nil?
    Log.log.warn{"Aspera Connect is not started (#{e}). Trying to start it ##{method_index}..."}
    if !Environment.open_uri_graphical(start_url)
      Environment.open_uri_graphical('https://www.ibm.com/aspera/connect/')
      raise StandardError, 'Connect is not installed'
    end
    sleep(SLEEP_SEC_BETWEEN_RETRY)
    retry
  end
end

Instance Method Details

#start_transfer(transfer_spec, token_regenerator: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aspera/agent/connect.rb', line 45

def start_transfer(transfer_spec, token_regenerator: nil)
  if transfer_spec['direction'] == 'send'
    Log.log.warn{"Connect requires upload selection using GUI, ignoring #{transfer_spec['paths']}".red}
    transfer_spec.delete('paths')
    selection = @connect_api.create('windows/select-open-file-dialog/', {
      'aspera_connect_settings' => @connect_settings,
      'title'                   => 'Select Files',
      'suggestedName'           => '',
      'allowMultipleSelection'  => true,
      'allowedFileTypes'        => ''})[:data]
    transfer_spec['paths'] = selection['dataTransfer']['files'].map { |i| {'source' => i['name']}}
  end
  @request_id = SecureRandom.uuid
  # if there is a token, we ask connect client to use well known ssh private keys
  # instead of asking password
  transfer_spec['authentication'] = 'token' if transfer_spec.key?('token')
  connect_transfer_args = {
    'aspera_connect_settings' => @connect_settings.merge({
      'request_id'    => @request_id,
      'allow_dialogs' => true
    }),
    'transfer_specs'          => [{
      'transfer_spec' => transfer_spec
    }]}
  # asynchronous anyway
  res = @connect_api.create('transfers/start', connect_transfer_args)[:data]
  @xfer_id = res['transfer_specs'].first['transfer_spec']['tags'][Transfer::Spec::TAG_RESERVED]['xfer_id']
end

#wait_for_transfers_completionObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/aspera/agent/connect.rb', line 74

def wait_for_transfers_completion
  connect_activity_args = {'aspera_connect_settings' => @connect_settings}
  started = false
  pre_calc = false
  session_id = @xfer_id
  begin
    loop do
      tr_info = @connect_api.create("transfers/info/#{@xfer_id}", connect_activity_args)[:data]
      Log.log.trace1{Log.dump(:tr_info, tr_info)}
      if tr_info['transfer_info'].is_a?(Hash)
        transfer = tr_info['transfer_info']
        if transfer.nil?
          Log.log.warn('no session in Connect')
          break
        end
        # TODO: get session id
        case transfer['status']
        when 'initiating', 'queued'
          notify_progress(session_id: nil, type: :pre_start, info: transfer['status'])
        when 'running'
          if !started
            notify_progress(session_id: session_id, type: :session_start)
            started = true
          end
          if !pre_calc && (transfer['bytes_expected'] != 0)
            notify_progress(type: :session_size, session_id: session_id, info: transfer['bytes_expected'])
            pre_calc = true
          else
            notify_progress(type: :transfer, session_id: session_id, info: transfer['bytes_written'])
          end
        when 'completed'
          notify_progress(type: :end, session_id: session_id)
          break
        when 'failed'
          notify_progress(type: :end, session_id: session_id)
          raise Transfer::Error, transfer['error_desc']
        when 'cancelled'
          notify_progress(type: :end, session_id: session_id)
          raise Transfer::Error, 'Transfer cancelled by user'
        else
          notify_progress(type: :end, session_id: session_id)
          raise Transfer::Error, "unknown status: #{transfer['status']}: #{transfer['error_desc']}"
        end
      end
      sleep(1)
    end
  rescue StandardError => e
    return [e]
  end
  return [:success]
end