mirror of
https://github.com/ZettaIO/restic-compose-backup.git
synced 2025-10-10 20:27:42 +00:00
Remove stale backup process containers
This commit is contained in:
@@ -1,26 +1,45 @@
|
||||
import os
|
||||
import logging
|
||||
from typing import List
|
||||
from contextlib import contextmanager
|
||||
import docker
|
||||
|
||||
from restic_compose_backup.config import Config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
TRUE_VALUES = ['1', 'true', 'True', True, 1]
|
||||
|
||||
|
||||
def list_containers():
|
||||
def docker_client():
|
||||
config = Config()
|
||||
return docker.DockerClient(base_url=config.docker_base_url)
|
||||
|
||||
|
||||
def list_containers() -> List[dict]:
|
||||
"""
|
||||
List all containers.
|
||||
|
||||
Returns:
|
||||
List of raw container json data from the api
|
||||
"""
|
||||
config = Config()
|
||||
client = docker.DockerClient(base_url=config.docker_base_url)
|
||||
client = docker_client()
|
||||
all_containers = client.containers.list(all=True)
|
||||
client.close()
|
||||
return [c.attrs for c in all_containers]
|
||||
|
||||
|
||||
def remove_containers(containers: List['Container']):
|
||||
client = docker_client()
|
||||
logger.info('Attempting to delete stale backup process containers')
|
||||
for container in containers:
|
||||
logger.info(' -> deleting %s', container.name)
|
||||
try:
|
||||
c = client.containers.get(container.name)
|
||||
c.remove()
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
|
||||
|
||||
def is_true(value):
|
||||
"""
|
||||
Evaluates the truthfullness of a bool value in container labels
|
||||
|
Reference in New Issue
Block a user