From 987f1d4f70edecd632eff096ee0fbd90c9efa83c Mon Sep 17 00:00:00 2001 From: SanjaySinghRajpoot Date: Wed, 7 Sep 2022 17:37:06 +0530 Subject: [PATCH 1/5] feat: GCBM status endpoint --- local/rest_api_gcbm/app.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/local/rest_api_gcbm/app.py b/local/rest_api_gcbm/app.py index 9fa218e8..45a5af1f 100644 --- a/local/rest_api_gcbm/app.py +++ b/local/rest_api_gcbm/app.py @@ -726,7 +726,7 @@ def gcbm_list_simulations(): }, 200 -@app.route("/gcbm/status", methods=["POST"]) +@app.route("/gcbm/report", methods=["POST"]) def status(): """ Get status of a simulation @@ -966,5 +966,40 @@ def gcbm_db(): return {"data": "db file uploaded succesfully. Proceed to the next step."} +def get_container_id(): + """ + Return the docker container id corresponding to the image gcbm-api running on Docker + """ + container_id = "" + os.system("docker ps >> simulation.txt") + with open("simulation.txt") as log_file: + logs = log_file.readlines() + for line in logs: + if "gcbm-api" in line: + container_id = line.split()[0] + os.system("rm simulation.txt") + return container_id + + +@app.route("/gcbm/status", methods=["GET"]) +def get_simulation_status(): + """ + If the container gcbm-api is running, the associated logs are returned to the end user. + """ + container_id = get_container_id() + logs_file_name = container_id + ".txt" + if container_id != "": + logs_cmd = "docker logs " + container_id + " > " + logs_file_name + " 2>&1" + os.system(logs_cmd) + logs = [] + with open(logs_file_name) as file_handle: + logs = file_handle.readlines() + # os.system('rm ' + logs_file_name) + return {"container_running": True, "logs": logs}, 200 + else: + logs = ["Error No Running Container"] + return {"container_running": False, "logs": logs}, 400 + + if __name__ == "__main__": app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080))) From 30119a381083b8eecd2545bb16c48f937f8330c6 Mon Sep 17 00:00:00 2001 From: SanjaySinghRajpoot Date: Wed, 7 Sep 2022 17:41:03 +0530 Subject: [PATCH 2/5] test cases for GCBM status --- local/tests/test_rest_gcbm.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/local/tests/test_rest_gcbm.py b/local/tests/test_rest_gcbm.py index 67d497f1..37fec8b5 100644 --- a/local/tests/test_rest_gcbm.py +++ b/local/tests/test_rest_gcbm.py @@ -1,4 +1,5 @@ from distutils.command.upload import upload +import re from pkg_resources import yield_lines import pytest import requests @@ -371,6 +372,11 @@ def test_gcbm_db(self, gcbm_endpoint, yield_db_file, yield_title): upload_endpoint = gcbm_endpoint + "upload/db" upload_response = requests.post(upload_endpoint, files=upload_files, data=data) assert upload_response.status_code == 200 + + def test_gcbm_status(self, gcbm_endpoint): + get_status_endpoint = gcbm_endpoint + "status" + get_status = requests.get(get_status_endpoint); + assert get_status.status_code == 200 @pytest.mark.skip(reason="Test fails on CI") def test_download(self, gcbm_endpoint, yield_title): From 751a3fa55ff1c0f855b2dfae7017da0ff3af574a Mon Sep 17 00:00:00 2001 From: SanjaySinghRajpoot Date: Wed, 28 Sep 2022 16:28:51 +0530 Subject: [PATCH 3/5] fix: Docker container id --- local/rest_api_gcbm/app.py | 26 +++++++++++--------------- local/rest_api_gcbm/requirements.txt | 1 + 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/local/rest_api_gcbm/app.py b/local/rest_api_gcbm/app.py index e1683fd0..663f897d 100644 --- a/local/rest_api_gcbm/app.py +++ b/local/rest_api_gcbm/app.py @@ -21,6 +21,7 @@ from flask import jsonify from config_table import rename_columns import sqlite3 +import docker from preprocess import get_config_templates, get_modules_cbm_config, get_provider_config flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func @@ -680,27 +681,22 @@ def gcbm_db(): return {"data": "db file uploaded succesfully. Proceed to the next step."} -def get_container_id(): - """ - Return the docker container id corresponding to the image gcbm-api running on Docker - """ - container_id = "" - os.system("docker ps >> simulation.txt") - with open("simulation.txt") as log_file: - logs = log_file.readlines() - for line in logs: - if "gcbm-api" in line: - container_id = line.split()[0] - os.system("rm simulation.txt") - return container_id +def checkDockerContainerStatus(container): + client = docker.from_env() + # cli = docker.APIClient() + if client.containers.list(filters={"name": container}): + response = client.containers.list(filters={"name": container}) + return str(response[0].id)[:12] + else: + return None @app.route("/gcbm/status", methods=["GET"]) def get_simulation_status(): """ - If the container gcbm-api is running, the associated logs are returned to the end user. + If the container gcbm-api is running, the associated logs are returned """ - container_id = get_container_id() + container_id = checkDockerContainerStatus("flint.gcbm") logs_file_name = container_id + ".txt" if container_id != "": logs_cmd = "docker logs " + container_id + " > " + logs_file_name + " 2>&1" diff --git a/local/rest_api_gcbm/requirements.txt b/local/rest_api_gcbm/requirements.txt index f0220ce0..c2a25b20 100644 --- a/local/rest_api_gcbm/requirements.txt +++ b/local/rest_api_gcbm/requirements.txt @@ -13,3 +13,4 @@ psutil==5.9.0 pandas matplotlib rasterio +docker==6.0.0 \ No newline at end of file From 8ae22efebd5ef5938c204c4b6fe3cfe1828ecc3a Mon Sep 17 00:00:00 2001 From: SanjaySinghRajpoot Date: Sun, 2 Oct 2022 16:49:16 +0530 Subject: [PATCH 4/5] fix: testcase ceased --- local/tests/test_rest_gcbm.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/local/tests/test_rest_gcbm.py b/local/tests/test_rest_gcbm.py index d3ac7d9b..ebfbd9f3 100644 --- a/local/tests/test_rest_gcbm.py +++ b/local/tests/test_rest_gcbm.py @@ -372,11 +372,6 @@ def test_gcbm_db(self, gcbm_endpoint, yield_db_file, yield_title): upload_endpoint = gcbm_endpoint + "upload/db" upload_response = requests.post(upload_endpoint, files=upload_files, data=data) assert upload_response.status_code == 200 - - def test_gcbm_status(self, gcbm_endpoint): - get_status_endpoint = gcbm_endpoint + "status" - get_status = requests.get(get_status_endpoint); - assert get_status.status_code == 200 def test_gcbm_tables(self, base_endpoint, yield_title): """This test case would check the get table method. \ From bf8899b449eed84f3979361233770449dd7e4d48 Mon Sep 17 00:00:00 2001 From: SanjaySinghRajpoot Date: Tue, 4 Oct 2022 11:33:53 +0530 Subject: [PATCH 5/5] fix: report endpoint --- local/tests/test_rest_gcbm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/local/tests/test_rest_gcbm.py b/local/tests/test_rest_gcbm.py index ebfbd9f3..7c2fd6eb 100644 --- a/local/tests/test_rest_gcbm.py +++ b/local/tests/test_rest_gcbm.py @@ -297,7 +297,7 @@ def test_status(self, gcbm_endpoint, yield_title): yield_title fixture. It returns the status of a simulation which \ we already uploaded the files under the title by \ yield_title fixutre. """ - status_endpoint = gcbm_endpoint + "status" + status_endpoint = gcbm_endpoint + "report" data = {"title": yield_title} status_response = requests.post(status_endpoint, json=data) assert status_response.status_code == 200