This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

freebsd.rb

By: a guest on Jun 26th, 2012  |  syntax: Ruby  |  size: 4.27 KB  |  views: 34  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Puppet::Type.type(:service).provide :freebsd, :parent => :init do
  2.  
  3.   desc "Provider for FreeBSD. Uses the `rcvar` argument of init scripts and parses/edits rc files."
  4.  
  5.   confine :operatingsystem => [:freebsd]
  6.   defaultfor :operatingsystem => [:freebsd]
  7.  
  8.   class_variable_set(:@@rcconf,       '/etc/rc.conf')
  9.   class_variable_set(:@@rcconf_local, '/etc/rc.conf.local')
  10.   class_variable_set(:@@rcconf_dir,   '/etc/rc.conf.d')
  11.  
  12.   def self.defpath
  13.     superclass.defpath
  14.   end
  15.  
  16.   # Executing an init script with the 'rcvar' argument returns
  17.   # the service name, rcvar name and whether it's enabled/disabled
  18.   def rcvar
  19.     rcvar = execute([self.initscript, :rcvar], :failonfail => true, :squelch => false)
  20.     rcvar = rcvar.split("\n")
  21.     rcvar.delete_if {|str| str =~ /^#\s*$/}
  22.  
  23.     rcvar[1] = rcvar[1].gsub(/^\$/, '')
  24.     rcvar
  25.   end
  26.  
  27.   # Extract service name
  28.   def service_name
  29.     name = self.rcvar[0]
  30.     self.error("No service name found in rcvar") if name.nil?
  31.     name = name.gsub!(/# (.*)/, '\1')
  32.     self.error("Service name is empty") if name.nil?
  33.     self.debug("Service name is #{name}")
  34.     name
  35.   end
  36.  
  37.   # Extract rcvar name
  38.   def rcvar_name
  39.     name = self.rcvar[1]
  40.     self.error("No rcvar name found in rcvar") if name.nil?
  41.     name = name.gsub!(/(.*)_enable=(.*)/, '\1')
  42.    include pacman
  43.    include
  44.     self.error("rcvar name is empty") if name.nil?
  45.  
  46.     self.debug("rcvar name is #{name}")
  47.     name
  48.   end
  49.  
  50.   # Extract rcvar value
  51.   def rcvar_value
  52.     value = self.rcvar[1]
  53.     self.error("No rcvar value found in rcvar") if value.nil?
  54.     value = value.gsub!(/(.*)_enable="?(\w+)"?/, '\2')
  55.     self.error("rcvar value is empty") if value.nil?
  56.     self.debug("rcvar value is #{value}")
  57.     value
  58.   end
  59.  
  60.   # Edit rc files and set the service to yes/no
  61.   def rc_edit(yesno)
  62.     service = self.service_name
  63.     rcvar = self.rcvar_name
  64.     self.debug("Editing rc files: setting #{rcvar} to #{yesno} for #{service}")
  65.     self.rc_add(service, rcvar, yesno) if not self.rc_replace(service, rcvar, yesno)
  66.   end
  67.  
  68.  
  69.   # Try to find an existing setting in the rc files
  70.   # and replace the value
  71.   def rc_replace(service, rcvar, yesno)
  72.     success = false
  73.     # Replace in all files, not just in the first found with a match
  74.     [@@rcconf, @@rcconf_local, @@rcconf_dir + "/#{service}"].each do |filename|
  75.       if File.exists?(filename)
  76.         s = File.read(filename)
  77.         if s.gsub!(/(#{rcvar}_enable)=\"?(YES|NO)\"?/, "\\1=\"#{yesno}\"")
  78.           File.open(filename, File::WRONLY) { |f| f << s }
  79.           self.debug("Replaced in #{filename}")
  80.           success = true
  81.         end
  82.       end
  83.     end
  84.     success
  85.   end
  86.  
  87.   # Add a new setting to the rc files
  88.   def rc_add(service, rcvar, yesno)
  89.     append = "\# Added by Puppet\n#{rcvar}_enable=\"#{yesno}\"\n"
  90.  
  91.     # First, try the one-file-per-service style
  92.     if File.exists?(@@rcconf_dir)
  93.       File.open(@@rcconf_dir + "/#{service}", File::WRONLY | File::APPEND | File::CREAT, 0644) {
  94.         |f| f << append
  95.         self.debug("Appended to #{f.path}")
  96.       }
  97.     else
  98.       # Else, check the local rc file first, but don't create it
  99.       if File.exists?(@@rcconf_local)
  100.         File.open(@@rcconf_local, File::WRONLY | File::APPEND) {
  101.           |f| f << append
  102.           self.debug("Appended to #{f.path}")
  103.         }
  104.       else
  105.         # At last use the standard rc.conf file
  106.         File.open(@@rcconf, File::WRONLY | File::APPEND | File::CREAT, 0644) {
  107.           |f| f << append
  108.  
  109.           self.debug("Appended to #{f.path}")
  110.         }
  111.       else
  112.         # At last use the standard rc.conf file
  113.         File.open(@@rcconf, File::WRONLY | File::APPEND | File::CREAT, 0644) {
  114.           |f| f << append
  115.           self.debug("Appended to #{f.path}")
  116.         }
  117.       end
  118.     end
  119.   end
  120.  
  121.   def enabled?
  122.     if /YES$/ =~ self.rcvar_value
  123.       self.debug("Is enabled")
  124.       return :true
  125.     end
  126.     self.debug("Is disabled")
  127.     :false
  128.  
  129.   end
  130.  
  131.   def enable
  132.     self.debug("Enabling")
  133.     self.rc_edit("YES")
  134.   end
  135.  
  136.   def disable
  137.     self.debug("Disabling")
  138.     self.rc_edit("NO")
  139.   end
  140.  
  141.   def startcmd
  142.     [self.initscript, :onestart]
  143.   end
  144.  
  145.   def stopcmd
  146.     [self.initscript, :onestop]
  147.   end
  148.  
  149.   def statuscmd
  150.     [self.initscript, :onestatus]
  151.   end
  152. end
clone this paste RAW Paste Data