[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 0.25.5-639-g8f94f35

test branch puppet-dev at googlegroups.com
Wed Jul 14 10:32:58 UTC 2010


The following commit has been merged in the upstream branch:
commit c3cd24bca33e6918598a73fd04996592509ab229
Author: Anselm Strauss <anselm at silversurfer.l.ttyv0.net>
Date:   Mon Feb 1 23:23:12 2010 +0100

    Rewrote FreeBSD service provider

diff --git a/lib/puppet/provider/service/freebsd.rb b/lib/puppet/provider/service/freebsd.rb
index baf5e5a..e124b20 100644
--- a/lib/puppet/provider/service/freebsd.rb
+++ b/lib/puppet/provider/service/freebsd.rb
@@ -1,56 +1,111 @@
-# Manage FreeBSD services.
-Puppet::Type.type(:service).provide :freebsd, :parent => :init do
-    desc "FreeBSD's (and probably NetBSD?) form of ``init``-style service management.
+Puppet::Type.type(:service).provide :freebsd2, :parent => :init do
 
-    Uses ``rc.conf.d`` for service enabling and disabling.
+  desc "Provider for FreeBSD. Makes use of rcvar argument of init scripts and parses/edits rc files."
 
-"
+  confine :operatingsystem => [:freebsd]
 
-    confine :operatingsystem => [:freebsd, :netbsd, :openbsd]
+  @@rcconf = '/etc/rc.conf'
+  @@rcconf_local = '/etc/rc.conf.local'
+  @@rcconf_dir = '/etc/rc.conf.d'
 
-    defaultfor :operatingsystem => :freebsd
+  def self.defpath
+    superclass.defpath
+  end
 
-    @@rcconf_dir = '/etc/rc.conf.d'
+  # Executing an init script with the 'rcvar' argument returns
+  # the service name and whether it's enabled/disabled
+  def rcvar
+    rcvar = execute([self.initscript, :rcvar], :failonfail => true, :squelch => false)
+    rcvar = rcvar.lines.to_a[1].gsub(/\n/, "")
+    self.debug("rcvar is #{rcvar}")
+    return rcvar
+  end
 
-    def self.defpath
-        superclass.defpath
-    end
+  # Extract the service name from the rcvar
+  def rcvar_name
+    name = self.rcvar.gsub(/(.*)_enable=(.*)/, '\1')
+    self.debug("rcvar name is #{name}")
+    return name
+  end
 
-    # remove service file from rc.conf.d to disable it
-    def disable
-        rcfile = File.join(@@rcconf_dir, @model[:name])
-        if File.exists?(rcfile)
-            File.delete(rcfile)
-        end
+  # Edit rc files and set the service to yes/no
+  def rc_edit(yesno)
+    name = self.rcvar_name
+    self.debug("Editing rc files: setting #{name} to #{yesno}")
+    if not self.rc_replace(yesno, name)
+      self.rc_add(yesno, name)
     end
+  end
 
-    # if the service file exists in rc.conf.d then it's already enabled
-    def enabled?
-        rcfile = File.join(@@rcconf_dir, @model[:name])
-        if File.exists?(rcfile)
-            return :true
+  # Try to find an existing setting in the rc files 
+  # and replace the value
+  def rc_replace(yesno, name)
+    success = false
+    # Replace in all files, not just in the first found with a match
+    [@@rcconf, @@rcconf_local, @@rcconf_dir + "/#{name}"].each do |filename|
+      if File.exists?(filename)
+        s = File.read(filename)
+        if s.gsub!(/(#{name}_enable)=\"?(YES|NO)\"?/, "\\1=\"#{yesno}\"")
+          File.open(filename, File::WRONLY) { |f| f << s }
+          self.debug("Replaced in #{filename}")
+          success = true
         end
-
-        return :false
+      end
     end
+    return success
+  end
 
-    # enable service by creating a service file under rc.conf.d with the
-    # proper contents
-    def enable
-        if not File.exists?(@@rcconf_dir)
-            Dir.mkdir(@@rcconf_dir)
-        end
-        rcfile = File.join(@@rcconf_dir, @model[:name])
-        open(rcfile, 'w') { |f| f << "%s_enable=\"YES\"\n" % @model[:name] }
+  # Add a new setting to the rc files
+  def rc_add(yesno, name)
+    append = "\n\# Added by Puppet\n#{name}_enable=\"#{yesno}\""
+    # First, try the one-file-per-service style
+    if File.exists?(@@rcconf_dir)
+      File.open(@@rcconf_dir + "/#{name}", File::WRONLY | File::APPEND | File::CREAT, 0644) {
+        |f| f << append
+        self.debug("Appended to #{f.path}")
+      }
+    else
+      # Else, check the local rc file first, but don't create it
+      if File.exists?(@@rcconf_local)
+        File.open(@@rcconf_local, File::WRONLY | File::APPEND) {
+          |f| f << append
+          self.debug("Appended to #{f.path}")
+        }
+      else
+        # At last use the standard rc.conf file
+        File.open(@@rcconf, File::WRONLY | File::APPEND | File::CREAT, 0644) {
+          |f| f << append
+          self.debug("Appended to #{f.path}")
+        }
+      end
     end
+  end
 
-    # Override stop/start commands to use one<cmd>'s and the avoid race condition
-    # where provider trys to stop/start the service before it is enabled
-    def startcmd
-        [self.initscript, :onestart]
+  def enabled?
+    if /YES$/ =~ self.rcvar then
+      self.debug("Is enabled")
+      return :true
     end
+    self.debug("Is disabled")
+    return :false
+  end
+
+  def enable
+    self.debug("Enabling")
+    self.rc_edit("YES")
+  end
+
+  def disable
+    self.debug("Disabling")
+    self.rc_edit("NO")
+  end
+
+  def startcmd
+    [self.initscript, :onestart]
+  end
+
+  def stopcmd
+    [self.initscript, :onestop]
+  end
 
-    def stopcmd
-        [self.initscript, :onestop]
-    end
 end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list