Skip to content

Instantly share code, notes, and snippets.

@leepa
Created April 30, 2015 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leepa/9818b46b29b161413824 to your computer and use it in GitHub Desktop.
Save leepa/9818b46b29b161413824 to your computer and use it in GitHub Desktop.
Jessie 'Service' Provider for Puppet - it will work around https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=705254 to ensure services don't keep getting enabled.
Puppet::Type.type(:service).provide :jessie, :parent => :systemd do
desc "Manages `systemd` services using `systemctl` on jessie."
defaultfor :osfamily => :debian, :operatingsystem => :debian, :operatingsystemmajrelease => "8"
def get_start_link_count
Dir.glob("/etc/rc*.d/S??#{@resource[:name]}").length
end
def enabled?
begin
systemctl_info = systemctl(
'show',
@resource[:name],
'--property', 'LoadState',
'--property', 'UnitFileState',
'--no-pager'
)
svc_info = Hash.new
systemctl_info.split.each do |svc|
entry_pair = svc.split('=')
svc_info[entry_pair.first.to_sym] = entry_pair.last
end
# The masked state is equivalent to the disabled state in terms of
# comparison so we only care to check if it is masked if we want to keep
# it masked.
#
# We only return :mask if we're trying to mask the service. This prevents
# flapping when simply trying to disable a masked service.
return :mask if (@resource[:enable] == :mask) && (svc_info[:LoadState] == 'masked')
return :true if svc_info[:UnitFileState] == 'enabled'
# If UnitFileState == UnitFileState then we query the older way.
if svc_info[:UnitFileState] == 'UnitFileState'
system("/usr/sbin/invoke-rc.d", "--quiet", "--query", @resource[:name], "start")
if [104, 106].include?($CHILD_STATUS.exitstatus)
return :true
elsif [101, 105].include?($CHILD_STATUS.exitstatus)
# 101 is action not allowed, which means we have to do the check manually.
# 105 is unknown, which generally means the iniscript does not support query
# The debian policy states that the initscript should support methods of query
# For those that do not, peform the checks manually
# http://www.debian.org/doc/debian-policy/ch-opersys.html
if get_start_link_count >= 4
return :true
else
return :false
end
else
return :false
end
end
rescue Puppet::ExecutionFailure
# The execution of the systemd command can fail for quite a few reasons.
# In all of these cases, the failure of the query indicates that the
# service is disabled and therefore we simply return :false.
end
return :false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment