From 40798d2294ed70c664956461d5dad28c7e300fc4 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 16 Nov 2023 14:04:42 +0100 Subject: [PATCH] optimized draft --- ...-compose-instances.py => update-docker.py} | 27 ++++++++++++++----- roles/update-docker/handlers/main.yml | 12 +++++++++ roles/update-docker/tasks/main.yml | 9 +++++++ .../templates/update-docker.service.j2 | 7 +++++ roles/update-docker/vars/main.yml | 1 + 5 files changed, 49 insertions(+), 7 deletions(-) rename roles/update-docker/files/{update-docker-compose-instances.py => update-docker.py} (77%) create mode 100644 roles/update-docker/handlers/main.yml create mode 100644 roles/update-docker/tasks/main.yml create mode 100644 roles/update-docker/templates/update-docker.service.j2 create mode 100644 roles/update-docker/vars/main.yml diff --git a/roles/update-docker/files/update-docker-compose-instances.py b/roles/update-docker/files/update-docker.py similarity index 77% rename from roles/update-docker/files/update-docker-compose-instances.py rename to roles/update-docker/files/update-docker.py index 45795001..b2b7096e 100644 --- a/roles/update-docker/files/update-docker-compose-instances.py +++ b/roles/update-docker/files/update-docker.py @@ -3,11 +3,16 @@ import subprocess import sys def run_command(command): - try: - subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) - except subprocess.CalledProcessError as e: - print(e.output.decode()) - sys.exit(e.returncode) + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + + # Iterate over the output lines + for line in iter(process.stdout.readline, b''): + sys.stdout.write(line.decode()) + + process.stdout.close() + return_code = process.wait() + if return_code: + raise subprocess.CalledProcessError(return_code, command) def git_pull(directory): os.chdir(directory) @@ -34,9 +39,17 @@ def update_docker(directory): os.chdir(directory) before_digests = get_image_digests(directory) print("Pulling docker images.") - run_command("docker-compose pull") - after_digests = get_image_digests(directory) + + try: + run_command("docker-compose pull") + except subprocess.CalledProcessError as e: + if "pull access denied" in e.output.decode() or "must be built from source" in e.output.decode(): + print("Need to build the image from source.") + else: + print("Failed to pull images with unexpected error.") + raise + after_digests = get_image_digests(directory) if before_digests != after_digests: print("Changes detected in image digests. Rebuilding containers.") run_command("docker-compose up -d --build --force-recreate") diff --git a/roles/update-docker/handlers/main.yml b/roles/update-docker/handlers/main.yml new file mode 100644 index 00000000..ad1c55a9 --- /dev/null +++ b/roles/update-docker/handlers/main.yml @@ -0,0 +1,12 @@ +- name: "reload disc-space-check.service" + systemd: + name: disc-space-check.service + state: reloaded + enabled: yes + daemon_reload: yes +- name: "restart disc-space-check.timer" + systemd: + name: disc-space-check.timer + state: restarted + enabled: yes + daemon_reload: yes diff --git a/roles/update-docker/tasks/main.yml b/roles/update-docker/tasks/main.yml new file mode 100644 index 00000000..82570d2e --- /dev/null +++ b/roles/update-docker/tasks/main.yml @@ -0,0 +1,9 @@ +- name: configure docker-volume-backup-cleanup.service + template: + src: docker-volume-backup-cleanup.service.j2 + dest: /etc/systemd/system/docker-volume-backup-cleanup.service + +- name: create {{update_docker_script}} + copy: + src: update-docker.py + dest: "{{update_docker_script}}" \ No newline at end of file diff --git a/roles/update-docker/templates/update-docker.service.j2 b/roles/update-docker/templates/update-docker.service.j2 new file mode 100644 index 00000000..a6690ea8 --- /dev/null +++ b/roles/update-docker/templates/update-docker.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=Updates Docker Instances +OnFailure=systemd-notifier@%n.service + +[Service] +Type=oneshot +ExecStart=/bin/sh -c '/usr/bin/python {{update_docker_script}} {{docker_volume_backup_cleanup_trigger_directory}}' \ No newline at end of file diff --git a/roles/update-docker/vars/main.yml b/roles/update-docker/vars/main.yml new file mode 100644 index 00000000..c74cdbd1 --- /dev/null +++ b/roles/update-docker/vars/main.yml @@ -0,0 +1 @@ +update_docker_script: "{{path_administrator_scripts}}/update-docker.py"