Class: Service
- Inherits:
-
Object
- Object
- Service
- Defined in:
- lib/resources/service.rb
Overview
Usage: describe service(‘dhcp’) do
it { should be_enabled }
it { should be_installed }
it { should be_running }
end
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
Direct Known Subclasses
BSDService, LaunchdService, RunitService, SysVService, SystemdService, UpstartService
Instance Attribute Summary collapse
-
#service_ctl ⇒ Object
readonly
Returns the value of attribute service_ctl.
Instance Method Summary collapse
-
#enabled?(_level = nil) ⇒ Boolean
verifies the service is enabled.
- #info ⇒ Object
-
#initialize(service_name, service_ctl = nil) ⇒ Service
constructor
A new instance of Service.
-
#installed?(_name = nil, _version = nil) ⇒ Boolean
verifies the service is registered.
-
#running?(_under = nil) ⇒ Boolean
verifies the service is currently running.
-
#select_service_mgmt ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #to_s ⇒ Object
Constructor Details
#initialize(service_name, service_ctl = nil) ⇒ Service
Returns a new instance of Service.
36 37 38 39 40 41 42 43 44 |
# File 'lib/resources/service.rb', line 36 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 |
Instance Attribute Details
#service_ctl ⇒ Object (readonly)
Returns the value of attribute service_ctl.
34 35 36 |
# File 'lib/resources/service.rb', line 34 def service_ctl @service_ctl end |
Instance Method Details
#enabled?(_level = nil) ⇒ Boolean
verifies the service is enabled
102 103 104 105 |
# File 'lib/resources/service.rb', line 102 def enabled?(_level = nil) return false if info.nil? info[:enabled] end |
#info ⇒ Object
96 97 98 99 |
# File 'lib/resources/service.rb', line 96 def info return nil if @service_mgmt.nil? @cache ||= @service_mgmt.info(@service_name) end |
#installed?(_name = nil, _version = nil) ⇒ Boolean
verifies the service is registered
108 109 110 111 |
# File 'lib/resources/service.rb', line 108 def installed?(_name = nil, _version = nil) return false if info.nil? info[:installed] end |
#running?(_under = nil) ⇒ Boolean
verifies the service is currently running
114 115 116 117 |
# File 'lib/resources/service.rb', line 114 def running?(_under = nil) return false if info.nil? info[:running] end |
#select_service_mgmt ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
46 47 48 49 50 51 52 53 54 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 |
# File 'lib/resources/service.rb', line 46 def select_service_mgmt # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity os = inspec.os family = os[:family] # 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. if %w{ubuntu}.include?(family) version = inspec.os[:release].to_f if version < 15.04 Upstart.new(inspec, service_ctl) else Systemd.new(inspec, service_ctl) end elsif %w{debian}.include?(family) version = inspec.os[:release].to_i if version > 7 Systemd.new(inspec, service_ctl) else SysV.new(inspec, service_ctl || '/usr/sbin/service') end elsif %w{redhat fedora centos}.include?(family) version = inspec.os[:release].to_i if (%w{ redhat centos }.include?(family) && version >= 7) || (family == 'fedora' && version >= 15) Systemd.new(inspec, service_ctl) else SysV.new(inspec, service_ctl || '/sbin/service') end elsif %w{wrlinux}.include?(family) SysV.new(inspec, service_ctl) elsif %w{darwin}.include?(family) LaunchCtl.new(inspec, service_ctl) elsif os.windows? WindowsSrv.new(inspec) elsif %w{freebsd}.include?(family) BSDInit.new(inspec, service_ctl) elsif %w{arch opensuse}.include?(family) Systemd.new(inspec, service_ctl) elsif %w{aix}.include?(family) SrcMstr.new(inspec) elsif os.solaris? Svcs.new(inspec) end end |
#to_s ⇒ Object
119 120 121 |
# File 'lib/resources/service.rb', line 119 def to_s "Service #{@service_name}" end |