diff --git a/.gitignore b/.gitignore index 489bdd0..5374ac4 100644 --- a/.gitignore +++ b/.gitignore @@ -159,3 +159,4 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +venv/ diff --git a/nomad_missing_services.py b/nomad_missing_services.py index e42028e..27180bf 100755 --- a/nomad_missing_services.py +++ b/nomad_missing_services.py @@ -5,7 +5,9 @@ from typing import Any from typing import cast import requests_unixsocket -requests = requests_unixsocket.Session() +from requests.exceptions import JSONDecodeError + +session = requests_unixsocket.Session() NOMAD_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646") NOMAD_TOKEN = environ.get("NOMAD_TOKEN") @@ -18,7 +20,7 @@ def nomad_req( if NOMAD_TOKEN: headers["X-Nomad-Token"] = NOMAD_TOKEN - response = requests.request( + response = session.request( method, f"{NOMAD_ADDR}/v1/{'/'.join(path)}", params=params, @@ -28,7 +30,7 @@ def nomad_req( try: return response.json() - except requests.exceptions.JSONDecodeError: + except JSONDecodeError: return response.text @@ -43,11 +45,14 @@ def extract_job_services(job: dict[str, Any]) -> dict[str, str]: return services + exit_code = 0 parser = ArgumentParser( description="Checks for missing services and optionally restarts their allocs.", ) -parser.add_argument("-r", "--restart", action="store_true", help="Restart allocs for missing services") +parser.add_argument( + "-r", "--restart", action="store_true", help="Restart allocs for missing services" +) args = parser.parse_args() for job in nomad_req("jobs"): @@ -87,7 +92,10 @@ for job in nomad_req("jobs"): restart_allocs: set[str] = set() for allocation in nomad_req("job", job_detail["ID"], "allocations"): allocation = cast(dict[str, Any], allocation) - if allocation["ClientStatus"] == "running" and allocation["TaskGroup"] in restart_groups: + if ( + allocation["ClientStatus"] == "running" + and allocation["TaskGroup"] in restart_groups + ): restart_allocs.add(allocation["ID"]) # Restart allocs associated with missing services diff --git a/nomad_orphan_services.py b/nomad_orphan_services.py index d673cf4..ff60502 100755 --- a/nomad_orphan_services.py +++ b/nomad_orphan_services.py @@ -5,7 +5,11 @@ from typing import Any from typing import cast import requests_unixsocket -requests = requests_unixsocket.Session() +from requests.exceptions import HTTPError +from requests.exceptions import JSONDecodeError + + +session = requests_unixsocket.Session() NOMAD_ADDR = environ.get("NOMAD_ADDR", "http://127.0.0.1:4646") @@ -19,7 +23,7 @@ def nomad_req( if NOMAD_TOKEN: headers["X-Nomad-Token"] = NOMAD_TOKEN - response = requests.request( + response = session.request( method, f"{NOMAD_ADDR}/v1/{'/'.join(path)}", params=params, @@ -29,7 +33,7 @@ def nomad_req( try: return response.json() - except requests.exceptions.JSONDecodeError: + except JSONDecodeError: return response.text @@ -37,7 +41,9 @@ exit_code = 0 parser = ArgumentParser( description="Checks for orphaned services and optionally deletes them.", ) -parser.add_argument("-d", "--delete", action="store_true", help="Delete orphan services") +parser.add_argument( + "-d", "--delete", action="store_true", help="Delete orphan services" +) args = parser.parse_args() @@ -55,7 +61,7 @@ for namespace in nomad_req("services"): try: alloc = nomad_req("allocation", alloc_id) continue - except requests.exceptions.HTTPError as e: + except HTTPError as e: if e.response.status_code == 404: alloc_found = False message = f"alloc {alloc_id} not found for {service_name}."