Merge pull request #228 from chef/upstart-systemv

Upstart with System V fallback
This commit is contained in:
Dominik Richter 2015-11-13 10:59:34 +01:00
commit f431ece79c
12 changed files with 50 additions and 22 deletions

View file

@ -149,40 +149,46 @@ end
class Upstart < ServiceManager
def info(service_name)
# get the status of upstart service
cmd = inspec.command("initctl status #{service_name}")
return nil if cmd.exit_status != 0
status = inspec.command("initctl status #{service_name}")
# fallback for systemv services, those are not handled via `initctl`
return SysV.new(inspec).info(service_name) if status.exit_status.to_i != 0
# @see: http://upstart.ubuntu.com/cookbook/#job-states
# grep for running to indicate the service is there
match_running = /running/.match(cmd.stdout)
!match_running.nil? ? (running = true) : (running = false)
# check if a service is enabled
# http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled
# $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled
# Ubuntu 10.04 show-config is not supported
# @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html
config = inspec.command("initctl show-config #{service_name}")
match_enabled = /^\s*start on/.match(config.stdout)
!match_enabled.nil? ? (enabled = true) : (enabled = false)
# implement fallback for Ubuntu 10.04
if inspec.os[:family] == 'ubuntu' &&
inspec.os[:release].to_f >= 10.04 &&
inspec.os[:release].to_f < 12.04 &&
cmd.exit_status == 0
enabled = true
end
running = !status.stdout[/running/].nil?
{
name: service_name,
description: nil,
installed: true,
running: running,
enabled: enabled,
enabled: info_enabled(status, service_name),
type: 'upstart',
}
end
private
def info_enabled(status, service_name)
# check if a service is enabled
# http://upstart.ubuntu.com/cookbook/#determine-if-a-job-is-disabled
# $ initctl show-config $job | grep -q "^ start on" && echo enabled || echo disabled
# Ubuntu 10.04 show-config is not supported
# @see http://manpages.ubuntu.com/manpages/maverick/man8/initctl.8.html
config = inspec.command("initctl show-config #{service_name}")
enabled = !config.stdout[/^\s*start on/].nil?
# implement fallback for Ubuntu 10.04
if inspec.os[:family] == 'ubuntu' &&
inspec.os[:release].to_f >= 10.04 &&
inspec.os[:release].to_f < 12.04 &&
status.exit_status == 0
enabled = true
end
enabled
end
end
class SysV < ServiceManager

View file

@ -7,3 +7,4 @@
include_recipe('os_prepare::apt')
include_recipe('os_prepare::file')
include_recipe('os_prepare::package')
include_recipe('os_prepare::service')

View file

@ -0,0 +1,12 @@
# encoding: utf-8
# author: Christoph Hartmann
# author: Dominik Richter
#
# prepares services
# install ntp as a service on ubuntu
case node['platform']
when 'ubuntu'
include_recipe('apt')
package 'ntp'
end

View file

@ -26,3 +26,12 @@ describe service(available_service) do
it { should be_installed }
it { should be_running }
end
# extra test for ubuntu upstart with systemv service
if os[:family] == 'ubuntu'
describe service('ntp') do
it { should be_enabled }
it { should be_installed }
it { should be_running }
end
end