Module: PleaseRun::Detector

Defined in:
lib/pleaserun/detector.rb

Overview

Detect the service platform that’s most likely to be successful on the running machine.

See the ‘detect` method.

Defined Under Namespace

Classes: UnknownSystem

Class Method Summary collapse

Class Method Details

.detectObject

Raises:



14
15
16
17
18
19
20
21
# File 'lib/pleaserun/detector.rb', line 14

def detect
  return @system unless @system.nil?

  @logger ||= Cabin::Channel.get
  @system = detect_platform
  raise UnknownSystem, "Unable to detect which service platform to use" if @system.nil?
  return @system
end

.detect_launchdObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/pleaserun/detector.rb', line 56

def detect_launchd
  return false unless File.directory?("/Library/LaunchDaemons")

  out, success = execute(["launchctl", "version"])
  return false unless success

  # TODO(sissel): Version?
  version = out.split("\n").first.split(":").first.split(/\s+/).last
  ["launchd", version]
end

.detect_platformObject

def self.detect



23
24
25
# File 'lib/pleaserun/detector.rb', line 23

def detect_platform
  detect_systemd || detect_upstart || detect_launchd || detect_runit || detect_sysv
end

.detect_runitObject



67
68
69
70
71
# File 'lib/pleaserun/detector.rb', line 67

def detect_runit
  return false unless File.directory?("/service")

  # TODO(sissel): Do more tests for runit
end

.detect_systemdObject



27
28
29
30
31
32
33
34
35
# File 'lib/pleaserun/detector.rb', line 27

def detect_systemd
  # Check the version. If `systemctl` fails, systemd isn't available.
  out, success = execute([ "systemctl", "--version" ])
  return false unless success

  # version is the last word on the first line of the --version output
  version = out.split("\n").first.split(/\s+/).last
  ["systemd", version]
end

.detect_sysvObject



49
50
51
52
53
54
# File 'lib/pleaserun/detector.rb', line 49

def detect_sysv
  return false unless File.directory?("/etc/init.d")

  # TODO(sissel): Do more specific testing.
  ["sysv", "lsb-3.1"]
end

.detect_upstartObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pleaserun/detector.rb', line 37

def detect_upstart
  # Expect a certain directory
  return false unless File.directory?("/etc/init")

  # Check the version. If `initctl` fails, upstart isn't available.
  out, success = execute(["initctl", "--version"])
  return false unless success

  version = out.split("\n").first.tr("()", "").split(/\s+/).last
  ["upstart", version]
end

.execute(command) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pleaserun/detector.rb', line 73

def execute(command)
  Open3.popen3(*command) do |stdin, stdout, stderr, wait_thr|
    stdin.close
    out = stdout.read
    stderr.close
    exit_status = wait_thr.value
    return out, exit_status.success?
  end
rescue Errno::ENOENT, Errno::EACCES, IOError => e
  # If the path doesn't exist or we cannot execute it, return the exception
  # message as the output and indicate a failure to run.
  return e.message, false
end