From 3a3b5bae2e05a9546f6bb32bff21db2bc66e9377 Mon Sep 17 00:00:00 2001 From: Hendrik Jäger Date: Mon, 25 Apr 2022 23:34:49 +0200 Subject: feat: act as a provider --- lib/puppet/provider/service/s6.rb | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/puppet/provider/service/s6.rb (limited to 'lib') diff --git a/lib/puppet/provider/service/s6.rb b/lib/puppet/provider/service/s6.rb new file mode 100644 index 0000000..7c265b9 --- /dev/null +++ b/lib/puppet/provider/service/s6.rb @@ -0,0 +1,73 @@ +Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do + desc <<~EOT + Service Management with S6 + EOT + + commands :s6_svscan => 's6-svscan' + commands :s6_svscanctl => 's6-svscanctl' + commands :s6_svc => 's6-svc' + commands :s6_svstat => 's6-svstat' + + class << self + # this is necessary to autodetect a valid resource + # default path, since there is no standard for such directory. + def defpath + @defpath ||= ["/etc/s6-services"].find do |path| + Puppet::FileSystem.exist?(path) && FileTest.directory?(path) + end + @defpath + end + end + + # find the service dir on this node + def servicedir + unless @servicedir + ["/etc/s6-scandir"].each do |path| + if Puppet::FileSystem.exist?(path) + @servicedir = path + break + end + end + raise "Could not find service directory" unless @servicedir + end + @servicedir + end + + def status + begin + output = s6_svstat "-u", self.daemon + return :running if output == 'true' + rescue Puppet::ExecutionFailure => detail + unless detail.message =~ /(warning: |runsv not running$)/ + raise Puppet::Error.new( "Could not get status for service #{resource.ref}: #{detail}", detail ) + end + end + :stopped + end + + def stop + s6_svc "-d", self.service + end + + def restart + s6_svc "-r", self.service + end + + def start + if enabled? != :true + enable + s6_svscanctl "-a", self.servicedir + end + s6_svc "-u", self.service + end + + # disable by removing the symlink so that runit + # doesn't restart our service behind our back + # note that runit doesn't need to perform a stop + # before a disable + def disable + # unlink the daemon symlink to disable it + Puppet::FileSystem.unlink(self.service) if Puppet::FileSystem.symlink?(self.service) + s6_svscanctl "-n", self.servicedir + end +end -- cgit v1.2.3