Support backup labels
This commit is contained in:
parent
de94bf2107
commit
73c3648617
|
@ -6,17 +6,43 @@ DOCKER_BASE_URL = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock"
|
||||||
VOLUME_TYPE_BIND = "bind"
|
VOLUME_TYPE_BIND = "bind"
|
||||||
VOLUME_TYPE_VOLUME = "volume"
|
VOLUME_TYPE_VOLUME = "volume"
|
||||||
|
|
||||||
|
|
||||||
class Container:
|
class Container:
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.id = data.get('Id')
|
self.id = data.get('Id')
|
||||||
|
self.state = data.get('State')
|
||||||
|
self.labels = data.get('Labels')
|
||||||
self.names = data.get('Names')
|
self.names = data.get('Names')
|
||||||
self.mounts = [Mount(mnt, container=self) for mnt in data.get('Mounts')]
|
self.mounts = [Mount(mnt, container=self) for mnt in data.get('Mounts')]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def backup_enabled(self):
|
||||||
|
return self.labels.get('restic-volume-backup.enabled') == 'True'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_running(self):
|
||||||
|
return self.state == 'running'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def service_name(self):
|
||||||
|
return self.labels['com.docker.compose.service']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def project_name(self):
|
||||||
|
return self.labels['com.docker.compose.project']
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_oneoff(self):
|
||||||
|
return self.labels['com.docker.compose.oneoff'] == 'True'
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {
|
return {
|
||||||
"Id": self.id,
|
'Id': self.id,
|
||||||
"Mounts": [mnt.data for mnt in self.mounts]
|
'Names': self.names,
|
||||||
|
'State': self.state,
|
||||||
|
'Labels': self.labels,
|
||||||
|
'Mounts': [mnt.data for mnt in self.mounts]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,18 +116,29 @@ class RunningContainers:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
self.containers = []
|
self.containers = []
|
||||||
|
self.all_containers = []
|
||||||
self.backup_container = None
|
self.backup_container = None
|
||||||
|
|
||||||
|
# Read all containers and find this container
|
||||||
for entry in all_containers:
|
for entry in all_containers:
|
||||||
if entry['Id'].startswith(os.environ['HOSTNAME']):
|
container = Container(entry)
|
||||||
self.backup_container = Container(entry)
|
|
||||||
|
if container.id.startswith(os.environ['HOSTNAME']):
|
||||||
|
self.backup_container = container
|
||||||
else:
|
else:
|
||||||
if entry['State'] == "running":
|
self.all_containers.append(container)
|
||||||
self.containers.append(Container(entry))
|
|
||||||
|
|
||||||
if not self.backup_container:
|
if not self.backup_container:
|
||||||
raise ValueError("Cannot find metadata for backup container")
|
raise ValueError("Cannot find metadata for backup container")
|
||||||
|
|
||||||
|
for container in self.all_containers:
|
||||||
|
# Weed out containers not beloging to this project.
|
||||||
|
if container.project_name == self.backup_container.project_name:
|
||||||
|
# Keep only containers with backup enabled
|
||||||
|
# and not oneoffs (started manually with run or similar)
|
||||||
|
if container.backup_enabled and not container.is_oneoff:
|
||||||
|
self.containers.append(container)
|
||||||
|
|
||||||
def backup_volumes(self):
|
def backup_volumes(self):
|
||||||
return self.backup_container.mounts
|
return self.backup_container.mounts
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue