From 209272713d3e8e5adf0dd6e722e9cd0827a70aa1 Mon Sep 17 00:00:00 2001 From: Hendrik Jäger Date: Tue, 26 Apr 2022 22:06:11 +0200 Subject: puppet provider should not be the default for 'service' resources --- lib/puppet/provider/service/s6.rb | 73 +++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/lib/puppet/provider/service/s6.rb b/lib/puppet/provider/service/s6.rb index 7c265b9..a6745f1 100644 --- a/lib/puppet/provider/service/s6.rb +++ b/lib/puppet/provider/service/s6.rb @@ -1,6 +1,21 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do desc <<~EOT - Service Management with S6 + skarnet.org's small and secure supervision software suite + + The scan directory is the first one found of: + * /etc/s6-scandir/ + + The first matching service definition found in the following directories + is used: + * /etc/s6-services/ + + + This provider supports: + + * start/stop + * enable/disable + * restart + * status EOT commands :s6_svscan => 's6-svscan' @@ -9,6 +24,10 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do commands :s6_svstat => 's6-svstat' class << self + def specificity + return 1 + end + # this is necessary to autodetect a valid resource # default path, since there is no standard for such directory. def defpath @@ -23,22 +42,43 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do def servicedir unless @servicedir ["/etc/s6-scandir"].each do |path| - if Puppet::FileSystem.exist?(path) + if Puppet::FileSystem.exist?(path) && FileTest.directory?(path) @servicedir = path break end end - raise "Could not find service directory" unless @servicedir + raise "Could not find service scan directory" unless @servicedir end @servicedir end + def self.instances + path = self.defpath + unless path + Puppet.info("#{self.name} is unsuitable because service directory is nil") + return + end + unless FileTest.directory?(path) + Puppet.notice "Service path #{path} does not exist" + return + end + + # reject entries that aren't either a directory + # or don't contain an executable run file + Dir.entries(path).reject { |e| + fullpath = File.join(path, e) + e =~ /^\./ or ! FileTest.directory?(fullpath) or ! Puppet::FileSystem.executable?(File.join(fullpath,"run")) + }.collect do |name| + new(:name => name, :path => path) + end + end + def status begin - output = s6_svstat "-u", self.daemon - return :running if output == 'true' + output = s6_svstat "-u", self.service + return :running if output.chomp == 'true' rescue Puppet::ExecutionFailure => detail - unless detail.message =~ /(warning: |runsv not running$)/ + unless detail.message =~ /(warning: |s6-supervise not running$)/ raise Puppet::Error.new( "Could not get status for service #{resource.ref}: #{detail}", detail ) end end @@ -56,15 +96,26 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do 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 enable + if ! FileTest.directory?(self.daemon) + Puppet.notice "No daemon dir, calling setupservice for #{resource[:name]}" + self.setupservice + end + if self.daemon + if ! Puppet::FileSystem.symlink?(self.service) + Puppet.notice "Enabling #{self.service}: linking #{self.daemon} -> #{self.service}" + Puppet::FileSystem.symlink(self.daemon, self.service) + end + end + s6_svscanctl "-a", self.servicedir + rescue Puppet::ExecutionFailure + raise Puppet::Error.new( "No daemon directory found for #{self.service}", $!) + end + def disable # unlink the daemon symlink to disable it Puppet::FileSystem.unlink(self.service) if Puppet::FileSystem.symlink?(self.service) -- cgit v1.2.3