Merge pull request #10 from jonbranan/fix-container-always-start

mostly stable
This commit is contained in:
Jonathan Branan 2023-03-21 12:16:45 -05:00 committed by GitHub
commit b83393286d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -8,6 +8,12 @@ def c_get_containers(req_obj, host, port, jwt, endpoint):
c_get_containers_response = req_obj.get(url, headers={"Authorization": f"Bearer {jwt}"},verify=False) c_get_containers_response = req_obj.get(url, headers={"Authorization": f"Bearer {jwt}"},verify=False)
return c_get_containers_response.json() return c_get_containers_response.json()
#filters={"name": ["restic","qbittorrent"],"status": ["paused","dead","created","exited","removing","restarting","created"]}
def c_get_filtered_containers(req_obj, host, port, jwt, endpoint, containers, statuses):
url = f'https://{host}:{port}/api/endpoints/{endpoint}/docker/containers/json?filters={"name": {containers},"status": {statuses}}'
c_get_containers_response = req_obj.get(url, headers={"Authorization": f"Bearer {jwt}"},verify=False)
return c_get_containers_response.json()
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)

View File

@ -1,4 +1,5 @@
# TODO add file for containers that need to be started # TODO add file for containers that need to be started
def build_cont_list(obj, hypercare_containers): def build_cont_list(obj, hypercare_containers):
cont_list = [] cont_list = []
for i, c in enumerate(obj): for i, c in enumerate(obj):

View File

@ -50,7 +50,7 @@ class Crn:
self.tl.info('Authenticated successfully.') self.tl.info('Authenticated successfully.')
self.cont_obj = c_get_containers(self.cc, self.host, self.port, self.jwt, self.endpoint) self.cont_obj = c_get_containers(self.cc, self.host, self.port, self.jwt, self.endpoint)
self.tl.debug('Collected container data.') self.tl.debug('Collected container data.')
self.cont_list = build_cont_list(self.cont_obj, self.observed_containers) self.cont_list = build_full_cont_list(self.cont_obj, self.observed_containers)
self.tl.debug('Building container list.') self.tl.debug('Building container list.')
self.process_cont_list_response = process_cont_list(self.cont_list, c_start_container, self.cc, self.host, self.port, self.jwt, self.endpoint) self.process_cont_list_response = process_cont_list(self.cont_list, c_start_container, self.cc, self.host, self.port, self.jwt, self.endpoint)
if self.process_cont_list_response: if self.process_cont_list_response:

View File

@ -1,7 +1,7 @@
import unittest import unittest
import requests import requests
from tomllib import load from tomllib import load
from cclient import c_auth, c_get_containers, c_start_container, c_stop_container from cclient import c_auth, c_get_containers, c_start_container, c_stop_container, c_get_filtered_containers
from cprocess import build_cont_list, process_cont_list, build_full_cont_list, process_cont_status from cprocess import build_cont_list, process_cont_list, build_full_cont_list, process_cont_status
unittest.TestLoader.sortTestMethodsUsing = None unittest.TestLoader.sortTestMethodsUsing = None
@ -17,8 +17,10 @@ class TestCrane(unittest.TestCase):
self.cid = 'aa5b217ca6217fd9d268396039da69ea9e4a5aff381b3dceb71edb5a1f4d429d' self.cid = 'aa5b217ca6217fd9d268396039da69ea9e4a5aff381b3dceb71edb5a1f4d429d'
self.req_obj = requests self.req_obj = requests
self.hypercare_containers = ['hello-world'] self.hypercare_containers = ['hello-world']
#self.status_filters = ["paused","dead","created","exited","removing","restarting","created"]
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_obj_f = c_get_filtered_containers(self.req_obj, self.host, self.port, self.jwt, self.endpoint,self.hypercare_containers,self.status_filters)
def test_c_auth(self): def test_c_auth(self):
self.assertTrue(self.jwt, "No JWT returned by cauth.") self.assertTrue(self.jwt, "No JWT returned by cauth.")
@ -26,6 +28,9 @@ class TestCrane(unittest.TestCase):
def test_c_get_containers(self): def test_c_get_containers(self):
self.assertTrue(self.cont_obj, "No cont object returned by c_get_containers.") self.assertTrue(self.cont_obj, "No cont object returned by c_get_containers.")
def test_c_get_containers_f(self):
self.assertTrue(self.cont_obj_f, "No cont object returned by c_get_containers.")
def test_a_is_hypercare_container_status(self): def test_a_is_hypercare_container_status(self):
self.cont_full_list = build_full_cont_list(self.cont_obj, self.hypercare_containers) self.cont_full_list = build_full_cont_list(self.cont_obj, self.hypercare_containers)
self.process_cont_status_response = process_cont_status(self.cont_full_list) self.process_cont_status_response = process_cont_status(self.cont_full_list)