Skip to content

Instantly share code, notes, and snippets.

@odyssey4me
Forked from gilles/compact_chef_couchdb.rb
Last active December 22, 2015 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odyssey4me/6421703 to your computer and use it in GitHub Desktop.
Save odyssey4me/6421703 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# vim:ts=2:expandtab
require 'net/http'
require 'uri'
require 'json'
#http://wiki.apache.org/couchdb/Compaction
STDOUT.sync = true
print "Checking chef database size."
uri = URI.parse("http://localhost:5984/chef")
res = Net::HTTP.get_response(uri)
current_size = JSON::parse(res.body)["disk_size"]
if current_size > 100_000_000
print " Current size of #{current_size} is too large. Initiating compact process.\n"
uri = URI.parse('http://localhost:5984/chef/_compact')
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.request_uri)
req.set_form_data({})
req["Content-Type"] = "application/json"
res = http.request(req)
puts JSON.pretty_generate(JSON.parse(res.body))
else
print " Current size of #{current_size} is fine. Compacting not necessary.\n"
end
%w(nodes roles registrations clients data_bags data_bag_items users checksums cookbooks sandboxes environments id_map).each do |view|
begin
uri = URI.parse("http://localhost:5984/chef/_design/#{view}/_info")
res = Net::HTTP.get_response(uri)
current_size = JSON::parse(res.body)["view_index"]["disk_size"]
print "Checking view size for #{view}."
if current_size > 100_000_000
print " Current size of #{current_size} is too large. Initiating compact process.\n"
uri = URI.parse("http://localhost:5984/chef/_compact/#{view}")
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new(uri.request_uri)
req.set_form_data({})
req["Content-Type"] = "application/json"
res = http.request(req)
puts JSON.pretty_generate(JSON.parse(res.body))
else
print " Current size of #{current_size} is fine. Compacting not necessary.\n"
end
rescue
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment