From 442a85135e71778764819a2934396136ee862b02 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 25 Dec 2022 13:40:38 +0100 Subject: [PATCH] Implemented docker health check --- .../files/docker-health-check.service | 7 ++++++ .../files/docker-health-check.sh | 10 +++++++++ .../files/docker-health-check.timer | 8 +++++++ .../handlers/main.yml | 12 ++++++++++ .../native-docker-health-check/meta/main.yml | 2 ++ .../native-docker-health-check/tasks/main.yml | 22 +++++++++++++++++++ roles/native-docker/meta/main.yml | 1 + 7 files changed, 62 insertions(+) create mode 100644 roles/native-docker-health-check/files/docker-health-check.service create mode 100644 roles/native-docker-health-check/files/docker-health-check.sh create mode 100644 roles/native-docker-health-check/files/docker-health-check.timer create mode 100644 roles/native-docker-health-check/handlers/main.yml create mode 100644 roles/native-docker-health-check/meta/main.yml create mode 100644 roles/native-docker-health-check/tasks/main.yml diff --git a/roles/native-docker-health-check/files/docker-health-check.service b/roles/native-docker-health-check/files/docker-health-check.service new file mode 100644 index 00000000..fdb78871 --- /dev/null +++ b/roles/native-docker-health-check/files/docker-health-check.service @@ -0,0 +1,7 @@ +[Unit] +Description=Check docker status +OnFailure=systemd-email@%n.service + +[Service] +Type=oneshot +ExecStart=/bin/bash /home/administrator/scripts/docker-health-check/docker-health-check.sh diff --git a/roles/native-docker-health-check/files/docker-health-check.sh b/roles/native-docker-health-check/files/docker-health-check.sh new file mode 100644 index 00000000..216f633c --- /dev/null +++ b/roles/native-docker-health-check/files/docker-health-check.sh @@ -0,0 +1,10 @@ +#!/bin/sh +docker_ps_grep_unhealthy="$(docker ps | grep unhealthy)" +if [ -z "$docker_ps_grep_unhealthy" ] +then + echo "All docker containers are healthy." + exit +else + echo "Some docker containers are unhealthy: $docker_ps_grep_unhealthy" + exit 1 +fi \ No newline at end of file diff --git a/roles/native-docker-health-check/files/docker-health-check.timer b/roles/native-docker-health-check/files/docker-health-check.timer new file mode 100644 index 00000000..e3bb3bfb --- /dev/null +++ b/roles/native-docker-health-check/files/docker-health-check.timer @@ -0,0 +1,8 @@ +[Unit] +Description=starts docker-health-check.service + +[Timer] +OnCalendar=10:00 + +[Install] +WantedBy=timers.target diff --git a/roles/native-docker-health-check/handlers/main.yml b/roles/native-docker-health-check/handlers/main.yml new file mode 100644 index 00000000..8aec3318 --- /dev/null +++ b/roles/native-docker-health-check/handlers/main.yml @@ -0,0 +1,12 @@ +- name: "restart docker-health-check.service" + systemd: + name: docker-health-check.service + state: restarted + enabled: yes + daemon_reload: yes +- name: "restart docker-health-check.timer" + systemd: + name: docker-health-check.timer + state: restarted + enabled: yes + daemon_reload: yes diff --git a/roles/native-docker-health-check/meta/main.yml b/roles/native-docker-health-check/meta/main.yml new file mode 100644 index 00000000..9d9d34e2 --- /dev/null +++ b/roles/native-docker-health-check/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - native-systemd-email diff --git a/roles/native-docker-health-check/tasks/main.yml b/roles/native-docker-health-check/tasks/main.yml new file mode 100644 index 00000000..e2c147f8 --- /dev/null +++ b/roles/native-docker-health-check/tasks/main.yml @@ -0,0 +1,22 @@ +- name: "create /home/administrator/scripts/docker-health-check/" + file: + path: "/home/administrator/scripts/docker-health-check" + state: directory + mode: 0755 + +- name: create docker-health-check.sh + copy: + src: docker-health-check.sh + dest: "/home/administrator/scripts/docker-health-check/docker-health-check.sh" + +- name: create docker-health-check.service + copy: + src: docker-health-check.service + dest: "/etc/systemd/system/docker-health-check.service" + notify: restart docker-health-check.service + +- name: create docker-health-check.timer + copy: + src: docker-health-check.timer + dest: "/etc/systemd/system/docker-health-check.timer" + notify: restart docker-health-check.timer diff --git a/roles/native-docker/meta/main.yml b/roles/native-docker/meta/main.yml index 9a413bd5..a1abc078 100644 --- a/roles/native-docker/meta/main.yml +++ b/roles/native-docker/meta/main.yml @@ -1,3 +1,4 @@ dependencies: - native-docker-volume-backup - native-user-administrator + - native-docker-health-check