Cron job and sprinkle script for weekly CouchDB compaction

Language: Ruby

# http://github.com/crafterm/sprinkle
# http://couchdb.apache.org/
# http://wiki.apache.org/couchdb/API_Cheatsheet

# Requires the rubygems: rubygems right_aws right_http_connection json 

package :couchdb_compaction do
  config_file = '/etc/cron.weekly/couchdb_compaction'
  config_text = %q[
#!/usr/bin/ruby
%w(rubygems right_aws right_http_connection json).each { |f| require f }

dbs = JSON.parse %x(curl -sS 'http://localhost:5984/_all_dbs')

dbs.each do |db|
  %x(curl -X POST -sS 'http://localhost:5984/#{db}/_compact')
  %x(curl -X POST -sS 'http://localhost:5984/#{db}/_view_cleanup')

  design_docs = JSON.parse %x(curl -sS 'http://localhost:5984/#{db}/_all_docs?startkey=%22_design%2F%22&endkey=%22_design0%22')

  design_docs.each do |doc|
    name = doc[1][0]['id'].gsub('_design/', '') rescue nil
    %x(curl -X POST -sS 'http://localhost:5984/#{db}/_compact/#{name}') if name
  end
end
].lstrip

  push_text config_text, config_file do
    pre :install, "touch #{config_file}"
    post :install, "chmod 0755 #{config_file}"
  end

  verify do
    has_file config_file
  end
end
Reveal More
Added about 2 years ago by Costco_normal trevorturk