Skip to content

Commit

Permalink
Sysctl class separated from the resource
Browse files Browse the repository at this point in the history
Sysctl class is now used only for loading values from Hiera.
Indiviaual sysctl variables (values) are set via sysctl::variable
defined type.

This should also fix the problem with create_resources(sysctl),
described in thias#16
  • Loading branch information
Pavel Smolka committed Nov 11, 2014
1 parent 2653e54 commit 467154e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 109 deletions.
4 changes: 0 additions & 4 deletions .fixtures.yml

This file was deleted.

4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
spec/fixtures
pkg/*

.idea
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Manage sysctl variable values. All changes are immediately applied, as well as
configured to become persistent. Tested on Red Hat Enterprise Linux 6.

* `sysctl` : Definition to manage sysctl variables by setting a value.
* `sysctl::variable`: Definition to manage sysctl variable by setting a value.
* `sysctl::base`: Base class (included from the definition).

For persistence to work, your Operating System needs to support looking for
Expand All @@ -27,25 +27,25 @@ puppet.

Enable IP forwarding globally :
```puppet
sysctl { 'net.ipv4.ip_forward': value => '1' }
sysctl::variable { 'net.ipv4.ip_forward': value => '1' }
```

Set a value for maximum number of connections per UNIX socket :
```puppet
sysctl { 'net.core.somaxconn': value => '65536' }
sysctl::variable { 'net.core.somaxconn': value => '65536' }
```

Make sure we don't have any explicit value set for swappiness, typically
because it was set at some point but no longer needs to be. The original
value for existing nodes won't be reset until the next reboot :
```puppet
sysctl { 'vm.swappiness': ensure => absent }
sysctl::variable { 'vm.swappiness': ensure => absent }
```

If the order in which the files get applied is important, you can set it by
using a file name prefix, which could also be set globally from `site.pp` :
```puppet
Sysctl { prefix => '60' }
Sysctl::Variable { prefix => '60' }
```

To enable purging of settings, you can use hiera to set the `sysctl::base`
Expand Down
16 changes: 4 additions & 12 deletions manifests/base.pp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
class sysctl::base (
$purge = false,
$values = undef,
$hiera_merge_values = false,
$symlink99 = $::sysctl::params::symlink99,
$sysctl_dir = $::sysctl::params::sysctl_dir,
$sysctl_dir_path = $::sysctl::params::sysctl_dir_path,
Expand All @@ -14,22 +12,14 @@
$sysctl_dir_mode = $::sysctl::params::sysctl_dir_mode,
) inherits ::sysctl::params {

# Hiera support
if $hiera_merge_values == true {
$values_real = hiera_hash('sysctl::base::values')
} else {
$values_real = $values
}
if $values_real != undef {
create_resources(sysctl,$values_real)
}

if $sysctl_dir {

if $purge {
$recurse = true
} else {
$recurse = false
}

file { $sysctl_dir_path:
ensure => directory,
owner => $sysctl_dir_owner,
Expand All @@ -39,12 +29,14 @@
purge => $purge,
recurse => $recurse,
}

if $symlink99 and $sysctl_dir_path =~ /^\/etc\/[^\/]+$/ {
file { "${sysctl_dir_path}/99-sysctl.conf":
ensure => link,
target => '../sysctl.conf',
}
}

}

}
Expand Down
88 changes: 3 additions & 85 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -1,89 +1,7 @@
# Define: sysctl
#
# Manage sysctl variable values.
#
# Parameters:
# $value:
# The value for the sysctl parameter. Mandatory, unless $ensure is 'absent'.
# $prefix:
# Optional prefix for the sysctl.d file to be created. Default: none.
# $ensure:
# Whether the variable's value should be 'present' or 'absent'.
# Defaults to 'present'.
#
# Sample Usage :
# sysctl { 'net.ipv6.bindv6only': value => '1' }
#
define sysctl (
$ensure = undef,
$value = undef,
$prefix = undef,
$suffix = '.conf',
$comment = undef,
$content = undef,
$source = undef,
) {
# Manages a set of sysctl values from Hiera config (sysctl::base::values)
class sysctl {

include '::sysctl::base'

# If we have a prefix, then add the dash to it
if $prefix {
$sysctl_d_file = "${prefix}-${title}${suffix}"
} else {
$sysctl_d_file = "${title}${suffix}"
}

# If we have an explicit content or source, use them
if $content or $source {
$file_content = $content
$file_source = $source
} else {
$file_content = template("${module_name}/sysctl.d-file.erb")
}

if $ensure != 'absent' {

# Present

# The permanent change
file { "/etc/sysctl.d/${sysctl_d_file}":
ensure => $ensure,
owner => 'root',
group => 'root',
mode => '0644',
content => $file_content,
source => $file_source,
notify => [
Exec["sysctl-${title}"],
Exec["update-sysctl.conf-${title}"],
],
}

# The immediate change + re-check on each run "just in case"
exec { "sysctl-${title}":
command => "/sbin/sysctl -p /etc/sysctl.d/${sysctl_d_file}",
refreshonly => true,
require => File["/etc/sysctl.d/${sysctl_d_file}"],
}

# For the few original values from the main file
exec { "update-sysctl.conf-${title}":
command => "sed -i -e 's#^${title} *=.*#${title} = ${value}#' /etc/sysctl.conf",
path => [ '/usr/sbin', '/sbin', '/usr/bin', '/bin' ],
refreshonly => true,
onlyif => "grep -E '^${title} *=' /etc/sysctl.conf",
}

} else {

# Absent
# We cannot restore values, since defaults can not be known... reboot :-/

file { "/etc/sysctl.d/${sysctl_d_file}":
ensure => absent,
}

}
create_resources(sysctl::variable, hiera_hash('sysctl::base::values'))

}

4 changes: 4 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Sysctl directory parameters
#
# This class is not supposed to be used on its own.
#
class sysctl::params {

# Keep the original symlink if we purge, to avoid ping-pong with initscripts
Expand Down
85 changes: 85 additions & 0 deletions manifests/variable.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Define: sysctl::variable
#
# Manage sysctl variable values.
#
# Parameters:
# $value : The value for the sysctl parameter. Mandatory, unless $ensure is 'absent'.
# $prefix : Optional prefix for the sysctl.d file to be created. Default: none.
# $ensure : Whether the variable's value should be 'present' or 'absent'.
#
# Sample Usage :
# sysctl::variable { 'net.ipv6.bindv6only': value => '1' }
#
define sysctl::variable (
$ensure = present,
$value = undef,
$prefix = undef,
$suffix = '.conf',
$comment = undef,
$content = undef,
$source = undef,
) {

include ::sysctl::base

# If we have a prefix, then add the dash to it
if $prefix {
$sysctl_d_file = "${prefix}-${title}${suffix}"
} else {
$sysctl_d_file = "${title}${suffix}"
}

# If we have an explicit content or source, use them
if $content or $source {
$file_content = $content
$file_source = $source
} else {
$file_content = template("${module_name}/sysctl.d-file.erb")
}

if $ensure != 'absent' {

# Present

# The permanent change
file { "/etc/sysctl.d/${sysctl_d_file}":
ensure => $ensure,
owner => 'root',
group => 'root',
mode => '0644',
content => $file_content,
source => $file_source,
notify => [
Exec["sysctl-${title}"],
Exec["update-sysctl.conf-${title}"],
],
}

# The immediate change + re-check on each run "just in case"
exec { "sysctl-${title}":
command => "/sbin/sysctl -p /etc/sysctl.d/${sysctl_d_file}",
refreshonly => true,
require => File["/etc/sysctl.d/${sysctl_d_file}"],
}

# For the few original values from the main file
exec { "update-sysctl.conf-${title}":
command => "sed -i -e 's#^${title} *=.*#${title} = ${value}#' /etc/sysctl.conf",
path => [ '/usr/sbin', '/sbin', '/usr/bin', '/bin' ],
refreshonly => true,
onlyif => "grep -E '^${title} *=' /etc/sysctl.conf",
}

} else {

# Absent
# We cannot restore values, since defaults can not be known... reboot :-/

file { "/etc/sysctl.d/${sysctl_d_file}":
ensure => absent,
}

}

}

33 changes: 33 additions & 0 deletions metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"description": "Manage sysctl variable values.",
"dependencies": [

],
"name": "thias-sysctl",
"project_page": "https://github.com/thias/puppet-sysctl",
"license": "Apache 2.0",
"source": "git://github.com/thias/puppet-sysctl",
"author": "Matthias Saou",
"summary": "Sysctl module",
"types": [

],
"checksums": {
"tests/base.pp": "1ba89838432dbc94339097327c19ae3d",
"ChangeLog": "c9f87a37f83809e6d348735bae6de409",
"manifests/base.pp": "be6f023ad7e949a72f2b80115f812f9c",
"tests/init.pp": "e70e5327b9840b44699bb7fae71d47cd",
"Modulefile": "502d97ec47ecb5e7d39771a993d8d90f",
"spec/defines/sysctl_init_spec.rb": "21d524df70961750cb22f6b83349093e",
"spec/spec_helper.rb": "3ea886dd135e120afa31e0aab12e85b0",
"Gemfile": "3ad486d60d90bfe4395b368b95481e01",
"LICENSE": "99219472697a01561e7630d63aaecdc1",
"README.md": "eecd73855d8815a56cbf13de841208f5",
"manifests/params.pp": "d9387d767c84b0bb0213f82267e186aa",
"Rakefile": "ab253b919e7093c2a5eb7adf0e39ffbc",
"templates/sysctl.d-file.erb": "47e03b3e2b70bc7586271be8b9a6473d",
"spec/classes/sysctl_base_spec.rb": "6241cf3e290871c00b1bb3bbd5490108",
"manifests/init.pp": "1fbf64472b67ecceeffde36b91145884"
},
"version": "1.0.0"
}

0 comments on commit 467154e

Please sign in to comment.