added stop container

This commit is contained in:
Jonathan Branan 2022-10-18 14:14:53 -05:00
parent f4e7273735
commit 808dc294b9
5 changed files with 31 additions and 10 deletions

View File

@ -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): def c_start_container(req_obj, host, port, jwt, endpoint, cid):
url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/{cid}/start' 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) 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 return c_start_container_response.status_code

View File

@ -9,5 +9,6 @@
"use_pushover": false, "use_pushover": false,
"use_log": true, "use_log": true,
"po_key": "", "po_key": "",
"po_token": "" "po_token": "",
"start_containers": false
} }

View File

@ -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 = [] cont_list = []
for i, c in enumerate(obj): for i, c in enumerate(obj):
if c["State"].lower() != "running": if c["State"].lower() != "running":
print(f'index: {i} container: {c["Names"][0].lstrip("/")} State: {c["State"]} ID: {c["Id"]}') if c["Names"][0].lstrip("/") in hypercare_containers:
cont_list.append(c) print(f'index: {i} container: {c["Names"][0].lstrip("/")} State: {c["State"]} ID: {c["Id"]}')
cont_list.append(c)
print(len(cont_list)) print(len(cont_list))
return cont_list return cont_list
def process_cont_list(full_cont_list, cont_fn):
pass

View File

@ -1,6 +1,6 @@
import pushover import pushover
from json import load 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 * from clogging import *
import time import time
import datetime import datetime
@ -14,6 +14,9 @@ class Crn:
self.st = datetime.datetime.now() self.st = datetime.datetime.now()
with open('./config.json') as c: with open('./config.json') as c:
self.config = load(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 # Create the api object
self.cc = requests self.cc = requests
# Create the logging and pushover objects # Create the logging and pushover objects
@ -32,6 +35,7 @@ class Crn:
self.username = self.config["username"] self.username = self.config["username"]
self.password = self.config["password"] self.password = self.config["password"]
self.endpoint = self.config["endpoint"] self.endpoint = self.config["endpoint"]
self.start_containers = self.config["start_containers"]
cont_log(self) cont_log(self)
cont_notify(self) cont_notify(self)
self.t = time self.t = time

View File

@ -1,7 +1,7 @@
import unittest import unittest
import requests import requests
from json import load 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 from cprocess import build_cont_list
class TestCrane(unittest.TestCase): class TestCrane(unittest.TestCase):
@ -13,8 +13,9 @@ class TestCrane(unittest.TestCase):
self.username = self.config["username"] self.username = self.config["username"]
self.password = self.config["password"] self.password = self.config["password"]
self.endpoint = self.config["endpoint"] self.endpoint = self.config["endpoint"]
self.cid = 'ef8fee86e02b2b82acbddf6f0da1ff023f60bfe52c0b4087cac29c1686ccbac4' self.cid = '06ffaed8153495db19b35f5982e0f9d4cfea0da7b803f825a6686a4017af8d6e'
self.req_obj = requests self.req_obj = requests
self.hypercare_containers = ['hello-world']
def test_c_auth(self): def test_c_auth(self):
self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password) self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password)
self.assertTrue(self.jwt, "No JWT returned by cauth.") self.assertTrue(self.jwt, "No JWT returned by cauth.")
@ -27,7 +28,7 @@ class TestCrane(unittest.TestCase):
def test_build_cont_list(self): def test_build_cont_list(self):
self.jwt = c_auth(self.req_obj, self.host, self.port, self.username, self.password) 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_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.") self.assertTrue(self.cont_list, "No cont_list returned by build_cont_list.")
def test_c_start_container(self): def test_c_start_container(self):
@ -36,6 +37,11 @@ class TestCrane(unittest.TestCase):
print(self.c_start_container_response) print(self.c_start_container_response)
self.assertTrue(self.c_start_container_response, "No c_start_container_resonse returned by c_start_container.") self.assertTrue(self.c_start_container_response, "No c_start_container_resonse returned by c_start_container.")
# 204 success 304 already on # 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__': if __name__ == '__main__':
unittest.main() unittest.main()