def run(self): """ Start the synchronization process """ while True: if self.finished: break time.sleep(SYNC_PERIOD_SEC) # get previous successfully synchronized sequence from global server prev_seq_n = int(self.get_previous_seq_num()) """ COMMENT THIS LINE (used for tests) prev_seq_n = 2 """ if prev_seq_n == -1: continue #print "Previous sequence: " + str(prev_seq_n) # if synch everything, then do not use the filter if self.graph == GLOBAL_TARGET_KEYSPACE: stream = ChangesStream(self.ers.public_db, feed="normal", since=prev_seq_n, include_docs=True, limit=LIMIT_MAXSEQ_PER_OP) else: stream = ChangesStream(self.ers.public_db, feed="normal", since=prev_seq_n, include_docs=True, limit=LIMIT_MAXSEQ_PER_OP, filter="filter/by_graph", g=self.graph) # dump to file the changes and post it to global server is not empty last_seq_n = self.dump_changes_to_file(prev_seq_n, stream) # are there entities to be synchronized? if last_seq_n == prev_seq_n: print 'Nothing new ...' continue """ Now upload the changes file to global server. """ r = requests.post( "http://" + GLOBAL_SERVER_HOST + ":" + str(GLOBAL_SERVER_PORT) + GLOBAL_SERVER_HTTP_BULKRUN, files={self.output_filename: open(self.output_filename, 'rb')}, data={"g": "<" + self.graph + ">"}) if r.status_code == 200: # it means that this step of synchronization is working """ UNCOMMENT NEXT LINE (not used in case of testing) """ self.set_new_seq_num(last_seq_n) print 'Last synchronization sequence number is ' + str( last_seq_n)
def test_wifi(self): inc = { "collection": "wifi", "status": "pending", "ssid": "spencer", "encryption_type": "wpa", "channel": 1, "mode": "g", "password_type": "txt", "password": "******" } not_inc = { "collection": "wifi", "status": "done", "ssid": "spencer", "encryption_type": "wpa", "channel": 1, "mode": "g", "password_type": "txt", "password": "******" } db = couchdb_config_parser.get_db() res = db.save_doc(inc) res2 = db.save_doc(not_inc) stream = ChangesStream(db, filter="homework-remote/wifi") self.assertEqual(1, len(list(stream))) self.assertEqual(res['id'], list(stream)[0]['id']) inc['hidden'] = True not_inc['hidden'] = True db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True)
def test_notifications(self): inc = { "collection": "notifications", "status": "pending", "name": "Rob", "service": "email", "user": "******" } not_inc = { "collection": "notifications", "status": "done", "name": "Rob", "service": "email", "user": "******" } db = couchdb_config_parser.get_db() res = db.save_doc(inc) res2 = db.save_doc(not_inc) stream = ChangesStream(db, filter="homework-remote/notifications") self.assertEqual(1, len(list(stream))) self.assertEqual(res['id'], list(stream)[0]['id']) inc['hidden'] = True not_inc['hidden'] = True db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True)
def test_undo(self): inc = { "collection": "events", "perform_undo": True, "undoable": True, "title": "test", "description": "test", "docs": [{ "doc_id": "aabbcc", "doc_rev": "1-aabbcc", "doc_collection": "notifications", "action": "add" }], "timestamp": datetime.datetime.now(tzutc()).isoformat() } not_inc_not_perform_undo = { "collection": "events", "perform_undo": False, "undoable": True, "title": "test", "description": "test", "docs": [{ "doc_id": "aabbcc", "doc_rev": "2-aabbcc", "doc_collection": "notifications", "action": "add" }], "timestamp": datetime.datetime.now(tzutc()).isoformat() } not_inc = { "collection": "events", "perform_undo": False, "undoable": False, "title": "test", "description": "test", "docs": [{ "doc_id": "aabbcc", "doc_rev": "4-aabbcc", "doc_collection": "notifications", "action": "add" }], "timestamp": datetime.datetime.now(tzutc()).isoformat() } db = couchdb_config_parser.get_db() res = db.save_doc(inc) res2 = db.save_doc(not_inc_not_perform_undo) res3 = db.save_doc(not_inc) stream = ChangesStream(db, filter="homework-remote/undo") self.assertEqual(1, len(list(stream))) self.assertEqual(res['id'], list(stream)[0]['id']) inc['_deleted'] = True not_inc_not_perform_undo['_deleted'] = True not_inc['_deleted'] = True db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True) db.save_doc(not_inc_not_perform_undo, force_update=True)
def test_devices_edit(self): not_inc = { "_id": "00:11:22:33:44:55", "action": "permit", "collection": "devices", "device_name": "test-device", "host_name": "test-device", "ip_address": "10.2.0.61", "lease_action": "add", "mac_address": "aa:bb:cc:dd:ee:ff", "name": "Rob", "state": "pending", "device_type": "laptop", "notification_service": "email", "timestamp": time.time(), "connection_event": "disconnect", "changed_by": "user" } inc = { "_id": "00:11:22:33:44:56", "action": "", "collection": "devices", "device_name": "test-device2", "host_name": "test-device", "ip_address": "10.2.0.65", "lease_action": "add", "mac_address": "ab:bc:cd:de:ef:fa", "name": "Rob", "state": "permit", "device_type": "laptop", "notification_service": "twitter", "timestamp": time.time(), "connection_event": "disconnect", "changed_by": "user" } db = couchdb_config_parser.get_db() db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True) stream = ChangesStream(db, filter="homework-remote/edit_device") self.assertEqual(len(list(stream)), 1) self.assertEqual("00:11:22:33:44:56", list(stream)[0]['id']) inc['_deleted'] = True not_inc['_deleted'] = True db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True)
def check_device_status(self): remote_stream = ChangesStream(self.selected_db, heartbeat=True, since=self.last_seq, filter='homework-remote/devices_pox') for change in remote_stream: self.last_seq = change['seq'] the_id = change['id'] the_rev = change['changes'][0]['rev'] doc = self.selected_db.open_doc(the_id, rev=the_rev) prompt = True if (doc['state'] == 'pending') else False doc_arr = [{'doc_id': the_id, 'doc_rev': the_rev, 'doc_collection': 'devices', 'action': 'edit'}] strings = self.get_history_strings(doc['device_name'], doc['action']) timestamp = doc['event_timestamp'] if 'event_timestamp' in doc else None add_history_item(strings['title'], strings['desc'], docs=doc_arr, undoable=True, prompt=prompt, ts=timestamp) device = {'mac': EthAddr(doc['mac_address']), 'action': doc['action']} devices = [device] self.raiseEvent(DeviceStateChange(devices)) core.callDelayed(1, self.check_device_status)
def test_revert(self): inc = { "collection": "request_revert", "status": "pending", "timestamp": "2015-05-27T14:10:53.829Z" } not_inc = { "collection": "request_revert", "status": "done", "timestamp": "2015-05-27T14:10:53.829Z" } db = couchdb_config_parser.get_db() res = db.save_doc(inc) res2 = db.save_doc(not_inc) stream = ChangesStream(db, filter="homework-remote/revert") self.assertEqual(1, len(list(stream))) self.assertEqual(res['id'], list(stream)[0]['id']) inc['_deleted'] = True not_inc['_deleted'] = True db.save_doc(inc, force_update=True) db.save_doc(not_inc, force_update=True)
def test_change(): with ChangesStream(self.db, feed="continuous") as stream: for change in stream: lines.append(change)
def test_change(): with ChangesStream(self.db, feed="longpoll") as stream: for change in stream: self.assert_(change["seq"] == 1)
# Fast track to this sequence sequence = result['last_seq'] # Go get all the current zones. zones = c.fetch() for zone in zones['results']: domain = zone['id'] try: doc = db.get(docid=domain) except ResourceNotFound, e: click.echo('%s not found (this is normal if the zone was deleted)' % domain) else: zone_update(domain, doc['data'], zone_dir) sequence_write(sequence_file, sequence) # Keep track of our sync point click.echo('Fast track syncing done') with ChangesStream(db, since=sequence, feed='continuous', heartbeat=True) as stream: click.echo('Waiting for changes...') try: for change in stream: domain = change['id'] seq = change['seq'] if change.get('deleted', False) is True: click.echo("%s Delete for %s." % (seq, domain)) try: zone_delete(domain, zone_dir) except Exception as e: # TODO: Add some alerting here click.echo(e) else: click.echo("%s Create/Update for %s." % (seq, domain)) try: