3
0
forked from jakub/ansible

Refactor update_collabora.yml: enhance capability fetching with improved error handling and JSON parsing

This commit is contained in:
fencl
2025-10-05 09:52:25 +02:00
parent bf60fdd9f1
commit 49904d991e

View File

@@ -10,12 +10,12 @@
vm_user: "{{ lookup('env', 'VM_USER') }}" vm_user: "{{ lookup('env', 'VM_USER') }}"
vm_pass: "{{ lookup('env', 'VM_PASS') }}" vm_pass: "{{ lookup('env', 'VM_PASS') }}"
use_sudo: false use_sudo: false
collabora_caps_url: "https://collabora.martinfencl.eu/hosting/capabilities" debug_caps: false
collabora_caps_url: "https://collabora.martinfencl.eu/hosting/capabilities"
collabora_project: "nextcloud-collabora" collabora_project: "nextcloud-collabora"
collabora_compose_file: "/data/compose/nextcloud/collabora-only.yml" collabora_compose_file: "/data/compose/nextcloud/collabora-only.yml"
# English comments: prefix ensures we bypass aliases/functions and use real docker
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
collabora_commands: collabora_commands:
@@ -67,27 +67,23 @@
success_msg: "All Collabora update commands succeeded." success_msg: "All Collabora update commands succeeded."
loop: "{{ collab_cmds.results }}" loop: "{{ collab_cmds.results }}"
- name: Collabora | Grace period before capability probe # English comments - name: Collabora | Wait for capabilities (controller first)
ansible.builtin.pause:
seconds: 5
# --- Attempt 1: fetch from controller (localhost) with retries ---
- name: Collabora | Fetch capabilities via controller (uri with retries)
ansible.builtin.uri: ansible.builtin.uri:
url: "{{ collabora_caps_url }}" url: "{{ collabora_caps_url }}"
method: GET method: GET
return_content: true return_content: true
validate_certs: true validate_certs: true
status_code: 200 # English: expect 200, anything else is "not ready yet" status_code: 200
register: collab_caps_uri register: caps_controller
delegate_to: localhost delegate_to: localhost
run_once: true run_once: true
retries: 3 retries: 10
delay: 3 delay: 2
failed_when: false # English: do not fail the play on non-200/transport errors until: caps_controller.status == 200
failed_when: false
changed_when: false
# --- Attempt 2 (fallback): fetch from VM via curl (often bypasses proxy/DNS quirks) --- - name: Collabora | VM-side fetch (pure JSON via Python)
- name: Collabora | Fetch capabilities via VM (curl fallback)
ansible.builtin.command: ansible.builtin.command:
argv: argv:
- sshpass - sshpass
@@ -101,63 +97,50 @@
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- bash - bash
- -lc - -lc
- > - |
set -euo pipefail; python3 - <<'PY'
curl -sSL --max-time 15 --retry 3 --retry-delay 2 import json, urllib.request, sys
"{{ collabora_caps_url }}" try:
register: collab_caps_vm with urllib.request.urlopen("{{ collabora_caps_url }}", timeout=15) as r:
sys.stdout.write(r.read().decode())
except Exception:
pass
PY
register: caps_vm
changed_when: false changed_when: false
failed_when: false failed_when: false
when: collab_caps_uri.status | default(0) != 200 when: caps_controller.status | default(0) != 200 or caps_controller.json is not defined
# --- Decide which payload to use (prefer uri if 200) ---
- name: Collabora | Select payload source - name: Collabora | Choose JSON (controller wins, else VM)
ansible.builtin.set_fact: ansible.builtin.set_fact:
collab_caps_raw: >- collab_caps_json: >-
{{ {{
(collab_caps_uri.content (caps_controller.json
if (collab_caps_uri.status | default(0)) == 200 else if (caps_controller.status|default(0))==200 and (caps_controller.json is defined)
(collab_caps_vm.stdout | default(''))) else (
(caps_vm.stdout | default('') | trim | length > 0)
| ternary((caps_vm.stdout | trim | from_json), omit)
)
)
}} }}
# --- Prefer JSON object provided by uri module when available ---
- name: Collabora | Prefer JSON from controller if available # English comments
ansible.builtin.set_fact:
collab_caps_json: "{{ collab_caps_uri.json }}"
when: collab_caps_uri.json is defined
# --- Fallback: parse JSON from raw only if it looks like JSON ---
- name: Collabora | Fallback parse from raw string
ansible.builtin.set_fact:
collab_caps_json: "{{ collab_caps_raw | from_json }}"
when:
- collab_caps_json is not defined
- collab_caps_raw is defined
- (collab_caps_raw | trim) is match('^\\{')
failed_when: false failed_when: false
# --- Always show a raw payload (useful for 502 HTML or text) --- - name: Collabora | Print concise summary
- name: Collabora | RAW capabilities payload
ansible.builtin.debug: ansible.builtin.debug:
msg: | msg: >-
RAW_CAPABILITIES: Collabora {{ collab_caps_json.productVersion | default('?') }}
{{ collab_caps_raw | default('') }} ({{ 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
# --- Pretty fields (only when we really got JSON) --- - name: Collabora | Capabilities unavailable (after retries)
- name: Collabora | Pretty-print selected fields
ansible.builtin.debug: ansible.builtin.debug:
msg: | msg: "Capabilities endpoint není dostupný ani po pokusech."
convert-to.available: {{ collab_caps_json['convert-to']['available'] | default('n/a') }} when: collab_caps_json is not defined
hasDocumentSigningSupport: {{ collab_caps_json.hasDocumentSigningSupport | default('n/a') }}
hasMobileSupport: {{ collab_caps_json.hasMobileSupport | default('n/a') }} # Volitelný detailní výpis (jen když chceš)
hasProxyPrefix: {{ collab_caps_json.hasProxyPrefix | default('n/a') }} - name: Collabora | Full JSON (debug)
hasSettingIframeSupport: {{ collab_caps_json.hasSettingIframeSupport | default('n/a') }} ansible.builtin.debug:
hasTemplateSaveAs: {{ collab_caps_json.hasTemplateSaveAs | default('n/a') }} var: collab_caps_json
hasTemplateSource: {{ collab_caps_json.hasTemplateSource | default('n/a') }} when: debug_caps and (collab_caps_json is defined)
hasWASMSupport: {{ collab_caps_json.hasWASMSupport | default('n/a') }}
hasWopiAccessCheck: {{ collab_caps_json.hasWopiAccessCheck | default('n/a') }}
hasZoteroSupport: {{ collab_caps_json.hasZoteroSupport | default('n/a') }}
productName: {{ collab_caps_json.productName | default('n/a') }}
productVersion: {{ collab_caps_json.productVersion | default('n/a') }}
productVersionHash: {{ collab_caps_json.productVersionHash | default('n/a') }}
serverId: {{ collab_caps_json.serverId | default('n/a') }}
when: collab_caps_json is defined