[PATCH/puppet 1/1] Add new type and provider for sysctl, ticket #4068 The sysctl type manages the /etc/sysctl.conf file and applies the seetings via the sysctl command, all options passed are verified before being applied with the sysctl command. The type is very simple, and only needs to peices of information, the name and the value. The name is the namevar and the sysctl key, and the value param is the sysctl value. The type is ensurable Signed-off-by: Thomas S Hatch <thatch45@gmail.com>

6 views
Skip to first unread message

Thomas S Hatch

unread,
Jun 27, 2010, 12:32:02 PM6/27/10
to puppe...@googlegroups.com
---
lib/puppet/provider/sysctl/linux.rb | 69 +++++++++++++++++++++++++++++++++++
lib/puppet/type/sysctl.rb | 36 ++++++++++++++++++
spec/unit/provider/sysctl/linux.rb | 25 +++++++++++++
spec/unit/type/sysctl.rb | 23 ++++++++++++
4 files changed, 153 insertions(+), 0 deletions(-)
create mode 100644 lib/puppet/provider/sysctl/linux.rb
create mode 100644 lib/puppet/type/sysctl.rb
create mode 100644 spec/unit/provider/sysctl/linux.rb
create mode 100644 spec/unit/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..ca9de35
--- /dev/null
+++ b/lib/puppet/provider/sysctl/linux.rb
@@ -0,0 +1,69 @@
+# Manage the linux kernel stack, should work for most sysctl unixes
+require 'pathname'
+
+Puppet::Type.type(:sysctl).provide(: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, :debian, :ubuntu, :gentoo]
+
+ commands :sysctl_cmd => "sysctl"
+
+ #Verify that the setting is available and add the setting to the file
+ def create
+ # First, apply the setting, if the setting is invalid the commands
+ # interface will throw and prevent the value from being applied.
+ sysctl_cmd "-w", "#{@resource[:name]}=#{@resource[:value]}"
+ 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_cmd "-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
+ 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
+
diff --git a/spec/unit/provider/sysctl/linux.rb b/spec/unit/provider/sysctl/linux.rb
new file mode 100644
index 0000000..95b5203
--- /dev/null
+++ b/spec/unit/provider/sysctl/linux.rb
@@ -0,0 +1,25 @@
+#/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../../spec_helper'
+
+provider_class = Puppet::Type.type(:sysctl).provider(:linux)
+
+describe provider_class do
+ before do
+ @resource = stub("resource", :name => "vm.swappiness")
+ @resource.stubs(:[]).with(:name).returns "vm.swappiness"
+ @provider = provider_class.new(@resource)
+ end
+ it "should have a create method" do
+ @provider.should respond_to(:create)
+ end
+
+ it "should have a destroy method" do
+ @provider.should respond_to(:destroy)
+ end
+
+ it "should have an exists? method" do
+ @provider.should respond_to(:exists?)
+ end
+
+end
diff --git a/spec/unit/type/sysctl.rb b/spec/unit/type/sysctl.rb
new file mode 100644
index 0000000..185d7ab
--- /dev/null
+++ b/spec/unit/type/sysctl.rb
@@ -0,0 +1,23 @@
+#/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+sysctl = Puppet::Type.type(:sysctl)
+
+describe sysctl do
+ properties = [:ensure]
+
+ properties.each do |property|
+ it "should have a %s property" % property do
+ sysctl.attrclass(property).ancestors.should be_include(Puppet::Property)
+ end
+ end
+ parameters = [:name, :value]
+
+ parameters.each do |parameter|
+ it "should have a %s parameter" % parameter do
+ sysctl.attrclass(parameter).ancestors.should be_include(Puppet::Parameter)
+ end
+ end
+end
+
--
1.7.0.1

Reply all
Reply to author
Forward
0 new messages