Skip to content

Commit

Permalink
Fix issue of having to run chef twice
Browse files Browse the repository at this point in the history
When the cookbook is run for the first time, start the cloudwatch agent overwrites the .json configuration.
This commit allows the config-translator to run prior to starting the service for the first time.
  • Loading branch information
Taylor Owen committed Dec 21, 2018
1 parent 94e7b00 commit cced38b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion libraries/agent_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,32 @@ class AgentInstaller < Chef::Resource
end

if new_resource.json_config
template ::File::join(config_path, node['aws_cloudwatch']['config']['json_file_name']) do
json_path = "#{::File::join(config_path, node['aws_cloudwatch']['config']['json_file_name'])}"
agent_tom_path = "#{::File::join(config_path, 'amazon-cloudwatch-agent.toml')}"
common_path = "#{::File::join(config_path, node['aws_cloudwatch']['config']['file_name'])}"
template json_path do
action :create
source new_resource.json_config
end
script 'amazon-cloudwatch-agent-config-translator' do
action :run
not_if { ::File.exists?(agent_tom_path) }
case node[:platform]
when 'windows'
# interpreter "powershell"
# # TODO: Powershell draft
# code <<-EOH
# ./amazon-cloudwatch-agent-ctl.ps1 -a start -m auto
# EOH
else
interpreter "bash"
code <<-EOH
res="$(sudo /opt/aws/amazon-cloudwatch-agent/bin/config-translator --input #{json_path} --output #{agent_tom_path} --mode auto --config #{common_path})"
echo "$res" | grep 'Valid Json input schema.'
exit $?
EOH
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
maintainer 'Gennady Potapov'
license 'Apache-2.0'
description 'Provides aws_cloudwatch_agent resource'
version '1.0.0'
version '1.0.1'
chef_version '>= 12.14' if respond_to?(:chef_version)

supports 'ubuntu', '= 14.04'
Expand Down

0 comments on commit cced38b

Please sign in to comment.