Class: Envoi::Mam::Agent::TransferClient::Aspera

Inherits:
Envoi::Mam::Agent::TransferClient show all
Defined in:
lib/envoi/mam/agent/transfer_client/aspera.rb

Constant Summary collapse

DEFAULT_DESTINATION_PATH =
'.'
DEFAULT_ASCP_ARGS =
'-v -k3 --overwrite=diff -P 33001'
ASCP_PATHS =
[
  '/usr/local/bin/ascp',
  '/usr/bin/ascp',
  '/Library/Aspera/bin/ascp',
  '~/Applications/Aspera CLI/bin/ascp',
  '~/Applications/Aspera Connect.app/Contents/Resources/ascp',
  '/Applications/Aspera Connect.app/Contents/Resources/ascp'
]

Instance Attribute Summary collapse

Attributes inherited from Envoi::Mam::Agent::TransferClient

#agent, #initial_args, #logger

Instance Method Summary collapse

Methods inherited from Envoi::Mam::Agent::TransferClient

#initialize, #initialize_logger

Constructor Details

This class inherits a constructor from Envoi::Mam::Agent::TransferClient

Instance Attribute Details

#ascp_pathObject

Returns the value of attribute ascp_path.



31
32
33
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 31

def ascp_path
  @ascp_path
end

#default_ascp_argsObject

Returns the value of attribute default_ascp_args.



31
32
33
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 31

def default_ascp_args
  @default_ascp_args
end

#default_ascp_pathObject

Returns the value of attribute default_ascp_path.



31
32
33
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 31

def default_ascp_path
  @default_ascp_path
end

Instance Method Details

#after_initialize(args = initial_args) ⇒ Object



33
34
35
36
37
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 33

def after_initialize(args = initial_args)
  @default_ascp_path = args[:default_aspera_ascp_path]
  @default_ascp_path ||= ASCP_PATHS.find { |v| File.exists? v } || ASCP_PATHS.first
  @default_ascp_args = args[:default_ascp_args] || args[:default_aspera_ascp_args] || DEFAULT_ASCP_ARGS
end

#ascp_path_exists?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 39

def ascp_path_exists?
  File.executable?(@ascp_path)
end

#build_ascp_command(config, mode, source_path, target_path) ⇒ Object



101
102
103
104
105
106
107
108
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
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 101

def build_ascp_command(config, mode, source_path, target_path)
  aspera_host_address = config['host_address'] || config['host']
  aspera_username = config['username']
  aspera_password = config['password']
  aspera_ssh_port = config['ssh_port']

  aspera_url = config['url'] || config['uri']
  if aspera_url
    aspera_uri = URI(aspera_url)
    aspera_host_address ||= aspera_uri.host if aspera_uri
    un, pw = (aspera_uri.userinfo || '').split(':')
    aspera_username ||= un if un
    aspera_password ||= pw if pw
    aspera_ssh_port ||= aspera_uri.port
  end

  aspera_token = config['aspera_transfer_token'] || config['aspera_token'] || config['token'] || config['transfer_token']
  if aspera_username && aspera_password && (aspera_token.nil? || aspera_token.empty?)
    aspera_token = %(Basic #{["#{aspera_username}:#{aspera_password}"]
                                  .pack('m')
                                  .delete("\r\n")})
    aspera_username = 'xfer'
  end
  @ascp_path = config['ascp_path'] || default_ascp_path
  @ascp_path = File.expand_path(@ascp_path)

  aspera_ascp_path = @ascp_path

  tags = config['tags'] || { }
  aspera_tags = tags['aspera'] || { }
  aspera_tags['xfer_id'] ||= SecureRandom.uuid


  ascp_args = config['ascp_args'] || default_ascp_args || agent.default_ascp_args

  # command = %(export ASPERA_SCP_PASS="#{aspera_password}"; "#{aspera_ascp_path}" #{ascp_args} --host="#{aspera_host_address}" --user=#{aspera_username} --mode=#{mode} "#{source_path}" "#{target_path}" )
  command = ''
  command << %(export ASPERA_SCP_PASS="#{aspera_password}"; ) if aspera_password
  command << %("#{aspera_ascp_path}" --mode=#{mode} --host="#{aspera_host_address}" --user="#{aspera_username}")
  command << %( -P #{aspera_ssh_port}) if aspera_ssh_port
  command << %(--tags64 #{Base64.strict_encode64(JSON.generate(tags))}) if tags && !tags.empty?
  if ascp_args && !ascp_args.empty?
    command << (ascp_args.is_a?(Array)) ? ascp_args.join(' ') : ascp_args
  end
  command << %( -W "#{aspera_token}") if aspera_token && !aspera_token.empty?
  command << %( "#{source_path.gsub('"', '\"')}" "#{target_path.gsub('"', '\"')}")
end

#build_asperala_transfer_args(config, mode, source_path, target_path) ⇒ Object



43
44
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
73
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
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 43

def build_asperala_transfer_args(config, mode, source_path, target_path)

  aspera_host_address = config['host_address'] || config['host']
  aspera_username = config['username']
  aspera_password = config['password']
  aspera_ssh_port = config['ssh_port']

  aspera_url = config['url'] || config['uri']
  if aspera_url
    aspera_uri = URI(aspera_url)
    aspera_host_address ||= aspera_uri.host if aspera_uri
    un, pw = (aspera_uri.userinfo || '').split(':')
    aspera_username ||= un if un
    aspera_password ||= pw if pw
    aspera_ssh_port = aspera_uri.port
  end

  aspera_token = config['aspera_transfer_token'] || config['aspera_token'] || config['token'] || config['transfer_token']
  if aspera_username && aspera_password && (aspera_token.nil? || aspera_token.empty?)
    aspera_token = %(Basic #{["#{aspera_username}:#{aspera_password}"]
                                  .pack('m')
                                  .delete("\r\n")})
    aspera_username = 'xfer'
    aspera_password = nil
  end

  # @ascp_path = config['ascp_path'] || default_ascp_path
  # @ascp_path = File.expand_path(@ascp_path)
  # aspera_ascp_path = @ascp_path

  env_vars = { }
  env_vars['ASPERA_SCP_PASS'] = aspera_password if aspera_password
  # env_vars['ASPERA_SCP_TOKEN'] = aspera_token if aspera_token

  ascp_args = config['ascp_args'] ||
      default_ascp_args ||
      (agent.respond_to?(:default_ascp_args) ? agent.default_ascp_args : '')

  tags = config['tags'] ||= { }
  aspera_tags = tags['aspera'] ||= { }
  aspera_tags['xfer_id'] ||= SecureRandom.uuid

  cmdline_args = [
    '--mode', mode,
    '--host', aspera_host_address,
    '--user', aspera_username,
    '--tags64', Base64.strict_encode64(JSON.generate(tags)),
  ]
  cmdline_args.concat [ '-P', aspera_ssh_port ] if aspera_ssh_port
  cmdline_args.concat ascp_args.is_a?(Array) ? ascp_args : ascp_args.split(' ') if ascp_args && !ascp_args.empty?
  if aspera_token && !aspera_token.empty?
    cmdline_args.concat ['-W', aspera_token, '-i', 'asperaweb_id_dsa.openssh' ]
  end
  cmdline_args.concat [ source_path.gsub('"', '\"'), target_path.gsub('"', '\"') ]

  { env: env_vars, args: cmdline_args, ascp_version: :ascp }
end

#download(config, path, destination_path = DEFAULT_DESTINATION_PATH) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 149

def download(config, path, destination_path = DEFAULT_DESTINATION_PATH)
  aspera_base_path = config['base_path'] || ''
  source_path = aspera_base_path.empty? ? path : File.join(aspera_base_path, path)

  mode = 'recv'
  transfer(config, mode, source_path, destination_path)
end

#shutdown(graceful = true) ⇒ Object



195
196
197
198
199
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 195

def shutdown(graceful = true)
  if @fasp && @fasp.respond_to?(:shutdown)
    @fasp.shutdown(graceful)
  end
end

#transfer(config, mode, source_path, destination_path) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 166

def transfer(config, mode, source_path, destination_path)
  if true
    transfer_using_asperala(config, mode, source_path, destination_path)
  else
    transfer_using_shell_execute(config, mode, source_path, destination_path)
  end
end

#transfer_using_asperala(config, mode, source_path, destination_path) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 174

def transfer_using_asperala(config, mode, source_path, destination_path)
  args_out = build_asperala_transfer_args(config, mode, source_path, destination_path)
  @fasp ||= Asperalm::Fasp::Local.instance
  Asperalm::Log.instance.logger = logger
  # Asperalm::Log.instance.level = :debug #logger.level
  @fasp.start_transfer_with_args_env(args_out, {})
  { success: true }
end

#transfer_using_shell_execute(config, mode, source_path, destination_path) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 183

def transfer_using_shell_execute(config, mode, source_path, destination_path)
  command = build_ascp_command(config, mode, source_path, destination_path)

  unless ascp_path_exists?
    msg = "ASCP not found. '#{ascp_path}'"
    warn msg
    return { message: msg, success: false }
  end

  agent.shell_execute(command)
end

#upload(config, path, destination_path = DEFAULT_DESTINATION_PATH) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/envoi/mam/agent/transfer_client/aspera.rb', line 157

def upload(config, path, destination_path = DEFAULT_DESTINATION_PATH)
  aspera_base_path = config['base_path'] || ''
  source_path = path
  destination_path = aspera_base_path.empty? ? destination_path : File.join(aspera_base_path, destination_path)

  mode = 'send'
  transfer(config, mode, source_path, destination_path)
end