forked from jakub/ansible
fix raid
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
# nextcloud/update_collabora.yml
|
# check_raid.yml
|
||||||
|
|
||||||
- name: Update Collabora CODE on VM via Proxmox
|
- name: Check Linux MD RAID health on VM via Proxmox
|
||||||
hosts: linux_servers
|
hosts: linux_servers
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
become: true
|
become: true
|
||||||
@@ -8,33 +8,25 @@
|
|||||||
become_method: sudo
|
become_method: sudo
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
# --- Connection to VM (provided by Semaphore env vars) ---
|
# VM connection (provided by Semaphore env vars)
|
||||||
vm_ip: "{{ lookup('env', 'VM_IP') }}"
|
vm_ip: "{{ lookup('env', 'VM_IP') }}"
|
||||||
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
|
||||||
|
|
||||||
# --- Debug mode (controlled via Semaphore variable) ---
|
# Debug / retries
|
||||||
DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}"
|
DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}"
|
||||||
RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}"
|
RETRIES: "{{ lookup('env', 'RETRIES') | default(10) | int }}"
|
||||||
|
|
||||||
# --- Collabora specifics ---
|
# RAID device to check (e.g. md0, md1...)
|
||||||
collabora_debug_caps: true
|
raid_md_device: "{{ lookup('env', 'RAID_MD') | default('md0', true) }}"
|
||||||
collabora_caps_url: "https://collabora.martinfencl.eu/hosting/capabilities"
|
|
||||||
|
|
||||||
# Use the FULL Nextcloud stack compose file; only target the 'collabora' service inside it
|
# If 0 => fail when resync/recovery/reshape/check/repair is detected in /proc/mdstat
|
||||||
collabora_project: "nextcloud-collabora"
|
# If 1 => allow sync operations (still fails only on degraded [U_], [_U], etc.)
|
||||||
collabora_compose_file: "/data/compose/nextcloud/nextcloud-collabora.yml"
|
raid_allow_sync: "{{ lookup('env', 'RAID_ALLOW_SYNC') | default(1, true) | int }}"
|
||||||
collabora_service: "collabora"
|
|
||||||
|
|
||||||
# Docker command prefix (consistent behavior and quiet hints)
|
# SSH options
|
||||||
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
ssh_connect_timeout: 15
|
||||||
|
|
||||||
# Commands to run on the target VM (quiet outputs)
|
|
||||||
collabora_commands:
|
|
||||||
- "{{ docker_prefix }} pull -q collabora/code:latest >/dev/null"
|
|
||||||
- "{{ docker_prefix }} compose -p {{ collabora_project }} -f {{ collabora_compose_file }} pull {{ collabora_service }} >/dev/null"
|
|
||||||
- "{{ docker_prefix }} compose -p {{ collabora_project }} -f {{ collabora_compose_file }} up -d --no-deps --force-recreate {{ collabora_service }} >/dev/null"
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
||||||
@@ -43,7 +35,7 @@
|
|||||||
state: present
|
state: present
|
||||||
update_cache: yes
|
update_cache: yes
|
||||||
|
|
||||||
- name: Run Collabora update commands on VM (via SSH) # use SSHPASS env, hide item value
|
- name: Read /proc/mdstat from VM (via SSH) # English comments
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv:
|
argv:
|
||||||
- sshpass
|
- sshpass
|
||||||
@@ -52,123 +44,74 @@
|
|||||||
- -o
|
- -o
|
||||||
- StrictHostKeyChecking=no
|
- StrictHostKeyChecking=no
|
||||||
- -o
|
- -o
|
||||||
- ConnectTimeout=15
|
- "ConnectTimeout={{ ssh_connect_timeout }}"
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
- "{{ vm_user }}@{{ vm_ip }}"
|
||||||
- bash
|
- bash
|
||||||
- -lc
|
- -lc
|
||||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
- "{{ ('sudo ' if use_sudo else '') + 'cat /proc/mdstat' }}"
|
||||||
environment:
|
environment:
|
||||||
SSHPASS: "{{ vm_pass }}"
|
SSHPASS: "{{ vm_pass }}"
|
||||||
loop: "{{ collabora_commands }}"
|
register: mdstat_raw
|
||||||
loop_control:
|
|
||||||
index_var: idx # <-- capture loop index here
|
|
||||||
label: "cmd-{{ idx }}" # <-- use idx instead of loop.index
|
|
||||||
register: collab_cmds
|
|
||||||
changed_when: false
|
changed_when: false
|
||||||
no_log: "{{ DEBUG == 0 }}"
|
no_log: "{{ DEBUG == 0 }}"
|
||||||
|
|
||||||
- name: Show outputs for each Collabora command
|
|
||||||
ansible.builtin.debug:
|
|
||||||
msg: |
|
|
||||||
CMD: {{ item.item }}
|
|
||||||
RC: {{ item.rc }}
|
|
||||||
STDOUT:
|
|
||||||
{{ (item.stdout | default('')).strip() }}
|
|
||||||
STDERR:
|
|
||||||
{{ (item.stderr | default('')).strip() }}
|
|
||||||
loop: "{{ collab_cmds.results }}"
|
|
||||||
when: DEBUG == 1
|
|
||||||
|
|
||||||
- name: Fail play if any Collabora command failed # also hide item label
|
|
||||||
ansible.builtin.assert:
|
|
||||||
that: "item.rc == 0"
|
|
||||||
fail_msg: "Collabora update failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
|
||||||
success_msg: "All Collabora update commands succeeded."
|
|
||||||
loop: "{{ collab_cmds.results }}"
|
|
||||||
loop_control:
|
|
||||||
index_var: idx
|
|
||||||
label: "cmd-{{ idx }}"
|
|
||||||
|
|
||||||
# -------------------------
|
|
||||||
# Readiness checks (controller first, then VM fallback)
|
|
||||||
# -------------------------
|
|
||||||
|
|
||||||
- name: Collabora | Wait for capabilities (controller first)
|
|
||||||
ansible.builtin.uri:
|
|
||||||
url: "{{ collabora_caps_url }}"
|
|
||||||
method: GET
|
|
||||||
return_content: true
|
|
||||||
validate_certs: true
|
|
||||||
status_code: 200
|
|
||||||
register: caps_controller
|
|
||||||
delegate_to: localhost
|
|
||||||
run_once: true
|
|
||||||
retries: "{{ RETRIES }}"
|
retries: "{{ RETRIES }}"
|
||||||
delay: 2
|
delay: 2
|
||||||
until: caps_controller.status == 200
|
until: mdstat_raw.rc == 0
|
||||||
failed_when: false
|
|
||||||
changed_when: false
|
|
||||||
|
|
||||||
- name: Collabora | VM-side fetch (pure JSON via Python) # use SSHPASS env here too
|
- name: Debug | Show raw /proc/mdstat # English comments
|
||||||
ansible.builtin.command:
|
ansible.builtin.debug:
|
||||||
argv:
|
msg: "{{ mdstat_raw.stdout }}"
|
||||||
- sshpass
|
when: DEBUG == 1
|
||||||
- -e
|
|
||||||
- ssh
|
|
||||||
- -o
|
|
||||||
- StrictHostKeyChecking=no
|
|
||||||
- -o
|
|
||||||
- ConnectTimeout=15
|
|
||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
|
||||||
- bash
|
|
||||||
- -lc
|
|
||||||
- |
|
|
||||||
python3 - <<'PY'
|
|
||||||
import json, urllib.request, sys
|
|
||||||
try:
|
|
||||||
with urllib.request.urlopen("{{ collabora_caps_url }}", timeout=15) as r:
|
|
||||||
sys.stdout.write(r.read().decode())
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
PY
|
|
||||||
environment:
|
|
||||||
SSHPASS: "{{ vm_pass }}"
|
|
||||||
register: caps_vm
|
|
||||||
changed_when: false
|
|
||||||
failed_when: false
|
|
||||||
when: caps_controller.status | default(0) != 200 or caps_controller.json is not defined
|
|
||||||
no_log: "{{ DEBUG == 0 }}"
|
|
||||||
|
|
||||||
- name: Collabora | Choose JSON (controller wins, else VM)
|
- name: Extract RAID status token for selected MD device # English comments
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
collab_caps_json: >-
|
raid_token: >-
|
||||||
{{
|
{{
|
||||||
(caps_controller.json
|
(mdstat_raw.stdout | regex_search(
|
||||||
if (caps_controller.status|default(0))==200 and (caps_controller.json is defined)
|
raid_md_device ~ '\\s*:\\s*active.*\\n\\s*\\d+\\s+blocks.*\\[[0-9]+/[0-9]+\\]\\s*\\[([U_]+)\\]',
|
||||||
else (
|
'\\1',
|
||||||
(caps_vm.stdout | default('') | trim | length > 0)
|
multiline=True
|
||||||
| ternary((caps_vm.stdout | trim | from_json), omit)
|
)) | default('')
|
||||||
)
|
|
||||||
)
|
|
||||||
}}
|
}}
|
||||||
failed_when: false
|
|
||||||
|
|
||||||
- name: Collabora | Print concise summary
|
- name: Detect sync operations in mdstat (resync/recovery/reshape/check/repair) # English comments
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
raid_syncing: "{{ (mdstat_raw.stdout is search('resync|recovery|reshape|check|repair')) | bool }}"
|
||||||
|
|
||||||
|
- name: Compute degraded flag (underscore means missing member) # English comments
|
||||||
|
ansible.builtin.set_fact:
|
||||||
|
raid_degraded: "{{ (raid_token | length > 0) and ('_' in raid_token) }}"
|
||||||
|
|
||||||
|
- name: Fail if MD device not found in /proc/mdstat # English comments
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- raid_token | length > 0
|
||||||
|
fail_msg: "RAID device {{ raid_md_device }} was not found in /proc/mdstat on VM ({{ vm_ip }})."
|
||||||
|
success_msg: "RAID device {{ raid_md_device }} found in /proc/mdstat."
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Fail if RAID is degraded (token contains '_') # English comments
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- not raid_degraded
|
||||||
|
fail_msg: "RAID {{ raid_md_device }} is DEGRADED: token=[{{ raid_token }}] (expected all 'U')."
|
||||||
|
success_msg: "RAID {{ raid_md_device }} is OK: token=[{{ raid_token }}]."
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Fail if RAID is syncing and syncing is not allowed # English comments
|
||||||
|
ansible.builtin.assert:
|
||||||
|
that:
|
||||||
|
- (raid_allow_sync | int) == 1 or (not raid_syncing)
|
||||||
|
fail_msg: "RAID {{ raid_md_device }} is running a sync operation (resync/recovery/reshape/check/repair) and RAID_ALLOW_SYNC=0."
|
||||||
|
success_msg: "No sync operation detected (or RAID_ALLOW_SYNC=1)."
|
||||||
|
changed_when: false
|
||||||
|
|
||||||
|
- name: Print concise summary (debug) # English comments
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: >-
|
msg: >-
|
||||||
Collabora {{ collab_caps_json.productVersion | default('?') }}
|
RAID={{ raid_md_device }},
|
||||||
({{ collab_caps_json.productName | default('?') }}),
|
token=[{{ raid_token }}],
|
||||||
convert-to.available={{ collab_caps_json['convert-to']['available'] | default('n/a') }},
|
degraded={{ raid_degraded }},
|
||||||
serverId={{ collab_caps_json.serverId | default('n/a') }}
|
syncing={{ raid_syncing }},
|
||||||
when: collab_caps_json is defined and DEBUG == 1
|
allow_sync={{ raid_allow_sync }}
|
||||||
|
when: DEBUG == 1
|
||||||
- name: Collabora | Capabilities unavailable (after retries)
|
|
||||||
ansible.builtin.debug:
|
|
||||||
msg: "Capabilities endpoint není dostupný ani po pokusech."
|
|
||||||
when: collab_caps_json is not defined and DEBUG == 1
|
|
||||||
|
|
||||||
# Optional full JSON (debug)
|
|
||||||
- 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
|
|
||||||
|
|||||||
Reference in New Issue
Block a user