2019-04-17 01:28:07 +00:00
|
|
|
import os
|
|
|
|
import docker
|
|
|
|
|
|
|
|
from restic_volume_backup.config import Config
|
|
|
|
|
2019-04-17 02:38:15 +00:00
|
|
|
|
2019-04-18 01:52:28 +00:00
|
|
|
def run(image: str = None, command: str = None, volumes: dict = None,
|
2019-11-12 11:39:49 +00:00
|
|
|
environment: dict = None, labels: dict = None):
|
2019-04-17 01:45:51 +00:00
|
|
|
config = Config()
|
|
|
|
client = docker.DockerClient(base_url=config.docker_base_url)
|
2019-04-17 01:28:07 +00:00
|
|
|
|
|
|
|
container = client.containers.run(
|
2019-04-17 02:38:15 +00:00
|
|
|
image,
|
|
|
|
command,
|
2019-04-18 01:52:28 +00:00
|
|
|
labels=labels,
|
2019-04-18 05:22:59 +00:00
|
|
|
# auto_remove=True,
|
2019-04-17 01:28:07 +00:00
|
|
|
detach=True,
|
2019-11-12 11:39:49 +00:00
|
|
|
environment=environment,
|
2019-04-17 18:20:52 +00:00
|
|
|
volumes=volumes,
|
2019-04-17 01:28:07 +00:00
|
|
|
working_dir=os.getcwd(),
|
2019-04-18 05:22:59 +00:00
|
|
|
tty=True,
|
2019-04-17 01:28:07 +00:00
|
|
|
)
|
|
|
|
|
2019-04-17 02:38:15 +00:00
|
|
|
print("Backup process container:", container.name)
|
2019-04-18 05:22:59 +00:00
|
|
|
log_generator = container.logs(stdout=True, stderr=True, stream=True, follow=True)
|
|
|
|
with open('backup.log', 'w') as fd:
|
|
|
|
for line in log_generator:
|
|
|
|
line = line.decode()
|
|
|
|
fd.write(line)
|
|
|
|
print(line, end='')
|
|
|
|
|
|
|
|
container.reload()
|
|
|
|
print("ExitCode", container.attrs['State']['ExitCode'])
|
|
|
|
container.remove()
|