restic-compose-backup/restic_compose_backup/utils.py

55 lines
1.1 KiB
Python
Raw Normal View History

2019-12-03 05:23:44 +00:00
import os
from contextlib import contextmanager
import docker
2019-04-15 17:49:57 +00:00
2019-12-03 08:40:02 +00:00
from restic_compose_backup.config import Config
TRUE_VALUES = ['1', 'true', 'True', True, 1]
def list_containers():
"""
List all containers.
Returns:
List of raw container json data from the api
"""
2019-04-16 16:22:03 +00:00
config = Config()
client = docker.DockerClient(base_url=config.docker_base_url)
all_containers = client.containers.list()
client.close()
return [c.attrs for c in all_containers]
2019-11-15 13:23:56 +00:00
def is_true(value):
"""
Evaluates the truthfullness of a bool value in container labels
"""
return value in TRUE_VALUES
2019-11-25 21:28:04 +00:00
def strip_root(path):
"""
Removes the root slash in a path.
Example: /srv/data becomes srv/data
"""
path = path.strip()
if path.startswith('/'):
return path[1:]
return path
2019-12-03 05:23:44 +00:00
@contextmanager
def environment(name, value):
"""Tempset env var"""
old_val = os.environ.get(name)
os.environ[name] = value
try:
yield
finally:
if old_val is None:
del os.environ[name]
else:
os.environ[name] = old_val