From ec585dad225759368376f497ad829558acddd458 Mon Sep 17 00:00:00 2001 From: Silas <69711739+SilasPeters@users.noreply.github.com> Date: Tue, 27 Aug 2024 00:52:17 +0200 Subject: [PATCH] feat: added healthcheck to outline --- ansible/roles/docker/tasks/compose-up.yml | 11 +++++++++-- ansible/roles/outline/tasks/main.yml | 15 +++++++++++++++ ansible/roles/outline/templates/Dockerfile.j2 | 17 +++++++++++++++++ .../outline/templates/docker-compose.yml.j2 | 5 +++-- ansible/roles/outline/vars/main.yml | 2 +- 5 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 ansible/roles/outline/templates/Dockerfile.j2 diff --git a/ansible/roles/docker/tasks/compose-up.yml b/ansible/roles/docker/tasks/compose-up.yml index d4e197ea..b21ef2f1 100644 --- a/ansible/roles/docker/tasks/compose-up.yml +++ b/ansible/roles/docker/tasks/compose-up.yml @@ -1,4 +1,8 @@ --- +# Possible variables which can be set to change the behaviour of these tasks: +# - project_source (required): the path to the directory the compose file is in +# - remove_images: whether to remove and rebuild all used images while composing up. +# Leave unset to keep the images, otherwise use "local" or "all" - name: "Check if 'project_source' is defined" ansible.builtin.fail: @@ -21,6 +25,7 @@ community.docker.docker_compose_v2: project_src: "{{ project_source }}" state: "absent" + remove_images: "{{ remove_images | default(omit) }}" - name: "Create and start services" community.docker.docker_compose_v2: @@ -36,7 +41,7 @@ project_src: "{{ project_source }}" register: "output" -- name: "Assert that all services properly started" +- name: "Assert that all services properly started, and that the docker compose file is idempotent" ansible.builtin.assert: that: "not output.changed" @@ -69,10 +74,12 @@ - "output.containers | selectattr('Name', 'equalto', item) | map(attribute='State') | list | first == 'exited'" loop: "{{ docker_compose_service_names }}" -- name: "Restart services" +- name: "Restart services and ensure they are healhty" community.docker.docker_compose_v2: project_src: "{{ project_source }}" state: "restarted" + wait: true + wait_timeout: 300 register: "output" # - name: Show results diff --git a/ansible/roles/outline/tasks/main.yml b/ansible/roles/outline/tasks/main.yml index 0a041b1d..a8b065f4 100644 --- a/ansible/roles/outline/tasks/main.yml +++ b/ansible/roles/outline/tasks/main.yml @@ -45,6 +45,16 @@ mode: "0600" notify: "trigger outline migration" + - name: "Create outline 'Dockerfile'" + ansible.builtin.template: + src: "Dockerfile.j2" + dest: "{{ outline_config.project_source }}/Dockerfile" + owner: "outline" + group: "outline" + mode: "0600" + notify: "trigger outline migration" + register: "dockerfile" + - name: "Create outline 'docker.env' file" ansible.builtin.template: src: "docker.env.j2" @@ -76,11 +86,16 @@ path: "{{ outline_config.project_source }}/.MIGRATION_PENDING" state: "absent" +- name: "Install required packages" + ansible.builtin.apt: + name: "wget" + state: "present" - name: "Docker compose" ansible.builtin.include_tasks: "../docker/tasks/compose-up.yml" vars: project_source: "{{ outline_config.project_source }}" + remove_images: "{{ 'local' if dockerfile.changed else omit }}" # Rebuild the image if file changed - name: "Install nginx config" ansible.builtin.template: diff --git a/ansible/roles/outline/templates/Dockerfile.j2 b/ansible/roles/outline/templates/Dockerfile.j2 new file mode 100644 index 00000000..a9974417 --- /dev/null +++ b/ansible/roles/outline/templates/Dockerfile.j2 @@ -0,0 +1,17 @@ +FROM outlinewiki/outline:{{ outline_config.version }} + +# Become root to install package +USER root + +RUN apt-get update \ + && apt-get install -y wget \ + && rm -rf /var/lib/apt/lists/* + +# Return to defaut user for security reasons +USER nodejs + +HEALTHCHECK --interval=5m --start-period=30s --start-interval=10s \ + CMD wget -qO- http://localhost:${PORT}/_health | grep -q 'OK' \ + && wget -qO- https://hc-ping.com/{{ secret_healthchecks_io.ping_key }}/outline \ + || exit 1 + diff --git a/ansible/roles/outline/templates/docker-compose.yml.j2 b/ansible/roles/outline/templates/docker-compose.yml.j2 index 89fb45ed..4022404c 100644 --- a/ansible/roles/outline/templates/docker-compose.yml.j2 +++ b/ansible/roles/outline/templates/docker-compose.yml.j2 @@ -3,9 +3,10 @@ services: outline: - image: outlinewiki/outline:{{ outline_config.version }} - restart: unless-stopped + build: + dockerfile: Dockerfile env_file: ./docker.env + restart: on-failure:3 # ports: # - "4568:3000" network_mode: host # TODO replace this diff --git a/ansible/roles/outline/vars/main.yml b/ansible/roles/outline/vars/main.yml index d210b456..d1998ea6 100644 --- a/ansible/roles/outline/vars/main.yml +++ b/ansible/roles/outline/vars/main.yml @@ -2,4 +2,4 @@ outline_config: project_source: "/var/www/outline/outline" - version: "0.78.0" + version: "0.78.0" # Git tag