def configure_database(): connection = connect(ReDB_HOST, ReDB_PORT, user=ReDB_USER, password=ReDB_PASS) create_table(database, empty_table, connection) delete_all(database, table, connection) data = [{ 'name': 'The Dark Knight Rises', 'year': 2012, 'runtime': 163, 'categories': ['adventure', 'thriller'], 'release-date': '2012-07-20', 'director': 'Christopher Nolan', 'writer': ['Jonathan Nolan', 'David S. Goyer', 'Christopher Nolan', 'Bob Kane'], 'actors': ['Christian Bale', 'Tom Hardy', 'Anne Hathway'], 'storyline': 'Eight years after the Joker\'s reign of anarchy,' ' the Dark Knight, with the help of the enigmatic' ' Catwoman, is forced from his imposed exile to save' ' Gotham City, now on the edge of total annihilation,' ' from the brutal guerrilla terrorist Bane.' }, { 'name': 'Avengers: Infinity War', 'year': 2018, 'runtime': 149, 'categories': ['Action', 'Adventure', 'Fantasy'], 'release-date': '2018-04-27', 'director': ['Anthony Russo', 'Joe Russo'], 'writer': ['Christopher Markus', 'Stephen McFeely'], 'actors': [ 'Robert Downey Jr.', 'Chris Hemsworth', 'Mark Ruffalo', 'Chris Evans', 'Scarlett Johansson', 'Benedict Cumberbatch' ], 'storyline': 'The Avengers and their allies must be willing to' ' sacrifice all in an attempt to defeat the' ' powerful Thanos before his blitz of devastation' ' and ruin puts an end to the universe.' }] result = insert(database, table, data, connection) global unique_id unique_id = result['generated_keys'][0]
def bootstrap(force=False, dev=False, quiet=False): if not force: # verify and skip if it is not necessary if db.present(): return db.delete_all() UserModel.create_table() UserProjectMap.create_table() ProjectModel.create_table() EnvironmentModel.create_table() SettingModel.create_table() MainSettingModel.create_table() InitProjectSettingModel.create_table() UserProjectMap.create_table() ProjectEnvironmentMap.create_table() ProjectSettingMap.create_table() ####### # Users alphabet = string.ascii_letters + string.digits admin_password = ''.join(secrets.choice(alphabet) for i in range(20)) new_user = UserModel(id=None, username="******", password=admin_password, admin=1) new_user.save() if not quiet: print(f'################### Admin user created ##################') print(f'######### Admin password: "******" ########') print(f'#########################################################') if dev: logging.debug('Creation of the developping users') new_user = UserModel(id=None, username="******", password="******", admin=1) new_user.save() new_user = UserModel(id=None, username="******", password="******", admin=0) new_user.save() new_user = UserModel(id=None, username="******", password="******", admin=0) new_user.save() new_user.username = "******" new_user.save() ########## # Settings # Main Settings new_setting = SettingModel( id=None, name="max_project_run_number", description="Number of executions per project that are stored", default_value=20) new_setting.save() new_main_setting = MainSettingModel(new_setting.name, new_setting.id) new_main_setting.save() new_setting = SettingModel( id=None, name="name_default_container_image", description="Default container image to use", value="fabrizio2210/docker_light-default_container", default_value="debian") new_setting.save() new_main_setting = MainSettingModel(new_setting.name, new_setting.id) new_main_setting.save() # Initial Project Settings new_setting = SettingModel( id=None, name="scm_url", description="Source Control Manager URL of the project") new_setting.save() new_main_setting = InitProjectSettingModel(new_setting.name, new_setting.id) new_main_setting.save() new_setting = SettingModel(id=None, name="image_use_docker", value=True, default_value=True, description="If the image use docker inside") new_setting.save() new_main_setting = InitProjectSettingModel(new_setting.name, new_setting.id) new_main_setting.save() ############## # Environments ############ # Executions ExecutionModel.cleanup()
async def data_manager(websocket, path): logging.info(f"[INFO] Current path: {str(path)}") try: if path == "/": raise Exception(f"No operation defined on the current path." f"\nReceived path: {str(path)}") data = await websocket.recv() data = json.loads(data) identifier = data["id"] request_type = data["type"] payload = data["payload"] time = data["time"] logging.info(f"[INFO] Data received: {data}") if request_type != RDM_CALL: raise Exception(f"Type of service not as expected. Probably a call" f"for other service. Verify logs for further infor" f"mation!") if save_audit(identifier, request_type, payload, time): pass else: logging.error(f"[ERROR] Error while trying to save data to audit" f" database. Verify Rethink Data Manager logs for " f"furhter information!") try: connection = configure_database(ReDB_HOST, ReDB_PORT, ReDB_DEFAULT_DB, ReDB_USER, ReDB_PASS) if str(path) == op[1]: database = payload["database"] table = payload["table"] payload = payload["data"] logging.info(f"[INFO] Inserting data:\n\tDATABASE: " f"{database}\n\tTABLE: {table}\n\tDATA: " f"{payload}") result = insert(database, table, payload, connection) if result == {}: raise Exception(f"[ERROR] Error trying to insert data. " f"Verify the logs for the full traceback. " f"The data was not inserted!") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[2]: database = payload["database"] table = payload["table"] logging.info(f"[INFO] Getting all data:\n\tDATABASE: " f"{database}\n\tTABLE: {table}") result = get_all(database, table, connection) if result == {}: raise Exception(f"[ERROR] Error trying to getting data. " f"Verify the logs for the full traceback. " f"Neither the table is empty or no table" f" was found!") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[3]: database = payload["database"] table = payload["table"] identifier = payload["identifier"] logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: " f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: " f"{identifier}") result = get(database, table, identifier, connection) if result == {}: raise Exception(f"[ERROR] Error trying to getting data. " f"Verify the logs for the full traceback. " f"Neither the object doesn't exists or no" f" table was found!") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[4]: database = payload["database"] table = payload["table"] identifier = payload["identifier"] statment = payload["data"] logging.info(f"[INFO] Updating object:\n\tDATABASE: " f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: " f"{identifier}\n\tUPDATE STATEMENT: {statment}") result = update(database, table, identifier, statment, connection) if result == {}: raise Exception(f"[ERROR] Error trying to update object. " f"Verify the logs for the full traceback. " f"Neither the object doesn't exists or no" f" table was found!") elif result["objects_updated"] == 0: raise Exception(f"[ERROR] Error trying to update object. " f"Verify the logs for the full traceback.") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[5]: database = payload["database"] table = payload["table"] identifier = payload["identifier"] logging.info(f"[INFO] Deleting object:\n\tDATABASE: " f"{database}\n\tTABLE: {table}\n\tIDENTIFIER: " f"{identifier}") result = delete(database, table, identifier, connection) if result == {}: raise Exception(f"[ERROR] Error trying to delete object. " f"Verify the logs for the full traceback. " f"Neither the object doesn't exists or no" f" table was found!") elif result["objects_deleted"] == 0: raise Exception(f"[ERROR] Error trying to delete object. " f"Verify the logs for the full traceback.") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[6]: database = payload["database"] table = payload["table"] logging.info(f"[INFO] Deleting all objects:\n\tDATABASE: " f"{database}\n\tTABLE: {table}") result = delete_all(database, table, connection) if result == {}: raise Exception(f"[ERROR] Error trying to delete objects. " f"Verify the logs for the full traceback. " f"Neither some error was found or no table" f" was found!") elif result["objects_deleted"] == 0: raise Exception(f"[ERROR] Error trying to delete object. " f"Verify the logs for the full traceback.") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[7]: database = payload["database"] table = payload["table"] logging.info(f"[INFO] Creating new table:\n\tDATABASE: " f"{database}\n\tTABLE: {table}") result = create_table(database, table, connection) if result == {}: raise Exception(f"[ERROR] Unknown error while trying to " f"create a new table. Verify the logs" f" for the the full traceback.") elif result["tables_created"] == 0: raise Exception(f"[ERROR] This table ({table})" f" already exists!") else: await websocket.send(json.dumps(success_msg(result))) elif str(path) == op[8]: database = payload["database"] table = payload["table"] statment = payload["filter"] statment = json.loads(statment) logging.info(f"[INFO] Getting objetct data:\n\tDATABASE: " f"{database}\n\tTABLE: {table}\n\tFILTER: " f"{statment}") result = get_where(database, table, statment, connection) if result == {}: raise Exception(f"[ERROR] Unknown error while trying to " f"create a new table. Verify the logs" f" for the the full traceback.") else: await websocket.send(json.dumps(success_msg(result))) else: raise Exception(f"Unknown operation on the current path." f"\nReceived path: {str(path)}") except Exception as err: if connection: disconnect_database(connection) raise err except KeyboardInterrupt as err: disconnect_database(connection) raise err except ConnectionClosed as err: disconnect_database(connection) raise err except CancelledError as err: disconnect_database(connection) raise err except Exception as err: logging.error(f"[ERROR] Error at services/data_manager." f"\nTraceback: {err}") await websocket.send(json.dumps(error_msg(path))) else: pass
def test_unsuccessful_delete_all(rethink_connect): result = delete_all(database, empty_table, rethink_connect) assert result is not None assert len(result) == 1 assert result["objects_deleted"] == 0
def test_successful_delete_all(rethink_connect): result = delete_all(database, table, rethink_connect) assert result is not None assert len(result) is 1 assert result['objects_deleted'] is 2