Class: Aspera::Ascp::Installation

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/ascp/installation.rb

Overview

Singleton that tells where to find ascp and other local resources (keys..) , using the “path(:name)” method. It is used by object : AgentDirect to find necessary resources By default it takes the first Aspera product found The user can specify ascp location by calling: Installation.instance.use_ascp_from_product(product_name) or Installation.instance.ascp_path=“”

Constant Summary collapse

EXE_FILES =

all ascp files (in SDK)

%i[ascp ascp4 async].freeze
SDK_ARCHIVE_FOLDERS =
['/bin/', '/aspera/'].freeze
CLIENT_SSH_KEY_OPTIONS =

options for SSH client private key

%i{dsa_rsa rsa per_client}.freeze

Instance Method Summary collapse

Instance Method Details

#ascp_infoObject

information for ‘ascp info`



228
229
230
231
232
233
234
# File 'lib/aspera/ascp/installation.rb', line 228

def ascp_info
  ascp_data = file_paths
  ascp_data.merge!(ascp_pvcl_info)
  ascp_data['sdk_locations'] = TRANSFER_SDK_LOCATION_URL
  ascp_data.merge!(ascp_ssl_info)
  return ascp_data
end

#ascp_pathObject



67
68
69
# File 'lib/aspera/ascp/installation.rb', line 67

def ascp_path
  path(:ascp)
end

#ascp_path=(v) ⇒ Object

set ascp executable path



60
61
62
63
64
65
# File 'lib/aspera/ascp/installation.rb', line 60

def ascp_path=(v)
  Aspera.assert_type(v, String)
  Aspera.assert(!v.empty?) {'ascp path cannot be empty: check your config file'}
  Aspera.assert(File.exist?(v)) {"No such file: [#{v}]"}
  @path_to_ascp = v
end

#ascp_pvcl_infoObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/aspera/ascp/installation.rb', line 182

def ascp_pvcl_info
  data = {}
  # read PATHs from ascp directly, and pvcl modules as well
  Open3.popen3(ascp_path, '-DDL-') do |_stdin, _stdout, stderr, thread|
    last_line = ''
    while (line = stderr.gets)
      line.chomp!
      last_line = line
      case line
      when /^DBG Path ([^ ]+) (dir|file) +: (.*)$/
        data[Regexp.last_match(1)] = Regexp.last_match(3)
      when /^DBG Added module group:"(?<module>[^"]+)" name:"(?<scheme>[^"]+)", version:"(?<version>[^"]+)" interface:"(?<interface>[^"]+)"$/
        c = Regexp.last_match.named_captures.symbolize_keys
        data[c[:interface]] ||= {}
        data[c[:interface]][c[:module]] ||= []
        data[c[:interface]][c[:module]].push("#{c[:scheme]} v#{c[:version]}")
      when %r{^DBG License result \(/license/(\S+)\): (.+)$}
        data[Regexp.last_match(1)] = Regexp.last_match(2)
      when /^LOG (.+) version ([0-9.]+)$/
        data['product_name'] = Regexp.last_match(1)
        data['product_version'] = Regexp.last_match(2)
      when /^LOG Initializing FASP version ([^,]+),/
        data['sdk_ascp_version'] = Regexp.last_match(1)
      end
    end
    if !thread.value.exitstatus.eql?(1) && !data.key?('root')
      raise last_line
    end
  end
  return data
end

#ascp_ssl_infoObject

extract some stings from ascp binary



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/aspera/ascp/installation.rb', line 215

def ascp_ssl_info
  data = {}
  File.binread(ascp_path).scan(/[\x20-\x7E]{10,}/) do |bin_string|
    if (m = bin_string.match(/OPENSSLDIR.*"(.*)"/))
      data['openssldir'] = m[1]
    elsif (m = bin_string.match(/OpenSSL (\d[^ -]+)/))
      data['openssl_version'] = m[1]
    end
  end if File.file?(ascp_path)
  return data
end

#aspera_token_ssh_key_paths(types) ⇒ Object

get paths of SSH keys to use for ascp client

Parameters:

  • types (Symbol)

    types to use



154
155
156
157
158
159
160
161
162
# File 'lib/aspera/ascp/installation.rb', line 154

def aspera_token_ssh_key_paths(types)
  Aspera.assert_values(types, CLIENT_SSH_KEY_OPTIONS)
  return case types
         when :dsa_rsa, :rsa
           types.to_s.split('_').map{|i|Installation.instance.path("ssh_private_#{i}".to_sym)}
         when :per_client
           raise 'Not yet implemented'
         end
end

#check_or_create_sdk_file(filename, force: false, &block) ⇒ Object



102
103
104
# File 'lib/aspera/ascp/installation.rb', line 102

def check_or_create_sdk_file(filename, force: false, &block)
  return Environment.write_file_restricted(File.join(Products::Trsdk.sdk_directory, filename), force: force, mode: 0o644, &block)
end

#extract_archive_files(sdk_archive_path) ⇒ Object

Parameters:

  • &block

    called with entry information



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/aspera/ascp/installation.rb', line 263

def extract_archive_files(sdk_archive_path)
  raise 'missing block' unless block_given?
  case sdk_archive_path
  # Windows and Mac use zip
  when /\.zip$/
    require 'zip'
    # extract files from archive
    Zip::File.open(sdk_archive_path) do |zip_file|
      zip_file.each do |entry|
        next if entry.name.end_with?('/')
        yield(entry.name, entry.get_input_stream, nil)
      end
    end
  # Other Unixes use tar.gz
  when /\.tar\.gz/
    require 'zlib'
    require 'rubygems/package'
    Zlib::GzipReader.open(sdk_archive_path) do |gzip|
      Gem::Package::TarReader.new(gzip) do |tar|
        tar.each do |entry|
          next if entry.directory?
          yield(entry.full_name, entry, entry.symlink? ? entry.header.linkname : nil)
        end
      end
    end
  else
    raise "unknown archive extension: #{sdk_archive_path}"
  end
end

#file_pathsHash

Returns with key = file name (String), and value = path to file.

Returns:

  • (Hash)

    with key = file name (String), and value = path to file



91
92
93
94
95
96
97
98
99
100
# File 'lib/aspera/ascp/installation.rb', line 91

def file_paths
  return FILES.each_with_object({}) do |v, m|
    m[v.to_s] =
      begin
        path(v)
      rescue => e
        e.message
      end
  end
end

#get_ascp_version(exe_path) ⇒ Object

use in plugin ‘config`



