Skip to content

Commit 8224a7a

Browse files
committed
Add optional parameter to select file in limits_dir for saving and allow managing multiple settings in one file. Switches file content management to puppetlabs-concat.
1 parent 3fc602d commit 8224a7a

File tree

7 files changed

+480
-42
lines changed

7 files changed

+480
-42
lines changed

.fixtures.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
fixtures:
2+
repositories:
3+
stdlib:
4+
repo: https://github.com/puppetlabs/puppetlabs-stdlib.git
5+
ref: 4.13.1
6+
concat:
7+
repo: https://github.com/puppetlabs/puppetlabs-concat.git
8+
ref: v7.3.0
29
symlinks:
310
limits: "#{source_dir}"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ It creates files in `/etc/security/limits.d` and does not manage the file `/etc/
3131
* `hard`: (Integer) the hard value
3232
* `soft`: (Integer) the soft value
3333
* `both`: (Integer) the value of both soft and hard
34+
* `target`: (String) Optional name of file in `limits_dir` to set limit settings to. Will ignore other file naming logic when provided.
3435

3536

3637
### Using hiera

manifests/limits.pp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# $hard - hard limit
88
# $soft - soft limit
99
# $both - set both limits (-)
10+
# $target - name of file in `limits::limits_dir` directory the settings will be applied. If provided, title with `.conf` extension will be not be used as target file.
1011
#
1112
# Example:
1213
# limits::limits{'*/nofile':
@@ -15,6 +16,10 @@
1516
# }
1617
# limits::limits{'root/nofile': both => 1234; }
1718
#
19+
# Example of multiple settings in single file
20+
# limits::limits{'root/nofile': both => 1234, target => '01-root.conf' }
21+
# limits::limits{'root/nproc': both => 1234, target => '01-root.conf' }
22+
#
1823
# Manages:
1924
# limit file in limits.d with the values provided
2025
define limits::limits (
@@ -24,6 +29,7 @@
2429
Variant[Integer,String,Undef] $hard = undef,
2530
Variant[Integer,String,Undef] $soft = undef,
2631
Variant[Integer,String,Undef] $both = undef,
32+
Optional[String] $target = undef,
2733
) {
2834
include limits
2935

@@ -47,7 +53,9 @@
4753
default => $limit_type,
4854
}
4955

50-
if $title =~ /\.conf$/ {
56+
if $target {
57+
$target_file = "${limits::limits_dir}/${target}"
58+
} elsif $title =~ /\.conf$/ {
5159
$target_file = "${limits::limits_dir}/${title}"
5260
} else {
5361
if $real_user == '*' {
@@ -57,10 +65,22 @@
5765
}
5866
}
5967

60-
file { $target_file:
61-
ensure => $ensure,
62-
owner => 'root',
63-
group => 'root',
68+
if (!defined(Concat[$target_file])) {
69+
concat { $target_file:
70+
ensure => $ensure,
71+
owner => 'root',
72+
group => 'root',
73+
}
74+
75+
concat::fragment { "top_${target_file}":
76+
target => $target_file,
77+
content => "# Managed by Puppet\n\n#<domain> <type> <item> <value>",
78+
order => '01',
79+
}
80+
}
81+
82+
concat::fragment { "${real_user}_${real_type}":
83+
target => $target_file,
6484
content => template('limits/limits.erb'),
6585
}
6686
}

metadata.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,13 @@
5858
}
5959
],
6060
"dependencies": [
61+
{
62+
"name": "puppetlabs/stdlib",
63+
"version_requirement": ">= 4.13.1 < 9.0.0"
64+
},
65+
{
66+
"name": "puppetlabs/concat",
67+
"version_requirement": ">= 7.3.0"
68+
}
6169
]
6270
}

spec/acceptance/limits_spec.rb

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,58 @@
3535

3636
describe file('/etc/security/limits.d/user_nofile.conf') do
3737
it { is_expected.to be_file }
38-
it { is_expected.to contain 'user - nofile 12345' }
38+
it { is_expected.to contain "# Managed by Puppet\n\n#<domain> <type> <item> <value>\nuser - nofile 12345" }
39+
end
40+
end
41+
42+
context 'with target specified managed file' do
43+
it 'create a puppet managed file' do
44+
pp = <<-PP
45+
limits::limits { 'item':
46+
user => 'user',
47+
limit_type => 'nofile',
48+
both => 12345,
49+
target => '00-item.conf'
50+
}
51+
PP
52+
53+
# Run it twice and test for idempotency
54+
apply_manifest(pp, catch_failures: true)
55+
expect(apply_manifest(pp, catch_failures: true).exit_code).to be_zero
56+
end
57+
58+
describe file('/etc/security/limits.d/00-item.conf') do
59+
it { is_expected.to be_file }
60+
it { is_expected.to contain "# Managed by Puppet\n\n#<domain> <type> <item> <value>\nuser - nofile 12345" }
61+
end
62+
end
63+
64+
context 'with multiple target specified managed limit' do
65+
it 'create a puppet managed file' do
66+
pp = <<-PP
67+
limits::limits { 'item':
68+
user => 'user',
69+
limit_type => 'nofile',
70+
both => 12345,
71+
target => '00-item.conf'
72+
}
73+
74+
limits::limits { 'item2':
75+
user => 'other_user',
76+
limit_type => 'nproc',
77+
both => 54321,
78+
target => '00-item.conf'
79+
}
80+
PP
81+
82+
# Run it twice and test for idempotency
83+
apply_manifest(pp, catch_failures: true)
84+
expect(apply_manifest(pp, catch_failures: true).exit_code).to be_zero
85+
end
86+
87+
describe file('/etc/security/limits.d/00-item.conf') do
88+
it { is_expected.to be_file }
89+
it { is_expected.to contain "# Managed by Puppet\n\n#<domain> <type> <item> <value>\nuser - nofile 12345\nother_user - nproc 54321\n" }
3990
end
4091
end
4192
end

0 commit comments

Comments
 (0)