Class: PleaseRun::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/pleaserun/detector.rb

Overview

Detect the operating system and version, and based on that information, choose the most appropriate runner platform. TODO(sissel): Make this a module, not a class.

Defined Under Namespace

Classes: UnknownSystem

Constant Summary collapse

MAPPING =

A mapping of of [os, version] => [runner, version]

{
  ["amazon", "2014.09"] => ["upstart", "0.6.5"],
  ["arch", "rolling"] => ["systemd", "default"],
  ["centos", "5"] => ["sysv", "lsb-3.1"],
  ["centos", "6"] => ["upstart", "0.6.5"],
  ["centos", "7"] => ["systemd", "default"],
  ["debian", "6"] => ["sysv", "lsb-3.1"],
  ["debian", "7"] => ["sysv", "lsb-3.1"],
  ["fedora", "18"] => ["systemd", "default"],
  ["fedora", "19"] => ["systemd", "default"],
  ["fedora", "20"] => ["systemd", "default"],
  ["mac_os_x", "10.8"] => ["launchd", "10.9"],
  ["mac_os_x", "10.9"] => ["launchd", "10.9"],
  ["mac_os_x", "10.10"] => ["launchd", "10.9"],
  ["ubuntu", "12.04"] => ["upstart", "1.5"],
  ["ubuntu", "12.10"] => ["upstart", "1.5"],
  ["ubuntu", "13.04"] => ["upstart", "1.5"],
  ["ubuntu", "13.10"] => ["upstart", "1.5"],
  ["ubuntu", "14.04"] => ["upstart", "1.5"]
}

Class Method Summary collapse

Class Method Details

.detectObject

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pleaserun/detector.rb', line 31

def self.detect
  return @system unless @system.nil?

  @logger ||= Cabin::Channel.get
  begin
    platform, version = detect_ohai
  rescue LoadError => e
    @logger.debug("Failed to load ohai", :exception => e)
    begin
      platform, version = detect_facter
    rescue LoadError
      raise UnknownSystem, "Could not detect because neither ohai nor facter libraries are found"
    end
  end

  @system = lookup([platform, version])
  raise UnknownSystem, "#{platform} #{version}" if @system.nil?
  return @system
end

.detect_facterObject

def detect_ohai



68
69
70
71
72
73
74
# File 'lib/pleaserun/detector.rb', line 68

def self.detect_facter
  require "facter"

  platform = Facter.fact["operatingsystem"]
  version = Facter.fact["operatingsystemrelease"]
  return platform, normalize_version(platform, version)
end

.detect_ohaiObject

def self.lookup



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

def self.detect_ohai
  require "ohai/system"
  ohai = Ohai::System.new
  # TODO(sissel): Loading all plugins takes a long time (seconds). 
  # TODO(sissel): Figure out how to load just the platform plugin correctly.
  ohai.all_plugins

  platform = ohai["platform"]
  version = ohai["platform_version"]

  return platform, normalize_version(platform, version)
end

.lookup(platform_and_version) ⇒ Object

def self.detect



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

def self.lookup(platform_and_version)
  return MAPPING[platform_and_version]
end

.normalize_version(platform, version) ⇒ Object

def detect_facter



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pleaserun/detector.rb', line 76

def self.normalize_version(platform, version)
  case platform
    # Take '6.0.8' and make it just '6' since debian never makes major
    # changes in a minor release
    when "debian", "centos"
      return version[/^[0-9]+/] # First digit is the 'major' version
    when "mac_os_x"
      return version[/^[0-9]+\.[0-9]+/] # 10.x
    when "arch"
      return "rolling"
    # TODO(sissel): Any other edge cases?
  end
  return version
end