summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-24 10:38:08 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-24 10:38:08 -0700
commitaa7ef36c0f8cab2703683981b8b56189daee650e (patch)
treeb0fc8f36fabd770e5fef548cbd67dc460593e944
parent3116f5a5dc8d80216e2c28e1be52c97e7eb4da8c (diff)
downloadchef-aa7ef36c0f8cab2703683981b8b56189daee650e.tar.gz
If append_log_file is set then use the -L flag
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/chef_client_cron.rb28
-rw-r--r--spec/unit/resource/chef_client_cron_spec.rb2
2 files changed, 27 insertions, 3 deletions
diff --git a/lib/chef/resource/chef_client_cron.rb b/lib/chef/resource/chef_client_cron.rb
index f90575c7f0..670bffe839 100644
--- a/lib/chef/resource/chef_client_cron.rb
+++ b/lib/chef/resource/chef_client_cron.rb
@@ -159,22 +159,46 @@ class Chef
end
action_class do
- # Generate a uniformly distributed unique number to sleep.
+ #
+ # Generate a uniformly distributed unique number to sleep from 0 to the splay time
+ #
+ # @param [Integer] splay The number of seconds to splay
+ #
+ # @return [Integer]
+ #
def splay_sleep_time(splay)
seed = node["shard_seed"] || Digest::MD5.hexdigest(node.name).to_s.hex
random = Random.new(seed.to_i)
random.rand(splay)
end
+ #
+ # The complete cron command to run
+ #
+ # @return [String]
+ #
def cron_command
cmd = ""
cmd << "/bin/sleep #{splay_sleep_time(new_resource.splay)}; "
cmd << "#{new_resource.chef_binary_path} "
cmd << "#{new_resource.daemon_options.join(" ")} " unless new_resource.daemon_options.empty?
- cmd << "#{new_resource.append_log_file ? ">>" : ">"} #{::File.join(new_resource.log_directory, new_resource.log_file_name)} 2>&1"
+ cmd << log_command
cmd << " || echo \"#{Chef::Dist::PRODUCT} execution failed\"" if new_resource.mailto
cmd
end
+
+ #
+ # The portion of the overall cron job that handles logging based on the append_log_file property
+ #
+ # @return [String]
+ #
+ def log_command
+ if new_resource.append_log_file
+ "-L #{::File.join(new_resource.log_directory, new_resource.log_file_name)}"
+ else
+ "> #{::File.join(new_resource.log_directory, new_resource.log_file_name)} 2>&1"
+ end
+ end
end
end
end
diff --git a/spec/unit/resource/chef_client_cron_spec.rb b/spec/unit/resource/chef_client_cron_spec.rb
index 094bedc91a..5ce3db84dd 100644
--- a/spec/unit/resource/chef_client_cron_spec.rb
+++ b/spec/unit/resource/chef_client_cron_spec.rb
@@ -103,7 +103,7 @@ describe Chef::Resource::ChefClientCron do
it "appends to the log file appending if set" do
resource.append_log_file true
- expect(provider.cron_command).to eql("/bin/sleep 123; /opt/chef/bin/chef-client >> /var/log/chef/client.log 2>&1")
+ expect(provider.cron_command).to eql("/bin/sleep 123; /opt/chef/bin/chef-client -L /var/log/chef/client.log")
end
end
end