def registration(doc, from_undo=False): router = get_router_id() data = urllib.urlencode({'service': doc['service'], 'userdetails': doc['user']}) hdr = {"Content-type": "application/x-www-form-urlencoded"} if len(router) > 0: try: req = urllib2.Request("%s/%s/register" % (base, router), data, hdr) conn = urllib2.urlopen(req) code = conn.getcode() if code == 200: response = conn.read() doc['suid'] = response doc['status'] = 'done' title = 'Added notification registration' desc = ('Added %s as %s for %s' % (doc['user'], prompts[doc['service']], doc['name'])) if from_undo is True: title = 'Undo removal of notification registration' ts = doc['event_timestamp'] if 'event_timestamp' in doc else None doc_arr = [{'doc_id': doc['_id'], 'doc_rev': doc['_rev'], 'doc_collection': 'notifications', 'action': 'add'}] add_history_item(title, desc, doc_arr, True, ts=ts) if 'event_timestamp' in doc: del doc['event_timestamp'] except urllib2.HTTPError, e: doc['status'] = 'error' except urllib2.URLError, e: doc['status'] = 'error'
def delete(doc, from_undo=False): router = get_router_id() data = urllib.urlencode({'suid': doc['suid']}) hdr = {'Content-Type': 'application/x-www-form-urlencoded'} if len(router) > 0: ret_val = 0 try: req = urllib2.Request("%s/%s/delete" % (base, router), data, hdr) conn = urllib2.urlopen(req) code = conn.getcode() title = 'Removed notification registration' desc = ('Removed %s as %s for %s' % (doc['user'], prompts[doc['service']], doc['name'])) if from_undo is True: title = 'Undo adding notification registration' ts = doc['event_timestamp'] if 'event_timestamp' in doc else None doc_arr = [{'doc_id': doc['_id'], 'doc_rev': doc['_rev'], 'doc_collection': 'notifications', 'action': 'delete'}] add_history_item(title, desc, doc_arr, True, ts=ts) if 'event_timestamp' in doc: del doc['event_timestamp'] del doc['suid'] doc['status'] = 'done' except urllib2.HTTPError, e: doc['status'] = 'error' except urllib2.URLError, e: doc['status'] = 'error'
def process_wifi(doc, from_undo=False): bss = True if 'with_bss' in doc and doc['with_bss'] is True: line_list = generate_config(doc) doc['bss_active'] = True else: line_list = generate_config(doc, False) doc['bss_active'] = False bss = False devices = get_connected_devices() doc['with_bss'] = True doc['status'] = 'done' ts = None if 'event_timestamp' in doc: ts = doc['event_timestamp'] del doc['event_timestamp'] title = "New WiFi Configuration" desc = "WiFi configuration has been updated. " desc += "You will need to reconnect your devices" undoable = True if from_undo is True: title = "Undo change to WiFi Configuration" if doc['_rev'].startswith('1-'): title = 'Initial WiFi Configuration' desc = 'WiFi initialised with default values.' undoable = False doc_arr = [{ 'doc_id': doc['_id'], 'doc_rev': doc['_rev'], 'doc_collection': 'wifi', 'action': 'edit' }] add_history.add_history_item(title, desc, doc_arr, undoable=undoable, ts=ts) db.save_doc(doc, force_update=True) if 'ENV_TESTS' not in os.environ: write_config_file(line_list) if notify(devices, doc['ssid']): reload_hostapd() if bss is True: add_vlan_to_bridge() scheduler = BackgroundScheduler() cur_time = datetime.datetime.now() dt = cur_time + datetime.timedelta(days=1) scheduler.add_job(remove_vlan.remove_vlan, 'date', run_date=dt) scheduler.start()
def rollback(self): if 'id' in self.change: the_id = self.change['id'] the_rev = self.change['changes'][0]['rev'] current_doc = self.db.get(the_id, rev=the_rev) r = self.revert(current_doc['timestamp']) dt = dateutil.parser.parse(current_doc['timestamp']) dt = dt.astimezone(tzlocal()) add_history_item("Rollback", "Roll back to %s" % (dt.isoformat(' ')), r, undoable=False) current_doc['status'] = 'done' res = self.db.save_doc(current_doc, force_update=True) return res
def break_dns(current_doc): title = 'Internet Connection Error' desc = "An error occurred when requesting DNS lookups. Check your settings." ts = current_doc[ 'event_timestamp'] if 'event_timestamp' in current_doc else None doc_arr = [{ 'doc_id': current_doc['_id'], 'doc_rev': current_doc['_rev'], 'doc_collection': 'dns', 'action': 'edit' }] add_history_item(title, desc, doc_arr, undoable=False, ts=ts) if 'event_timestamp' in current_doc: del current_doc['event_timestamp'] current_doc['status'] = 'done' db.save_doc(current_doc, force_update=True)
def edit_device(current_doc, from_undo=False): title = 'Edited device details' desc = 'Edited details for %s' % (current_doc['device_name']) if from_undo is True: title = 'Undo %s' % (title) desc = 'Undo %s' % (desc) ts = current_doc[ 'event_timestamp'] if 'event_timestamp' in current_doc else None doc_arr = [{ 'doc_id': current_doc['_id'], 'doc_rev': current_doc['_rev'], 'doc_collection': 'devices', 'action': 'edit' }] add_history_item(title, desc, doc_arr, True, ts=ts) if 'event_timestamp' in current_doc: del current_doc['event_timestamp'] db.save_doc(current_doc, force_update=True)
def set_dns(current_doc, from_undo=False): title = 'DNS settings updated' desc = "You are now using Google's DNS servers." undoable = True if from_undo is True: title = "Undo DNS change" desc = "DNS configuration change has been undone. You are no longer using Google's DNS Servers." undoable = False ts = current_doc[ 'event_timestamp'] if 'event_timestamp' in current_doc else None doc_arr = [{ 'doc_id': current_doc['_id'], 'doc_rev': current_doc['_rev'], 'doc_collection': 'dns', 'action': 'edit' }] add_history_item(title, desc, doc_arr, undoable=undoable, ts=ts) if 'event_timestamp' in current_doc: del current_doc['event_timestamp'] current_doc['status'] = 'done' db.save_doc(current_doc, force_update=True)
#!/usr/bin/python import urllib2 from add_history import add_history_item import couchdb_config_parser db = couchdb_config_parser.get_db() doc = db.get('connection_state') req = urllib2.Request('http://rjspencer1989.koding.io') try: response = urllib2.urlopen(req) if doc['state'] != 'connected': doc['state'] = 'connected' res = db.save_doc(doc, force_update=True) add_history_item('Internet Connection restored', 'Internet should be working again', res['id'], res['rev'], 'connection_state', undoable=False) except urllib2.URLError as e: if doc['state'] != 'disconnected': doc['state'] = 'disconnected' res = db.save_doc(doc, force_update=True) add_history_item('Internet connection error', str(e.reason), res['id'], res['rev'], 'connection_state', undoable=False)