From 40586253a5794510245428d4c69080c4083a7abc Mon Sep 17 00:00:00 2001 From: fencl Date: Sun, 5 Oct 2025 16:00:27 +0200 Subject: [PATCH] . --- nextcloud/update_collabora.yml | 11 +- update_homarr.yml | 186 +++++++++++++++++++++++++++++++++ update_uptime_kuma.yml | 2 +- 3 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 update_homarr.yml diff --git a/nextcloud/update_collabora.yml b/nextcloud/update_collabora.yml index ae4dbae..921ff3f 100644 --- a/nextcloud/update_collabora.yml +++ b/nextcloud/update_collabora.yml @@ -60,7 +60,7 @@ loop: "{{ collabora_commands }}" register: collab_cmds changed_when: false - no_log: "{{ DEBUG == 0 }}" # 🔒 Hide sensitive info when DEBUG=0 + no_log: "{{ DEBUG == 0 }}" - name: Show outputs for each Collabora command ansible.builtin.debug: @@ -72,7 +72,7 @@ STDERR: {{ (item.stderr | default('')).strip() }} loop: "{{ collab_cmds.results }}" - when: DEBUG == 1 # 🪵 Only show debug output if DEBUG=1 + when: DEBUG == 1 - name: Fail play if any Collabora command failed ansible.builtin.assert: @@ -128,8 +128,7 @@ changed_when: false failed_when: false when: caps_controller.status | default(0) != 200 or caps_controller.json is not defined - no_log: "{{ DEBUG == 0 }}" # 🔒 Hide command and output when not debugging - + no_log: "{{ DEBUG == 0 }}" - name: Collabora | Choose JSON (controller wins, else VM) ansible.builtin.set_fact: collab_caps_json: >- @@ -151,7 +150,7 @@ ({{ collab_caps_json.productName | default('?') }}), convert-to.available={{ collab_caps_json['convert-to']['available'] | default('n/a') }}, serverId={{ collab_caps_json.serverId | default('n/a') }} - when: collab_caps_json is defined and DEBUG == 1 # 🪵 Only print in debug mode + when: collab_caps_json is defined and DEBUG == 1 - name: Collabora | Capabilities unavailable (after retries) ansible.builtin.debug: @@ -162,4 +161,4 @@ - name: Collabora | Full JSON (debug) ansible.builtin.debug: var: collab_caps_json - when: collabora_debug_caps and (collab_caps_json is defined) and DEBUG == 1 # 🪵 Only when debugging + when: collabora_debug_caps and (collab_caps_json is defined) and DEBUG == 1 diff --git a/update_homarr.yml b/update_homarr.yml new file mode 100644 index 0000000..187ef5c --- /dev/null +++ b/update_homarr.yml @@ -0,0 +1,186 @@ +# update_homarr.yml + +- name: Update Homarr on VM via Proxmox + hosts: proxmox + gather_facts: false + become: true + become_user: root + become_method: sudo + + 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 mode (controlled via Semaphore variable) --- + DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}" + + # Homarr specifics + homarr_project: "homarr" + homarr_compose_file: "/data/compose/homarr.yml" + homarr_service: "homarr" + homarr_image: "ghcr.io/homarr-labs/homarr:latest" + homarr_port: 7575 + + # Optional external URL for controller-side readiness check (e.g., https://homarr.example.com) + # If empty/undefined, controller check is skipped and we only probe from the VM. + homarr_url: "{{ lookup('env', 'HOMARR_URL') | default('', true) }}" + + # Fixed container name used in your compose (avoid conflicts with any leftover container) + homarr_container_name: "homarr" + + # Retry policy (same pattern as Kuma): 25x with 2s delay + homarr_retries: 25 + homarr_delay: 2 + + # 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 (quiet outputs) + homarr_commands: + - "{{ docker_prefix }} pull -q {{ homarr_image }} >/dev/null" + - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} pull {{ homarr_service }} >/dev/null" + # remove conflicting container name before compose up (silently) + - "{{ docker_prefix }} rm -f {{ homarr_container_name }} >/dev/null 2>&1 || true" + - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} up -d --no-deps --force-recreate {{ homarr_service }} >/dev/null" + + tasks: + - name: Ensure sshpass is installed (for password-based SSH) # English comments + ansible.builtin.apt: + name: sshpass + state: present + update_cache: yes + + - name: Run Homarr update commands on VM (via SSH) + ansible.builtin.command: + argv: + - sshpass + - -p + - "{{ vm_pass }}" + - ssh + - -o + - StrictHostKeyChecking=no + - -o + - ConnectTimeout=15 + - "{{ vm_user }}@{{ vm_ip }}" + - bash + - -lc + - "{{ ('sudo ' if use_sudo else '') + item }}" + loop: "{{ homarr_commands }}" + register: homarr_cmds + changed_when: false + no_log: "{{ DEBUG == 0 }}" + + - name: Show outputs for each Homarr command + ansible.builtin.debug: + msg: | + CMD: {{ item.item }} + RC: {{ item.rc }} + STDOUT: + {{ (item.stdout | default('')).strip() }} + STDERR: + {{ (item.stderr | default('')).strip() }} + loop: "{{ homarr_cmds.results }}" + when: DEBUG == 1 + + - name: Fail play if any Homarr command failed + ansible.builtin.assert: + that: "item.rc == 0" + fail_msg: "Homarr update failed on VM: {{ item.item }} (rc={{ item.rc }})" + success_msg: "All Homarr update commands succeeded." + loop: "{{ homarr_cmds.results }}" + + # ------------------------- + # Readiness checks (controller first, then VM fallback) + # ------------------------- + + - name: Homarr | Wait for homepage (controller first, with retries) + ansible.builtin.uri: + url: "{{ (homarr_url | regex_replace('/$','')) + '/' }}" + method: GET + return_content: true + # Validate TLS only when using https:// + validate_certs: "{{ (homarr_url | default('')) is match('^https://') }}" + status_code: 200 + register: homarr_controller + delegate_to: localhost + run_once: true + when: homarr_url is defined and (homarr_url | length) > 0 + retries: "{{ homarr_retries }}" + delay: "{{ homarr_delay }}" + until: homarr_controller.status == 200 + failed_when: false + changed_when: false + + - name: Homarr | VM-side fetch (HTML via Python, with retries) + ansible.builtin.command: + argv: + - sshpass + - -p + - "{{ vm_pass }}" + - ssh + - -o + - StrictHostKeyChecking=no + - -o + - ConnectTimeout=15 + - "{{ vm_user }}@{{ vm_ip }}" + - bash + - -lc + - | + python3 - <<'PY' + # Fetch Homarr homepage from localhost and print HTML to stdout + import urllib.request, sys + try: + with urllib.request.urlopen("http://127.0.0.1:{{ homarr_port }}/", timeout=15) as r: + sys.stdout.write(r.read().decode(errors='ignore')) + except Exception: + pass + PY + register: homarr_vm + changed_when: false + failed_when: false + when: homarr_controller.status | default(0) != 200 or homarr_controller.content is not defined + retries: "{{ homarr_retries }}" + delay: "{{ homarr_delay }}" + # Wait until we actually got some HTML that likely belongs to Homarr + until: (homarr_vm.stdout | default('') | trim | length) > 0 and ('Homarr' in (homarr_vm.stdout | default(''))) + no_log: "{{ DEBUG == 0 }}" + + - name: Homarr | Choose homepage HTML (controller wins, else VM) # safe guard against empty result + ansible.builtin.set_fact: + homarr_home_html: >- + {{ + ( + homarr_controller.content + if (homarr_controller is defined) + and ((homarr_controller.status|default(0))==200) + and (homarr_controller.content is defined) + else + (homarr_vm.stdout | default('') | trim) + ) + }} + when: + - (homarr_controller is defined and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) + or ((homarr_vm.stdout | default('') | trim | length) > 0) + + - name: Homarr | Print concise summary + ansible.builtin.debug: + msg: >- + Homarr homepage {{ 'reachable' if (homarr_home_html is defined) else 'NOT reachable' }}. + Source={{ 'controller' if ((homarr_controller is defined) and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) else 'vm' if (homarr_vm.stdout|default('')|trim|length>0) else 'n/a' }}; + length={{ (homarr_home_html | default('')) | length }}; + contains('Homarr')={{ (homarr_home_html is defined) and ('Homarr' in homarr_home_html) }} + when: DEBUG == 1 + + - name: Homarr | Homepage unavailable (after retries) + ansible.builtin.debug: + msg: "Homarr web není dostupný ani po pokusech." + when: homarr_home_html is not defined and DEBUG == 1 + + # Optional detailed dump (short excerpt only) + - name: Homarr | HTML excerpt (debug) + ansible.builtin.debug: + msg: "{{ (homarr_home_html | default(''))[:500] }}" + when: homarr_home_html is defined and DEBUG == 1 diff --git a/update_uptime_kuma.yml b/update_uptime_kuma.yml index 569a590..58588a7 100644 --- a/update_uptime_kuma.yml +++ b/update_uptime_kuma.yml @@ -83,7 +83,7 @@ STDERR: {{ (item.stderr | default('')).strip() }} loop: "{{ kuma_cmds.results }}" - when: DEBUG == 1 # 🪵 Only show debug output if DEBUG=1 + when: DEBUG == 1 - name: Fail play if any Uptime Kuma command failed ansible.builtin.assert: