class Inspec::Resources::Service

We detect the init system for each operating system, based on the operating system.

Fedora 15 : systemd RedHat 7 : systemd Ubuntu 15.04 : systemd Ubuntu < 15.04 : upstart

TODO: extend the logic to detect the running init system, independently of OS

Attributes

service_ctl[R]

Public Class Methods

new(service_name, service_ctl = nil) click to toggle source
# File lib/inspec/resources/service.rb, line 93
def initialize(service_name, service_ctl = nil)
  @service_name = service_name
  @service_mgmt = nil
  @service_ctl ||= service_ctl
  @cache = nil
  @service_mgmt = select_service_mgmt

  return skip_resource "The `service` resource is not supported on your OS yet." if @service_mgmt.nil?
end

Public Instance Methods

description() click to toggle source

returns the service description from info

# File lib/inspec/resources/service.rb, line 247
def description
  return nil if info.nil?

  info[:description]
end
enabled?(_level = nil) click to toggle source

verifies if the service is enabled

# File lib/inspec/resources/service.rb, line 199
def enabled?(_level = nil)
  return false if info.nil?

  info[:enabled]
end
installed?(_name = nil, _version = nil) click to toggle source

verifies the service is registered

# File lib/inspec/resources/service.rb, line 212
def installed?(_name = nil, _version = nil)
  return false if info.nil?

  info[:installed]
end
name() click to toggle source

returns the service name from info

# File lib/inspec/resources/service.rb, line 240
def name
  return @service_name if info.nil?

  info[:name]
end
params() click to toggle source
# File lib/inspec/resources/service.rb, line 205
def params
  return {} if info.nil?

  Hashie::Mash.new(info[:params] || {})
end
runlevels(*args) click to toggle source

get all runlevels that are available and their configuration

# File lib/inspec/resources/service.rb, line 226
def runlevels(*args)
  return Runlevels.new(self) if info.nil? || info[:runlevels].nil?

  Runlevels.from_hash(self, info[:runlevels], args)
end
running?(_under = nil) click to toggle source

verifies the service is currently running

# File lib/inspec/resources/service.rb, line 219
def running?(_under = nil)
  return false if info.nil?

  info[:running]
end
select_service_mgmt() click to toggle source
# File lib/inspec/resources/service.rb, line 103
def select_service_mgmt # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  os = inspec.os
  platform = os[:name]

  return WindowsSrv.new(inspec) if os.windows?

  # Ubuntu
  # @see: https://wiki.ubuntu.com/SystemdForUpstartUsers
  # Ubuntu 15.04 : Systemd
  #   Systemd runs with PID 1 as /sbin/init.
  #   Upstart runs with PID 1 as /sbin/upstart.
  # Ubuntu < 15.04 : Upstart
  # Upstart runs with PID 1 as /sbin/init.
  # Systemd runs with PID 1 as /lib/systemd/systemd.

  case platform
  when "ubuntu"
    version = os[:release].to_f
    if version < 15.04
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "linuxmint"
    version = os[:release].to_f
    if version < 18
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "debian"
    if os[:release] == "buster/sid"
      version = 10
    else
      version = os[:release].to_i
    end
    if version > 7
      Systemd.new(inspec, service_ctl)
    elsif version > 0
      SysV.new(inspec, service_ctl || "/usr/sbin/service")
    end
  when "redhat", "fedora", "centos", "oracle", "cloudlinux", "scientific"
    version = os[:release].to_i

    systemd = ((platform != "fedora" && version >= 7) ||
               (platform == "fedora" && version >= 15))

    if systemd
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "alibaba"
    if os[:release].to_i >= 3
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "wrlinux"
    SysV.new(inspec, service_ctl)
  when "mac_os_x", "darwin"
    LaunchCtl.new(inspec, service_ctl)
  when "freebsd"
    BSDInit.new(inspec, service_ctl)
  when "arch"
    Systemd.new(inspec, service_ctl)
  when "coreos"
    Systemd.new(inspec, service_ctl)
  when "suse", "opensuse"
    if os[:release].to_i >= 12
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "aix"
    SrcMstr.new(inspec)
  when "amazon"
    if os[:release] =~ /^20\d\d/
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "solaris", "smartos", "omnios", "openindiana", "opensolaris", "nexentacore"
    Svcs.new(inspec)
  when "yocto"
    Systemd.new(inspec, service_ctl)
  end
end
startmode() click to toggle source

returns the service start up mode from info

# File lib/inspec/resources/service.rb, line 254
def startmode
  return nil if info.nil?

  info[:startmode]
end
startname() click to toggle source

returns the service's user from info

# File lib/inspec/resources/service.rb, line 261
def startname
  return nil if info.nil?

  info[:startname]
end
to_s() click to toggle source
# File lib/inspec/resources/service.rb, line 267
def to_s
  "Service #{@service_name}"
end
type() click to toggle source

returns the service type from info

# File lib/inspec/resources/service.rb, line 233
def type
  return nil if info.nil?

  info[:type]
end

Private Instance Methods

info() click to toggle source
# File lib/inspec/resources/service.rb, line 192
def info
  return nil if @service_mgmt.nil?

  @cache ||= @service_mgmt.info(@service_name)
end