Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dist option to support community branches #691

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
'verify_api_cert' => true,
}

default['chef_client']['dist'] = Gem::Requirement.new('>= 15.0.225').satisfied_by?(Gem::Version.new(Chef::VERSION)) ? Chef::Dist::EXEC : 'chef'

# Accept the chef license when running the chef service
default['chef_client']['chef_license'] = nil

Expand Down Expand Up @@ -113,7 +115,7 @@
case node['platform_family']
when 'aix'
default['chef_client']['init_style'] = 'src'
default['chef_client']['svc_name'] = 'chef'
default['chef_client']['svc_name'] = node['chef_client']['dist']
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'
Expand All @@ -133,11 +135,11 @@
# 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']['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
Expand Down
14 changes: 7 additions & 7 deletions recipes/bsd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -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
2 changes: 1 addition & 1 deletion recipes/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
23 changes: 12 additions & 11 deletions recipes/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,39 @@ 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'
variables client_bin: client_bin
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
Expand All @@ -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']
Expand All @@ -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
Expand All @@ -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']
Expand All @@ -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
8 changes: 4 additions & 4 deletions recipes/init_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
14 changes: 7 additions & 7 deletions recipes/launchd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
16 changes: 8 additions & 8 deletions recipes/smf_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion recipes/src_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 13 additions & 13 deletions recipes/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand All @@ -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' => {
Expand Down Expand Up @@ -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' },
Expand All @@ -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
5 changes: 3 additions & 2 deletions recipes/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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