Created
April 10, 2020 08:53
-
-
Save gionn/c2dc03faca207a11b9a7084582281062 to your computer and use it in GitHub Desktop.
Chef recipe to automatically restart docker containers or generic services affected by an update of dynatrace agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby_block "check-restart-dynatrace" do | |
block do | |
headers = {:Authorization => "Api-Token #{provider['api_key']}"} | |
client = Chef::HTTP.new(provider['url']) | |
processes = JSON.parse(client.get('/processes?includeDetails=true', headers)) | |
hosts = JSON.parse(client.get('/hosts?includeDetails=true', headers)) | |
host_found = hosts.select { |host| host['discoveredName'] == node['hostname'] }.first | |
if host_found | |
host_id = host_found['entityId'] | |
processes.select { |process| process['fromRelationships']['isProcessOf'].include?(host_id) } | |
.select { |process| process['monitoringState']['restartRequired'] }.each do |p| | |
if p['metadata']['dockerContainerNames'] | |
container_name = p['metadata']['dockerContainerNames'].first | |
Chef::Log.info("#{container_name} needs restart") | |
resources(:docker_container => container_name).run_action(:restart) | |
else | |
begin | |
service_name = p['discoveredName'] | |
resources(:service => service_name).run_action(:restart) | |
rescue Chef::Exceptions::ResourceNotFound | |
Chef::Log.info("#{service_name} not found") | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment