Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
phunehehe committed Apr 1, 2014
1 parent b5c6538 commit 209d678
Show file tree
Hide file tree
Showing 25 changed files with 399 additions and 81 deletions.
5 changes: 4 additions & 1 deletion sysctl/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ Berksfile.lock
.*.sw[a-z]
*.un~
/cookbooks
vendor

# Bundler
Gemfile.lock
bin/*
.bundle/*
ruby/

.kitchen/
.kitchen.local.yml
.idea/
.DS_Store
.coverage
4 changes: 2 additions & 2 deletions sysctl/.travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ language: ruby
rvm:
- 1.9.3
- 2.0.0
before_script:
- bundle exec berks install
- 2.1.0
bundler_args: --without integration development
script:
- bundle exec foodcritic -f any . --tags ~FC007 --tags ~FC015 --tags ~FC023
- bundle exec rubocop
- bundle exec rspec
13 changes: 13 additions & 0 deletions sysctl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
##

* [GH-24] On RHEL Adjust Init file to follow chkconfig standards (Alex Farhadi)
* [GH-22] lwrp parameters are written to the sysctl config file (Sander van Zoest, Guilhem Lettron)
* Entries in the sysctl config file are now sorted
* Removed Thor development dependency
* Added LWRP Matcher for use with ChefSpec by wrapper cookbooks
* Added ChefSpec 3 unit tests
* Ported bats tests to ServerSpec integration tests
* Use platform_family? in attributes (requires Ohai 0.6.12)
* Renamed ruby_block[sysctl config notifier] to ruby_block[save-sysctl-params] for clarity
* [GH-19] Make sysctl template logic idempotent (Roy Tewalt)

## v0.3.5

* Scientific Linux 6 support confirmed
Expand Down
28 changes: 20 additions & 8 deletions sysctl/Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem 'berkshelf', '~> 2.0'
gem 'foodcritic', '~> 3.0'
gem 'thor-foodcritic', '~> 1.1'
gem 'rubocop', '~> 0.12'
gem 'berkshelf', '~> 2.0.14'

group :unit do
gem 'foodcritic', '~> 3.0'
gem 'rubocop', '~> 0.18.0'
gem 'chefspec', '~> 3.2.0'
end

group :integration do
gem 'test-kitchen', '~> 1.1'
gem 'kitchen-vagrant'
end

group :release do
gem 'stove', '~> 1.1'
gem 'rspec_junit_formatter'
gem 'rubocop-checkstyle_formatter'
end

group :development do
gem 'guard'
gem 'guard-rubocop'
gem 'guard-foodcritic'
gem 'guard', '~> 1.8'
gem 'guard-rubocop', '~> 0.2'
gem 'guard-foodcritic', '~> 1.0'
gem 'guard-kitchen', '~> 0.0'
gem 'guard-rspec', '~> 3.0'
gem 'rb-fsevent', :require => false
gem 'rb-inotify', :require => false
gem 'terminal-notifier-guard', :require => false
end
18 changes: 17 additions & 1 deletion sysctl/Guardfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# More info at https://github.com/guard/guard#readme

guard 'kitchen' do
watch(%r{test/.+})
watch(%r{^recipes/(.+)\.rb$})
watch(%r{^attributes/(.+)\.rb$})
watch(%r{^files/(.+)})
watch(%r{^templates/(.+)})
watch(%r{^providers/(.+)\.rb})
watch(%r{^resources/(.+)\.rb})
end

guard :rubocop do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end

guard :foodcritic, :cli => "--epic-fail any --tags ~FC007 --tags ~FC015 --tags ~FC023", :cookbook_paths => "." do
guard :foodcritic, :cli => "--epic-fail any --tags ~FC007 --tags ~FC015 --tags ~FC023", :cookbook_paths => ".", :all_on_start => false do
watch(%r{attributes/.+\.rb$})
watch(%r{providers/.+\.rb$})
watch(%r{recipes/.+\.rb$})
watch(%r{resources/.+\.rb$})
end

guard :rspec, cmd: 'bundle exec rspec', :all_on_start => false do
watch(%r{^spec/(.+)_spec\.rb$})
watch(%r{^(recipes)/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { 'spec' }
end
44 changes: 31 additions & 13 deletions sysctl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ There are two main ways to interact with the cookbook. This is via chef [attribu

## Attributes

* node['sysctl']['params'] - A namespace for setting sysctl parameters
* node['sysctl']['conf_dir'] - Specifies the sysctl.d directory to be used. Defaults to /etc/sysctl.d on the Debian and RHEL platform families, otherwise nil
* node['sysctl']['allow_sysctl_conf'] - Defaults to false. Using conf_dir is highly recommended. On some platforms that is not supported. For those platforms, set this to true and the cookbook will rewrite the /etc/sysctl.conf file directly with the params provided. Be sure to save any local edits of /etc/sysctl.conf before enabling this to avoid losing them.
* `node['sysctl']['params']` - A namespace for setting sysctl parameters
* `node['sysctl']['conf_dir']` - Specifies the sysctl.d directory to be used. Defaults to `/etc/sysctl.d` on the Debian and RHEL platform families, otherwise `nil`
* `node['sysctl']['allow_sysctl_conf']` - Defaults to false. Using `conf_dir` is highly recommended. On some platforms that is not supported. For those platforms, set this to `true` and the cookbook will rewrite the `/etc/sysctl.conf` file directly with the params provided. Be sure to save any local edits of `/etc/sysctl.conf` before enabling this to avoid losing them.

## LWRP

Expand Down Expand Up @@ -59,34 +59,50 @@ Attributes

# Development

This cookbook can be tested using vagrant, but it depends on the following vagrant plugins
We have written unit tests using [chefspec](http://code.sethvargo.com/chefspec/) and integration tests in [bats](https://github.com/sstephenson/bats) executed via [test-kitchen](http://kitchen.ci).
Much of the tooling around this cookbook is exposed via guard and test kitchen, so it is highly recommended to learn more about those tools.

## Vagrant Plugin Dependencies

The integration tests can be run via test-kitchen using vagrant, but it depends on the following vagrant plugins:

```
vagrant plugin install vagrant-omnibus
vagrant plugin install vagrant-berkshelf
```

Tested with
* Vagrant (version 1.4.0)
* Vagrant (version 1.4.3)
* vagrant-berkshelf (1.3.5)
* vagrant-omnibus (1.1.2)

To test we have written tests in [bats](https://github.com/sstephenson/bats) and executed via [test-kitchen](http://kitchen.ci).
## Running tests

Much of the tooling around this cookbook is exposed via thor and test kitchen, so it is highly recommended to learn more about those tools.
However, to give a quick glance at how to do some tests, you can execute the following commmands
The following commands will run the tests:

```
bundle install
bundle exec rubocop
bundle exec thor foodcritic:lint
bundle exec foodcritic .
bundle exec rspec
bundle exec kitchen test default-ubuntu-1204
bundle exec kitchen test default-centos-64
bundle exec kitchen test default-centos-65
```

The above will do ruby style ([rubocop](https://github.com/bbatsov/rubocop)) and cookbook style ([foodcritic](http://www.foodcritic.io/)) checks followed rspec unit tests ensuring proper cookbook operation.Integration tests will be run next on two separate linux platforms (Ubuntu 12.04 LTS Precise 64-bit and CentOS 6.5). Please run the tests on any pull requests that you are about to submit and write tests for defects or new features to ensure backwards compatibility and a stable cookbook that we can all rely upon.

## Running tests continuously with guard

This cookbook is also setup to run the checks while you work via the [guard gem](http://guardgem.org/).

```
bundle install
bundle exec guard start
```

The above will do ruby style ([rubocop](https://github.com/bbatsov/rubocop)) and cookbook style ([foodcritic](http://www.foodcritic.io/)) checks followed by ensuring proper cookbook operation on two separate linux platforms (Ubuntu 12.04 LTS Precise 64-bit and CentOS 6.4). Please run the tests on any pull requests that you are about to submit and write tests for defects or new features to ensure backwards compatibility and a stable cookbook that we can all rely upon.
## ChefSpec LWRP Matchers

This coobook is also setup to run the style checks while you work via the [guard gem](http://guardgem.org/).
The cookbook exposes a chefspec matcher to be used by wrapper cookbooks to test the cookbooks LWRP. See `library/matchers.rb` for basic usage.

# Links

Expand All @@ -98,4 +114,6 @@ There are a lot of different documents that talk about system control parameters
* [RHEL 5 VM/Page Cache Tuning Presentation (2009) pdf](http://people.redhat.com/dshaks/Larry_Shak_Perf_Summit1_2009_final.pdf)
* [Arch Linux SysCtl Tutorial (Feb 2013)](http://gotux.net/arch-linux/sysctl-config/)
* [Old RedHat System Tuning Overview (2001!)](http://people.redhat.com/alikins/system_tuning.html)

* [Tuning TCP For The Web at Velocity 2013 (video)](http://vimeo.com/70369211), [slides](http://cdn.oreillystatic.com/en/assets/1/event/94/Tuning%20TCP%20For%20The%20Web%20Presentation.pdf)
* [Adventures in Linux TCP Tuning (Nov 2013)](http://thesimplecomputer.info/adventures-in-linux-tcp-tuning-page2/)
* [Part 1: Lessons learned tuning TCP and Nginx in EC2 (Jan 2014)](http://engineering.chartbeat.com/2014/01/02/part-1-lessons-learned-tuning-tcp-and-nginx-in-ec2/)
13 changes: 0 additions & 13 deletions sysctl/Thorfile

This file was deleted.

7 changes: 2 additions & 5 deletions sysctl/attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
case platform_family
when 'debian', 'rhel'
if platform_family?('debian', 'rhel')
default['sysctl']['conf_dir'] = '/etc/sysctl.d'
else
default['sysctl']['conf_dir'] = nil
end
default['sysctl']['params'] = {}
default['sysctl']['allow_sysctl_conf'] = false

# Default is 32MB which is too low to be useful
default['sysctl']['params']['kernel']['shmmax'] = 67108864 # 64MB
19 changes: 19 additions & 0 deletions sysctl/libraries/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if defined?(ChefSpec)
ChefSpec::Runner.define_runner_method :sysctl_param

# @example This is an example
# expect(chef_run.to apply_sysctl_param('foo')
#
# @param [String] resource_name
# the resource name
#
# @return [ChefSpec::Matchers::ResourceMatcher]
#
def apply_sysctl_param(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:sysctl_param, :apply, resource_name)
end

def remove_sysctl_param(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:sysctl_param, :remove, resource_name)
end
end
14 changes: 13 additions & 1 deletion sysctl/libraries/sysctl.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# Sysctl
module Sysctl
class << self
def config_file(node)
if node['sysctl']['conf_dir']
return File.join(node['sysctl']['conf_dir'], '99-chef-attributes.conf')
else
if node['sysctl']['allow_sysctl_conf']
return '/etc/sysctl.conf'
else
return nil
end
end
end

def compile_attr(prefix, v)
case v
when Array
Expand All @@ -9,7 +21,7 @@ def compile_attr(prefix, v)
"#{prefix}=#{v}"
when Hash, Chef::Node::Attribute
prefix += '.' unless prefix.empty?
return v.map { |key, value| compile_attr("#{prefix}#{key}", value) }.flatten
return v.map { |key, value| compile_attr("#{prefix}#{key}", value) }.flatten.sort
else
fail Chef::Exceptions::UnsupportedAction, "Sysctl cookbook can't handle values of type: #{v.class}"
end
Expand Down
1 change: 1 addition & 0 deletions sysctl/metadata.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

name 'sysctl'
maintainer 'OneHealth Solutions, Inc.'
maintainer_email '[email protected]'
Expand Down
10 changes: 9 additions & 1 deletion sysctl/providers/param.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Support whyrun
def whyrun_supported?
true
end

def load_current_resource
new_resource.key new_resource.name unless new_resource.key
end
Expand All @@ -11,14 +16,16 @@ def load_current_resource
end
unless location[key_path.last] == new_resource.value
location[key_path.last] = new_resource.value
node.default['sysctl']['params'] = sys_attrs
node.save unless Chef::Config[:solo]
execute "sysctl[#{new_resource.key}]" do
command "sysctl -w \"#{new_resource.key}=#{new_resource.value}\""
not_if do
cparam = Mixlib::ShellOut.new("sysctl -n #{new_resource.key}").run_command
cparam.stdout.strip == new_resource.value.to_s
end
notifies :run, 'ruby_block[save-sysctl-params]', :immediately
end
node.default['sysctl']['params'] = sys_attrs
new_resource.updated_by_last_action(true)
end
end
Expand All @@ -43,6 +50,7 @@ def load_current_resource
end
end
node.default['sysctl']['params'] = sys_attrs
node.save unless Chef::Config[:solo]
new_resource.updated_by_last_action(true)
end
end
36 changes: 15 additions & 21 deletions sysctl/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,39 @@
#
# Copyright 2011, Fewbytes Technologies LTD
# Copyright 2012, Chris Roberts <[email protected]>
# Copyright 2013, OneHealth Solutions, Inc.
# Copyright 2013-2014, OneHealth Solutions, Inc.
#

template '/etc/rc.d/init.d/procps' do
source 'procps.init-rhel.erb'
mode '0755'
only_if { platform_family?('rhel', 'pld') }
end

service 'procps'
include_recipe 'sysctl::service'

sysctl_path =
if node['sysctl']['conf_dir']
directory node['sysctl']['conf_dir'] do
owner 'root'
group 'root'
mode 0755
action :create
end
File.join(node['sysctl']['conf_dir'], '99-chef-attributes.conf')
else
node['sysctl']['allow_sysctl_conf'] ? '/etc/sysctl.conf' : nil
end

if sysctl_path
template sysctl_path do
if Sysctl.config_file(node)
# this is called by the sysctl_param lwrp to trigger template creation
ruby_block 'save-sysctl-params' do
action :nothing
block do
end
notifies :create, "template[#{Sysctl.config_file(node)}]", :delayed
end

# this needs to have an action in case node.sysctl.params has changed
# and also needs to be called for persistence on lwrp changes via the
# ruby_block
template Sysctl.config_file(node) do
action :create
source 'sysctl.conf.erb'
mode '0644'
notifies :start, 'service[procps]', :immediately
only_if do
node['sysctl']['params'] && !node['sysctl']['params'].empty?
end
end

ruby_block 'sysctl config notifier' do
block do
true
end
notifies :create, "template[#{sysctl_path}]", :immediately
end
end
17 changes: 17 additions & 0 deletions sysctl/recipes/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Cookbook Name:: sysctl
# Recipe:: service
#
# Copyright 2013-2014, OneHealth Solutions, Inc.
#

template '/etc/rc.d/init.d/procps' do
source 'procps.init-rhel.erb'
mode '0755'
only_if { platform_family?('rhel', 'pld') }
end

service 'procps' do
supports :restart => true, :reload => true, :status => false
action :enable
end
Loading

0 comments on commit 209d678

Please sign in to comment.