summaryrefslogtreecommitdiff
path: root/lib/chef/resource
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-02-03 13:17:35 -0800
committerGitHub <noreply@github.com>2020-02-03 13:17:35 -0800
commitb827bc9644565badd9a9aeb593a2912387214179 (patch)
treeee708d6a08b99a271545e7e5c9ca9b772dba3857 /lib/chef/resource
parent49808f67bb58afab477b86b785b4ad0ba005f5c9 (diff)
parent659074bbff65a6a56e5fe01f798aabb5e32726b2 (diff)
downloadchef-b827bc9644565badd9a9aeb593a2912387214179.tar.gz
Merge pull request #9324 from chef/systctl_comments
sysctl: Add new comments property for adding comments to configs
Diffstat (limited to 'lib/chef/resource')
-rw-r--r--lib/chef/resource/sysctl.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/chef/resource/sysctl.rb b/lib/chef/resource/sysctl.rb
index a6c316c5bc..edbeb23daa 100644
--- a/lib/chef/resource/sysctl.rb
+++ b/lib/chef/resource/sysctl.rb
@@ -1,6 +1,6 @@
#
# Copyright:: 2018, Webb Agile Solutions Ltd.
-# Copyright:: 2018-2018, Chef Software Inc.
+# Copyright:: 2018-2020, Chef Software Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@@ -45,6 +45,11 @@ class Chef
coerce: proc { |v| coerce_value(v) },
required: true
+ property :comment, [Array, String],
+ description: "Comments, placed above the resource setting in the generated file. For multi-line comments, use an array of strings, one per line.",
+ default: [],
+ introduced: "15.8"
+
property :conf_dir, String,
description: "The configuration directory to write the config to.",
default: "/etc/sysctl.d"
@@ -81,7 +86,7 @@ class Chef
directory new_resource.conf_dir
file "#{new_resource.conf_dir}/99-chef-#{new_resource.key.tr("/", ".")}.conf" do
- content "#{new_resource.key} = #{new_resource.value}"
+ content contruct_sysctl_content
end
execute "Load sysctl values" do
@@ -112,9 +117,28 @@ class Chef
end
action_class do
+ #
+ # Shell out to set the sysctl value
+ #
+ # @param [String] key The sysctl key
+ # @param [String] value The value of the sysctl key
+ #
def set_sysctl_param(key, value)
shell_out!("sysctl #{"-e " if new_resource.ignore_error}-w \"#{key}=#{value}\"")
end
+
+ #
+ # construct a string, joining members of new_resource.comment and new_resource.value
+ #
+ # @return [String] The text file content
+ #
+ def contruct_sysctl_content
+ sysctl_lines = Array(new_resource.comment).map { |c| "# #{c.strip}" }
+
+ sysctl_lines << "#{new_resource.key} = #{new_resource.value}"
+
+ sysctl_lines.join("\n")
+ end
end
private