Consul-template with handling service upstart and systemd

Hi My name is will, I am working on a project to upgrade to handle linux2 ami and also work with linux1 ami. The problem I am having is linux2 is using systemd and linux1 is using upstart. I put in a condition to handle both but now I am coming across the following error.

           System Info:
       ------------
       chef_version=14.15.6
       platform=amazon
       platform_version=2
       ruby=ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
       program_name=/opt/chef/bin/chef-client
       executable=/opt/chef/bin/chef-client
       
   Recipe: nginx::package
     * service[nginx] action reload
       - reload service service[nginx]
   Recipe: datadog::dd-agent
     * service[datadog-agent] action restart
       - restart service service[datadog-agent]
   Recipe: php-fpm::install
     * service[php-fpm] action restart
       - restart service service[php-fpm]
   Recipe: consul::default
     * consul_service[consul] action restart
       * poise_service[consul] action restart
         * service[consul] action restart
    - restart service service[consul]
       
     
     * consul_service[consul] action reload
       * poise_service[consul] action reload
         * service[consul] action reload
    - reload service service[consul]
       
     
   Recipe: leadid_consul::template
     * service[consul-template] action restart
       
       ================================================================================
       Error executing action `restart` on resource 'service[consul-template]'
       ================================================================================
       
       Mixlib::ShellOut::ShellCommandFailed
       ------------------------------------
       Expected process to exit with [0], but received '5'
       ---- Begin output of /bin/systemctl --system restart consul-template ----
       STDOUT: 
       STDERR: Failed to restart consul-template.service: Unit not found.
       ---- End output of /bin/systemctl --system restart consul-template ----
       Ran /bin/systemctl --system restart consul-template returned 5
       
       Resource Declaration:
       ---------------------
       # In /tmp/kitchen/cache/cookbooks/leadid_consul/recipes/template.rb
       
        94: service 'consul-template' do
        95:   provider Chef::Provider::Service::Upstart
        96:   if node['platform_family'] == 'amazon' && node['platform_version'] == '2'
        97:     provider Chef::Provider::Service::Systemd
        98:   else
        99:     provider Chef::Provider::Service::Upstart
       100:   end
       101:   action [service_enable, service_start] unless one_shot
       
       Compiled Resource:
       ------------------
       # Declared in /tmp/kitchen/cache/cookbooks/leadid_consul/recipes/template.rb:94:in `from_file'
       
       service("consul-template") do
         provider Chef::Provider::Service::Systemd
         action [:enable, :start]
         default_guard_interpreter :default
         service_name "consul-template"
         enabled nil
         running nil
         masked nil
         pattern "consul-template"
         declared_type :service
         cookbook_name "leadid_consul"
         recipe_name "template"
         supports {:status=>true, :restart=>true, :reload=>true}
       end
       
       System Info:
       ------------
       chef_version=14.15.6
       platform=amazon
       platform_version=2
       ruby=ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
       program_name=/opt/chef/bin/chef-client
       executable=/opt/chef/bin/chef-client
       
   
   Running handlers:
   [2020-11-04T19:41:14+00:00] ERROR: Running exception handlers
   Running handlers complete
   [2020-11-04T19:41:14+00:00] ERROR: Exception handlers complete
   Chef Client failed. 128 resources updated in 05 minutes 32 seconds
   [2020-11-04T19:41:14+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
   [2020-11-04T19:41:14+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
   [2020-11-04T19:41:14+00:00] FATAL: Chef::Exceptions::MultipleFailures: Multiple failures occurred:
   * Mixlib::ShellOut::ShellCommandFailed occurred in chef run: service[consul-template] (leadid_consul::template line 94) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '5'
   ---- Begin output of /bin/systemctl --system restart consul-template ----
   STDOUT: 
   STDERR: Failed to restart consul-template.service: Unit not found.
   ---- End output of /bin/systemctl --system restart consul-template ----
   Ran /bin/systemctl --system restart consul-template returned 5
   * Mixlib::ShellOut::ShellCommandFailed occurred in delayed notification: service[consul-template] (leadid_consul::template line 94) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '5'
   ---- Begin output of /bin/systemctl --system restart consul-template ----
   STDOUT: 
   STDERR: Failed to restart consul-template.service: Unit not found.
   ---- End output of /bin/systemctl --system restart consul-template ----
   Ran /bin/systemctl --system restart consul-template returned 5

templete.rb

chef_gem ‘rhcl’ do
action :install
end

package ‘unzip’

remote_file download_file_path do
source download_url
owner ‘root’
group ‘root’
mode 0o0755
checksum checksum
backup false
action :create_if_missing
notifies :run, ‘execute[extract-consul-template]’, :immediately
end

execute ‘extract-consul-template’ do
command <<-COMMAND
unzip #{download_file_path} -d #{install_path}
chmod +x #{install_path}/consul-template
COMMAND
action :nothing
end

directory config_path do
owner ‘root’
group ‘root’
mode 0o0755
end

template “#{config_path}/global.hcl” do
source ‘hcl.erb’
owner ‘root’
group ‘root’
mode 0o0600
variables(
options: node[‘leadid_consul’][‘template’][‘global_config’]
)
notifies :restart, ‘service[consul-template]’, :delayed if restart
end

template ‘/etc/init/consul-template.conf’ do
source ‘upstart-consul-template.conf.erb’
owner ‘root’
group ‘root’
variables(
env: env,
one_shot: one_shot
)
notifies :reload, ‘service[consul-template]’, :delayed
end

service ‘consul-template’ do
provider Chef::Provider::Service::Upstart
if node[‘platform_family’] == ‘amazon’ && node[‘platform_version’] == ‘2’
provider Chef::Provider::Service::Systemd
else
provider Chef::Provider::Service::Upstart
end
action [service_enable, service_start] unless one_shot
supports status: true, restart: true, start: true, reload: true
subscribes :restart, “file[#{vault_token_file}]”, :immediately if restart

end

node.default[‘logrotate’][‘cron’][‘install’] = true
include_recipe ‘logrotate’

logrotate_spec = node[‘leadid_consul’][‘template’][‘logrotate’]
logrotate_app ‘consul-template’ do
logrotate_spec.each do |k, v|
send k.to_sym, v
end
action :enable
end

unless node[‘leadid_consul’][‘template’][‘file_configs’].nil?
node[‘leadid_consul’][‘template’][‘file_configs’].each do |file, configs|

leadid_consul_template_file file do
  configs.each do |k, v|
    send k.to_sym, v
  end
  notifies :restart, 'service[consul-template]'
  action :create
end

end
end

bash ‘consul-template-oneshot’ do
code <<-CODE
export VAULT_TOKEN=$(< /etc/leadid/vault-token)
export ENVIRONMENT="#{env}"
/usr/local/bin/consul-template --once
-config="/etc/consul-template.d/" 2>&1
CODE
only_if { one_shot && vault_enabled }
end

This seems to be at the crux of your issue. It looks like you meant for chef to create the systemd *.service file but, based on that error, it looks like it didn’t. That is it looks like that is an error with the systemd setup, not consul-template. More evidence is that “not found” error is accompanied by the return code 5, which is not a return code used by consul-template.