From 808dc294b98dee425d8b3567e2e3e20ffaf7b34a Mon Sep 17 00:00:00 2001 From: Jonathan Branan Date: Tue, 18 Oct 2022 14:14:53 -0500 Subject: [PATCH] added stop container --- cclient.py | 5 +++++ config.json.example | 3 ++- cprocess.py | 13 +++++++++---- crane.py | 6 +++++- test_crane.py | 14 ++++++++++---- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/cclient.py b/cclient.py index 7386342..05ed029 100644 --- a/cclient.py +++ b/cclient.py @@ -11,4 +11,9 @@ def c_get_containers(req_obj, host, port, jwt, endpoint): def c_start_container(req_obj, host, port, jwt, endpoint, cid): url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/{cid}/start' c_start_container_response = req_obj.post(url, headers={"Authorization": f"Bearer {jwt}"},verify=False) + return c_start_container_response.status_code + +def c_stop_container(req_obj, host, port, jwt, endpoint, cid): + url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/{cid}/stop' + c_start_container_response = req_obj.post(url, headers={"Authorization": f"Bearer {jwt}"},verify=False) return c_start_container_response.status_code \ No newline at end of file diff --git a/config.json.example b/config.json.example index 417a259..7673c78 100644 --- a/config.json.example +++ b/config.json.example @@ -9,5 +9,6 @@ "use_pushover": false, "use_log": true, "po_key": "", - "po_token": "" + "po_token": "", + "start_containers": false } \ No newline at end of file diff --git a/cprocess.py b/cprocess.py index 5be4a18..ff99463 100644 --- a/cprocess.py +++ b/cprocess.py @@ -1,8 +1,13 @@ -def build_cont_list(obj): +# TODO add file for containers that need to be started +def build_cont_list(obj,hypercare_containers): cont_list = [] for i, c in enumerate(obj): if c["State"].lower() != "running": - print(f'index: {i} container: {c["Names"][0].lstrip("/")} State: {c["State"]} ID: {c["Id"]}') - cont_list.append(c) + if c["Names"][0].lstrip("/") in hypercare_containers: + print(f'index: {i} container: {c["Names"][0].lstrip("/")} State: {c["State"]} ID: {c["Id"]}') + cont_list.append(c) print(len(cont_list)) - return cont_list \ No newline at end of file + return cont_list + +def process_cont_list(full_cont_list, cont_fn): + pass \ No newline at end of file diff --git a/crane.py b/crane.py index e97fa6c..b73c4d2 100644 --- a/crane.py +++ b/crane.py @@ -1,6 +1,6 @@ import pushover from json import load -from cclient import c_auth, c_get_containers +from cclient import c_auth, c_get_containers, c_start_container from clogging import * import time import datetime @@ -14,6 +14,9 @@ class Crn: self.st = datetime.datetime.now() with open('./config.json') as c: self.config = load(c) + with open('./containers.json') as cts: + self.containers = load(cts) + self.observed_containers = self.containers.values() # Create the api object self.cc = requests # Create the logging and pushover objects @@ -32,6 +35,7 @@ class Crn: self.username = self.config["username"] self.password = self.config["password"] self.endpoint = self.config["endpoint"] + self.start_containers = self.config["start_containers"] cont_log(self) cont_notify(self) self.t = time diff --git a/test_crane.py b/test_crane.py index 1651ffe..96c955f 100644 --- a/test_crane.py +++ b/test_crane.py @@ -1,7 +1,7 @@ import unittest import requests from json import load -from cclient import c_auth, c_get_containers, c_start_container +from cclient import c_auth, c_get_containers, c_start_container, c_stop_container from cprocess import build_cont_list class TestCrane(unittest.TestCase): @@ -13,8 +13,9 @@ class TestCrane(unittest.TestCase): self.username = self.config["username"] self.password = self.config["password"] self.endpoint = self.config["endpoint"] - self.cid = 'ef8fee86e02b2b82acbddf6f0da1ff023f60bfe52c0b4087cac29c1686ccbac4' + self.cid = '06ffaed8153495db19b35f5982e0f9d4cfea0da7b803f825a6686a4017af8d6e' self.req_obj = requests + self.hypercare_containers = ['hello-world'] def test_c_auth(self): self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password) self.assertTrue(self.jwt, "No JWT returned by cauth.") @@ -27,7 +28,7 @@ class TestCrane(unittest.TestCase): def test_build_cont_list(self): self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password) self.cont_obj = c_get_containers(self.req_obj, self.host, self.port, self.jwt, self.endpoint) - self.cont_list = build_cont_list(self.cont_obj) + self.cont_list = build_cont_list(self.cont_obj, self.hypercare_containers) self.assertTrue(self.cont_list, "No cont_list returned by build_cont_list.") def test_c_start_container(self): @@ -36,6 +37,11 @@ class TestCrane(unittest.TestCase): print(self.c_start_container_response) self.assertTrue(self.c_start_container_response, "No c_start_container_resonse returned by c_start_container.") # 204 success 304 already on - + + def test_c_stop_container(self): + self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password) + self.c_stop_container_response = c_stop_container(self.req_obj, self.host, self.port, self.jwt, self.endpoint, self.cid) + print(self.c_stop_container_response) + self.assertTrue(self.c_stop_container_response, "No c_start_container_resonse returned by c_start_container.") if __name__ == '__main__': unittest.main() \ No newline at end of file