From 1d3d60b947204e5f819cb8bc7952e0aa1b66cbf4 Mon Sep 17 00:00:00 2001 From: Patrick Cable Date: Mon, 6 Jul 2020 17:06:36 -0400 Subject: [PATCH] Attempt at creating a dist flag for this cookbook Signed-off-by: Patrick Cable --- attributes/default.rb | 61 ++++++++++++++++++++------------------ recipes/bsd_service.rb | 14 ++++----- recipes/config.rb | 2 +- recipes/cron.rb | 23 +++++++------- recipes/init_service.rb | 8 ++--- recipes/launchd_service.rb | 14 ++++----- recipes/smf_service.rb | 16 +++++----- recipes/src_service.rb | 2 +- recipes/systemd_service.rb | 26 ++++++++-------- recipes/task.rb | 5 ++-- 10 files changed, 88 insertions(+), 83 deletions(-) diff --git a/attributes/default.rb b/attributes/default.rb index 8ebdc8ad..7f172cf8 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -30,18 +30,21 @@ 'verify_api_cert' => true, } +# If you're using a community maintained fork of Chef, then set this to the appropriate name. +default['chef_client']['dist'] = 'chef' + # Accept the chef license when running the chef service default['chef_client']['chef_license'] = nil default['chef_client']['log_file'] = 'client.log' default['chef_client']['interval'] = '1800' default['chef_client']['splay'] = '300' -default['chef_client']['conf_dir'] = '/etc/chef' -default['chef_client']['bin'] = '/opt/chef/bin/chef-client' +default['chef_client']['conf_dir'] = "/etc/#{node['chef_client']['dist']}" +default['chef_client']['bin'] = "/opt/#{node['chef_client']['dist']}/bin/#{node['chef_client']['dist']}-client" # Set a sane default log directory location, overriden by specific # platforms below. -default['chef_client']['log_dir'] = '/var/log/chef' +default['chef_client']['log_dir'] = "/var/log/#{node['chef_client']['dist']}" # If log file is used, default permissions so everyone can read default['chef_client']['log_perm'] = '640' @@ -77,7 +80,7 @@ default['chef_client']['task']['password'] = nil # Password is only required for non-system users default['chef_client']['task']['start_time'] = nil default['chef_client']['task']['start_date'] = nil -default['chef_client']['task']['name'] = 'chef-client' +default['chef_client']['task']['name'] = "#{node['chef_client']['dist']}-client" default['chef_client']['load_gems'] = {} @@ -113,31 +116,31 @@ case node['platform_family'] when 'aix' default['chef_client']['init_style'] = 'src' - default['chef_client']['svc_name'] = 'chef' - default['chef_client']['run_path'] = '/var/run/chef' - default['chef_client']['file_cache_path'] = '/var/spool/chef' - default['chef_client']['file_backup_path'] = '/var/lib/chef' - default['chef_client']['log_dir'] = '/var/adm/chef' + default['chef_client']['svc_name'] = node['chef_client']['dist'] + default['chef_client']['run_path'] = "/var/run/#{node['chef_client']['dist']}" + default['chef_client']['file_cache_path'] = "/var/spool/#{node['chef_client']['dist']}" + default['chef_client']['file_backup_path'] = "/var/lib/#{node['chef_client']['dist']}" + default['chef_client']['log_dir'] = "/var/adm/#{node['chef_client']['dist']}" when 'amazon', 'rhel', 'fedora', 'debian', 'suse', 'clearlinux' default['chef_client']['init_style'] = node['init_package'] - default['chef_client']['run_path'] = '/var/run/chef' - default['chef_client']['file_cache_path'] = '/var/cache/chef' - default['chef_client']['file_backup_path'] = '/var/lib/chef' + default['chef_client']['run_path'] = "/var/run/#{node['chef_client']['dist']}" + default['chef_client']['file_cache_path'] = "/var/cache/#{node['chef_client']['dist']}" + default['chef_client']['file_backup_path'] = "/var/lib/#{node['chef_client']['dist']}" default['chef_client']['chkconfig']['start_order'] = 98 default['chef_client']['chkconfig']['stop_order'] = 02 when 'freebsd' default['chef_client']['init_style'] = 'bsd' default['chef_client']['run_path'] = '/var/run' - default['chef_client']['file_cache_path'] = '/var/chef/cache' - default['chef_client']['file_backup_path'] = '/var/chef/backup' + default['chef_client']['file_cache_path'] = "/var/#{node['chef_client']['dist']}/cache" + default['chef_client']['file_backup_path'] = "/var/#{node['chef_client']['dist']}/backup" # don't use bsd paths per COOK-1379 when 'mac_os_x' default['chef_client']['init_style'] = 'launchd' - default['chef_client']['log_dir'] = '/Library/Logs/Chef' + default['chef_client']['log_dir'] = "/Library/Logs/#{node['chef_client']['dist'].capitalize}" # Launchd doesn't use pid files - default['chef_client']['run_path'] = '/var/run/chef' - default['chef_client']['file_cache_path'] = '/Library/Caches/Chef' - default['chef_client']['file_backup_path'] = '/Library/Caches/Chef/Backup' + default['chef_client']['run_path'] = "/var/run/#{node['chef_client']['dist']}" + default['chef_client']['file_cache_path'] = "/Library/Caches/#{node['chef_client']['dist'].capitalize}" + default['chef_client']['file_backup_path'] = "/Library/Caches/#{node['chef_client']['dist'].capitalize}/Backup" # Set to 'daemon' if you want chef-client to run # continuously with the -d and -s options, or leave # as 'interval' if you want chef-client to be run @@ -147,25 +150,25 @@ default['chef_client']['launchd_self-update'] = false when 'openindiana', 'opensolaris', 'nexentacore', 'solaris2', 'omnios' default['chef_client']['init_style'] = 'smf' - default['chef_client']['run_path'] = '/var/run/chef' - default['chef_client']['file_cache_path'] = '/var/chef/cache' - default['chef_client']['file_backup_path'] = '/var/chef/backup' + default['chef_client']['run_path'] = "/var/run/#{node['chef_client']['dist']}" + default['chef_client']['file_cache_path'] = "/var/#{node['chef_client']['dist']}/cache" + default['chef_client']['file_backup_path'] = "/var/#{node['chef_client']['dist']}/backup" default['chef_client']['method_dir'] = '/lib/svc/method' default['chef_client']['bin_dir'] = '/usr/bin' default['chef_client']['locale'] = 'en_US.UTF-8' default['chef_client']['env_path'] = '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' when 'smartos' default['chef_client']['init_style'] = 'smf' - default['chef_client']['run_path'] = '/var/run/chef' - default['chef_client']['file_cache_path'] = '/var/chef/cache' - default['chef_client']['file_backup_path'] = '/var/chef/backup' + default['chef_client']['run_path'] = "/var/run/#{node['chef_client']['dist']}" + default['chef_client']['file_cache_path'] = "/var/#{node['chef_client']['dist']}/cache" + default['chef_client']['file_backup_path'] = "/var/#{node['chef_client']['dist']}/backup" default['chef_client']['method_dir'] = '/opt/local/lib/svc/method' default['chef_client']['bin_dir'] = '/opt/local/bin' default['chef_client']['locale'] = 'en_US.UTF-8' default['chef_client']['env_path'] = '/usr/local/sbin:/usr/local/bin:/opt/local/sbin:/opt/local/bin:/usr/sbin:/usr/bin:/sbin' when 'windows' default['chef_client']['init_style'] = 'windows' - default['chef_client']['conf_dir'] = 'C:/chef' + default['chef_client']['conf_dir'] = "C:/#{node['chef_client']['dist']}" default['chef_client']['run_path'] = "#{node['chef_client']['conf_dir']}/run" default['chef_client']['file_cache_path'] = "#{node['chef_client']['conf_dir']}/cache" default['chef_client']['file_backup_path'] = "#{node['chef_client']['conf_dir']}/backup" @@ -174,8 +177,8 @@ else default['chef_client']['init_style'] = 'none' default['chef_client']['run_path'] = '/var/run' - default['chef_client']['file_cache_path'] = '/var/chef/cache' - default['chef_client']['file_backup_path'] = '/var/chef/backup' + default['chef_client']['file_cache_path'] = "/var/#{node['chef_client']['dist']}/cache" + default['chef_client']['file_backup_path'] = "/var/#{node['chef_client']['dist']}/backup" end # Need to set this Option in client.rb @@ -186,7 +189,7 @@ default['chef_client']['log_rotation']['prerotate'] = nil default['chef_client']['log_rotation']['postrotate'] = case node['chef_client']['init_style'] when 'systemd' - node['chef_client']['systemd']['timer'] ? '' : 'systemctl reload chef-client.service >/dev/null || :' + node['chef_client']['systemd']['timer'] ? '' : "systemctl reload #{node['chef_client']['dist']}-client.service >/dev/null || :" else - '/etc/init.d/chef-client reload >/dev/null || :' + "/etc/init.d/#{node['chef_client']['dist']}-client reload >/dev/null || :" end diff --git a/recipes/bsd_service.rb b/recipes/bsd_service.rb index 527e065a..ed4d3e1a 100644 --- a/recipes/bsd_service.rb +++ b/recipes/bsd_service.rb @@ -8,7 +8,7 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.debug("Using chef-client binary at #{client_bin}") +Chef::Log.debug("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin create_chef_directories @@ -20,7 +20,7 @@ class ::Chef::Recipe action :create end - template '/usr/local/etc/rc.d/chef-client' do + template "/usr/local/etc/rc.d/#{node['chef_client']['dist']}-client" do source 'default/freebsd/chef-client.erb' owner 'root' group 'wheel' @@ -29,21 +29,21 @@ class ::Chef::Recipe end # Remove wrong rc.d script created by an older version of cookbook - file '/etc/rc.d/chef-client' do + file "/etc/rc.d/#{node['chef_client']['dist']}-client" do action :delete end - template '/etc/rc.conf.d/chef' do + template "/etc/rc.conf.d/#{node['chef_client']['dist']}" do source 'default/freebsd/chef.erb' mode '0644' - notifies :start, 'service[chef-client]', :delayed + notifies :start, "service[#{node['chef_client']['dist']}-client]", :delayed end - service 'chef-client' do + service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action [:start] end else - log "You specified service style 'bsd'. You will need to set up your rc.local file. Hint: chef-client -i #{node['chef_client']['client_interval']} -s #{node['chef_client']['client_splay']}" + log "You specified service style 'bsd'. You will need to set up your rc.local file. Hint: #{node['chef_client']['dist']}-client -i #{node['chef_client']['client_interval']} -s #{node['chef_client']['client_splay']}" end diff --git a/recipes/config.rb b/recipes/config.rb index 81834a9c..0bb416d9 100644 --- a/recipes/config.rb +++ b/recipes/config.rb @@ -32,7 +32,7 @@ class ::Chef::Recipe node.default['chef_client']['config']['log_location'] = log_path if node['os'] == 'linux' - logrotate_app 'chef-client' do + logrotate_app "#{node['chef_client']['dist']}-client" do path [log_path] rotate node['chef_client']['logrotate']['rotate'] frequency node['chef_client']['logrotate']['frequency'] diff --git a/recipes/cron.rb b/recipes/cron.rb index b4a7762b..78bd414b 100644 --- a/recipes/cron.rb +++ b/recipes/cron.rb @@ -32,27 +32,27 @@ class ::Chef::Recipe # Stop any running chef-client services if node['os'] == 'linux' - service 'chef-client' do + service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action [:disable, :stop] ignore_failure true end - file '/etc/init.d/chef-client' do + file "/etc/init.d/#{node['chef_client']['dist']}-client" do action :delete end end case node['platform_family'] when 'openindiana', 'opensolaris', 'nexentacore', 'solaris2', 'smartos', 'omnios' - service 'chef-client' do + service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action [:disable, :stop] ignore_failure true end when 'freebsd' - template '/etc/rc.d/chef-client' do + template "/etc/rc.d/#{node['chef_client']['dist']}-client" do source 'default/freebsd/chef-client.erb' owner 'root' group 'wheel' @@ -60,11 +60,11 @@ class ::Chef::Recipe mode '0755' end - file '/etc/rc.conf.d/chef' do + file "/etc/rc.conf.d/#{node['chef_client']['dist']}" do action :delete end - service 'chef-client' do + service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action [:stop] end @@ -73,11 +73,12 @@ class ::Chef::Recipe # If "use_cron_d" is set to true, delete the cron entry that uses the cron # resource built in to Chef and instead use the cron_d LWRP. if node['chef_client']['cron']['use_cron_d'] - cron 'chef-client' do + cron "#{node['chef_client']['dist']}-client" do action :delete end - chef_client_cron 'chef-client cron.d job' do + chef_client_cron "#{node['chef_client']['dist']}-client cron.d job" do + job_name "#{node['chef_client']['dist']}-client" minute node['chef_client']['cron']['minute'] hour node['chef_client']['cron']['hour'] weekday node['chef_client']['cron']['weekday'] @@ -92,7 +93,7 @@ class ::Chef::Recipe # Non-linux platforms don't support cron.d so we won't try to remove a cron_d resource. # https://github.com/chef-cookbooks/cron/blob/master/resources/d.rb#L55 if node['os'] == 'linux' - cron_d 'chef-client' do + cron_d "#{node['chef_client']['dist']}-client" do action :delete end end @@ -102,7 +103,7 @@ class ::Chef::Recipe append_log = node['chef_client']['cron']['append_log'] ? '>>' : '>' daemon_options = " #{node['chef_client']['daemon_options'].join(' ')} " if node['chef_client']['daemon_options'].any? - cron 'chef-client' do + cron "#{node['chef_client']['dist']}-client" do minute node['chef_client']['cron']['minute'] hour node['chef_client']['cron']['hour'] weekday node['chef_client']['cron']['weekday'] @@ -114,7 +115,7 @@ class ::Chef::Recipe cmd << "#{env_vars} " if env_vars? cmd << "#{node['chef_client']['cron']['nice_path']} -n #{process_priority} " if process_priority cmd << "#{client_bin} #{daemon_options}#{append_log} #{log_file} 2>&1" - cmd << ' || echo "Chef client execution failed"' if node['chef_client']['cron']['mailto'] + cmd << " || echo \"#{node['chef_client']['dist']} client execution failed\"" if node['chef_client']['cron']['mailto'] command cmd end end diff --git a/recipes/init_service.rb b/recipes/init_service.rb index 19eb33a4..37f04539 100644 --- a/recipes/init_service.rb +++ b/recipes/init_service.rb @@ -11,7 +11,7 @@ class ::Chef::Recipe node.default['chef_client']['bin'] = client_bin create_chef_directories -template '/etc/init.d/chef-client' do +template "/etc/init.d/#{node['chef_client']['dist']}-client" do source 'redhat/init.d/chef-client.erb' mode '0755' variables(client_bin: client_bin, @@ -20,13 +20,13 @@ class ::Chef::Recipe notifies :restart, 'service[chef-client]', :delayed end -template '/etc/sysconfig/chef-client' do +template "/etc/sysconfig/#{node['chef_client']['dist']}-client" do source 'redhat/sysconfig/chef-client.erb' mode '0644' - notifies :restart, 'service[chef-client]', :delayed + notifies :restart, "service[#{node['chef_client']['dist']}-client]", :delayed end -service 'chef-client' do +service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action [:enable, :start] end diff --git a/recipes/launchd_service.rb b/recipes/launchd_service.rb index 49ae89da..751ce388 100644 --- a/recipes/launchd_service.rb +++ b/recipes/launchd_service.rb @@ -7,12 +7,12 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.debug("Using chef-client binary at #{client_bin}") +Chef::Log.debug("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin create_chef_directories -template '/Library/LaunchDaemons/com.chef.chef-client.plist' do +template "/Library/LaunchDaemons/com.#{node['chef_client']['dist']}.#{node['chef_client']['dist']}-client.plist" do source 'com.chef.chef-client.plist.erb' mode '0644' variables( @@ -25,15 +25,15 @@ class ::Chef::Recipe splay: node['chef_client']['splay'], working_dir: node['chef_client']['launchd_working_dir'] ) - notifies :restart, 'macosx_service[com.chef.chef-client]' if node['chef_client']['launchd_self-update'] + notifies :restart, "macosx_service[com.#{node['chef_client']['dist']}.#{node['chef_client']['dist']}-client]" if node['chef_client']['launchd_self-update'] end -macosx_service 'com.chef.chef-client' do +macosx_service "com.#{node['chef_client']['dist']}.#{node['chef_client']['dist']}-client" do action :nothing end -macosx_service 'chef-client' do - service_name 'com.chef.chef-client' - plist '/Library/LaunchDaemons/com.chef.chef-client.plist' +macosx_service "#{node['chef_client']['dist']}-client" do + service_name "com.#{node['chef_client']['dist']}.#{node['chef_client']['dist']}-client" + plist "/Library/LaunchDaemons/com.#{node['chef_client']['dist']}.#{node['chef_client']['dist']}-client.plist" action :start end diff --git a/recipes/smf_service.rb b/recipes/smf_service.rb index a1d1dd7d..a23eb4b6 100644 --- a/recipes/smf_service.rb +++ b/recipes/smf_service.rb @@ -5,7 +5,7 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.debug("Using chef-client binary at #{client_bin}") +Chef::Log.debug("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin create_chef_directories @@ -18,15 +18,15 @@ class ::Chef::Recipe end local_path = ::File.join(Chef::Config[:file_cache_path], '/') -template "#{node['chef_client']['method_dir']}/chef-client" do +template "#{node['chef_client']['method_dir']}/#{node['chef_client']['dist']}-client" do source 'solaris/chef-client.erb' owner 'root' group 'root' mode '0555' - notifies :restart, 'service[chef-client]' + notifies :restart, "service[#{node['chef_client']['dist']}-client]" end -template(local_path + 'chef-client.xml') do +template(local_path + node['chef_client']['dist'] + '-client.xml') do if node['platform_version'].to_f >= 5.11 && !platform?('smartos') source 'solaris/manifest-5.11.xml.erb' else @@ -37,14 +37,14 @@ class ::Chef::Recipe mode '0644' end -execute 'load chef-client manifest' do +execute "load #{node['chef_client']['dist']}-client manifest" do action :nothing command "/usr/sbin/svccfg import #{local_path}chef-client.xml" - notifies :restart, 'service[chef-client]' + notifies :restart, "service[#{node['chef_client']['dist']}-client]" end -service 'chef-client' do +service "#{node['chef_client']['dist']}-client" do action [:enable, :start] provider Chef::Provider::Service::Solaris - notifies :run, 'execute[load chef-client manifest]', :before + notifies :run, "execute[load #{node['chef_client']['dist']}-client manifest]", :before end diff --git a/recipes/src_service.rb b/recipes/src_service.rb index 056023c1..3547bf61 100644 --- a/recipes/src_service.rb +++ b/recipes/src_service.rb @@ -26,7 +26,7 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.debug("Using chef-client binary at #{client_bin}") +Chef::Log.debug("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin create_chef_directories diff --git a/recipes/systemd_service.rb b/recipes/systemd_service.rb index b7fb5710..24082fc4 100644 --- a/recipes/systemd_service.rb +++ b/recipes/systemd_service.rb @@ -4,17 +4,17 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.debug("Using chef-client binary at #{client_bin}") +Chef::Log.debug("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin create_chef_directories dist_dir, conf_dir, env_file = value_for_platform_family( - ['amazon'] => %w(redhat sysconfig chef-client), - ['fedora'] => %w(fedora sysconfig chef-client), - ['rhel'] => %w(redhat sysconfig chef-client), - ['suse'] => %w(redhat sysconfig chef-client), - ['debian'] => %w(debian default chef-client), - ['clearlinux'] => %w(clearlinux chef chef-client) + ['amazon'] => ['redhat', 'sysconfig', "#{node['chef_client']['dist']}-client"], + ['fedora'] => ['fedora', 'sysconfig', "#{node['chef_client']['dist']}-client"], + ['rhel'] => ['redhat', 'sysconfig', "#{node['chef_client']['dist']}-client"], + ['suse'] => ['redhat', 'sysconfig', "#{node['chef_client']['dist']}-client"], + ['debian'] => ['debian', 'default', "#{node['chef_client']['dist']}-client"], + ['clearlinux'] => ['clearlinux', 'chef', "#{node['chef_client']['dist']}-client"] ) timer = node['chef_client']['systemd']['timer'] @@ -28,7 +28,7 @@ class ::Chef::Recipe env_file = template "/etc/#{conf_dir}/#{env_file}" do source "default/#{dist_dir}/#{conf_dir}/chef-client.erb" mode '0644' - notifies :restart, 'service[chef-client]', :delayed unless timer + notifies :restart, "service[#{node['chef_client']['dist']}-client]", :delayed unless timer end directory '/etc/systemd/system' do @@ -41,7 +41,7 @@ class ::Chef::Recipe service_unit_content = { 'Unit' => { - 'Description' => 'Chef Infra Client', + 'Description' => "#{node['chef_client']['dist'].capitalize} Infra Client", 'After' => 'network.target auditd.service', }, 'Service' => { @@ -70,13 +70,13 @@ class ::Chef::Recipe node['chef_client']['systemd']['killmode'] end -systemd_unit 'chef-client.service' do +systemd_unit "#{node['chef_client']['dist']}-client.service" do content service_unit_content action :create - notifies(:restart, 'service[chef-client]', :delayed) unless timer + notifies(:restart, "service[#{node['chef_client']['dist']}-client]", :delayed) unless timer end -systemd_unit 'chef-client.timer' do +systemd_unit "#{node['chef_client']['dist']}-client.timer" do content( 'Unit' => { 'Description' => 'chef-client periodic run' }, 'Install' => { 'WantedBy' => 'timers.target' }, @@ -90,7 +90,7 @@ class ::Chef::Recipe notifies :restart, to_s, :delayed unless timer end -service 'chef-client' do +service "#{node['chef_client']['dist']}-client" do supports status: true, restart: true action(timer ? [:disable, :stop] : [:enable, :start]) end diff --git a/recipes/task.rb b/recipes/task.rb index 564e6a2c..2199e1dd 100644 --- a/recipes/task.rb +++ b/recipes/task.rb @@ -25,10 +25,11 @@ class ::Chef::Recipe # libraries/helpers.rb method to DRY directory creation resources client_bin = find_chef_client -Chef::Log.info("Using chef-client binary at #{client_bin}") +Chef::Log.info("Using #{node['chef_client']['dist']}-client binary at #{client_bin}") node.default['chef_client']['bin'] = client_bin chef_client_scheduled_task 'Chef Client' do + task_name "#{node['chef_client']['dist']}-client" user node['chef_client']['task']['user'] password node['chef_client']['task']['password'] frequency node['chef_client']['task']['frequency'] @@ -47,5 +48,5 @@ class ::Chef::Recipe startup_type :disabled action [:configure_startup, :stop] ignore_failure true - only_if { ::Win32::Service.exists?('chef-client') } + only_if { ::Win32::Service.exists?("#{node['chef_client']['dist']}-client") } end