diff --git a/nextcloud/update_uptime_kuma.yml b/nextcloud/update_uptime_kuma.yml index 86ea768..19203d5 100644 --- a/nextcloud/update_uptime_kuma.yml +++ b/nextcloud/update_uptime_kuma.yml @@ -1,4 +1,5 @@ # nextcloud/update_uptime_kuma.yml + - name: Update Uptime Kuma on VM via Proxmox hosts: proxmox gather_facts: false @@ -7,48 +8,34 @@ become_method: sudo vars: - # --- Connection to VM (provided by Semaphore env vars) --- + # VM connection (provided by Semaphore env vars) vm_ip: "{{ lookup('env', 'VM_IP') }}" vm_user: "{{ lookup('env', 'VM_USER') }}" vm_pass: "{{ lookup('env', 'VM_PASS') }}" use_sudo: false - # --- Debug toggle (set DEBUG=1 in Semaphore to see raw stdout/stderr) --- - kuma_debug: "{{ (lookup('env','DEBUG') | default('0')) | bool }}" - - # --- Uptime Kuma specifics --- - kuma_project: "uptime-kuma" # docker compose project name + # Uptime Kuma specifics + kuma_project: "uptime-kuma" kuma_compose_file: "/data/compose/uptime-kuma.yml" - kuma_service: "uptime-kuma" # service name in the compose file + kuma_service: "uptime-kuma" kuma_image: "louislam/uptime-kuma:latest" kuma_port: 3001 - kuma_url: "{{ lookup('env', 'KUMA_URL') | default('', true) }}" # optional public URL - # Fixed container name used in your compose (may conflict with pre-existing container) - kuma_container_name: "uptime-kuma-dev" - kuma_force_replace_conflict: true # remove conflicting container automatically - kuma_remove_orphans: true # remove containers not present in the compose file + # Optional external URL for controller-side readiness check (e.g., https://kuma.example.com). + # If empty/undefined, controller check is skipped and we only probe from the VM. + kuma_url: "{{ lookup('env', 'KUMA_URL') | default('', true) }}" - # Host path used by your volume mapping in compose (ensure it exists) - kuma_data_dir: "/data/compose/kuma/data" + # Debug toggle for the final HTML excerpt + kuma_debug_home: true - # Docker command prefix (consistent behavior and quiet hints) + # Docker command prefix (consistent behavior) docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" - # Commands to run on the target VM (outputs are visible when kuma_debug=true) + # Commands to run on the target VM (quiet outputs like in Collabora play) kuma_commands: - # 0) pull image (helpful cache warm-up) - - "{{ docker_prefix }} pull -q {{ kuma_image }}" - # 1) show services resolved from the compose file (helps debugging service names) - - "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} config --services" - # 2) pull service - - "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull {{ kuma_service }}" - # 3) up the service (optionally remove orphans) - - >- - {{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} - up -d --no-deps --force-recreate - {{ '--remove-orphans' if kuma_remove_orphans else '' }} - {{ kuma_service }} + - "{{ docker_prefix }} pull -q {{ kuma_image }} >/dev/null" + - "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} pull {{ kuma_service }} >/dev/null" + - "{{ docker_prefix }} compose -p {{ kuma_project }} -f {{ kuma_compose_file }} up -d --no-deps --force-recreate {{ kuma_service }} >/dev/null" tasks: - name: Ensure sshpass is installed (for password-based SSH) # English comments @@ -57,44 +44,7 @@ state: present update_cache: yes - # ------------------------- - # Preflight: connectivity and inputs - # ------------------------- - - - name: Preflight | Validate required inputs are present - ansible.builtin.assert: - that: - - vm_ip | length > 0 - - vm_user | length > 0 - - vm_pass | length > 0 - fail_msg: "Missing VM connection variables (VM_IP/VM_USER/VM_PASS). Check Semaphore template variables." - success_msg: "Inputs look good." - - - name: Preflight | Check SSH connectivity to VM - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=8 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "echo OK" - register: preflight_ssh_ok - changed_when: false - failed_when: preflight_ssh_ok.rc != 0 or (preflight_ssh_ok.stdout | trim) != 'OK' - no_log: "{{ not kuma_debug }}" - - # ------------------------- - # Preflight: compose sanity - # ------------------------- - - - name: Preflight | Compose file exists on VM (do not fail here) + - name: Run Uptime Kuma update commands on VM (via SSH) ansible.builtin.command: argv: - sshpass @@ -108,199 +58,52 @@ - "{{ vm_user }}@{{ vm_ip }}" - bash - -lc - - "test -r {{ kuma_compose_file }}" - register: preflight_compose_exists - changed_when: false - failed_when: false - no_log: "{{ not kuma_debug }}" - - - name: Preflight | Validate compose file syntax (do not fail here) - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "{{ docker_prefix }} compose -f {{ kuma_compose_file }} config -q" - register: preflight_compose_valid - changed_when: false - failed_when: false - no_log: "{{ not kuma_debug }}" - - - name: Preflight | Ensure service exists in compose (do not fail here) - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - >- - {{ docker_prefix }} compose -f {{ kuma_compose_file }} config --services | grep -x {{ kuma_service }} - register: preflight_service_exists - changed_when: false - failed_when: false - no_log: "{{ not kuma_debug }}" - - - name: Preflight | Assert compose prerequisites - ansible.builtin.assert: - that: - - preflight_compose_exists.rc == 0 - - preflight_compose_valid.rc == 0 - - preflight_service_exists.rc == 0 - fail_msg: >- - Compose preflight failed on {{ vm_ip }} as {{ vm_user }}. - - exists: rc={{ preflight_compose_exists.rc }} - - syntax: rc={{ preflight_compose_valid.rc }} - - service "{{ kuma_service }}": rc={{ preflight_service_exists.rc }} - Hints: - * Verify {{ kuma_compose_file }} path on VM and its permissions. - * Check that service name in the compose matches "kuma_service". - * If SSH connectivity is flaky, re-check VM_IP/USER/PASS. - success_msg: "Compose file present, valid, and service '{{ kuma_service }}' found." - - - name: Preflight | Ensure Kuma data dir exists (host path from compose) - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "mkdir -p {{ kuma_data_dir }}" - changed_when: false - no_log: "{{ not kuma_debug }}" - - - name: Preflight | Detect conflicting container by fixed name - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "docker ps -a --filter name=^/{{ kuma_container_name }}$ --format '{{'{{'}}.ID{{'}}'}}'" - register: kuma_conflict - changed_when: false - failed_when: false - no_log: "{{ not kuma_debug }}" - - - name: Preflight | Remove conflicting container if present (and allowed) - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "docker rm -f {{ kuma_container_name }}" - when: - - kuma_force_replace_conflict | bool - - (kuma_conflict.stdout | default('') | trim) | length > 0 - changed_when: true - no_log: "{{ not kuma_debug }}" - - # ------------------------- - # Update commands - # ------------------------- - - - name: Run Uptime Kuma update commands on VM (via SSH) # Shows details when DEBUG=1 - ansible.builtin.command: - argv: - - sshpass - - -p - - "{{ vm_pass }}" - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=20 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - "{{ ('sudo ' if use_sudo else '') + item }}" loop: "{{ kuma_commands }}" register: kuma_cmds changed_when: false - ignore_errors: true - no_log: "{{ not kuma_debug }}" - - name: Show summarized outputs for each command (sanitized) + - name: Show outputs for each Uptime Kuma command ansible.builtin.debug: msg: | - CMD: {{ item_short }} + CMD: {{ item.item }} RC: {{ item.rc }} STDOUT: {{ (item.stdout | default('')).strip() }} STDERR: {{ (item.stderr | default('')).strip() }} loop: "{{ kuma_cmds.results }}" - vars: - # Print only the remote "bash -lc" payload (no password leaks) - item_short: "{{ (item.cmd | default([]))[-1] | default('N/A') }}" - when: kuma_cmds is defined - changed_when: false - name: Fail play if any Uptime Kuma command failed ansible.builtin.assert: that: "item.rc == 0" - fail_msg: "Uptime Kuma update failed on VM: {{ (item.cmd | default([]))[-1] | default('N/A') }} (rc={{ item.rc }})" + fail_msg: "Uptime Kuma update failed on VM: {{ item.item }} (rc={{ item.rc }})" success_msg: "All Uptime Kuma update commands succeeded." loop: "{{ kuma_cmds.results }}" # ------------------------- - # Readiness checks + # Readiness checks (controller first, then VM fallback) # ------------------------- - - name: Kuma | Wait for web to respond (controller-side, if URL provided) + - name: Kuma | Wait for homepage (controller first) ansible.builtin.uri: url: "{{ (kuma_url | regex_replace('/$','')) + '/' }}" method: GET return_content: true + # Validate TLS only when using https:// validate_certs: "{{ (kuma_url | default('')) is match('^https://') }}" status_code: 200 register: kuma_controller delegate_to: localhost run_once: true when: kuma_url is defined and (kuma_url | length) > 0 - retries: 20 + retries: 10 delay: 2 - until: kuma_controller.status == 200 and ('Uptime Kuma' in (kuma_controller.content | default(''))) + until: kuma_controller.status == 200 failed_when: false changed_when: false - - name: Kuma | VM-side probe (fallback to localhost:port, no auth) + - name: Kuma | VM-side fetch (HTML via Python) ansible.builtin.command: argv: - sshpass @@ -316,52 +119,48 @@ - -lc - | python3 - <<'PY' - # Simple readiness probe: fetch "/" and look for "Uptime Kuma" in HTML - import sys, urllib.request + # Fetch Kuma homepage from localhost and print HTML to stdout + import urllib.request, sys try: with urllib.request.urlopen("http://127.0.0.1:{{ kuma_port }}/", timeout=15) as r: - body = r.read(8192).decode(errors='ignore') - if 'Uptime Kuma' in body: - print("OK") + sys.stdout.write(r.read().decode(errors='ignore')) except Exception: pass PY register: kuma_vm changed_when: false failed_when: false - when: (kuma_url is not defined) or (kuma_url | length == 0) or (kuma_controller.status | default(0)) != 200 + when: kuma_controller.status | default(0) != 200 or kuma_controller.content is not defined - - name: Kuma | Decide readiness source + - name: Kuma | Choose homepage HTML (controller wins, else VM) ansible.builtin.set_fact: - kuma_ready: >- + kuma_home_html: >- {{ - ( - (kuma_controller is defined) - and (kuma_controller.status|default(0))==200 - and ('Uptime Kuma' in (kuma_controller.content|default(''))) - ) or - ( - (kuma_vm.stdout | default('')) | trim == 'OK' + (kuma_controller.content + if (kuma_controller.status|default(0))==200 and (kuma_controller.content is defined) + else ( + (kuma_vm.stdout | default('') | trim | length > 0) + | ternary((kuma_vm.stdout | trim), omit) + ) ) }} - kuma_ready_source: >- - {{ - ( - (kuma_controller is defined) - and (kuma_controller.status|default(0))==200 - and ('Uptime Kuma' in (kuma_controller.content|default(''))) - ) - | ternary('controller', 'vm') - }} + failed_when: false - name: Kuma | Print concise summary ansible.builtin.debug: msg: >- - Uptime Kuma is {{ 'READY' if kuma_ready else 'NOT READY' }} - (checked via {{ kuma_ready_source }}). - URL={{ (kuma_url if (kuma_url|default('')|length>0) else 'http://'+vm_ip+':'+(kuma_port|string)) }} + Uptime Kuma homepage {{ 'reachable' if (kuma_home_html is defined) else 'NOT reachable' }}. + Source={{ 'controller' if ((kuma_controller.status|default(0))==200 and (kuma_controller.content is defined)) else 'vm' if (kuma_vm.stdout|default('')|trim|length>0) else 'n/a' }}; + length={{ (kuma_home_html | default('')) | length }}; + contains('Uptime Kuma')={{ (kuma_home_html is defined) and ('Uptime Kuma' in kuma_home_html) }} - - name: Kuma | Not ready after retries + - name: Kuma | Homepage unavailable (after retries) ansible.builtin.debug: msg: "Kuma web není dostupná ani po pokusech." - when: not kuma_ready \ No newline at end of file + when: kuma_home_html is not defined + + # Optional detailed dump (short excerpt only) + - name: Kuma | HTML excerpt (debug) + ansible.builtin.debug: + msg: "{{ (kuma_home_html | default(''))[:500] }}" + when: kuma_debug_home and (kuma_home_html is defined) \ No newline at end of file