Class: Aspera::Plugins::Transferd::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/plugins/transferd/product.rb

Overview

A Transfer Daemon (transferd) started by this process on a local address, with the SDK of Products::Transferd.sdk_directory: configuration file and logs in the global temporary folder.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address:, port:, api_key:, stop:) ⇒ Product

Start the daemon, and wait for its initialization

Parameters:

  • address (String) —

    Local address the daemon listens on

  • port (Integer) —

    Port the daemon listens on, 0: a random available port (see listen_port)

  • api_key (String, nil) —

    API key given by the user (see daemon_api_key)

  • stop (Boolean) —

    false: the daemon is left running on exit

Raises:

  • (RuntimeError) —

    The daemon exited during its initialization



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
100
101
102
103
104
105
# File 'lib/aspera/plugins/transferd/product.rb', line 55

def initialize(address:, port:, api_key:, stop:)
  # The daemon listens on a local port: clients must give its API key, so that other users of the host cannot use it
  @api_key = self.class.daemon_api_key(api_key, stop: stop)
  # create a config file for daemon
  config = {
    address:      address,
    port:         port,
    fasp_runtime: {
      use_embedded: false,
      user_defined: {
        bin: Products::Transferd.sdk_directory,
        etc: Products::Transferd.sdk_directory
      }
    }
  }
  config[:authentication] = {enabled: true, api_key: @api_key} unless @api_key.nil?
  # config file and logs are created in same folder
  transferd_base_tmp = TempFileManager.instance.new_file_path_global('transferd')
  Log.log.debug { "transferd base tmp #{transferd_base_tmp}" }
  conf_file = "#{transferd_base_tmp}.conf"
  @log_stdout = "#{transferd_base_tmp}.out"
  @log_stderr = "#{transferd_base_tmp}.err"
  # Folder of the port file of a daemon started with port zero (see `listen_port`): of this daemon only (the default is shared),
  # created here, readable only by the user
  @runtime_dir = nil
  if port.zero?
    @runtime_dir = "#{transferd_base_tmp}.run"
    Dir.mkdir(@runtime_dir, 0o700)
    config[:runtime_dir] = @runtime_dir
  end
  self.class.write_config(conf_file, config)
  @pid = Environment.secure_execute(
    Ascp::Installation.instance.path(:transferd), '--config', conf_file,
    mode: :background,
    out: @log_stdout,
    err: @log_stderr
  )
  begin
    # wait for process to initialize, max 2 seconds
    Timeout.timeout(2.0) do
      # this returns if process dies (within 2 seconds)
      _, status = ::Process.wait2(@pid)
      remove_runtime_dir
      raise "Transfer daemon exited with status #{status.exitstatus}. Check files: #{log_files.join(' and ')}"
    end
  rescue Timeout::Error
    nil
  end
  Log.log.debug { "Daemon started with pid #{@pid}" }
  ::Process.detach(@pid) unless stop
end

Instance Attribute Details

#api_key ⇒ String? (readonly)

Returns API key of the daemon, nil: no authentication.

Returns:

  • (String, nil) —

    API key of the daemon, nil: no authentication



110
111
112
# File 'lib/aspera/plugins/transferd/product.rb', line 110

def api_key
  @api_key
end

#pid ⇒ Integer? (readonly)

Returns PID of the daemon, nil once stopped.

Returns:

  • (Integer, nil) —

    PID of the daemon, nil once stopped



108
109
110
# File 'lib/aspera/plugins/transferd/product.rb', line 108

def pid
  @pid
end

Class Method Details

.daemon_api_key(api_key, stop:) ⇒ String?

API key of a daemon started here

Parameters:

  • api_key (String, nil) —

    API key given by the user

  • stop (Boolean) —

    true if the daemon is stopped on exit

Returns:

  • (String, nil) —

    The key given, else a random one if the daemon is stopped on exit, else nil (no authentication): the next executions that attach to the daemon left running cannot know a random key



34
35
36
37
38
39
# File 'lib/aspera/plugins/transferd/product.rb', line 34

def daemon_api_key(api_key, stop:)
  return api_key unless api_key.nil?
  return SecureRandom.hex(API_KEY_BYTES) if stop
  Log.log.warn('Daemon left running (stop: false) without authentication: other users of the host can use it. Set parameter api_key.')
  nil
end

.write_config(path, config) ⇒ Object

Write the configuration file of a daemon started here: created only by this call, readable only by the user (it may contain the API key)

Parameters:

  • path (String) —

    Path of the file, in the global temporary folder

  • config (Hash) —

    Configuration of the daemon



44
45
46
# File 'lib/aspera/plugins/transferd/product.rb', line 44

def write_config(path, config)
  File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |file| file.write(config.to_json) }
end

Instance Method Details

#listen_port ⇒ Integer?

Returns Port the daemon started with port 0 listens on: from its port file (transferd 1.1.7 and later), else from its log; nil: it does not listen yet.

Returns:

  • (Integer, nil) —

    Port the daemon started with port 0 listens on: from its port file (transferd 1.1.7 and later), else from its log; nil: it does not listen yet

Raises:

  • (AssertError) —

    Not started with port zero, or port file not valid: retrying does not help



120
121
122
123
124
# File 'lib/aspera/plugins/transferd/product.rb', line 120

def listen_port
  Aspera.assert(!@runtime_dir.nil?) { 'Daemon not started with port zero' }
  # Written before the log
  Products::Transferd.daemon_port_from_file(@runtime_dir) || Products::Transferd.daemon_port_from_log(@log_stdout)
end

#log_files ⇒ Array(String, String)

Returns Files of the standard output and error of the daemon.

Returns:

  • (Array(String, String)) —

    Files of the standard output and error of the daemon



113
114
115
# File 'lib/aspera/plugins/transferd/product.rb', line 113

def log_files
  [@log_stdout, @log_stderr]
end

#stop ⇒ Object

Stop the daemon, if not stopped yet



127
128
129
130
131
132
133
134
135
# File 'lib/aspera/plugins/transferd/product.rb', line 127

def stop
  return if @pid.nil?
  Log.log.debug("Stopping daemon #{@pid}")
  ::Process.kill(:KILL, @pid)
  _, status = ::Process.wait2(@pid)
  Log.log.debug("daemon stopped #{status}")
  @pid = nil
  remove_runtime_dir
end