def syncProjects(lock): # DB connection dbconnection = connect() # acquires lock with lock: logger.info("Worker projects starting synchronization") # MySliceLib Query Slices p = q(Project).get() # update local projects table if len(p)>0: lprojects = db.projects(dbconnection, p.dict()) for ls in lprojects : # add status if not present and update on db if not 'status' in ls: ls['status'] = Status.ENABLED ls['enabled'] = format_date() db.projects(dbconnection, ls) if not p.has(ls['id']) and ls['status'] is not Status.PENDING: # delete projects that have been deleted elsewhere db.delete(dbconnection, 'projects', ls['id']) logger.info("Project {} deleted".format(ls['id'])) else: logger.warning("Query projects is empty, check myslicelib and the connection with SFA Registry")
def WebServer(): """ Tornado Web Server launcher :return: """ # Rethinkdb setting for tornado r.set_loop_type("tornado") dbconnection = yield db.connect() http_server = httpserver.HTTPServer(Application(dbconnection)) http_server.listen(8111)
def confirmEmails(qConfirmEmails): """ Process Event and send an Email to the user to confirm his/her email """ # db connection is shared between threads dbconnection = connect() while True: try: event = Event(qConfirmEmails.get()) except Exception as e: logger.exception(e) logger.error("Problem with event: {}".format(e)) continue else: if event.notify: # We try to send the email only once event.notify = False dispatch(dbconnection, event) try: # Recipients # Status did NOT changed if event.status == event.previous_status: logger.warning("TODO: send specific emails with messages") recipients = set() url = s.web['url'] if s.web['port'] and s.web['port'] != 80: url = url +':'+ s.web['port'] # Look for the user email in the Event if event.object.type == ObjectType.USER: recipients.add(User(event.data)) elif event.object.type == ObjectType.AUTHORITY: for user in event.data['users']: recipients.add(User(user)) else: for user in event.data['pi_users']: recipients.add(User(user)) url = url+'/confirm/'+event.id subject, template = build_subject_and_template('confirm', event) buttonLabel = "Confirm Email" sendEmail(event, recipients, subject, template, url, buttonLabel) except Exception as e: import traceback traceback.print_exc() msg = "Error in event {} while trying to send a confirmation email: {}".format(event.id, e) logger.error(msg) event.logWarning(msg) continue finally: dispatch(dbconnection, event)
def run(): """ A thread that will check resource availability and information """ logger.info("Agent endpoints starting") """ DB connection """ dbconnection = db.connect() """ MySliceLib Query Testbeds """ testbeds = q(Testbed).get() logger.info(testbeds.dict())
def events_run(lock, qPasswordEvents): """ Process the user after approval """ logger.info("Worker password events starting") dbconnection = connect() while True: try: event = Event(qPasswordEvents.get()) except Exception as e: logger.error("Problem with event: {}".format(e)) event.logError(str(e)) event.setError() db.dispatch(dbconnection, event) continue else: logger.info("Processing password event from user {}".format( event.user)) event.setRunning() event.logInfo("Event is running") logger.debug("Event %s is running" % event.id) ## # Creating a new password for user if event.creatingObject(): logger.info("Creating password {}".format(event.object.id)) try: cursor = db.users(dbconnection, email=event.data['email']) u = cursor.next() u['password'] = event.data['password'] db.users(dbconnection, u, u['id']) except Exception as e: logger.error( "Problem updating password of user: {} - {}".format( event.object.id, e)) event.logError(str(e)) event.setError() continue else: event.setSuccess() event.logInfo("Event success") logger.debug("Event %s Success" % event.id) ## # we then dispatch the event db.dispatch(dbconnection, event)
def main(): conn = sqlite3.connect('/var/manifold/db.sqlite') dbconnection = db.connect() cursor = conn.cursor() cursor.execute('''SELECT email, password, config, user_id from user''') cursor2 = conn.cursor() # XXX TODO: get the account config of the user # generate a private key if necessary for row in cursor: user_info = json.loads(row[2]) data = { 'email': row[0], 'password': row[1], 'last_name': user_info['lastname'], 'first_name': user_info['firstname'] } if row[0] == 'admin': data['email'] = '*****@*****.**' cursor2.execute( '''SELECT config, auth_type from account where platform_id=5 AND user_id=%s''' % row[3]) for c in cursor2: config = json.loads(c[0]) pprint(data) data.update(map_dict(config)) data['id'] = hrn_to_urn(data['hrn'], 'user') data['authority'] = hrn_to_urn( ('.').join(data['hrn'].split('.')[:-1]), 'authority') # We don't handle manual delegation in MySlice v2 # Let's generate a new key pair if c[1] != "managed" or not 'private_key' in data or data[ 'private_key'] == 'N/A': data['generate_keys'] = True print("Generate key for user %s" % data['hrn']) else: data['generate_keys'] = False try: user = User(data) isSuccess = user.save(dbconnection) except Exception as e: import traceback traceback.print_exc() print("user will be ignored") continue
def main(argv): try: opts, args = getopt.getopt(argv, "ht:c:i:f:", ["table=", "command=", "id=", "filter="]) except getopt.GetoptError: print('rethink.py -t <table> -c <command> -i <id> -f <filter>') sys.exit(2) t = None command = None id = None filter = None for opt, arg in opts: if opt == '-h': print('rethink.py -t <table> -c <command> -i <id> -f <filter>') sys.exit() elif opt in ("-t", "--table"): t = arg elif opt in ("-c", "--command"): command = arg elif opt in ("-i", "--id"): id = arg elif opt in ("-f", "--filter"): filter = arg if not t: print('please specify the table you want to query') print('rethink.py -t <table> -c <command> -i <id> -f <filter>') sys.exit() if not command: command = "get" dbconnection = db.connect() if command in ["select", "SELECT", "get", "GET"]: if id: print("yes we have the id") result = r.db('myslice').table(t).get(id).run(dbconnection) elif filter: print("yes we have a filter") result = r.db('myslice').table(t).filter(f).run(dbconnection) else: print("neither id nor filter") result = r.db('myslice').table(t).run(dbconnection) pprint(result)
def sync(lock): """ A thread that will sync Leases with the local rethinkdb """ # db connection is shared between threads dbconnection = connect() logger = logging.getLogger('myslice.leases') while True: logger.info("syncing Leases") try: syncLeases() except Exception as e: logger.exception(e) continue logger.info("sleeping") # sleep for 5 minutes # to be fine tuned time.sleep(300)
def sendEmail(event, recipients, subject, template, url, buttonLabel): # db connection is shared between threads dbconnection = connect() mail_to = [] for r in recipients: try: if hasattr(r, 'first_name') and hasattr(r, 'last_name'): username = "******".format(r.first_name, r.last_name) else: username = r.email r.setAttribute('first_name','') r.setAttribute('last_name','') except: import traceback traceback.print_exc() username = "" mail_to.append("{} <{}>".format(username, r.email)) logger.debug("about mail body") mail_body = template.generate( title = subject, entity = str(event.object.type), event = event, theme = s.email['theme'], name = s.email['name'], recipients = recipients, url = url, buttonLabel = buttonLabel, ) # use premailer module to get CSS inline mail_body_inline = transform(mail_body.decode()) m = Message(mail_from=[s.email['sender']], mail_to = mail_to, subject = subject, html_content = mail_body_inline ) Mailer().send(m)
def emails_run(qEmails): """ Process Requests and send Emails accordingly """ # db connection is shared between threads dbconnection = connect() while True: try: event = Event(qEmails.get()) except Exception as e: logger.error("Problem with event: {}".format(e)) continue else: if event.notify: # We try to send the email only once event.notify = False dispatch(dbconnection, event) # Recipients # TODO: Send specific emails # Status did NOT changed # Comments about an event with a message if event.status == event.previous_status: logger.warning("TODO: send specific emails with messages") recipients = set() url = s.web['url'] if s.web['port'] and s.web['port'] != 80: url = url +':'+ s.web['port'] buttonLabel = "View details" if event.object.type == ObjectType.PASSWORD: recipients.add(User(event.object.id)) url = url+'/password/'+event.data['hashing'] subject, template = build_subject_and_template('password', event) buttonLabel = "Change password" else: if event.isPending(): # Find the authority of the event object # Then according the authority, put the pi_emails in pis_email try: authority_id = event.data['authority'] except KeyError: msg = 'Authority id not specified ({})'.format(event.id) logger.error(msg) event.logWarning('Authority not specified in event {}, email not sent'.format(event.id)) pass else: authority = Authority(db.get(dbconnection, table='authorities', id=authority_id)) if not authority: # get admin users users = db.get(dbconnection, table='users') for u in users: user = User(u) if user.isAdmin(): logger.debug("user %s is admin" % user.id) recipients.add(user) else: for pi_id in authority.pi_users: recipients.add(User(pi_id)) if not recipients: msg = 'Emails cannot be sent because no one is the PI of {}'.format(event.object.id) logger.error(msg) event.logWarning('No recipients could be found for event {}, email not sent'.format(event.id)) subject, template = build_subject_and_template('request', event) buttonLabel = "Approve / Deny" url = url + '/activity' else: if event.user: recipients.add(User(event.user)) else: # Look for the user email in the Event if event.object.type == ObjectType.USER: recipients.add(User({'email':event.data['email'], 'first_name':event.data['first_name'], 'last_name':event.data['last_name']})) elif event.object.type == ObjectType.AUTHORITY: for user in event.data['users']: recipients.add(User(user)) else: for user in event.data['pi_users']: recipients.add(User(user)) if event.isSuccess(): subject, template = build_subject_and_template('approve', event) elif event.isDenied(): subject, template = build_subject_and_template('deny', event) try: sendEmail(event, recipients, subject, template, url, buttonLabel) except Exception as e: import traceback traceback.print_exc() msg = "Error in event {} while trying to send an email: {} {}".format(event.id, e, traceback.print_exc()) logger.error(msg) event.logWarning(msg) continue finally: dispatch(dbconnection, event)
def syncSlices(id=None): with lock: logger.info("Worker slices starting synchronization") try: # DB connection dbconnection = db.connect() # Update an existing Slice if id: slices = Slices([Slice(db.get(dbconnection, table='slices', id=id))]) # MySliceLib Query Slices else: slices = q(Slice).get() if len(slices)==0: logger.warning("Query slices is empty, check myslicelib and the connection with SFA Registry") # ------------------------------------------------------ # Synchronize resources of a Slice at AMs # !!! only if the slice_id is specified !!! # Otherwise it is way too long to synchronize all slices # ------------------------------------------------------ # TODO: trigger this function in background for a user # that want to refresh his slice / when he selected one # ------------------------------------------------------ if id: for slice in slices: if len(slice.users) > 0: try: u = User(db.get(dbconnection, table='users', id=slice.users[0])) logger.info("Synchronize slice %s:" % slice.hrn) # Synchronize resources of the slice only if we have the user's private key or its credentials # XXX Should use delegated Credentials #if (hasattr(u,'private_key') and u.private_key is not None and len(u.private_key)>0) or (hasattr(u,'credentials') and len(u.credentials)>0): if u.private_key or (hasattr(u,'credentials') and len(u.credentials)>0): user_setup = UserSetup(u,myslicelibsetup.endpoints) logger.info("Slice.id(%s).get() with user creds" % slice.hrn) s = q(Slice, user_setup).id(slice.id).get().first() db.slices(dbconnection, s.dict(), slice.id) except Exception as e: import traceback traceback.print_exc() logger.error("Problem with slice %s" % slice.id) logger.exception(str(e)) raise else: logger.info("slice %s has no users" % slice.hrn) # update local slice table else: if len(slices)>0: local_slices = db.slices(dbconnection) # Add slices from Registry unkown from local DB for s in slices: if not db.get(dbconnection, table='slices', id=s.id): logger.info("Found new slice from Registry: %s" % s.id) db.slices(dbconnection, s.dict(), s.id) # Update slices known in local DB for ls in local_slices : logger.info("Synchronize Slice {}".format(ls['id'])) # add status if not present and update on db if not 'status' in ls: ls['status'] = Status.ENABLED ls['enabled'] = format_date() if not slices.has(ls['id']) and ls['status'] is not Status.PENDING: # delete slices that have been deleted elsewhere db.delete(dbconnection, 'slices', ls['id']) logger.info("Slice {} deleted".format(ls['id'])) else: db.slices(dbconnection, ls, ls['id']) else: logger.warning("Query slices is empty, check myslicelib and the connection with SFA Registry") except Exception as e: import traceback traceback.print_exc() logger.exception(str(e)) raise logger.info("Worker slices finished period synchronization")
from myslicelib.model.resource import Resource from myslice.db.slice import Slice, SliceException from myslicelib.model.slice import Slices from myslice.db.user import User from myslicelib.query import q import myslice.lib.log as logging ## # lock shared byt the two workers sync and manage lock = threading.Lock() logger = logging.getLogger("experiments") # db connection is shared between threads dbconnection = connect() def events_run(qSliceEvents): """ Process the slice after approval """ logger.info("Worker slices events starting") while True: try: event = Event(qSliceEvents.get()) except Exception as e: logger.error("Problem with event: {}".format(e))
def events_run(lock, qUserEvents): """ Process the user after approval """ logger.info("Worker users events starting") dbconnection = connect() while True: try: event = Event(qUserEvents.get()) except Exception as e: logger.error("Problem with event: {}".format(e)) event.logError(str(e)) event.setError() db.dispatch(dbconnection, event) continue else: logger.info("Processing event from user {}".format(event.user)) with lock: try: event.setRunning() event.logInfo("Event is running") logger.debug("Event %s is running" % event.id) isSuccess = False # If we generate a new key pair the Query will not work, use the myslice account for that if event.user and hasattr( event.object, 'generate_keys' ) and event.object.generate_keys is False: u = User( db.get(dbconnection, table='users', id=event.user)) user_setup = UserSetup(u, myslicelibsetup.endpoints) else: user_setup = None ## # Creating a new user if event.creatingObject(): logger.info("Creating user {}".format(event.object.id)) user = User(event.data) isSuccess = user.save(dbconnection, user_setup) ## # Deleting user if event.deletingObject(): logger.info("Deleting user {}".format(event.object.id)) user = User( db.get(dbconnection, table='users', id=event.object.id)) if not user: raise Exception("User doesn't exist") user.id = event.object.id isSuccess = user.delete(dbconnection, user_setup) ## # Updating user if event.updatingObject(): logger.info("Updating user {}".format(event.object.id)) user = User(event.data) local_user = User( db.get(dbconnection, table='users', id=event.object.id)) # we don't allow users to change their email user.email = local_user.email user.id = event.object.id isSuccess = user.save(dbconnection, user_setup) except Exception as e: import traceback traceback.print_exc() logger.error("Problem updating user: {} - {}".format( event.object.id, e)) event.logError(str(e)) event.setError() continue if isSuccess: event.setSuccess() event.logInfo("Event success") logger.debug("Event %s Success" % event.id) else: logger.error("Error event {}: action failed".format( event.id)) event.setError() event.logError("Error in worker users: action failed") ## # we then dispatch the event db.dispatch(dbconnection, event)
def events_run(lock, qLeasesEvents): """ Process the leases events """ logger.info("Worker leases events starting") # db connection is shared between threads dbconnection = connect() while True: try: event = Event(qLeasesEvents.get()) except Exception as e: logger.error("Problem with event: {}".format(e)) event.logError("Error in worker leases: {}".format(e)) event.setError() db.dispatch(dbconnection, event) continue else: logger.info("Processing event from user {}".format(event.user)) try: event.setRunning() event.logInfo("Event is running") logger.debug("Event %s is running" % event.id) isSuccess = False u = User(db.get(dbconnection, table='users', id=event.user)) user_setup = UserSetup(u, myslicelibsetup.endpoints) if event.creatingObject(): leases = [] if isinstance(event.data, list): for l in event.data: slice_id = l['slice_id'] leases.append(Lease(l)) else: slice_id = event.data['slice_id'] leases.append(Lease(event.data)) sli = Slice( db.get(dbconnection, table='slices', id=slice_id)) if not sli: raise Exception("Slice doesn't exist") for lease in leases: for val in lease.resources: r = db.get(dbconnection, table='resources', id=val) # Add resource only if it exists in DB if r is not None: r = Resource(r) sli.addResource(r) else: r = Resource({'id': val}) lease.removeResource(r) if len(lease.resources) > 0: sli.addLease(lease) else: raise Exception("Invalid resources") isSuccess = sli.save(dbconnection, user_setup) if event.deletingObject(): lease = Lease( db.get(dbconnection, table='leases', id=event.object.id)) if not lease: raise Exception("Lease doesn't exist") sli = Slice( db.get(dbconnection, table='slices', id=event.data['slice_id'])) if not sli: raise Exception("Slice doesn't exist") for val in event.data['resources']: r = Resource( db.get(dbconnection, table='resources', id=val)) # Remove resource only if it exists in DB if r: sli.removeResource(r) else: r = Resource({'id': val}) lease.removeResource(r) sli.removeLease(lease) isSuccess = sli.save(dbconnection, user_setup) except SliceException as e: # CREATE, DELETE # If at least one of the AMs replies with success, it's ok # If all AMs have failed -> Error for err in e.stack: event.logError("Error in worker leases: {}".format(err)) logger.error("Error in worker leases: {}".format(err)) # XXX TO BE REFINED event.setError() continue except SliceWarningException as e: for err in e.stack: event.logWarning(str(err)) logger.warning(str(err)) event.setWarning() continue except Exception as e: import traceback traceback.print_exc() logger.error("Problem with event {}: {}".format(event.id, e)) event.logError("Error in worker leases: {}".format(e)) event.setError() continue else: if isSuccess: event.setSuccess() event.logInfo("Event success") logger.debug("Event %s Success" % event.id) else: logger.error("Error event {}: action failed".format( event.id)) event.setError() event.logError("Error in worker leases: action failed") db.dispatch(dbconnection, event)
def syncUsers(lock, email=None): # DB connection dbconnection = db.connect() # acquires lock with lock: logger.info("Worker users starting synchronization") try: if email: users = q(User).filter('email', email).get() else: users = q(User).get() """ update local user table """ if len(users) > 0: # Add users from Registry unkown from local DB # this is used to bootstrap with init_user script for u in users: logger.debug("looking for {} in local db".format(u.id)) if not db.get(dbconnection, table='users', id=u.id): local_users = db.get(dbconnection, table='users') logger.warning("Number of users in local db: %s" % len(local_users)) #print("this user is not in local db, add it") logger.info("Found new user from Registry: %s" % u.id) #logger.info("We don't add the missing users yet, as portal is the single point of entry") db.users(dbconnection, u.dict()) local_users = db.get(dbconnection, table='users') # Update users known in local DB for lu in local_users: logger.info("Synchronize user %s" % lu['id']) try: # add status if not present and update on db if not 'status' in lu: lu['status'] = Status.ENABLED lu['enabled'] = format_date() if not users.has(lu['id']) and lu[ 'status'] is not Status.PENDING: # delete user that has been deleted in Reg db.delete(dbconnection, 'users', lu['id']) logger.info("User {} deleted".format(lu['id'])) else: remote_user = next( (item for item in users if item.id == lu['id']), False) if remote_user: # merge fields of local user with remote # keep local values for # password, private_key, public_key and generate_keys updated_user = remote_user.merge(dbconnection) updated_user = updated_user.dict() # if user has private key # update its Credentials if 'private_key' in updated_user and updated_user[ 'private_key'] is not None: updated_user = update_credentials( dbconnection, updated_user) # Update user #logger.debug("Update user %s" % updated_user['id']) db.users(dbconnection, updated_user, updated_user['id']) except Exception as e: logger.warning("Could not synchronize user %s" % lu['id']) logger.exception(e) raise else: logger.warning( "Query users is empty, check myslicelib and the connection with SFA Registry" ) except Exception as e: import traceback traceback.print_exc() logger.exception(e) raise logger.info("Worker users finished period synchronization")
class SessionsHandler(Api): # DB connection dbconnection = db.connect() @gen.coroutine def getSession(self, slice_id): try: data = yield r.db(s.db.name).table('sessions')\ .filter({'slice_id':slice_id,'status':'started'})\ .max('start_date').run(self.dbconnection) except ReqlNonExistenceError: return None return data @gen.coroutine def startFakeInterop(self, session_id, slice_id): fi = FakeInterop(session_id) msg = 'Periodic message from FakeInterop' for y in range(1): ts = Process(target=fi.fake, args=(msg, )) self.threads[slice_id].append(ts) ts.start() @gen.coroutine def listenSession(self, session_id, slice_id): for y in range(1): t = Process(target=startSession, args=(session_id, )) self.threads[slice_id].append(t) t.start() @gen.coroutine def get(self, id=None, p=None): """ - GET /finterop/sessions/<id|hrn>/(start|stop) :return: """ response = [] current_user = self.get_current_user() logger.info('get session') if not current_user: self.userError('not authenticated ') return # Security: only allow to watch sessions for which the user has rights (one of his slices) # -------------------------------------- # XXX DISABLED for development # -------------------------------------- #if id not in current_user['slices']: # self.userError('permission denied') # return if id and p == 'start': # TODO: Check the status of the session in DB if id in self.threads: self.userError('this session has already started') return else: data = { 'slice_id': id, 'status': 'started', 'start_date': format_date() } # Create a session in DB session = yield r.db(s.db.name).table('sessions').insert( data, conflict='update').run(self.dbconnection) session_id = session['generated_keys'][0] # Add the session to the threads self.threads[id] = [] # TODO: F-Interop Orchestrator Integration # send a call to the F-Interop Orchestrator # XXX Meanwhile start the FakeInterop session yield self.startFakeInterop(session_id, id) # listen to the session logger.info('Listen to the session %s' % session_id) yield self.listenSession(session_id, id) elif id and p == 'stop': # TODO: Check the status of the session in DB # Get session in DB data = yield self.getSession(id) if data: data['status'] = 'stopped' data['end_date'] = format_date() # Update session in DB r.db(s.db.name).table('sessions').get( data['id']).update(data).run(self.dbconnection) if id in self.threads: # TODO: send a call to the F-Interop Orchestrator # XXX Meanwhile stop FakeInterop process # Stop Listening process for t in self.threads[id]: t.terminate() del self.threads[id] logger.info('Stop listening to the session') stopSession(id) else: self.userError('this session is not running') return else: self.userError('not supported by the REST API') return # TODO: send back the result success with data about the session self.write( json.dumps( { "result": "success", "data": [], "error": None, "debug": None }, cls=myJSONEncoder))
def events_run(lock, qProjectEvents): """ Process the authority after approval """ logger.info("Worker authorities events starting") # db connection is shared between threads dbconnection = connect() while True: try: event = Event(qProjectEvents.get()) except Exception as e: logger.error("Problem with event: {}".format(e)) event.logError(str(e)) event.setError() db.dispatch(dbconnection, event) continue else: logger.info("Processing event: {} from user {}".format(event.id, event.user)) with lock: try: if event.isApproved(): user_id = event.manager else: user_id = event.user u = User(db.get(dbconnection, table='users', id=user_id)) # Registry Only user_setup = UserSetup(u, myslicelibsetup.registry_endpoints) event.setRunning() event.logInfo("Event is running") logger.debug("Event %s is running" % event.id) isSuccess = False if event.creatingObject() or event.updatingObject(): logger.info("creating or updating the object project {}".format(event.data)) proj = Project(event.data) proj.id = event.object.id # XXX CASES TO BE CHECKED if event.user not in proj.pi_users: pi = User(db.get(dbconnection, table='users', id=event.user)) proj.addPi(pi) isSuccess = proj.save(dbconnection, user_setup) else: p = db.get(dbconnection, table='projects', id=event.object.id) if not p: raise Exception("Project doesn't exist") proj = Project(p) if event.deletingObject(): logger.info("deleting the object project {}".format(event.object.id)) isSuccess = proj.delete(dbconnection, user_setup) if event.addingObject(): logger.info("adding data to the object project {}".format(event.object.id)) if event.data.type == DataType.USER: logger.info("Project only supports PI at the moment, need new feature in SFA Reg") if event.data.type == DataType.PI or event.data.type == DataType.USER: for val in event.data.values: pi = User(db.get(dbconnection, table='users', id=val)) proj.addPi(pi) isSuccess = proj.save(dbconnection, user_setup) if event.removingObject(): logger.info("removing data from the object project {}".format(event.object.id)) if event.data.type == DataType.USER: logger.info("Project only supports PI at the moment, need new feature in SFA Reg") if event.data.type == DataType.PI or event.data.type == DataType.USER: for val in event.data.values: pi = User(db.get(dbconnection, table='users', id=val)) proj.removePi(pi) isSuccess = proj.save(dbconnection, user_setup) except Exception as e: import traceback traceback.print_exc() logger.error("Problem with event {}: {}".format(event.id,e)) event.logError("Error in worker projects: {}, traceback: {}".format(e,traceback.print_exc())) event.setError() continue else: if isSuccess: event.setSuccess() event.logInfo("Event success") logger.debug("Event %s Success" % event.id) else: logger.error("Error event {}: action failed".format(event.id)) event.setError() event.logError("Error in worker projects: action failed") finally: db.dispatch(dbconnection, event)
def run(q): """ Manages newly created events """ logger.info("Worker activity events starting") # db connection is shared between threads dbconnection = connect() while True: try: event = Event(q.get()) except Exception as e: import traceback traceback.print_exc() logger.error("Problem with event: {}".format(e)) event.logError("Error in worker activity: {}".format(e)) event.setError() dispatch(dbconnection, event) continue else: logger.debug("%s - Manage event".format(event.id)) # TODO: if event.creatingObject() # Check if the event.object.id already exists or not # if it exists -> add a numer to the id & hrn to make it unique if event.object.type == ObjectType.PASSWORD: try: event.setPending() event.logInfo("Event is pending, please check your email") logger.debug("Event %s is pending" % event.id) except Exception as e: logger.error( "Error in processing Event (1): {}".format(event)) logger.error("Error while processing: {}".format(e)) event.logError(str(e)) continue # Register a new object for a new user # id should be generated into the web to avoid duplicates elif event.isNew() and event.object.type in [ ObjectType.USER, ObjectType.AUTHORITY ] and event.user is None and event.creatingObject(): try: # The user must confirm his/her email logger.debug("Event Type: {}".format(type(event))) event.setConfirm() logger.debug("Event %s is confirm" % event.id) event.logInfo( "Event is expecting your confirmation, please check your email" ) except Exception as e: logger.error( "Error in processing Event (2): {}".format(event)) logger.error("Error while processing: {}".format(e)) event.logError(str(e)) continue else: try: logger.debug("%s - get user %s" % (event.id, event.user)) db_user = db.get(dbconnection, table='users', id=event.user) if db_user: user = User(db_user) logger.debug("%s - Does user %s has privilege?" % (event.id, event.user)) if user.has_privilege(event): logger.debug("%s - setWaiting" % event.id) event.logInfo("Event waiting to be processed") event.setWaiting() else: logger.debug("%s - setPending" % event.id) event.logInfo( "your user has no rights on %s, event pending until approved" % event.user) event.setPending() else: event.setError() logger.error("User {} not found in event {}".format( event.user, event.id)) event.logError("User %s not found" % event.user) # raising an Exception here, blocks the REST API #raise Exception("User %s not found" % event.user) except Exception as e: logger.error("Error processing Event") logger.error(event) import traceback logger.error(traceback.print_exc()) traceback.print_exc() event.setError() event.logError(str(e)) logger.error("Unable to fetch the user {} from db".format( event.user)) logger.exception(e) continue # dispatch the updated event dispatch(dbconnection, event)
def main(argv): try: opts, args = getopt.getopt( argv, "he:P:k:p:s", ["email=", "password="******"private_key=", "public_key=", "sync="]) except getopt.GetoptError: print( 'init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>' ) sys.exit(2) if (len(opts) < 4): print('Missing parameters:') print( 'init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>' ) sys.exit(2) sync = False for opt, arg in opts: if opt == '-h': print( 'init_user.py -e <email> -P <password> -k <private_key path> -p <public_key path> -s <synchronize with Registry>' ) sys.exit() elif opt in ("-e", "--email"): email = arg elif opt in ("-P", "--password"): password = crypt_password(arg) elif opt in ("-k", "--private_key"): f = open(arg, 'r') private_key = f.read() elif opt in ("-p", "--public_key"): f = open(arg, 'r') public_key = f.read() elif opt in ("-s", "--sync"): if arg.lower() in ["true", "yes", "y"]: sync = True dbconnection = db.connect() lock = threading.Lock() # Synchronize the users from SFA Registry into the DB if sync: print("sync user %s" % email) syncUsers(lock, email=email, job=False) else: try: # Get the user from SFA Registry print("get user %s from SFA Reg" % email) remote_user = q(User).filter('email', email).get().first() pprint(remote_user) # merge fields from script with remote_user remote_user.setAttribute('password', password) remote_user.setAttribute('private_key', private_key) remote_user.setAttribute('generate_keys', False) remote_user.setAttribute('public_key', public_key) remote_user.setAttribute('keys', [public_key]) r.db('myslice').table('users').insert( remote_user.dict()).run(dbconnection) result = r.db('myslice').table('users').get( remote_user.id).run(dbconnection) #result = remote_user.save(dbconnection) if result: print("User saved") else: print("Error during save") pprint(result) # if user has private key # update its Credentials #if 'private_key' in updated_user: # updated_user = update_credentials(updated_user) except Exception as e: import traceback traceback.print_exc()