Class: Aspera::Plugins::Transferd::Service::Host

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

Overview

Access to the system: root folder, commands and network. Replaceable in tests.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root: '/', runner: nil, listening: nil, timeout: 30, os: nil, euid: nil, systemctl: nil) ⇒ Host

Returns a new instance of Host.

Parameters:

  • root (String) (defaults to: '/') —

    Root folder of the file system

  • runner (#call) (defaults to: nil) —

    Receives the command words, returns [output, success], default: run the command

  • listening (#call) (defaults to: nil) —

    Receives address and port, returns true if a server listens, default: try to connect

  • timeout (Numeric) (defaults to: 30) —

    Seconds to wait for the daemon to listen

  • os (Symbol) (defaults to: nil) —

    Operating system, default: the current one

  • euid (Integer) (defaults to: nil) —

    Effective user id, default: the one of the process

  • systemctl (Boolean) (defaults to: nil) —

    true if systemctl is available, default: look for it in PATH



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aspera/plugins/transferd/service.rb', line 67

def initialize(root: '/', runner: nil, listening: nil, timeout: 30, os: nil, euid: nil, systemctl: nil)
  @root = root
  @os = os || Environment.instance.os
  @euid = euid || ::Process.euid
  @systemctl = systemctl.nil? ? ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).any? { |dir| File.executable?(File.join(dir, 'systemctl')) } : systemctl
  @runner = runner || lambda do |*cmd|
    out, err, status = Environment.secure_execute(*cmd, mode: :capture, exception: false)
    [out.to_s + err.to_s, status.success?]
  end
  @listening = listening || lambda do |address, port|
    Socket.tcp(address, port, connect_timeout: 1) { true }
  rescue SystemCallError, IOError
    false
  end
  @timeout = timeout
end

Instance Attribute Details

#euid ⇒ String, ... (readonly)

Returns:

  • (String) —

    Root folder: all files are written below it (/ for a real installation)

  • (Numeric) —

    Seconds to wait for the daemon to listen

  • (Symbol) —

    Operating system, see Environment::OS_*

  • (Integer) —

    Effective user id of the process



58
59
60
# File 'lib/aspera/plugins/transferd/service.rb', line 58

def euid
  @euid
end

#os ⇒ String, ... (readonly)

Returns:

  • (String) —

    Root folder: all files are written below it (/ for a real installation)

  • (Numeric) —

    Seconds to wait for the daemon to listen

  • (Symbol) —

    Operating system, see Environment::OS_*

  • (Integer) —

    Effective user id of the process



58
59
60
# File 'lib/aspera/plugins/transferd/service.rb', line 58

def os
  @os
end

#root ⇒ String, ... (readonly)

Returns:

  • (String) —

    Root folder: all files are written below it (/ for a real installation)

  • (Numeric) —

    Seconds to wait for the daemon to listen

  • (Symbol) —

    Operating system, see Environment::OS_*

  • (Integer) —

    Effective user id of the process



58
59
60
# File 'lib/aspera/plugins/transferd/service.rb', line 58

def root
  @root
end

#timeout ⇒ String, ... (readonly)

Returns:

  • (String) —

    Root folder: all files are written below it (/ for a real installation)

  • (Numeric) —

    Seconds to wait for the daemon to listen

  • (Symbol) —

    Operating system, see Environment::OS_*

  • (Integer) —

    Effective user id of the process



58
59
60
# File 'lib/aspera/plugins/transferd/service.rb', line 58

def timeout
  @timeout
end

Instance Method Details

#listening?(address, port) ⇒ Boolean

Returns true if a server listens on this address and port.

Returns:

  • (Boolean) —

    true if a server listens on this address and port



97
98
99
# File 'lib/aspera/plugins/transferd/service.rb', line 97

def listening?(address, port)
  @listening.call(address, port)
end

#path(path) ⇒ String

Returns Path below the root folder.

Parameters:

  • path (String) —

    Absolute path in the real file system

Returns:

  • (String) —

    Path below the root folder



86
87
88
# File 'lib/aspera/plugins/transferd/service.rb', line 86

def path(path)
  File.join(@root, path)
end

#run(*cmd) ⇒ Array(String, Boolean)

Returns Output and success.

Parameters:

  • cmd (Array<String>) —

    Command to run

Returns:

  • (Array(String, Boolean)) —

    Output and success



92
93
94
# File 'lib/aspera/plugins/transferd/service.rb', line 92

def run(*cmd)
  @runner.call(*cmd)
end

#systemctl? ⇒ Boolean

Returns true if systemctl is available.

Returns:

  • (Boolean) —

    true if systemctl is available



102
103
104
# File 'lib/aspera/plugins/transferd/service.rb', line 102

def systemctl?
  @systemctl
end