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: sdk_folder= method

Constant Summary collapse

CLIENT_SSH_KEY_OPTIONS =

options for SSH client private key

i{dsa_rsa rsa per_client}.freeze
USE_PRODUCT_PREFIX =

prefix

'product:'
FIRST_FOUND =

policy for product selection

'FIRST'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transferd_urlsObject

Returns the value of attribute transferd_urls.



337
338
339
# File 'lib/aspera/ascp/installation.rb', line 337

def transferd_urls
  @transferd_urls
end

Instance Method Details

#ascp_infoObject

information for ‘ascp info`



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

def ascp_info
  ascp_data = file_paths
  ascp_data.merge!(ascp_info_from_log)
  ascp_data.merge!(ascp_info_from_file)
  return ascp_data
end

#ascp_info_from_fileObject

Extract some stings from ascp binary Openssl information



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

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

#ascp_info_from_logObject

Extract some stings from ascp logs Folder, PVCL, version, license information



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

def ascp_info_from_log
  data = {}
  # read PATHs from ascp directly, and pvcl modules as well
  Open3.popen3(path(:ascp), '-DDL-') do |_stdin, _stdout, stderr, thread|
    last_line = ''
    while (line = stderr.gets)
      line.chomp!
      # skip lines that may have accents
      next unless line.valid_encoding?
      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['ascp_version'] = Regexp.last_match(1)
      end
    end
    raise last_line if !thread.value.exitstatus.eql?(1) && !data.key?('root')
  end
  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



149
150
151
152
153
154
155
156
157
# File 'lib/aspera/ascp/installation.rb', line 149

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
           Aspera.error_not_implemented
         end
end

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

TODO: if using another product than SDK, should use files from there



99
100
101
102
# File 'lib/aspera/ascp/installation.rb', line 99

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

#extract_archive_files(sdk_archive_path) ⇒ Object

Parameters:

  • &block

    called with entry information



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/aspera/ascp/installation.rb', line 247

def extract_archive_files(sdk_archive_path)
  Aspera.assert(block_given?){'missing block'}
  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?('/')
        entry.get_input_stream do |io|
          yield(entry.name, io, nil)
        end
      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



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/aspera/ascp/installation.rb', line 85

def file_paths
  return SDK_FILES.each_with_object({}) do |v, m|
    m[v.to_s] =
      begin
        path(v)
      rescue Errno::ENOENT => e
        e.message.gsub(/.*assertion failed: /, '').gsub(/\): .*/, ')')
      rescue => e
        e.message
      end
  end
end

#get_ascp_version(exe_path) ⇒ Object

use in plugin config



160
161
162
# File 'lib/aspera/ascp/installation.rb', line 160

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



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/aspera/ascp/installation.rb', line 165

def get_exe_version(exe_path, vers_arg)
  Aspera.assert_type(exe_path, String)
  Aspera.assert_type(vers_arg, String)
  return 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, version: nil, folder: nil, backup: true, with_exe: true, &block) ⇒ Array

Retrieves ascp binary for current system architecture from URL or file

Parameters:

  • url (String) (defaults to: nil)

    URL to SDK archive, or SpecialValues::DEF

  • folder (String) (defaults to: nil)

    Destination folder path

  • backup (Boolean) (defaults to: true)

    If destination folder exists, then rename

  • with_exe (Boolean) (defaults to: true)

    If false, only retrieves files, but do not generate or restrict access

  • &block (Proc)

    A lambda that receives a file path from archive and tells destination sub folder(end with /) or file, or nil to not extract

Returns:

  • (Array)

    name, ascp version (from execution), folder



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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
# File 'lib/aspera/ascp/installation.rb', line 286

def install_sdk(url: nil, version: nil, folder: nil, backup: true, with_exe: true, &block)
  url = sdk_url_for_platform(version: version) if url.nil? || url.eql?('DEF')
  folder = Products::Transferd.sdk_directory if folder.nil?
  subfolder_lambda = block
  if subfolder_lambda.nil?
    # default files to extract directly to main folder if in selected source folders
    subfolder_lambda = ->(name) do
      Products::Transferd::RUNTIME_FOLDERS.any?{ |i| name.match?(%r{^[^/]*/#{i}/})} ? '/' : nil
    end
  end
  FileUtils.mkdir_p(folder)
  # 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
  sdk_archive_path = UriReader.read_as_file(url)
  extract_archive_files(sdk_archive_path) do |entry_name, entry_stream, link_target|
    dest_folder = subfolder_lambda.call(entry_name)
    next if dest_folder.nil?
    dest_folder = File.join(folder, dest_folder)
    if dest_folder.end_with?('/')
      dest_file = File.join(dest_folder, File.basename(entry_name))
    else
      dest_file = dest_folder
      dest_folder = File.dirname(dest_file)
    end
    FileUtils.mkdir_p(dest_folder)
    if link_target.nil?
      File.open(dest_file, 'wb'){ |output_stream| IO.copy_stream(entry_stream, output_stream)}
    else
      File.symlink(link_target, dest_file)
    end
  end
  return unless with_exe
  # Ensure necessary files are there, or generate them
  SDK_FILES.each do |file_id_sym|
    file_path = path(file_id_sym)
    if file_path && EXE_FILES.include?(file_id_sym)
      Environment.restrict_file_access(file_path, mode: 0o755) if File.exist?(file_path)
    end
  end
  sdk_ascp_version = get_ascp_version(path(:ascp))
  transferd_version = get_exe_version(path(:transferd), '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, folder
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



368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/aspera/ascp/installation.rb', line 368

def installed_products
  return @found_products unless @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::Transferd.locations +
    Products::Desktop.locations +
    Products::Connect.locations +
    Products::Other::LOCATION_ON_THIS_OS
  # search installed products: with ascp
  @found_products = Products::Other.find(scan_locations)
end

#path(k) ⇒ String?

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

Parameters:

  • k (Symbol)

    key of the resource file

Returns:

  • (String, nil)

    Full path to the resource file or nil if not found



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

def path(k)
  file_is_required = true
  case k
  when *EXE_FILES
    file_is_required = k.eql?(:ascp)
    file = Products::Transferd.transferd_path
    file = File.join(File.dirname(file), Environment.instance.exe_file(k.to_s)) unless k.eql?(:transferd)
  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::Transferd.sdk_directory, 'aspera_fallback_cert_private_key.pem')
    file_cert = File.join(Products::Transferd.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
      private_key = OpenSSL::PKey::RSA.new(4096)
      cert = WebServerSimple.self_signed_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 unless file_is_required || File.exist?(file)
  Aspera.assert(File.exist?(file), type: Errno::ENOENT){"#{k} not found (#{file})"}
  return file
end

#sdk_folderObject



80
81
82
# File 'lib/aspera/ascp/installation.rb', line 80

def sdk_folder
  path(:ascp)
end

#sdk_folder=(ascp_location) ⇒ Object

Set ascp executable “location” It can be:

  • Full path to folder where ascp executable is located

  • “product:PRODUCT_NAME” to use ascp from named product

  • “product:FIRST” to use ascp from first found product



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aspera/ascp/installation.rb', line 58

def sdk_folder=(ascp_location)
  Aspera.assert_type(ascp_location, String){'ascp_location'}
  Aspera.assert(!ascp_location.empty?){'ascp location cannot be empty: check your config file'}
  folder =
    if ascp_location.start_with?(USE_PRODUCT_PREFIX)
      product_name = ascp_location[USE_PRODUCT_PREFIX.length..-1]
      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 transferd 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
      File.dirname(pl[:ascp_path])
    else
      ascp_location.include?('/ascp') ? File.dirname(ascp_location) : ascp_location
    end
  Log.log.debug{"ascp_folder=#{folder}"}
  Products::Transferd.sdk_directory = folder
  return
end

#sdk_locationsObject

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

Returns:

  • location structure



42
43
44
45
46
47
48
49
50
51
# File 'lib/aspera/ascp/installation.rb', line 42

def sdk_locations
  location_url = @transferd_urls
  transferd_locations = UriReader.read(location_url)
  Log.log.debug{"Retrieving SDK locations from #{location_url}"}
  begin
    return YAML.load(transferd_locations)
  rescue Psych::SyntaxError
    raise "Error when parsing yaml data from: #{location_url}"
  end
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



235
236
237
238
239
240
241
242
243
244
# File 'lib/aspera/ascp/installation.rb', line 235

def sdk_url_for_platform(platform: nil, version: nil)
  all_locations = sdk_locations
  platform = Environment.instance.architecture if platform.nil?
  locations = all_locations.select{ |l| l['platform'].eql?(platform)}
  raise "No SDK for platform: #{platform}, available: #{all_locations.map{ |i| i['platform']}.uniq}" 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



143
144
145
# File 'lib/aspera/ascp/installation.rb', line 143

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