diff --git a/roles/cron/README.md b/roles/cron/README.md index 3d9b9d8..1c54cb6 100644 --- a/roles/cron/README.md +++ b/roles/cron/README.md @@ -11,15 +11,22 @@ jobs on the server, never on the machine running Ansible. | 09:10 daily | root | `/usr/local/bin/update_warhammer_feed.sh`: Warhammer RSS feed | ### Dockcheck (container updates) -- Downloads [dockcheck](https://github.com/mag37/dockcheck) and its Gotify - notify templates into `/home/phil/.local/bin/`. +- Installs [dockcheck](https://github.com/mag37/dockcheck), its Gotify notify + templates, `urls.list` and `regctl` into `/home/phil/.local/bin/`, pinned + to `dockcheck_version` and `regctl_version` in `defaults/main.yml`. Bump + those and run `bin/deploy --tags cron` to upgrade. - Templates `/home/phil/.config/dockcheck.config` from `dockcheck.config.j2` (Gotify token from `vault_dockcheck`). - Settings: `OnlyLabel=true` and `AutoMode=true`, so containers labelled `mag37.dockcheck.update=true` are pulled and recreated automatically; any other container with an update gets a Gotify notice. `dawarich_db` and `dawarich_redis` are skipped. -- `run_dockcheck.sh` logs to `/var/log/dockcheck/dockcheck.log`. +- `DaysOld=3`: images newer than 3 days wait for a later run. +- `BackupForDays=7`: the replaced image is kept for 7 days for rollback. + List backups with `dockcheck.sh -B` (or `docker images | grep _`); + to roll back, retag the backup and recreate the container. +- `run_dockcheck.sh` logs to `/var/log/dockcheck/dockcheck.log`, rotated + weekly (a year kept). To see what needs updating by hand, run dockcheck on the server: ```bash diff --git a/roles/cron/defaults/main.yml b/roles/cron/defaults/main.yml new file mode 100644 index 0000000..36de45a --- /dev/null +++ b/roles/cron/defaults/main.yml @@ -0,0 +1,6 @@ +--- +# Pinned so a deploy installs a known version. Bump these to upgrade: +# https://github.com/mag37/dockcheck/releases +# https://github.com/regclient/regclient/releases (regctl, which dockcheck uses to query registries) +dockcheck_version: v0.8.3 +regctl_version: v0.11.7 diff --git a/roles/cron/tasks/main.yml b/roles/cron/tasks/main.yml index 8b712e2..6da77fd 100644 --- a/roles/cron/tasks/main.yml +++ b/roles/cron/tasks/main.yml @@ -29,15 +29,44 @@ owner: phil group: phil -# Install dockcheck script in phil's .local/bin +# Install dockcheck in phil's .local/bin, pinned to dockcheck_version. +# force: a version bump replaces the file (get_url otherwise never re-downloads). - name: Download dockcheck.sh script get_url: - url: https://raw.githubusercontent.com/mag37/dockcheck/main/dockcheck.sh + url: "https://raw.githubusercontent.com/mag37/dockcheck/{{ dockcheck_version }}/dockcheck.sh" dest: /home/phil/.local/bin/dockcheck.sh + force: true mode: '0755' owner: phil group: phil +# dockcheck only downloads regctl when it's missing, so it never updates by itself +- name: Download regctl + get_url: + url: "https://github.com/regclient/regclient/releases/download/{{ regctl_version }}/regctl-linux-amd64" + dest: /home/phil/.local/bin/regctl + force: true + mode: '0755' + owner: phil + group: phil + +# Release-note links for known images, shown with each update (PrintReleaseURL). +# dockcheck reads it from beside dockcheck.sh. +- name: Download dockcheck release-note URL list + get_url: + url: "https://raw.githubusercontent.com/mag37/dockcheck/{{ dockcheck_version }}/notify_templates/urls.list" + dest: /home/phil/.local/bin/urls.list + force: true + mode: '0644' + owner: phil + group: phil + +# Left over from an old install; dockcheck reads notify_templates/notify_v2.sh +- name: Remove stale notify_v2.sh beside dockcheck.sh + file: + path: /home/phil/.local/bin/notify_v2.sh + state: absent + # Create .config directory for phil user - name: Ensure .config directory exists for phil file: @@ -59,8 +88,9 @@ # Download notify_v2.sh script for dockcheck notifications - name: Download notify_v2.sh script get_url: - url: https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_v2.sh + url: "https://raw.githubusercontent.com/mag37/dockcheck/{{ dockcheck_version }}/notify_templates/notify_v2.sh" dest: /home/phil/.local/bin/notify_templates/notify_v2.sh + force: true mode: '0755' owner: phil group: phil @@ -68,8 +98,9 @@ # Download notify_gotify.sh script for dockcheck notifications - name: Download notify_gotify.sh script get_url: - url: https://raw.githubusercontent.com/mag37/dockcheck/main/notify_templates/notify_gotify.sh + url: "https://raw.githubusercontent.com/mag37/dockcheck/{{ dockcheck_version }}/notify_templates/notify_gotify.sh" dest: /home/phil/.local/bin/notify_templates/notify_gotify.sh + force: true mode: '0755' owner: phil group: phil @@ -103,7 +134,27 @@ #!/bin/bash cd /home/phil /home/phil/.local/bin/dockcheck.sh >> /var/log/dockcheck/dockcheck.log 2>&1 - echo "$(date "+%Y-%m-%d %H:%M:%S") - Dockcheck completed with exit code $?" >> /var/log/dockcheck/dockcheck.log + # Save the code first: $(date) inside the echo would reset $? + code=$? + echo "$(date "+%Y-%m-%d %H:%M:%S") - Dockcheck completed with exit code $code" >> /var/log/dockcheck/dockcheck.log + +# The log grows by about 3 MB a year; keep a year in weekly files +- name: Rotate the dockcheck log + copy: + dest: /etc/logrotate.d/dockcheck + mode: '0644' + owner: root + group: root + content: | + /var/log/dockcheck/dockcheck.log { + weekly + rotate 52 + compress + delaycompress + missingok + notifempty + su phil phil + } # Create cron job for dockcheck as phil user with logging - name: Create cron job for dockcheck container updates diff --git a/roles/cron/templates/dockcheck.config.j2 b/roles/cron/templates/dockcheck.config.j2 index 8475be8..b6a8782 100644 --- a/roles/cron/templates/dockcheck.config.j2 +++ b/roles/cron/templates/dockcheck.config.j2 @@ -1,4 +1,4 @@ -# Dockcheck Configuration +# Dockcheck Configuration (options: default.config in mag37/dockcheck) # Auto-updates containers labelled mag37.dockcheck.update=true, and sends a # Gotify notice for anything else with an update. Uncomment DontUpdate to # only check. @@ -6,6 +6,25 @@ OnlyLabel=true AutoMode=true +# Only take an image once it's at least this many days old, so a broken +# release has time to be fixed before it's applied unattended. Newer ones are +# listed with a "+" and their age, and picked up on a later run. +DaysOld="3" + +# Keep the replaced image for 7 days (tagged _) so a bad update +# can be rolled back, then delete it. With backups on, dockcheck doesn't +# prune dangling images (AutoPrune has no effect). +BackupForDays=7 + +# Check registries in parallel +MaxAsync=10 + +# Plain output for the cron log: no colour codes or progress bar +MonoMode=true + +# Add release-note links (from urls.list) to each update listed +PrintReleaseURL=true + # Enable notifications Notify=true @@ -17,4 +36,4 @@ NOTIFY_CHANNELS="gotify" # Gotify notification configuration GOTIFY_DOMAIN="https://{{ subdomains.gotify }}" -GOTIFY_TOKEN="{{ vault_dockcheck.gotify_token }}" \ No newline at end of file +GOTIFY_TOKEN="{{ vault_dockcheck.gotify_token }}"