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

Feature: allow muting a host during an MSI install #778

Merged
merged 8 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Linting
  • Loading branch information
albertvaka committed Feb 12, 2021
commit c6253ec5acdd4c2a8834cd996fe1e635865a5391
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace :style do
fail_tags: ['correctness'],
tags: [
'~FC121', # Disables: Cookbook depends on cookbook made obsolete by Chef 14 (chef_handler)
'~FC014', # Disables: Consider extracting long ruby_block to library
]
}
end
Expand Down
26 changes: 11 additions & 15 deletions recipes/_install-windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ def unmute_host(context)
notifies :remove, 'package[Datadog Agent removal]', :immediately
end

if node['datadog']['windows_mute_hosts_during_install'] then
if not Chef::Datadog.application_key(node) then
if node['datadog']['windows_mute_hosts_during_install']
unless Chef::Datadog.application_key(node)
Chef::Log.error('windows_mute_hosts_during_install requires an application_key but it is not set.')
end
ruby_block 'Mute host while installing' do
Expand All @@ -172,21 +172,19 @@ def unmute_host(context)
require 'uri'
require 'json'
hostname = node['datadog']['hostname']
api_key = Chef::Datadog.api_key(node)
app_key = Chef::Datadog.application_key(node)
uri = URI.parse("https://api.datadoghq.com/api/v1/host/#{hostname}/mute")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request["Dd-Api-Key"] = api_key
request["Dd-Application-Key"] = app_key
request.content_type = 'application/json'
request['Dd-Api-Key'] = Chef::Datadog.api_key(node)
request['Dd-Application-Key'] = Chef::Datadog.application_key(node)
request.body = JSON.dump({
'message': 'Muted during install by datadog-chef cookbook',
'end': Time.now.getutc.to_i + (60*60), # Set to automatically unmute in 60 minutes
'end': Time.now.getutc.to_i + (60 * 60), # Set to automatically unmute in 60 minutes
})
response = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: true }) do |http|
http.request(request)
end
if response.code != '200' then
if response.code != '200'
Chef::Log.warn("Mute request failed with code #{response.code}: #{response.body}")
end
end
Expand All @@ -198,16 +196,14 @@ def unmute_host(context)
require 'uri'
require 'json'
hostname = node['datadog']['hostname']
api_key = Chef::Datadog.api_key(node)
app_key = Chef::Datadog.application_key(node)
uri = URI.parse("https://api.datadoghq.com/api/v1/host/#{hostname}/unmute")
request = Net::HTTP::Post.new(uri)
request["Dd-Api-Key"] = api_key
request["Dd-Application-Key"] = app_key
request['Dd-Api-Key'] = Chef::Datadog.api_key(node)
request['Dd-Application-Key'] = Chef::Datadog.application_key(node)
response = Net::HTTP.start(uri.hostname, uri.port, { use_ssl: true }) do |http|
http.request(request)
end
if response.code != '200' then
if response.code != '200'
Chef::Log.warn("Unmute request failed with code #{response.code}: #{response.body}")
end
end
Expand All @@ -234,7 +230,7 @@ def unmute_host(context)
else
success_codes [0, 3010]
end
if node['datadog']['windows_mute_hosts_during_install'] then
if node['datadog']['windows_mute_hosts_during_install']
notifies :run, 'ruby_block[Mute host while installing]', :before
notifies :run, 'ruby_block[Unmute host after installing]', :immediately
end
Expand Down