diff --git a/roles/cron/README.md b/roles/cron/README.md index 1c54cb6..6794b83 100644 --- a/roles/cron/README.md +++ b/roles/cron/README.md @@ -11,26 +11,34 @@ 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) -- 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` +- [dockcheck](https://github.com/mag37/dockcheck) is a git clone in + `/home/phil/.local/share/dockcheck`, linked as `~/.local/bin/dockcheck.sh`. + It updates itself every run (`AutoSelfUpdate`: `git pull`, which also + updates its notify templates and `urls.list`). Ansible only makes the first + clone. +- `regctl` (`~/.local/bin/regctl`), which dockcheck uses to query + registries, is refreshed weekly from its latest release by + `run_dockcheck.sh`. +- `/home/phil/.config/dockcheck.config` comes 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. +- At 08:00 `run_dockcheck.sh` runs dockcheck twice: + 1. Update run: containers labelled `mag37.dockcheck.update=true` are + pulled and recreated. No notification. + 2. Check run: every container is checked and one Gotify notice lists the + updates still waiting (unlabelled containers, and images held back by + `DaysOld`). - `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). + List backups with `dockcheck.sh -B`; to roll back, retag the backup and + recreate the container. +- `dawarich_db` and `dawarich_redis` are skipped. +- Logs go to `/var/log/dockcheck/dockcheck.log`, rotated weekly (a year + kept). -To see what needs updating by hand, run dockcheck on the server: +To see what needs updating by hand (checks everything, changes nothing, +sends nothing): ```bash -ssh phil@100.70.169.99 '~/.local/bin/dockcheck.sh -n' +ssh phil@100.70.169.99 '~/.local/bin/dockcheck.sh' ``` ### Warhammer RSS feed diff --git a/roles/cron/defaults/main.yml b/roles/cron/defaults/main.yml deleted file mode 100644 index 36de45a..0000000 --- a/roles/cron/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -# 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 6da77fd..3620eab 100644 --- a/roles/cron/tasks/main.yml +++ b/roles/cron/tasks/main.yml @@ -29,45 +29,70 @@ owner: phil group: phil -# 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/{{ dockcheck_version }}/dockcheck.sh" - dest: /home/phil/.local/bin/dockcheck.sh - force: true - mode: '0755' - owner: phil - group: phil +# --- Dockcheck (container updates) --- +# dockcheck lives in a git clone and updates itself on every run +# (AutoSelfUpdate does a `git pull`, which also brings new notify templates +# and urls.list). Ansible only makes the first clone and never resets it. +- name: Clone dockcheck + become_user: phil + git: + repo: https://github.com/mag37/dockcheck.git + dest: /home/phil/.local/share/dockcheck + version: main + update: false -# 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 +# Keep the old path working for running it by hand +- name: Link dockcheck.sh into .local/bin file: - path: /home/phil/.local/bin/notify_v2.sh + src: /home/phil/.local/share/dockcheck/dockcheck.sh + dest: /home/phil/.local/bin/dockcheck.sh + state: link + force: true + owner: phil + group: phil + +# dockcheck reads release-note links from beside dockcheck.sh; upstream keeps +# the list in notify_templates/, so link it (and keep git status clean) +- name: Link urls.list beside dockcheck.sh + file: + src: notify_templates/urls.list + dest: /home/phil/.local/share/dockcheck/urls.list + state: link + owner: phil + group: phil + +- name: Keep local files out of the dockcheck clone's git status + copy: + dest: /home/phil/.local/share/dockcheck/.git/info/exclude + content: | + urls.list + snooze.list + *.bak + owner: phil + group: phil + mode: '0644' + +# regctl (dockcheck's registry client) isn't updated by dockcheck; +# run_dockcheck.sh refreshes it weekly. This only seeds it. +- name: Install regctl + get_url: + url: https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64 + dest: /home/phil/.local/bin/regctl + mode: '0755' + owner: phil + group: phil + +# Left over from the old download-into-.local/bin install +- name: Remove the old dockcheck files from .local/bin + file: + path: "/home/phil/.local/bin/{{ item }}" state: absent + loop: + - notify_templates + - notify_v2.sh + - urls.list + - snooze.list -# Create .config directory for phil user - name: Ensure .config directory exists for phil file: path: /home/phil/.config @@ -76,36 +101,6 @@ owner: phil group: phil -# Create notify_templates directory alongside dockcheck.sh -- name: Ensure notify_templates directory exists in .local/bin - file: - path: /home/phil/.local/bin/notify_templates - state: directory - mode: '0755' - owner: phil - group: phil - -# Download notify_v2.sh script for dockcheck notifications -- name: Download notify_v2.sh script - get_url: - 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 - -# Download notify_gotify.sh script for dockcheck notifications -- name: Download notify_gotify.sh script - get_url: - 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 - -# Template dockcheck configuration file - name: Template dockcheck configuration template: src: dockcheck.config.j2 @@ -114,7 +109,6 @@ owner: phil group: phil -# Create log directory for dockcheck - name: Create dockcheck log directory file: path: /var/log/dockcheck @@ -123,7 +117,8 @@ owner: phil group: phil -# Create dockcheck wrapper script to avoid cron escaping issues +# Two runs: update labelled containers quietly, then check everything and +# send one Gotify notice for what's left (see dockcheck.config.j2) - name: Create dockcheck wrapper script copy: dest: /home/phil/.local/bin/run_dockcheck.sh @@ -132,13 +127,38 @@ group: phil content: | #!/bin/bash - cd /home/phil - /home/phil/.local/bin/dockcheck.sh >> /var/log/dockcheck/dockcheck.log 2>&1 - # 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 + # Daily container updates (cron, 08:00). Managed by ansible (roles/cron). + export PATH="$HOME/.local/bin:$PATH" + log=/var/log/dockcheck/dockcheck.log + cd "$HOME" || exit 1 -# The log grows by about 3 MB a year; keep a year in weekly files + stamp() { date "+%Y-%m-%d %H:%M:%S"; } + + # Refresh regctl weekly from its latest release + regctl="$HOME/.local/bin/regctl" + if [[ ! -x $regctl || -n $(find "$regctl" -mtime +6) ]]; then + tmp=$(mktemp) + if curl -fsSL -o "$tmp" https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64 \ + && chmod 755 "$tmp" && "$tmp" version >/dev/null; then + mv "$tmp" "$regctl" + echo "$(stamp) - regctl refreshed: $("$regctl" version --format '{{ "{{" }}.VCSTag{{ "}}" }}')" >> "$log" + else + rm -f "$tmp" + echo "$(stamp) - regctl refresh failed, keeping the current one" >> "$log" + fi + fi + + echo "$(stamp) - Update run (labelled containers)" >> "$log" + DC_UPDATE=1 "$HOME/.local/bin/dockcheck.sh" >> "$log" 2>&1 + code=$? + echo "$(stamp) - Update run completed with exit code $code" >> "$log" + + echo "$(stamp) - Check run (all containers, notifies)" >> "$log" + DC_NOTIFY=1 "$HOME/.local/bin/dockcheck.sh" >> "$log" 2>&1 + code=$? + echo "$(stamp) - Check run completed with exit code $code" >> "$log" + +# The log grows by about 6 MB a year; keep a year in weekly files - name: Rotate the dockcheck log copy: dest: /etc/logrotate.d/dockcheck @@ -156,7 +176,6 @@ su phil phil } -# Create cron job for dockcheck as phil user with logging - name: Create cron job for dockcheck container updates cron: name: "Check Docker container updates" diff --git a/roles/cron/templates/dockcheck.config.j2 b/roles/cron/templates/dockcheck.config.j2 index b6a8782..fe5652c 100644 --- a/roles/cron/templates/dockcheck.config.j2 +++ b/roles/cron/templates/dockcheck.config.j2 @@ -1,10 +1,22 @@ -# 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. -# DontUpdate=true -OnlyLabel=true +# Dockcheck configuration (all options: default.config in mag37/dockcheck) +# Managed by ansible (roles/cron). This file is sourced as bash. +# +# Modes, picked by run_dockcheck.sh with an environment variable: +# DC_UPDATE=1 auto-update containers labelled mag37.dockcheck.update=true, +# no notification +# DC_NOTIFY=1 check every container, send one Gotify notice for updates +# neither (running dockcheck.sh by hand) check every container, no +# updates, no notification AutoMode=true +if [[ "${DC_UPDATE:-}" == 1 ]]; then + OnlyLabel=true +else + DontUpdate=true +fi +Notify=$([[ "${DC_NOTIFY:-}" == 1 ]] && echo true || echo false) + +# dockcheck updates itself (git pull in ~/.local/share/dockcheck) each run +AutoSelfUpdate=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 @@ -25,9 +37,6 @@ MonoMode=true # Add release-note links (from urls.list) to each update listed PrintReleaseURL=true -# Enable notifications -Notify=true - # Exclude containers from checking Exclude="dawarich_redis,dawarich_db"