From: Thomas S Hatch <[email protected]>

Signed-off-by: Thomas S Hatch <[email protected]>
---
 lib/puppet/provider/sysctl/linux.rb |   65 +++++++++++++++++++++++++++++++++++
 lib/puppet/type/sysctl.rb           |   36 +++++++++++++++++++
 2 files changed, 101 insertions(+), 0 deletions(-)
 create mode 100644 lib/puppet/provider/sysctl/linux.rb
 create mode 100644 lib/puppet/type/sysctl.rb

diff --git a/lib/puppet/provider/sysctl/linux.rb 
b/lib/puppet/provider/sysctl/linux.rb
new file mode 100644
index 0000000..cda08f2
--- /dev/null
+++ b/lib/puppet/provider/sysctl/linux.rb
@@ -0,0 +1,65 @@
+# Manage the linux kernel stack, should work for most sysctl unixes
+
+Puppet::Type.type(:sysctl).provide :sysctl_linux do
+    desc "Support for managing the linux kernel stack
+    
+    Manages the /etc/sysctl.conf file and uses the sysctl command to apply 
changes
+
+    "
+
+    defaultfor :operatingsystem => [:redhat, :fedora, :suse, :centos, :sles, 
:oel, :ovm, :debian, :ubuntu, :gentoo]
+
+    #Verify that the setting is available and add the setting to the file
+    def create
+        lines = File.new('/etc/sysctl.conf', 'r').readlines
+        done = false
+        lines.each_index do |i|
+            if lines[i].split('=')[0].strip == @resource[:name]
+                lines[i] = "#...@resource[:name]} = #...@resource[:value]}\n"
+                done = true
+            end
+        end
+        unless done
+            lines << "#...@resource[:name]} = #...@resource[:value]}\n"
+        end
+            sysfile = File.new('/etc/sysctl.conf', 'w')
+        for line in lines
+            sysfile.write(line)
+        end
+        sysfile.close
+        `sysctl -p`
+    end
+
+    # Remove the setting from the file - Will not return the setting to the 
kernel default,
+    def destroy
+        lines = File.new('/etc/sysctl.conf', 'r').readlines
+        lines.each_index do |i|
+            if lines[i].split('=')[0].strip == @resource[:name]
+                lines[i] = ""
+            end
+            sysfile = File.new('/etc/sysctl.conf', 'w')
+            for line in lines
+                sysfile.write(line)
+            end
+            sysfile.close
+            `sysctl -p`
+        end
+    end
+    
+    # Checks for the setting in the sysctl.conf file, if the rule is set to be 
absent
+    # then it can exist with any value.
+    def exists?
+        lines = File.new('/etc/sysctl.conf', 'r').readlines
+        lines.each do |line|
+            if line.split('=')[0].strip == @resource[:name]
+                if line.split('=')[1].strip == @resource[:value]
+                return true
+                elsif @resource[:ensure] == :absent
+                    return true
+                end
+            end
+        end
+        return false
+    end
+end
+
diff --git a/lib/puppet/type/sysctl.rb b/lib/puppet/type/sysctl.rb
new file mode 100644
index 0000000..2633e2e
--- /dev/null
+++ b/lib/puppet/type/sysctl.rb
@@ -0,0 +1,36 @@
+module Puppet
+    newtype(:sysctl) do
+        @doc = "Manages the sysctl interface for unix-like systems.
+        The sysctl module works primarily by managing the /etc/sysctl.conf
+        file, and then by calling the 'sysctl -p' command to apply the state
+        of the /etc/sysctl.conf file.
+    
+        This is a very simple type and only makes use of a few paramaters.
+        The type only supports three paramaters, the namevar paramater, name,
+        is the dot notation reference to the desired sysctl setting, aka
+        'vm.swappiness'.  The value paramater is always a string and is the
+        value to pass to the gives sysctl setting.  The sysctl trype is also
+        ensurable, so all rules need to have the regular ensure => present
+        option set.
+    
+        A typical rule will look like this:
+    
+            sysctl {'vm.swappiness':
+                ensure => present,
+                value => '20',
+            }
+        
+        This rule would ensure that the kernel swappiness setting be set to 
'20'"
+
+        ensurable
+
+        newparam(:name, :namevar => true) do
+            desc "The name of the variable in the sysctl tree, given in dot 
notation, eg vm.swappiness"
+        end
+
+        newparam(:value) do
+            desc "The value to enforce for the sysctl variable."
+        end
+    end
+end
+
-- 
1.7.0.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to