Created
December 27, 2012 23:44
-
-
Save paulmooring/4393245 to your computer and use it in GitHub Desktop.
orgmapper functions to delete data bags
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
| # return the data bag object given an org and databag name | |
| def get_bag(org, bag_name) | |
| if ORGS[org] == nil | |
| raise RuntimeError, "Bad org name provided" | |
| else | |
| bag = ORGS[org].databags[bag_name] | |
| bag.kind_of? Mixlib::Authorization::Models::DataBag or raise RuntimeError, "Bad data bag name provided" | |
| end | |
| bag | |
| end | |
| # return the CouchRest::Database object for an orgs child database | |
| def get_db(org) | |
| db = ORGS[org].org_db | |
| db.kind_of? CouchRest::Database or raise RuntimeError, "Unable to load database for #{org} org" | |
| db | |
| end | |
| # Return an array of hashes containing the couchdb_id and couchdb_rev of all couchdb documents for | |
| # a given data bag name in a given db | |
| def get_couch_values(db, data_bag_name) | |
| JSON.parse(RestClient.get "#{db.uri}/_design/data_bags/_view/all")['rows'].select do |row| | |
| row['value'].name == data_bag_name | |
| end.map do |doc| | |
| { | |
| :rev => doc['value'].couchdb_rev, | |
| :id => doc['value'].couchdb_id | |
| } | |
| end | |
| end | |
| # Deletes a data bag in an org | |
| def delete_data_bag(org_name, data_bag) | |
| begin | |
| bag = get_bag(org_name, data_bag) | |
| db = get_db(org_name) | |
| # This makes sure the databag hasn't already been deleted, in some cases the data bag is actually | |
| # gone but there's still some records pointint to it causing knife list to show the databag but | |
| # throw permissions error when it's used | |
| if bag.id == nil | |
| raise RuntimeError, "Can't find data bag id, has it been removed from couch already?" | |
| else | |
| # This grabs the id... | |
| bag_id = bag.id | |
| # ...destroys the data bag object... | |
| bag.destroy | |
| # ...and removes the documents in couch causing the problem mentioned above | |
| db.destroy_doc db.get(bag_id) | |
| end | |
| rescue RuntimeError => e | |
| puts "Couldn't load data bag or database: #{e.to_s}" | |
| rescue RestClient::ResourceNotFound | |
| puts "CouchDB document was already deleted" | |
| ensure | |
| docs = get_couch_values(get_db(org_name), data_bag) | |
| # if the data bag object is gone but the couchdb documents exist delete the documents | |
| if ORGS[org_name].databags.keys.grep(data_bag).empty? and not docs.empty? | |
| puts "I found some left over couch documents, trying to clean up..." | |
| docs.each do |doc| | |
| get_db(org_name).delete_doc({ | |
| "_id" => doc[:id], | |
| "_rev" => doc[:rev] | |
| }) | |
| end | |
| puts "All clean" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment