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 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 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)) 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: event.setRunning() isSuccess = False u = User(db.get(dbconnection, table='users', id=event.user)) user_setup = UserSetup(u, myslicelibsetup.endpoints) if event.creatingObject(): sli = Slice(event.data) # XXX To be checked sli.id = event.object.id # Add all Project PIs to the Slice project = db.get(dbconnection, table='projects', id=sli.project) for us in project['pi_users']: us = User(db.get(dbconnection, table='users', id=us)) sli.addUser(us) if 'users' in event.data and 'geni_users' not in event.data: for u_id in event.data['users']: u = User(db.get(dbconnection, table='users', id=u_id)) sli.addUser(u) # Take into account the Resources on Create if 'resources' in event.data: for resource in event.data['resources']: if not isinstance(resource, dict): res = db.get(dbconnection, table='resources', id=resource) if not res: raise Exception("Resource %s doesn't exist" % resource) resource = res sli.addResource(Resource(resource)) # expiration_date = Renew at AMs isSuccess = sli.save(dbconnection, user_setup) if event.updatingObject(): logger.debug("Update users / resources of Slice %s" % event.object.id) sli = Slice(db.get(dbconnection, table='slices', id=event.object.id)) ## adding users sli = add_users(event.data, sli) ## removing users sli = remove_users(event.data, sli) ## adding resources sli = add_resources(event.data, sli) ## removing resources sli = remove_resources(event.data, sli) isSuccess = sli.save(dbconnection, user_setup) if event.deletingObject(): sli = Slice(db.get(dbconnection, table='slices', id=event.object.id)) if not sli: raise Exception("Slice doesn't exist") isSuccess = sli.delete(dbconnection, user_setup) if event.addingObject(): sli = Slice(db.get(dbconnection, table='slices', id=event.object.id)) if event.data.type == DataType.USER: for val in event.data.values: if isinstance(val, dict): val = val['id'] u = User(db.get(dbconnection, table='users', id=val)) sli.addUser(u) isSuccess = sli.save(dbconnection, user_setup) if event.data['type'] == DataType.RESOURCE: for val in event.data.values: if isinstance(val, dict): val = val['id'] res = Resource(db.get(dbconnection, table='resources', id=val)) sli.addResource(res) isSuccess = sli.save(dbconnection, user_setup) if event.removingObject(): sli = Slice(db.get(dbconnection, table='slices', id=event.object.id)) if event.data.type == DataType.USER: for val in event.data['values']: if isinstance(val, dict): val = val['id'] u = User(db.get(dbconnection, table='users', id=val)) sli.removeUser(u) isSuccess = sli.save(dbconnection, user_setup) if event.data.type == DataType.RESOURCE: for val in event.data.values: if isinstance(val, dict): val = val['id'] r = Resource(db.get(dbconnection, table='resources', id=val)) sli.removeResource(r) isSuccess = sli.save(dbconnection, user_setup) except SliceException as e: # CREATE, UPDATE, DELETE # Calls toward Registry # If an AM sends an Error it is not blocking if event.creatingObject() or event.updatingObject() or event.deletingObject(): logger.debug("DEBUG services worker slices") for err in e.stack: logger.debug(err) if err['type'] == 'Reg': event.setError() break else: # XXX TO BE REFINED event.setSuccess() #event.setWarning() # TODO: # if ALL AMs have failed -> Error # if One AM succeeded -> Warning else: # XXX TO BE REFINED logger.debug("DEBUG services worker slices") for err in e.stack: logger.debug(err) event.setWarning() #event.setError() continue except Exception as e: import traceback traceback.print_exc() logger.error("Problem with event: {}".format(e)) event.logError(str(e)) event.setError() continue else: if isSuccess: event.setSuccess() else: event.setError() 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 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 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)