def pull_production(self, domain='yourdomainhere'): cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) results = ch.get_view('dns', 'configs') doc = None doc_list = ['production', 'staging'] for r in results.rows: if r.value['environment'] in ['production', 'staging']: doc_list.remove(r.value['environment']) doc = couchdb.Document(r.value) if doc: if doc["config"] != cotendo.dns.config: doc["config"] = cotendo.dns.config doc["token"] = cotendo.dns.token ch.save(doc) for d in doc_list: doc = couchdb.Document({ "doc_type": "dns", "domain": domain, "environment": d, "token": cotendo.dns.token, "config": cotendo.dns.config }) ch.save(doc)
def save_edit(self, domain='yourdomainhere'): cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) # Update CouchDB to have a copy of the latest dns entries. results = ch.get_view('dns', 'configs') doc = None for r in results.rows: if r.value['environment'] == 'staging': doc = couchdb.Document(r.value) break if doc: if doc["config"] != cotendo.dns.config: doc["config"] = cotendo.dns.config ch.save(doc) else: doc = couchdb.Document({ "doc_type": "dns", "domain": domain, "environment": "staging", "token": cotendo.dns.token, "config": cotendo.dns.config }) ch.save(doc) return redirect(url(controller="dns", action="diff"))
def remove_record(self): for item in request.params.iteritems(): params = json.loads(item[0]) break domain = params['domain'] cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) # check if record exists, if it does, update it otherwise results = ch.get_view('dns', 'configs') doc = None for r in results.rows: if r.value['environment'] == 'staging': cotendo.ImportDNS(r.value["config"], r.value['token']) doc = couchdb.Document(r.value) break cotendo.dns.del_record(params['type'], params['host']) if doc: if doc["config"] != cotendo.dns.config: doc["config"] = cotendo.dns.config doc["token"] = cotendo.dns.token ch.save(doc) else: doc = couchdb.Document({ "doc_type": "dns", "domain": domain, "environment": "staging", "token": cotendo.dns.token, "config": cotendo.dns.config }) ch.save(doc)
def saveResults(db, nid, nodes, vbuckets, got, missing, alg): failed_nodes = [{ 'id': n.id, 'active': n.active, 'replica': n.replica, 'failed': n.failed } for n in nodes if n.failed] doc = couchdb.Document(_id=str(uuid.uuid1())) doc.update({ 'test': nid, 'n_nodes': len(nodes), 'n_vbuckets': vbuckets, 'n_dead_nodes': len(failed_nodes), 'n_alive_nodes': len(nodes) - len(failed_nodes), 'failed': failed_nodes, 'missing': list(missing) }) global SAVING SAVING.append(doc) if len(SAVING) > BATCH_SIZE: print "Storing a batch" db.update(SAVING) SAVING = []
def edit_record(self, domain='yourdomainhere', type=None, host=''): c.title = "Nodetraq -- DNS - Edit Record" c.selected_page = "dns" c.subpage = "edit_record" c.domain = domain cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) # check if record exists, if it does, update it otherwise results = ch.get_view('dns', 'configs') doc = None for r in results.rows: if r.value['environment'] == 'staging': cotendo.ImportDNS(r.value["config"], r.value['token']) doc = couchdb.Document(r.value) break c.record = cotendo.dns.get_record(type, host) if not c.record: session['flash'] = "Not a valid record" session.save() return redirect( url(controller="dns", action="index", domain=domain)) return render('/dns/edit_record.mako')
def write( self, uid, content ): #-------------------------------------------------------------------------- """ Write received list of services into the according couchDB document. The invoker of this method should have supremacy to write changes to a service document due to the fact that the method retrieves current '_rev' and retries until it can override existing information without raising a couchDB resource conflict. ANY PREVIOUS CONTENT OF THE DOCUMENT WILL BE DISCARDED!!! If the document doesn't exist, a new one will be generated. :param uid: '_id' of the target document :param content: data which will override the previous document body """ content = simplejson.loads(content) document = couchdb.Document() document = content document["_id"] = uid while True: try: rev = self.database.get(uid) if rev is not None: rev = rev["_rev"] document["_rev"] = rev self.database.save(document) break except couchdb.http.ResourceConflict: continue
def row2doc(row, table): doc = couchdb.Document() doc['type'] = table.name for col in table.columns.values(): load_value(doc, col, row) return doc
def create_tokens_database(port, uuid, token_value): tokens_database = couchdb.Server( 'http://127.0.0.1:%d' % port).create('tokens') token = couchdb.Document() token['_id'] = hashlib.sha512(token_value).hexdigest() token['user_id'] = uuid token['type'] = 'Token' tokens_database.save(token)
def update_tina_doc(doc_id, update_doc_body): """ Updates the document with _id corresponding to doc_id. Any keys not present will be added, any keys present will have values overwritten, and keys that aren't in the updated dictionary will be removed from the document. """ tina_db = TinaCouchDB._get_tina_db() old_doc = TinaCouchDB.get_tina_doc(doc_id) new_doc = couchdb.Document({ '_id': old_doc['_id'], '_rev': old_doc['_rev'] }) new_doc.update(update_doc_body) tina_db.save(new_doc)
def index(self, domain='yourdomainhere'): c.title = "Nodetraq -- DNS" c.selected_page = "dns" c.subpage = "list" c.domain = domain cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) ch.select("nodetraq") # Sync views ch.sync_view('nodetraq/model/views') # Update CouchDB to have a copy of the latest dns entries. results = ch.get_view('dns', 'configs') doc = None for r in results.rows: if r.value['environment'] == 'staging': doc = couchdb.Document(r.value) cotendo.ImportDNS(r.value["config"], r.value['token']) if doc: if doc["config"] != cotendo.dns.config: doc["config"] = cotendo.dns.config doc["token"] = cotendo.dns.token ch.save(doc) if not doc: doc = couchdb.Document({ "doc_type": "dns", "domain": domain, "environment": "staging", "token": cotendo.dns.token, "config": cotendo.dns.config }) ch.save(doc) c.records = cotendo.dns._entries return render('dns/list.mako')
def get_stats(ndays = 30): """Returns the stats for the past `ndays`""" docs = [couchdb.Document(doc) for doc in _get_count_docs(ndays)] retval = dict(human_edits = Stats(docs, "human_edits", "human_edits"), bot_edits = Stats(docs, "bot_edits", "bot_edits"), lists = Stats(docs, "lists", "total_lists"), visitors = Stats(docs, "visitors", "visitors"), loans = Stats(docs, "loans", "loans"), members = Stats(docs, "members", "total_members"), works = Stats(docs, "works", "total_works"), editions = Stats(docs, "editions", "total_editions"), ebooks = Stats(docs, "ebooks", "total_ebooks"), covers = Stats(docs, "covers", "total_covers"), authors = Stats(docs, "authors", "total_authors"), subjects = Stats(docs, "subjects", "total_subjects")) return retval
def revert(self, domain='yourdomainhere'): c.title = "Nodetraq -- DNS - Publish" c.selected_page = "dns" c.subpage = "publish" c.domain = domain results = ch.get_view('dns', 'configs') for r in results.rows: if r.value['environment'] == 'staging': doc = couchdb.Document(r.value) break c.rev_list = [] for d in ch.db.revisions(doc['_id']): c.rev_list.append((d['_rev'], d['_rev'])) return render('/dns/revert.mako')
def addChild(self, name, val): if isinstance(val, PageHandler): if val.interesting: try: print unicode(val) except UnicodeEncodeError: print "(Found one, but couldn't print it...)" pass doc = couchdb.Document(_id=val.title, text=val.text, revinfo=val.revdata) if val.is_redirect: doc['is_redirect'] = True if val.geo: doc['geo'] = val.geo self.allDocs.append(doc) if len(self.allDocs) >= 10000: self.commit()
def check_iota_sync(email, base_url="http://couchdb:5984/"): # list all documents associated with user server = couchdb.Server(base_url) db = server['medblocks'] docs = [db[medblock.id] for medblock in db.view('preview/patient', key=email)] # Decompose document into constituants db_medfrags = to_set(decompose_medblocks(docs)) # Get all associated transactions with address iota_medfrags = to_set(retrieve_from_tangle(email)) transmit_to_iota = db_medfrags - iota_medfrags # print(transmit_to_iota) # print("Printing Difference set") # print([i['tag'] for i in to_dict_list(transmit_to_iota)]) reconstruction_medfrags = iota_medfrags | db_medfrags reconstruction_medfrags = to_dict_list(reconstruction_medfrags) new_documents = reconstruct_medblocks(reconstruction_medfrags) # include _rev print("Newly constructed docs...") print(new_documents) for i in range(len(new_documents)): id = new_documents[i]['_id'] try: new_documents[i]['_rev'] = db['_id'].rev except couchdb.http.ResourceNotFound: pass new_documents[i] = couchdb.Document(new_documents[i]) # print(new_documents) print(len(new_documents)) print("Updating {} documents on the database".format(len(new_documents))) db.update(new_documents) print("Transmitting {} elements to the tangle".format(len(transmit_to_iota))) async_broadcast_on_tangle.delay(to_dict_list(transmit_to_iota)) return
def check_iota_sync(email): # list all documents associated with user db = server['medblocks'] results, iota_new = retrieve_from_tangle(email) simple_sync = True if simple_sync: docs = [ db[medblock.id] for medblock in db.view('preview/patient', key=email) ] db_medfrags = to_set(approved_decompose_medblocks(docs)) iota_medfrags = to_set(results) transmit_to_iota = db_medfrags - iota_medfrags print("DB MEDFRAGS: {} , IOTA MEDFRAGS: {}".format( len(db_medfrags), len(iota_medfrags))) db_update = len(iota_medfrags - db_medfrags) > 0 if len(transmit_to_iota) > 0: print("Transmitting {} transaction to IOTA".format( len(transmit_to_iota))) broadcast_on_tangle(to_dict_list(transmit_to_iota)) if db_update: print("Difference {}".format(iota_medfrags - db_medfrags)) reconstruction_medfrags = iota_medfrags | db_medfrags reconstruction_medfrags = to_dict_list(reconstruction_medfrags) new_documents = reconstruct_medblocks(reconstruction_medfrags) print("Updating {} documents on the database".format( len(new_documents))) for doc in new_documents: id = doc['_id'] doc = couchdb.Document(doc) try: old_document = db[id] doc['_rev'] = old_document.rev db.save(doc) except couchdb.http.ResourceNotFound: db[id] = doc return True
def get_document_diff(self, domain='yourdomainhere', rev=None): cotendo = NodetraqCotendo() cotendo.GrabDNS(domain, 1) results = ch.get_view('dns', 'configs') for r in results.rows: if r.value['environment'] == 'staging': doc = couchdb.Document(r.value) break for d in ch.db.revisions(doc['_id']): if d['_rev'] == rev: doc = d if rev: c.diff = ''.join( difflib.unified_diff(doc['config'].splitlines(1), cotendo.dns.config.splitlines(1))) else: c.diff = ''.join(cotendo.dns.config.splitlines(1), difflib.unified_diff(doc['config'].splitlines(1))) return render('/diff/diff_table.mako')