diff options
author | Hendrik Jäger <gitcommit@henk.geekmail.org> | 2022-05-22 12:52:05 +0200 |
---|---|---|
committer | Hendrik Jäger <gitcommit@henk.geekmail.org> | 2022-07-25 23:53:04 +0200 |
commit | 630c0ffabfd003113daadd580b6b030db21ef432 (patch) | |
tree | 96bcc8a41956f6da983c433fb5214afe15b8ec95 | |
parent | c64fda634f8609c54196a6223f1e67fb3e0fddc3 (diff) |
restructure provider code
-rw-r--r-- | lib/puppet/provider/service/s6.rb | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/lib/puppet/provider/service/s6.rb b/lib/puppet/provider/service/s6.rb index a6745f1..5e790e2 100644 --- a/lib/puppet/provider/service/s6.rb +++ b/lib/puppet/provider/service/s6.rb @@ -28,6 +28,8 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do return 1 end + attr_writer :defpath + # this is necessary to autodetect a valid resource # default path, since there is no standard for such directory. def defpath @@ -36,8 +38,34 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do end @defpath end + + # returns all providers for all existing services in @defpath + # ie enabled or not + def 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 + end + attr_writer :servicedir + # find the service dir on this node def servicedir unless @servicedir @@ -52,31 +80,12 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do @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.service - return :running if output.chomp == 'true' + if output.chomp == 'true' + return :running + end rescue Puppet::ExecutionFailure => detail unless detail.message =~ /(warning: |s6-supervise not running$)/ raise Puppet::Error.new( "Could not get status for service #{resource.ref}: #{detail}", detail ) @@ -85,25 +94,21 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do :stopped end - def stop - s6_svc "-d", self.service - end - - def restart - s6_svc "-r", self.service - end - - def start - if enabled? != :true - enable + def enabled? + # the service is enabled if it is linked + case Puppet::FileSystem.symlink?(self.service) ? :true : :false + when :true + return :true + when :false + return :false + else + raise Puppet::Error.new( "Received unknown state for #{self.service}", $!) end - s6_svc "-u", self.service end 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) @@ -121,4 +126,19 @@ Puppet::Type.type(:service).provide(:s6, :parent => :daemontools) do Puppet::FileSystem.unlink(self.service) if Puppet::FileSystem.symlink?(self.service) s6_svscanctl "-n", self.servicedir end + + def restart + s6_svc "-r", self.service + end + + def start + if enabled? != :true + enable + end + s6_svc "-u", self.service + end + + def stop + s6_svc "-d", self.service + end end |