Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use systemctl's helper command to determine enabled & active status #863

Merged
merged 1 commit into from
Aug 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions lib/resources/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ def initialize(inspec, service_ctl = nil)
super
end

def is_enabled?(service_name)
inspec.command("#{service_ctl} is-enabled #{service_name} --quiet").exit_status == 0
end

def is_active?(service_name)
inspec.command("#{service_ctl} is-active #{service_name} --quiet").exit_status == 0
end

def info(service_name)
cmd = inspec.command("#{service_ctl} show --all #{service_name}")
return nil if cmd.exit_status.to_i != 0
Expand All @@ -246,20 +254,13 @@ def info(service_name)

# LoadState values eg. loaded, not-found
installed = params['LoadState'] == 'loaded'
# test via 'systemctl is-active service'
# SubState values running
running = (params['ActiveState'] == 'active') ||
(params['SubState'] == 'running')
# test via systemctl --quiet is-enabled
# ActiveState values eg.g inactive, active
enabled = %w{enabled static}.include? params['UnitFileState']

{
name: params['Id'],
description: params['Description'],
installed: installed,
running: running,
enabled: enabled,
running: is_active?(service_name),
enabled: is_enabled?(service_name),
type: 'systemd',
params: params,
}
Expand Down