summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-09-04 18:13:42 -0700
committerTim Smith <tsmith84@gmail.com>2020-09-05 20:15:00 -0700
commit3858a046f6d0f554fed931911099111a41337150 (patch)
tree296148df9f567b0c8b2031ca778564c90f2af2ab
parentbef4d5046cfe2234c8224fc8a280f0efba5d93ec (diff)
downloadchef-3858a046f6d0f554fed931911099111a41337150.tar.gz
chef_client_launchd: create a launchd daemon to handle the client restart
Launchd doesn't have the concept of a reload aka restart. Instead to update a daemon config you have to unload it and then reload the new plist. That's usually fine (actually just like upstart), but not if Chef is trying to restart itself. Currently if you change the nice level or interval the client will unload its own config which kills the run and it never gets started back up. So to work around this we're going to install a launchd daemon that has a watcher on the chef-client daemon plist file. It's only job to to unload and reload the client. That way when the daemonized chef-client process updates the plist the restarter job will handle the restart. To ensure we're not in the middle of the next recipe we use a chef_sleep resource to sleep a bit post launchd update. I also updated the resource to work better with cinc while I was in here. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/chef_client_launchd.rb23
1 files changed, 20 insertions, 3 deletions
diff --git a/lib/chef/resource/chef_client_launchd.rb b/lib/chef/resource/chef_client_launchd.rb
index a2b024c030..17a180119e 100644
--- a/lib/chef/resource/chef_client_launchd.rb
+++ b/lib/chef/resource/chef_client_launchd.rb
@@ -110,7 +110,7 @@ class Chef
end
end
- launchd "com.chef.chef-client" do
+ launchd "com.#{Chef::Dist::SHORT}.#{Chef::Dist::CLIENT}" do
username new_resource.user
working_directory new_resource.working_directory
start_interval new_resource.interval * 60
@@ -118,13 +118,30 @@ class Chef
environment_variables new_resource.environment unless new_resource.environment.empty?
nice new_resource.nice
low_priority_io true
+ notifies :sleep, "chef_sleep[Sleep before client restart]", :immediately
action :enable
end
+
+ launchd "com.#{Chef::Dist::SHORT}.restarter" do
+ username "root"
+ watch_paths ["/Library/LaunchDaemons/com.#{Chef::Dist::SHORT}.#{Chef::Dist::CLIENT}.plist"]
+ standard_out_path "#{::File.join(new_resource.log_directory, new_resource.log_file_name)}"
+ standard_error_path "#{::File.join(new_resource.log_directory, new_resource.log_file_name)}"
+ program_arguments ["/bin/bash",
+ "-c",
+ "echo; echo #{Chef::Dist::PRODUCT} launchd daemon config has been updated. Manually unloading and reloading the daemon; echo Now unloading the daemon; launchctl unload /Library/LaunchDaemons/com.#{Chef::Dist::SHORT}.#{Chef::Dist::CLIENT}.plist; sleep 2; echo Now loading the daemon; launchctl load /Library/LaunchDaemons/com.#{Chef::Dist::SHORT}.#{Chef::Dist::CLIENT}.plist"]
+ action :enable
+ end
+
+ chef_sleep "Sleep before client restart" do
+ seconds 10
+ action :nothing
+ end
end
action :disable do
- service "chef-client" do
- service_name "com.chef.chef-client"
+ service "#{Chef::Dist::CLIENT}" do
+ service_name "com.#{Chef::Dist::SHORT}.#{Chef::Dist::CLIENT}"
action :disable
end
end