Working container spawner
This commit is contained in:
parent
7a647c06b7
commit
357e387ed2
|
@ -1,18 +1,19 @@
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
import docker
|
import docker
|
||||||
|
|
||||||
from restic_volume_backup.config import Config
|
from restic_volume_backup.config import Config
|
||||||
|
|
||||||
def run():
|
|
||||||
|
def run(image: str, command: str):
|
||||||
config = Config()
|
config = Config()
|
||||||
client = docker.DockerClient(base_url=config.docker_base_url)
|
client = docker.DockerClient(base_url=config.docker_base_url)
|
||||||
|
|
||||||
container = client.containers.run(
|
container = client.containers.run(
|
||||||
'restic-volume-backup_backup',
|
image,
|
||||||
'echo "Hello"',
|
command,
|
||||||
labels={"restic-volume-backup.backup_process": 'True'},
|
labels={"restic-volume-backup.backup_process": 'True'},
|
||||||
auto_remove=True,
|
auto_remove=True,
|
||||||
remove=True,
|
|
||||||
detach=True,
|
detach=True,
|
||||||
environment={
|
environment={
|
||||||
'test1': 'value1',
|
'test1': 'value1',
|
||||||
|
@ -25,4 +26,14 @@ def run():
|
||||||
working_dir=os.getcwd(),
|
working_dir=os.getcwd(),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Pull logs and exist status of container
|
print("Backup process container:", container.name)
|
||||||
|
logs = container.logs(stdout=True, stderr=True, stream=True)
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
time.sleep(3)
|
||||||
|
for line in logs:
|
||||||
|
print(line.decode(), end='')
|
||||||
|
# Raises requests.exceptions.HTTPError if continer is dead
|
||||||
|
container.top()
|
||||||
|
except Exception as ex:
|
||||||
|
print("Container stopped")
|
||||||
|
|
|
@ -25,11 +25,20 @@ def main():
|
||||||
print()
|
print()
|
||||||
|
|
||||||
elif args.action == 'backup':
|
elif args.action == 'backup':
|
||||||
print("Starting backup ..")
|
if containers.backup_process_running:
|
||||||
|
raise ValueError("Backup process alredy running")
|
||||||
|
|
||||||
|
print("Initializing repository")
|
||||||
|
|
||||||
# TODO: Errors when repo already exists
|
# TODO: Errors when repo already exists
|
||||||
restic.init_repo(config.repository)
|
restic.init_repo(config.repository)
|
||||||
|
|
||||||
backup_runner.run()
|
print("Starting backup container..")
|
||||||
|
backup_runner.run(
|
||||||
|
containers.this_container.image,
|
||||||
|
# "sleep 10",
|
||||||
|
'./test.sh',
|
||||||
|
)
|
||||||
# for vol in containers.backup_volumes():
|
# for vol in containers.backup_volumes():
|
||||||
# restic.backup_volume(Config.repository, vol)
|
# restic.backup_volume(Config.repository, vol)
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ 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.state = data.get('State')
|
||||||
|
self.image = data.get('Image')
|
||||||
self.labels = data.get('Labels', {})
|
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')]
|
||||||
|
|
Loading…
Reference in New Issue