$OpenBSD: patch-lib_puppet_provider_service_openbsd_rb,v 1.1 2016/04/28 11:53:09 jasper Exp $

* enhance initial service provisioning with regard to service_flags

* print a warning when rcctl failed to run, instead of having the user guess
  at Ruby errors

--- lib/puppet/provider/service/openbsd.rb.orig	Thu Apr 28 12:22:57 2016
+++ lib/puppet/provider/service/openbsd.rb	Thu Apr 28 12:23:02 2016
@@ -9,6 +9,15 @@ Puppet::Type.type(:service).provide :openbsd, :parent 
   has_feature :flaggable
 
   def startcmd
+    if @resource[:flags] and flags != @resource[:flags]
+      # Unfortunately, the startcmd gets called, before
+      # the service is enabled (in case its supposed to be enabled).
+      # Setting flags via rcctl is only possible, when the service is enabled
+      # In case the service is not to be enabled, it will be automatically
+      # disabled later in the same puppet run.
+      self.enable
+      self.flags = @resource[:flags]
+    end
     [command(:rcctl), '-f', :start, @resource[:name]]
   end
 
@@ -31,35 +40,38 @@ Puppet::Type.type(:service).provide :openbsd, :parent 
     instances = []
 
     begin
-      execpipe([command(:rcctl), :getall]) do |process|
-        process.each_line do |line|
-          match = /^(.*?)(?:_flags)?=(.*)$/.match(line)
-          attributes_hash = {
-            :name      => match[1],
-            :flags     => match[2],
-            :hasstatus => true,
-            :provider  => :openbsd,
-          }
+      execpipe([command(:rcctl), :ls, :all]) do |all_output|
+        all_output.each_line do |svc|         
+          execpipe([command(:rcctl), :get, svc.chomp, :flags]) do |flags_output|
+            flags_output.each_line do |flags|
+              attributes_hash = {
+                :name      => svc,
+                :flags     => flags,
+                :hasstatus => true,
+                :provider  => :openbsd,
+              }
 
-          instances << new(attributes_hash);
+              instances << new(attributes_hash)
+            end
+          end
         end
       end
       instances
     rescue Puppet::ExecutionFailure
+      Puppet.warning("Failed to execute rcctl")
       return nil
     end
   end
 
   def enabled?
-    output = execute([command(:rcctl), "get", @resource[:name], "status"],
+    output = Puppet::Util::Execution.execute([command(:rcctl), "get", @resource[:name], "status"],
                      :failonfail => false, :combine => false, :squelch => false)
-
-    if output.exitstatus == 1
-      self.debug("Is disabled")
-      return :false
-    else
+    if output.exitstatus == 0
       self.debug("Is enabled")
       return :true
+    else
+      self.debug("Is disabled")
+      return :false
     end
   end
 