165
166
167
# File 'lib/aspera/ascp/installation.rb', line 165

def get_ascp_version(exe_path)
  return get_exe_version(exe_path, '-A')
end

#get_exe_version(exe_path, vers_arg) ⇒ Object

Check that specified path is ascp and get version



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/aspera/ascp/installation.rb', line 170

def get_exe_version(exe_path, vers_arg)
  raise 'ERROR: nil arg' if exe_path.nil?
  return nil unless File.exist?(exe_path)
  exe_version = nil
  cmd_out = %x("#{exe_path}" #{vers_arg})
  raise "An error occurred when testing #{exe_path}: #{cmd_out}" unless $CHILD_STATUS == 0
  # get version from ascp, only after full extract, as windows requires DLLs (SSL/TLS/etc...)
  m = cmd_out.match(/ version ([0-9.]+)/)
  exe_version = m[1].gsub(/\.$/, '') unless m.nil?
  return exe_version
end

#install_sdk(url: nil, folder: nil, backup: true, with_exe: true, &block) ⇒ Object

download aspera SDK or use local file extracts ascp binary for current system architecture

Parameters:

  • url (String) (defaults to: nil)

    URL to SDK archive, or SpecialValues::DEF

  • folder (String) (defaults to: nil)

    destination

  • backup (Bool) (defaults to: true)
  • with_exe (Bool) (defaults to: true)
  • &block (Proc)

    a lambda that receives a file path from archive and tells detination sub folder, or nil to not extract

Returns:

  • ascp version (from execution)



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/aspera/ascp/installation.rb', line 301

def install_sdk(url: nil, folder: nil, backup: true, with_exe: true, &block)
  url = sdk_url_for_platform if url.nil? || url.eql?('DEF')
  folder = Products::Trsdk.sdk_directory if folder.nil?
  subfolder_lambda = block
  if subfolder_lambda.nil?
    subfolder_lambda = ->(name) do
      if SDK_ARCHIVE_FOLDERS.any?{|i|name.include?(i)}
        '/'
      elsif name.end_with?(EXT_RUBY_PROTOBUF)
        RB_SDK_SUBFOLDER
      end
    end
  end
  if url.start_with?('file:')
    # require specific file scheme: the path part is "relative", or absolute if there are 4 slash
    raise 'use format: file:///<path>' unless url.start_with?(FILE_SCHEME_PREFIX)
    sdk_archive_path = url[FILE_SCHEME_PREFIX.length..-1]
    delete_archive = false
  else
    sdk_archive_path = File.join(Dir.tmpdir, File.basename(url))
    Aspera::Rest.new(base_url: url, redirect_max: 3).call(operation: 'GET', save_to_file: sdk_archive_path)
    delete_archive = true
  end
  # rename old install
  if backup && !Dir.empty?(folder)
    Log.log.warn('Previous install exists, renaming folder.')
    File.rename(folder, "#{folder}.#{Time.now.strftime('%Y%m%d%H%M%S')}")
    # TODO: delete old archives ?
  end
  extract_archive_files(sdk_archive_path) do |entry_name, entry_stream, link_target|
    subfolder = subfolder_lambda.call(entry_name)
    next if subfolder.nil?
    dest_folder = File.join(folder, subfolder)
    FileUtils.mkdir_p(dest_folder)
    new_file = File.join(dest_folder, File.basename(entry_name))
    if link_target.nil?
      File.open(new_file, 'wb') { |output_stream|IO.copy_stream(entry_stream, output_stream)}
    else
      File.symlink(link_target, new_file)
    end
  end
  File.unlink(sdk_archive_path) rescue nil if delete_archive # Windows may give error
  return unless with_exe
  # ensure license file are generated so that ascp invocation for version works
  path(:aspera_license)
  path(:aspera_conf)
  sdk_ascp_file = Environment.exe_file('ascp')
  sdk_ascp_path = File.join(folder, sdk_ascp_file)
  raise "No #{sdk_ascp_file} found in SDK archive" unless File.exist?(sdk_ascp_path)
  EXE_FILES.each do |exe_sym|
    exe_path = sdk_ascp_path.gsub('ascp', exe_sym.to_s)
    Environment.restrict_file_access(exe_path, mode: 0o755) if File.exist?(exe_path)
  end
  sdk_ascp_version = get_ascp_version(sdk_ascp_path)
  sdk_daemon_path = Products::Trsdk.transferd_path
  Log.log.warn{"No #{sdk_daemon_path} in SDK archive"} unless File.exist?(sdk_daemon_path)
  Environment.restrict_file_access(sdk_daemon_path, mode: 0o755) if File.exist?(sdk_daemon_path)
  transferd_version = get_exe_version(sdk_daemon_path, 'version')
  sdk_name = 'IBM Aspera Transfer SDK'
  sdk_version = transferd_version || sdk_ascp_version
  File.write(File.join(folder, Products::Other::INFO_META_FILE), "<product><name>#{sdk_name}</name><version>#{sdk_version}</version></product>")
  return sdk_name, sdk_version
end

#installed_productsObject

Returns the list of installed products in format of product_locations_on_current_os.

Returns:

  • the list of installed products in format of product_locations_on_current_os



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/aspera/ascp/installation.rb', line 379

def installed_products
  if @found_products.nil?
    # :expected  M app name is taken from the manifest if present, else defaults to this value
    # :app_root  M main folder for the application
    # :log_root  O location of log files (Linux uses syslog)
    # :run_root  O only for Connect Client, location of http port file
    # :sub_bin   O subfolder with executables, default : bin
    scan_locations = Products::Trsdk.locations.concat(
      Products::Alpha.locations,
      Products::Connect.locations,
      Products::Other::LOCATION_ON_THIS_OS
    )
    # .each {|item| item.deep_do {|h, _k, _v, _m|h.freeze}}.freeze
    # search installed products: with ascp
    @found_products = Products::Other.find(scan_locations)
  end
  return @found_products
end

#path(k) ⇒ Object

get path of one resource file of currently activated product keys and certs are generated locally… (they are well known values, arch. independent)



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
# File 'lib/aspera/ascp/installation.rb', line 108

def path(k)
  file_is_optional = false
  case k
  when *EXE_FILES
    file_is_optional = k.eql?(:async)
    use_ascp_from_product(FIRST_FOUND) if @path_to_ascp.nil?
    # NOTE: that there might be a .exe at the end
    file = @path_to_ascp.gsub('ascp', k.to_s)
  when :transferd
    file_is_optional = true
    file = Products::Trsdk.transferd_path
  when :ssh_private_dsa, :ssh_private_rsa
    # assume last 3 letters are type
    type = k.to_s[-3..-1].to_sym
    file = check_or_create_sdk_file("aspera_bypass_#{type}.pem") {DataRepository.instance.item(type)}
  when :aspera_license
    file = check_or_create_sdk_file('aspera-license') {DataRepository.instance.item(:license)}
  when :aspera_conf
    file = check_or_create_sdk_file('aspera.conf') {DEFAULT_ASPERA_CONF}
  when :fallback_certificate, :fallback_private_key
    file_key = File.join(Products::Trsdk.sdk_directory, 'aspera_fallback_cert_private_key.pem')
    file_cert = File.join(Products::Trsdk.sdk_directory, 'aspera_fallback_cert.pem')
    if !File.exist?(file_key) || !File.exist?(file_cert)
      require 'openssl'
      # create new self signed certificate for http fallback
      cert = OpenSSL::X509::Certificate.new
      private_key = OpenSSL::PKey::RSA.new(4096)
      WebServerSimple.fill_self_signed_cert(cert, private_key)
      check_or_create_sdk_file('aspera_fallback_cert_private_key.pem', force: true) {private_key.to_pem}
      check_or_create_sdk_file('aspera_fallback_cert.pem', force: true) {cert.to_pem}
    end
    file = k.eql?(:fallback_certificate) ? file_cert : file_key
  else Aspera.error_unexpected_value(k)
  end
  return nil if file_is_optional && !File.exist?(file)
  Aspera.assert(File.exist?(file)){"no such file for #{k}: [#{file}]"}
  return file
end

#sdk_folder=(v) ⇒ Object

Compatibility



72
73
74
# File 'lib/aspera/ascp/installation.rb', line 72

def sdk_folder=(v)
  Products::Trsdk.sdk_directory = v
end

#sdk_locationsObject

Loads YAML from cloud with locations of SDK archives for all platforms

Returns:

  • location structure



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aspera/ascp/installation.rb', line 238

def sdk_locations
  yaml_text = Aspera::Rest.new(
    base_url: TRANSFER_SDK_LOCATION_URL,
    redirect_max: 3).call(
      operation: 'GET',
      headers: {
        'Referer' => "http://version.#{Cli::VERSION}"
      }
    )[:data]
  YAML.load(yaml_text)
end

#sdk_url_for_platform(platform: nil, version: nil) ⇒ Object

Returns the url for download of SDK archive for the given platform and version.

Returns:

  • the url for download of SDK archive for the given platform and version



251
252
253
254
255
256
257
258
259
260
# File 'lib/aspera/ascp/installation.rb', line 251

def sdk_url_for_platform(platform: nil, version: nil)
  locations = sdk_locations
  platform = Environment.architecture if platform.nil?
  locations = locations.select{|l|l['platform'].eql?(platform)}
  raise "No SDK for platform: #{platform}" if locations.empty?
  version = locations.max_by { |entry| Gem::Version.new(entry['version']) }['version'] if version.nil?
  info = locations.select{|entry| entry['version'].eql?(version)}
  raise "No such version: #{version} for #{platform}" if info.empty?
  return info.first['url']
end

#ssh_cert_uuidObject

default bypass key phrase



148
149
150
# File 'lib/aspera/ascp/installation.rb', line 148

def ssh_cert_uuid
  return DataRepository.instance.item(:uuid)
end

#use_ascp_from_product(product_name) ⇒ Object

find ascp in named product (use value : FIRST_FOUND=‘FIRST’ to just use first one) or select one from installed_products()



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/aspera/ascp/installation.rb', line 78

def use_ascp_from_product(product_name)
  if product_name.eql?(FIRST_FOUND)
    pl = installed_products.first
    raise "no Aspera transfer module or SDK found.\nRefer to the manual or install SDK with command:\nascli conf ascp install" if pl.nil?
  else
    pl = installed_products.find{|i|i[:name].eql?(product_name)}
    raise "no such product installed: #{product_name}" if pl.nil?
  end
  self.ascp_path = pl[:ascp_path]
  Log.log.debug{"ascp_path=#{@path_to_ascp}"}
end