Last active
July 20, 2018 21:58
-
-
Save andy-dufour/2ddff47c6d17bee1644c9c68abaecc10 to your computer and use it in GitHub Desktop.
Cleanup missing nodes in Chef Automate
This file contains 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
#!/opt/delivery/embedded/bin/ruby | |
require 'elasticsearch' | |
require 'json' | |
# Get the user configuration for the time threshold we will use to mark the | |
# nodes as missing. We read it from the delivery config if available, or use the | |
# environment variable which might be set or set it to the default of 12 hours. | |
threshold_in_mins = begin | |
if File.exist?("/etc/delivery/delivery-running.json") | |
config = JSON.parse(File.read("/etc/delivery/delivery-running.json")) | |
end | |
end || ENV['NODE_MISSING_THRESHOLD_MINS'] || 7200 | |
client = Elasticsearch::Client.new host: ENV['ESURL'] || 'localhost:9200' | |
# Initial Search | |
all_nodes = client.search index: 'node-state', | |
scroll: '5m', | |
size: '100', | |
body: { | |
query: { | |
match: { | |
status: "missing" | |
} | |
}, | |
_source: %w{ _type _id } | |
} | |
# Subsequent scrolling searches | |
loop do | |
# if all_nodes isn't truthy, we have no more results to process | |
break unless all_nodes | |
# if we received a scroll return but with empty results, we're done. | |
break if all_nodes['hits']['hits'].empty? | |
# Take the results returned by the scroll and update any documents that are missing | |
all_nodes['hits']['hits'].each do |node| | |
p node | |
client.delete index: 'node-state', | |
type: 'node-state', | |
id: node['_id'] | |
end | |
# Scroll at the end to fix the off-by 1 error. | |
all_nodes = client.scroll(body: { | |
scroll_id: all_nodes['_scroll_id'], | |
scroll: '5m' | |
}) | |
end | |
# refresh is set here because node-summary command will immediately use this information | |
client.indices.refresh index: 'node-state' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment