summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-20 09:36:20 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-23 15:46:31 -0700
commit72d9beaf59af27939e3f962279eb6162d642c823 (patch)
treeefed172a8da931e63171876fd95cc32ffa260de1
parent984b43786e32ebed1a0e0f78ae51870aa4ecc215 (diff)
downloadchef-72d9beaf59af27939e3f962279eb6162d642c823.tar.gz
Add chef_client_cron resource from chef-client cookbook
Add the chef_client_cron resource so users can manage chef-client as a cron job out of the box. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/chef_client_cron.rb78
-rw-r--r--lib/chef/resources.rb1
2 files changed, 79 insertions, 0 deletions
diff --git a/lib/chef/resource/chef_client_cron.rb b/lib/chef/resource/chef_client_cron.rb
new file mode 100644
index 0000000000..cadb8f1af7
--- /dev/null
+++ b/lib/chef/resource/chef_client_cron.rb
@@ -0,0 +1,78 @@
+#
+# Copyright:: 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require_relative "../resource"
+
+class Chef
+ class Resource
+ class ChefClientCron < Chef::Resource
+ unified_mode true
+
+ provides :chef_client_cron
+
+ description "Use the chef_client_cron resource to setup the Chef Infra Client to run as a cron job."
+ introduced "16.0"
+
+ property :user, String, default: 'root'
+
+ property :minute, [String, Integer], default: '0,30'
+ property :hour, [String, Integer], default: '*'
+ property :weekday, [String, Integer], default: '*'
+ property :mailto, String
+
+ property :job_name, String, default: 'chef-client'
+ property :splay, [Integer, String], default: 300
+
+ property :env_vars, Hash
+
+ property :config_directory, String, default: '/etc/chef'
+ property :log_directory, String, default: lazy { platform?('mac_os_x') ? '/Library/Logs/Chef' : '/var/log/chef' }
+ property :log_file_name, String, default: 'client.log'
+ property :append_log_file, [true, false], default: false
+ property :chef_binary_path, String, default: '/opt/chef/bin/chef-client'
+ property :daemon_options, Array, default: []
+
+ action :add do
+ cron_d new_resource.job_name do
+ minute new_resource.minute
+ hour new_resource.hour
+ weekday new_resource.weekday
+ mailto new_resource.mailto if new_resource.mailto
+ user new_resource.user
+ command cron_command
+ end
+ end
+
+ action :remove do
+ cron_d new_resource.job_name do
+ action :delete
+ end
+ end
+
+ action_class do
+ def cron_command
+ cmd = ''
+ cmd << "/bin/sleep #{splay_sleep_time(new_resource.splay)}; "
+ cmd << "#{new_resource.env_vars} " if new_resource.env_vars
+ cmd << "#{new_resource.chef_binary_path} #{new_resource.daemon_options.join(' ')}"
+ cmd << " #{new_resource.append_log_file ? '>>' : '>'} #{::File.join(new_resource.log_directory, new_resource.log_file_name)} 2>&1"
+ cmd << ' || echo "Chef Infra Client execution failed"' if new_resource.mailto
+ cmd
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index 1fc707f642..2256804eb7 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -27,6 +27,7 @@ require_relative "resource/batch"
require_relative "resource/breakpoint"
require_relative "resource/build_essential"
require_relative "resource/cookbook_file"
+require_relative "resource/chef_client_cron"
require_relative "resource/chef_gem"
require_relative "resource/chef_handler"
require_relative "resource/chef_sleep"