def init_peers(): ram_peers = RAMCollection('peers') peers = SQLCollection('peers') for objuuid in peers.list_objuuids(): ram_peer = ram_peers.get_object(objuuid) ram_peer.object = peers.get_object(objuuid).object ram_peer.set()
def delete_peers(): peers = SQLCollection('peers') for peer in peers.find(): peer.destroy() peers = RAMCollection('peers') for peer in peers.find(): peer.destroy()
def forward_cascade_request(message): ctr_increment('cascades forwarded') peers = SQLCollection('peers') for objuuid in peers.list_objuuids(): try: message['dest'] = peers.get_object(objuuid).object['agtuuid'] except: peers.get_object(objuuid).destroy() try: if message['dest'] != kvstore.get(name='agtuuid'): push_message(message) except: pass
def execute(scruuid): script = SQLCollection('scripts').get_object(scruuid) status, stdout, stderr = interpret(script.object['body']) script.object['status'] = status script.object['stdout'] = stdout script.object['stderr'] = stderr if script.object['silent'] in (False, 'false'): script.set() ctr_decrement('threads (script-{0})'.format(scruuid)) jobs_lock.acquire() del jobs[scruuid] jobs_lock.release()
def execute(cronuuid): try: cron = SQLCollection('crons').get_object(cronuuid) status, stdout, stderr = process_sync(cron.object['command'], timeout=cron.object['timeout']) cron.object['stdout b64data'] = b64encode(stdout).decode() cron.object['stderr b64data'] = b64encode(stderr).decode() cron.object['status'] = status cron.set() except: pass ctr_decrement('threads (cron-{0})'.format(cronuuid)) jobs_lock.acquire() del jobs[cronuuid] jobs_lock.release()
def prune(): routes = RAMCollection('routes') peers = RAMCollection('peers') peer_agtuuids = [] for peer in peers.find(): try: if 'destroy time' in peer.object: if peer.object['destroy time'] < time(): peer.destroy() else: peer_agtuuids.append(peer.object['agtuuid']) else: peer_agtuuids.append(peer.object['agtuuid']) except: peer.destroy() peers = SQLCollection('peers') for peer in peers.find(): try: if 'destroy time' in peer.object: if peer.object['destroy time'] < time(): peer.destroy() else: peer_agtuuids.append(peer.object['agtuuid']) else: peer_agtuuids.append(peer.object['agtuuid']) except: peer.destroy() for route in routes.find(): try: if (len(peers.find(agtuuid=route.object['agtuuid'])) > 0 or route.object['agtuuid'] == cherrypy.config.get('agtuuid') or route.object['gtwuuid'] not in peer_agtuuids): route.destroy() except: route.destroy()
def create_peer(agtuuid, url=None, ttl=None, polling=False): collection = SQLCollection('peers') peers = collection.find(agtuuid=agtuuid) if len(peers) == 1: peer = peers[0] else: peer = collection.get_object() peer.object = {'agtuuid': agtuuid, 'url': url, 'polling': polling} if ttl != None: peer.object['destroy time'] = time() + ttl peer.object['refresh time'] = time() + PEER_REFRESH peer.set() collection = RAMCollection('peers') peers = collection.find(agtuuid=agtuuid) if len(peers) == 1: peer = peers[0] else: peer = collection.get_object() peer.object = {'agtuuid': agtuuid, 'url': url, 'polling': polling} if ttl != None: peer.object['destroy time'] = time() + ttl peer.object['refresh time'] = time() + PEER_REFRESH peer.set() return peer
def execute_cascade_request(message): ctr_increment('cascades executed') request = message['request'] response = {} try: if request['type'] == 'process sync': if 'timeout' in request: status, stdout, stderr = process_sync( request['command'], timeout=request['timeout']) else: status, stdout, stderr = process_sync(request['command']) response['type'] = request['type'] response['stdout'] = b64encode(stdout).decode() response['stderr'] = b64encode(stderr).decode() response['status'] = status if message['anonymous'] == False: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'file read': response['type'] = request['type'] response['b64data'] = b64encode(file_read( request['filename'])).decode() push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'file write': file_write(request['filename'], b64decode(request['b64data'])) elif request['type'] == 'delete route': for route in RAMCollection('routes').find( agtuuid=request['agtuuid']): route.destroy() for route in RAMCollection('routes').find( gtwuuid=request['agtuuid']): route.destroy() elif request['type'] == 'find collection objects': collection = SQLCollection(request['name']) response = [] for objuuid in collection.find_objuuids(**request['query']): response.append(collection.get_object(objuuid).object) if len(response) > 0: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) elif request['type'] == 'execute python': response['status'], response['stdout'], response[ 'stderr'] = interpret(request['body']) if message['anonymous'] == False: push_message({ 'type': 'cascade response', 'dest': message['src'], 'cscuuid': message['cscuuid'], 'response': response, 'src': kvstore.get(name='agtuuid') }) except: pass
def delete_peer(agtuuid): for peer in RAMCollection('peers').find(agtuuid=agtuuid): peer.destroy() for peer in SQLCollection('peers').find(agtuuid=agtuuid): peer.destroy()
temp['gtwuuid'] = cherrypy.config.get('agtuuid') advertisement['routes'].append(temp) except: peer.destroy() return advertisement def init_peers(): ram_peers = RAMCollection('peers') peers = SQLCollection('peers') for objuuid in peers.list_objuuids(): ram_peer = ram_peers.get_object(objuuid) ram_peer.object = peers.get_object(objuuid).object ram_peer.set() collection = SQLCollection('peers') collection.create_attribute('agtuuid', "['agtuuid']") collection = RAMCollection('peers') collection.create_attribute('agtuuid', "['agtuuid']") collection = RAMCollection('routes') collection.create_attribute('agtuuid', "['agtuuid']") collection.create_attribute('gtwuuid', "['gtwuuid']") collection.create_attribute('weight', "['weight']") init_peers()
def worker(): global last_worker_time collection = SQLCollection('scripts') for scruuid in collection.list_objuuids(): try: script = collection.get_object(scruuid) if 'enabled' not in script.object: script.object['enabled'] = False script.set() if 'silent' not in script.object: script.object['silent'] = False script.set() if 'seconds' not in script.object: script.object['seconds'] = '0' script.set() if 'minutes' not in script.object: script.object['minutes'] = '*' script.set() if 'hours' not in script.object: script.object['hours'] = '*' script.set() if 'dayofmonth' not in script.object: script.object['dayofmonth'] = '*' script.set() if 'dayofweek' not in script.object: script.object['dayofweek'] = '*' script.set() if 'year' not in script.object: script.object['year'] = '*' script.set() if 'body' not in script.object: script.object['body'] = '' script.set() if script.object['enabled'] in (True, 'true'): for t in range(int(last_worker_time), int(time())): now = datetime.fromtimestamp(t).now() if (eval_cron_field(script.object['seconds'], now.second) and eval_cron_field(script.object['minutes'], now.minute) and eval_cron_field(script.object['hours'], now.hour) and eval_cron_field(script.object['dayofmonth'], now.day) and eval_cron_field(script.object['dayofweek'], now.weekday()) and eval_cron_field(script.object['year'], now.year)): queue(scruuid) break except: if script.object['silent'] in (False, 'false'): script = collection.get_object(scruuid) script.object['status'] = 1 script.object['stdout'] = '' script.object['stderr'] = str(traceback.format_exc()) script.set() last_worker_time = time() register_timer(name='script_worker', target=worker, timeout=1).start()
def execute(scruuid): script = SQLCollection('scripts').get_object(scruuid) status, stdout, stderr = interpret(script.object['body']) script.object['status'] = status script.object['stdout'] = stdout script.object['stderr'] = stderr if script.object['silent'] in (False, 'false'): script.set() ctr_decrement('threads (script-{0})'.format(scruuid)) jobs_lock.acquire() del jobs[scruuid] jobs_lock.release() collection = SQLCollection('scripts') collection.create_attribute('name', "['name']") collection.create_attribute('enabled', "['enabled']") collection.create_attribute('silent', "['silent']") collection.create_attribute('status', "['status']") collection.create_attribute('stdout', "['stdout']") collection.create_attribute('stderr', "['stderr']") Thread(target=worker).start()
def worker(): global last_worker_time collection = SQLCollection('crons') for cronuuid in collection.list_objuuids(): try: cron = collection.get_object(cronuuid) if 'enabled' not in cron.object: cron.object['enabled'] = False cron.set() if 'minutes' not in cron.object: cron.object['minutes'] = '*' cron.set() if 'hours' not in cron.object: cron.object['hours'] = '*' cron.set() if 'dayofmonth' not in cron.object: cron.object['dayofmonth'] = '*' cron.set() if 'dayofweek' not in cron.object: cron.object['dayofweek'] = '*' cron.set() if 'year' not in cron.object: cron.object['year'] = '*' cron.set() if 'timeout' not in cron.object: cron.object['timeout'] = 60 cron.set() if 'command' not in cron.object: cron.object['command'] = '' cron.set() if cron.object['enabled'] in (True, 'true'): for t in range(int(last_worker_time), int(time()), 60): now = datetime.fromtimestamp(t).now() if (eval_cron_field(cron.object['minutes'], now.minute) and eval_cron_field(cron.object['hours'], now.hour) and eval_cron_field(cron.object['dayofmonth'], now.day) and eval_cron_field(cron.object['dayofweek'], now.weekday()) and eval_cron_field(cron.object['year'], now.year)): queue(cronuuid) break except: cron = collection.get_object(cronuuid) cron.object['status'] = 1 cron.object['stdout b64data'] = b64encode(''.encode()).decode() cron.object['stderr b64data'] = b64encode( str(traceback.format_exc()).encode()).decode() cron.set() last_worker_time = time() register_timer(name='cron_worker', target=worker, timeout=60).start()
def execute(cronuuid): try: cron = SQLCollection('crons').get_object(cronuuid) status, stdout, stderr = process_sync(cron.object['command'], timeout=cron.object['timeout']) cron.object['stdout b64data'] = b64encode(stdout).decode() cron.object['stderr b64data'] = b64encode(stderr).decode() cron.object['status'] = status cron.set() except: pass ctr_decrement('threads (cron-{0})'.format(cronuuid)) jobs_lock.acquire() del jobs[cronuuid] jobs_lock.release() collection = SQLCollection('crons') collection.create_attribute('name', "['name']") collection.create_attribute('enabled', "['enabled']") collection.create_attribute('status', "['status']") collection.create_attribute('command', "['command']") Thread(target=worker).start()