Skip to content

Commit ddc04a7

Browse files
committed
Merge branch 'dynamic-template-lwrps' of https://github.com/hw-cookbooks/sysctl into integration/hw-cookbook-dynamic-template
Conflicts: recipes/default.rb
2 parents 28522a6 + 9c05965 commit ddc04a7

File tree

6 files changed

+104
-20
lines changed

6 files changed

+104
-20
lines changed

attributes/default.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
default[:sysctl][:conf_dir] = case node.platform_family
2+
when 'debian'
3+
'/etc/sysctl.d'
4+
else
5+
nil
6+
end
7+
default[:sysctl][:attributes] = {}
8+
default[:sysctl][:allow_sysctl_conf] = false

libraries/sysctl.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Sysctl
2+
class << self
3+
def compile_attr(prefix, v)
4+
case v
5+
when Array
6+
return "#{prefix}=#{v.join(" ")}"
7+
when String, Fixnum, Bignum, Float, Symbol
8+
"#{prefix}=#{v}"
9+
when Hash, Chef::Node::Attribute
10+
prefix += "." unless prefix.empty?
11+
return v.map {|key, value| compile_attr("#{prefix}#{key}", value)}.flatten
12+
else
13+
raise Chef::Exceptions::UnsupportedAction, "Sysctl cookbook can't handle values of type: #{v.class}"
14+
end
15+
end
16+
end
17+
end

providers/param.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def load_current_resource
2+
new_resource.key new_resource.name unless new_resource.key
3+
end
4+
5+
action :apply do
6+
key_path = new_resource.key.split('.')
7+
sys_attrs = Mash.new(node[:sysctl][:attributes].to_hash)
8+
location = key_path.slice(0, key_path.size - 1).inject(sys_attrs) do |m,o|
9+
m[o] ||= {}
10+
m[o]
11+
end
12+
unless(location[key_path.last] == new_resource.value)
13+
location[key_path.last] = new_resource.value
14+
execute "sysctl[#{new_resource.key}]" do
15+
command "sysctl -w #{new_resource.key}=#{new_resource.value}"
16+
not_if do
17+
%x{sysctl -n #{new_resource.key}}.strip == new_resource.value.to_s
18+
end
19+
end
20+
node.set[:sysctl][:attributes] = sys_attrs
21+
new_resource.updated_by_last_action(true)
22+
end
23+
end
24+
25+
action :remove do
26+
key_path = new_resource.key.split('.')
27+
sys_attrs = Mash.new(node[:sysctl][:attributes].to_hash)
28+
location = key_path.slice(0, key_path.size - 1).inject(sys_attrs) do |m,o|
29+
m.nil? ? nil : m[o]
30+
end
31+
if(location && location[key_path.last])
32+
location.delete(key_path.last)
33+
if(location.empty?)
34+
key_path.size.times do |i|
35+
int_key = key_path.size - i - 1
36+
l = key_path.slice(0, int_key).inject(node[:sysctl][:attributes]) do |m,o|
37+
m.nil? ? nil : m[o]
38+
end
39+
if(l && l[key_path[int_key]] && l[key_path[int_key]].empty?)
40+
l.delete(key_path[int_key])
41+
end
42+
end
43+
end
44+
node.set[:sysctl][:attributes] = sys_attrs
45+
new_resource.updated_by_last_action(true)
46+
end
47+
end

recipes/default.rb

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,40 @@
33
# Recipe:: default
44
#
55
# Copyright 2011, Fewbytes Technologies LTD
6+
# Copyright 2012, Chris Roberts <[email protected]>
67
#
78

8-
def compile_attr(prefix, v)
9-
case v
10-
when Array
11-
return "#{prefix}=#{v.join(" ")}"
12-
when String, Fixnum, Bignum, Float, Symbol
13-
"#{prefix}=#{v}"
14-
when Hash, Chef::Node::Attribute
15-
prefix += "." unless prefix.empty?
16-
return v.map {|key, value| compile_attr("#{prefix}#{key}", value)}.flatten
17-
else
18-
raise Chef::Exceptions::UnsupportedAction, "Sysctl cookbook can't handle values of type: #{v.class}"
19-
end
20-
end
9+
service "procps"
2110

22-
if node.attribute?(:sysctl)
11+
sysctl_path = if(node[:sysctl][:conf_dir])
12+
File.join(node[:sysctl][:conf_dir], '99-chef-attributes.conf')
13+
else
14+
node[:sysctl][:allow_sysctl_conf] ? '/etc/sysctl.conf' : nil
15+
end
2316

24-
attr_txt = compile_attr("", node[:sysctl]).join("\n") + "\n"
17+
if(sysctl_path)
18+
template sysctl_path do
19+
action :nothing
20+
source 'sysctl.conf.erb'
21+
mode '0644'
22+
notifies :start, resources(:service => 'procps'), :immediately
23+
only_if do
24+
node[:sysctl][:attributes] && !node[:sysctl][:attributes].empty?
25+
end
26+
end
2527

26-
file "/etc/sysctl.d/68-chef-attributes.conf" do
27-
content attr_txt
28-
mode "0644"
29-
notifies :start, "service[procps]", :immediately
28+
ruby_block 'sysctl config notifier' do
29+
block do
30+
true
31+
end
32+
notifies :create, resources(:template => sysctl_path), :delayed
3033
end
3134
end
3235

3336
cookbook_file "/etc/sysctl.d/69-chef-static.conf" do
3437
ignore_failure true
3538
mode "0644"
36-
notifies :start, "service[procps]", :immediately
39+
notifies :start, "service[procps]"
3740
end
3841

3942
service "procps"

resources/param.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
actions :apply, :remove
2+
default_action :apply
3+
4+
attribute :key, :kind_of => String
5+
attribute :value, :required => true

templates/default/sysctl.conf.erb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Dynamically generated file dropped off by Chef!
2+
3+
<%= Sysctl.compile_attr('', node[:sysctl][:attributes]).join("\n") %>
4+

0 commit comments

Comments
 (0)