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, 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, 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, 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)