optimized draft

This commit is contained in:
Kevin Veen-Birkenbach 2023-11-16 14:04:42 +01:00
parent 9b0d744052
commit 40798d2294
5 changed files with 49 additions and 7 deletions

View File

@ -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")

View File

@ -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

View File

@ -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}}"

View File

@ -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}}'

View File

@ -0,0 +1 @@
update_docker_script: "{{path_administrator_scripts}}/update-docker.py"