refactor classes for Connector

This commit is contained in:
2026-01-02 18:05:09 -08:00
parent fa1fa6402c
commit cb9a5f27b4
2 changed files with 35 additions and 18 deletions

View File

@@ -81,6 +81,7 @@ class Composer:
Attributes:
docker: DockerClient instance used to interact with the compose stack
name: string of the name of the compose file
services: list of Service objects defined by the stack
scheduler: BackgroundScheduler for updates
"""
@@ -90,6 +91,7 @@ class Composer:
compose_files=["store/stack/compose.yml"],
compose_env_files=["store/stack/.env"]
)
name = docker.compose.config(return_json=True)["name"]
def __init__(self, remote_url):
@@ -124,7 +126,7 @@ class Composer:
for service in self.services:
self.services[service].close()
services_dict = self.docker.compose.config().services
self.services = {service: Service(service, services_dict[service].labels, self) for service in services_dict}
self.services = {service: Service(service, services_dict[service], self) for service in services_dict}
def start_update_job(self):
@@ -190,6 +192,7 @@ class Composer:
logger.critical("Failed to autheticate remote, aborting!")
if current != self.repo.head.commit:
self._create_services()
self.name = self.docker.compose.config(return_json=True)["name"]
self.set_status("up")
logger.info(f"New compose stack deployed! Commit: {self.repo.head.commit}")
@@ -198,23 +201,23 @@ class Service:
"""Service instance meant to represent a container in a Composer stack
Attributes:
labels: labels given to the service
config: config of the service
name: name given to the service
parent: Composer instance containing the service
"""
def __init__(self, name, labels, parent):
def __init__(self, name, config, parent):
"""Initializes the Service instance
Args:
name: name given to the service
labels: labels given to the service
config: config of the service
parent: Composer instance containing the service
"""
self.name = name
self.labels = labels
self.config = config
self.parent = parent
self._update_task = self.start_update_job()
@@ -226,18 +229,18 @@ class Service:
return None
self._update_task.remove()
def get_container_info(self):
def get_status(self):
"""Gets the docker container info
"""Gets the status of the docker container
Returns:
A docker container if it exists, if not returns None.
str specifying the status of the docker container
"""
container = self.parent.docker.compose.ps(services=[self.name])
if not container:
return None
return container[0]
return "down"
return container[0].state.status
def start_update_job(self):
@@ -250,13 +253,14 @@ class Service:
"""
self.update_time = self.parent.update_time
if 'composer.update' in self.labels:
labels = self.config.labels
if 'composer.update' in labels:
try:
self.update_time = int(self.labels['composer.update'])
self.update_time = int(labels['composer.update'])
assert self.update_time > 0
except (AssertionError, ValueError):
self.update_time = self.parent.update_time
logging.warn(f"Service {self.name} has an invalid composer.update value of {self.labels['composer.update']}, defaulting to update interval of {self.update_time} minutes")
logging.warn(f"Service {self.name} has an invalid composer.update value of {labels['composer.update']}, defaulting to update interval of {self.update_time} minutes")
if self.update_time != 0:
return self.parent.scheduler.add_job(self.update, 'interval', minutes=self.update_time)
return None

View File

@@ -6,15 +6,26 @@ class Connector:
def __init__(self, connector):
self.connector = connector
def get_connector_info(self):
info = {
"name": self.connector.name,
"running": len(self.connector.docker.compose.ps()),
"stopped": len(self.connector.services) - len(self.connector.docker.compose.ps())
}
return json.dumps(info)
def get_container_info(self, container_name):
if container_name not in self.connector.services:
return None
container = self.connector.services[container_name].get_container_info()
return json.loads({
"image": container.image,
"status": container.state.status
container = self.connector.services[container_name]
return json.dumps({
"image": container.config.image,
"status": container.get_status()
})
def set_connector_status(self, status):
self.connector.set_status(status)
def set_container_status(self, container_name, status):
if container_name not in self.connector.services:
return None
@@ -26,6 +37,8 @@ remote = Setup.create_credential_remote()
composer = Composer(remote)
connector = Connector(composer)
while True:
print(connector.get_connector_info())
container = input("Container: ")
status = input("Connector: ")
print(connector.get_container_info(container))
status = input("Status: ")
connector.set_container_status(container, status)