Files
docker-mods-uptime-kuma-tim…/root/app/auto_uptime_kuma/docker_service.py
T
labmonkey c25d25227f - Refactored whole code so that its not such a mess
- Updated documentation so that it explains better how the mod works, requirements, setting up labels and notifications
- Reduced number of API calls when swag container restarts
- Added better support for setting up notifications and fixed issue with default notifications not being applied
- Fixed an issue with mod crashing when there were Manually added monitors in Uptime Kuma
- Fixed an issue with labels that container numeric values
- Added basic support for Monitor Groups
- More log messages so that its easier to grasp what is happening
- Fixed a crash when mod was executed while uptime kuma was not running (mod just gently stops now)
2024-03-18 14:47:07 +01:00

52 lines
1.8 KiB
Python

import docker
class DockerService:
"""
A service class for interacting with Docker containers that are used by SWAG mods.
"""
client = None
_containers = None
label_prefix = None
def __init__(self, label_prefix: str):
self.label_prefix = label_prefix
self.client = docker.from_env()
def get_swag_containers(self):
"""
Retrieve Docker containers filtered by "swag.my_mod.enabled=true":
>>> swag = SwagDocker("swag.my_mod")
>>> containers = swag.getSwagContainers()
"""
if self._containers is None:
self._containers = self.client.containers.list(
filters={"label": [f"{self.label_prefix}.enabled=true"]}
)
return self._containers
def parse_container_labels(self, container_labels, extra_prefix=""):
"""
Having following example container labels:
swag.my_mod.enabled: true
swag.my_mod.config.apple: "123"
swag.my_mod.config.orange: "456"
>>> for container in containers:
>>> containerConfigA = swagDocker.parseContainerLabels(container.labels)
# Above will return {"enabled": true, "config.apple": "123", "config.orange": "456"}
>>> containerConfigB = swagDocker.parseContainerLabels(container.labels, ".config.")
# Above will return {"apple": "123", "orange": "456"}
"""
filtered_container_labels = {}
full_prefix = f"{self.label_prefix}{extra_prefix}"
prefix_length = len(full_prefix)
for label, value in container_labels.items():
if label.startswith(full_prefix):
parsed_label = label[prefix_length:]
filtered_container_labels[parsed_label] = value
return filtered_container_labels