Class: Inspec::Resources::Service
- Inherits:
-
Object
- Object
- Inspec::Resources::Service
- Defined in:
- lib/resources/service.rb
Overview
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
-
#description ⇒ Object
returns the service description from info.
-
#enabled?(_level = nil) ⇒ Boolean
verifies if the service is enabled.
-
#initialize(service_name, service_ctl = nil) ⇒ Service
constructor
A new instance of Service.
-
#installed?(_name = nil, _version = nil) ⇒ Boolean
verifies the service is registered.
-
#name ⇒ Object
returns the service name from info.
- #params ⇒ Object
-
#runlevels(*args) ⇒ Object
get all runlevels that are available and their configuration.
-
#running?(_under = nil) ⇒ Boolean
verifies the service is currently running.
-
#select_service_mgmt ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength.
-
#startmode ⇒ Object
returns the service start up mode from info.
- #to_s ⇒ Object
-
#type ⇒ Object
returns the service type from info.
Constructor Details
#initialize(service_name, service_ctl = nil) ⇒ Service
Returns a new instance of Service.
94 95 96 97 98 99 100 101 102 |
# File 'lib/resources/service.rb', line 94 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.
92 93 94 |
# File 'lib/resources/service.rb', line 92 def service_ctl @service_ctl end |
Instance Method Details
#description ⇒ Object
returns the service description from info
209 210 211 212 |
# File 'lib/resources/service.rb', line 209 def description return nil if info.nil? info[:description] end |
#enabled?(_level = nil) ⇒ Boolean
verifies if the service is enabled
168 169 170 171 |
# File 'lib/resources/service.rb', line 168 def enabled?(_level = nil) return false if info.nil? info[:enabled] end |
#installed?(_name = nil, _version = nil) ⇒ Boolean
verifies the service is registered
179 180 181 182 |
# File 'lib/resources/service.rb', line 179 def installed?(_name = nil, _version = nil) return false if info.nil? info[:installed] end |
#name ⇒ Object
returns the service name from info
203 204 205 206 |
# File 'lib/resources/service.rb', line 203 def name return @service_name if info.nil? info[:name] end |
#params ⇒ Object
173 174 175 176 |
# File 'lib/resources/service.rb', line 173 def params return {} if info.nil? Hashie::Mash.new(info[:params] || {}) end |
#runlevels(*args) ⇒ Object
get all runlevels that are available and their configuration
191 192 193 194 |
# File 'lib/resources/service.rb', line 191 def runlevels(*args) return Runlevels.new(self) if info.nil? or info[:runlevels].nil? Runlevels.from_hash(self, info[:runlevels], args) end |
#running?(_under = nil) ⇒ Boolean
verifies the service is currently running
185 186 187 188 |
# File 'lib/resources/service.rb', line 185 def running?(_under = nil) return false if info.nil? info[:running] end |
#select_service_mgmt ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/resources/service.rb', line 104 def select_service_mgmt # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength os = inspec.os platform = os[:name] # 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?(platform) version = os[:release].to_f if version < 15.04 Upstart.new(inspec, service_ctl) else Systemd.new(inspec, service_ctl) end elsif %w{debian}.include?(platform) version = 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?(platform) version = os[:release].to_i if (%w{ redhat centos }.include?(platform) && version >= 7) || (platform == 'fedora' && version >= 15) Systemd.new(inspec, service_ctl) else SysV.new(inspec, service_ctl || '/sbin/service') end elsif %w{wrlinux}.include?(platform) SysV.new(inspec, service_ctl) elsif %w{mac_os_x}.include?(platform) LaunchCtl.new(inspec, service_ctl) elsif os.windows? WindowsSrv.new(inspec) elsif %w{freebsd}.include?(platform) BSDInit.new(inspec, service_ctl) elsif %w{arch}.include?(platform) Systemd.new(inspec, service_ctl) elsif %w{suse opensuse}.include?(platform) if os[:release].to_i >= 12 Systemd.new(inspec, service_ctl) else SysV.new(inspec, service_ctl || '/sbin/service') end elsif %w{aix}.include?(platform) SrcMstr.new(inspec) elsif %w{amazon}.include?(platform) Upstart.new(inspec, service_ctl) elsif os.solaris? Svcs.new(inspec) end end |
#startmode ⇒ Object
returns the service start up mode from info
215 216 217 218 |
# File 'lib/resources/service.rb', line 215 def startmode return nil if info.nil? info[:startmode] end |
#to_s ⇒ Object
220 221 222 |
# File 'lib/resources/service.rb', line 220 def to_s "Service #{@service_name}" end |
#type ⇒ Object
returns the service type from info
197 198 199 200 |
# File 'lib/resources/service.rb', line 197 def type return nil if info.nil? info[:type] end |