Class: Inspec::Resources::Systemd
- Inherits:
-
ServiceManager
- Object
- ServiceManager
- Inspec::Resources::Systemd
- Defined in:
- lib/inspec/resources/service.rb
Overview
Instance Attribute Summary
Attributes inherited from ServiceManager
Instance Method Summary collapse
- #info(service_name) ⇒ Object
-
#initialize(inspec, service_ctl = nil) ⇒ Systemd
constructor
A new instance of Systemd.
- #is_active?(service_name) ⇒ Boolean
- #is_enabled?(service_name) ⇒ Boolean
Constructor Details
#initialize(inspec, service_ctl = nil) ⇒ Systemd
Returns a new instance of Systemd.
271 272 273 274 |
# File 'lib/inspec/resources/service.rb', line 271 def initialize(inspec, service_ctl = nil) @service_ctl = service_ctl || "systemctl" super end |
Instance Method Details
#info(service_name) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/inspec/resources/service.rb', line 294 def info(service_name) cmd = inspec.command("#{service_ctl} show --no-pager --all #{service_name}") return nil if cmd.exit_status.to_i != 0 # parse data params = SimpleConfig.new( cmd.stdout.chomp, assignment_regex: /^\s*([^=]*?)\s*=\s*(.*?)\s*$/, multiple_values: false ).params # LoadState values eg. loaded, not-found installed = params["LoadState"] == "loaded" { name: params["Id"], description: params["Description"], installed: installed, running: is_active?(service_name), enabled: is_enabled?(service_name), type: "systemd", params: params, } end |
#is_active?(service_name) ⇒ Boolean
290 291 292 |
# File 'lib/inspec/resources/service.rb', line 290 def is_active?(service_name) inspec.command("#{service_ctl} is-active #{service_name} --quiet").exit_status == 0 end |
#is_enabled?(service_name) ⇒ Boolean
276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/inspec/resources/service.rb', line 276 def is_enabled?(service_name) result = inspec.command("#{service_ctl} is-enabled #{service_name} --quiet") return true if result.exit_status == 0 # Some systems may not have a `.service` file for a particular service # which causes the `systemctl is-enabled` check to fail despite the # service being enabled. In that event we fallback to `sysv_service`. if result.stderr =~ /Failed to get.*No such file or directory/ return inspec.sysv_service(service_name).enabled? end false end |