This commit is contained in:
martin.fencl
2026-08-06 18:04:05 +02:00
parent 4239ab4ed0
commit feec8ec7a3
2 changed files with 282 additions and 315 deletions
+15 -10
View File
@@ -8,7 +8,6 @@
become_method: sudo become_method: sudo
vars: vars:
# Internal normalized variables
vm_use_sudo: "{{ use_sudo | default(false) | bool }}" vm_use_sudo: "{{ use_sudo | default(false) | bool }}"
vm_commands: vm_commands:
@@ -30,8 +29,8 @@
- >- - >-
set -o pipefail; set -o pipefail;
timeout 180s bash -x timeout 180s
/data/compose/nextcloud/stack-health.sh bash -x /data/compose/nextcloud/stack-health.sh
</dev/null </dev/null
pre_tasks: pre_tasks:
@@ -46,7 +45,7 @@
- vm_pass | string | length > 0 - vm_pass | string | length > 0
fail_msg: >- fail_msg: >-
Missing vm_ip, vm_user or vm_pass. Missing vm_ip, vm_user or vm_pass.
Configure these variables in the attached Semaphore Variable Group. Configure them in the attached Semaphore Variable Group.
quiet: true quiet: true
no_log: true no_log: true
@@ -69,13 +68,17 @@
- UserKnownHostsFile=/dev/null - UserKnownHostsFile=/dev/null
- -o - -o
- ConnectTimeout=15 - ConnectTimeout=15
- -o
- ServerAliveInterval=10
- -o
- ServerAliveCountMax=3
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- >- - >-
{{ {{
('sudo -n bash -lc ' + (item | quote)) ('sudo -n bash -lc ' ~ (item | quote))
if vm_use_sudo if vm_use_sudo
else else
('bash -lc ' + (item | quote)) ('bash -lc ' ~ (item | quote))
}} }}
environment: environment:
SSHPASS: "{{ vm_pass }}" SSHPASS: "{{ vm_pass }}"
@@ -91,10 +94,12 @@
msg: | msg: |
CMD: {{ item.item }} CMD: {{ item.item }}
RC: {{ item.rc }} RC: {{ item.rc }}
STDOUT: STDOUT:
{{ (item.stdout | default('') | trim) }} {{ item.stdout | default('') | trim }}
STDERR: STDERR:
{{ (item.stderr | default('') | trim) }} {{ item.stderr | default('') | trim }}
loop: "{{ vm_cmds.results }}" loop: "{{ vm_cmds.results }}"
loop_control: loop_control:
label: "{{ item.item }}" label: "{{ item.item }}"
@@ -103,9 +108,9 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- item.rc == 0 - item.rc == 0
fail_msg: >- fail_msg: |
Command failed on VM: {{ item.item }} Command failed on VM: {{ item.item }}
(rc={{ item.rc }}) RC: {{ item.rc }}
STDERR: STDERR:
{{ item.stderr | default('') | trim }} {{ item.stderr | default('') | trim }}
+130 -168
View File
@@ -1,4 +1,4 @@
# nextcloud/update_nextcloud.yml # nextcloud/update_nextcloud_v2.yml
- name: Update Nextcloud on VM via Proxmox - name: Update Nextcloud on VM via Proxmox
hosts: proxmox_nextcloud hosts: proxmox_nextcloud
@@ -8,7 +8,7 @@
become_method: sudo become_method: sudo
vars: vars:
# Internal normalized variables # Normalized values from Semaphore Variable Group
vm_use_sudo: "{{ use_sudo | default(false) | bool }}" vm_use_sudo: "{{ use_sudo | default(false) | bool }}"
debug_level: "{{ DEBUG | default(0) | int }}" debug_level: "{{ DEBUG | default(0) | int }}"
retry_count: "{{ RETRIES | default(25) | int }}" retry_count: "{{ RETRIES | default(25) | int }}"
@@ -21,51 +21,64 @@
nextcloud_container: "nextcloud" nextcloud_container: "nextcloud"
nextcloud_db_container: "nextcloud-db" nextcloud_db_container: "nextcloud-db"
backup_dir: >-
/data/compose/nextcloud/backup-{{
lookup('pipe', 'date +%F-%H%M%S')
}}
nextcloud_base_url: "https://cloud.martinfencl.eu" nextcloud_base_url: "https://cloud.martinfencl.eu"
nextcloud_status_url: "{{ nextcloud_base_url }}/status.php" nextcloud_status_url: "{{ nextcloud_base_url }}/status.php"
backup_timestamp: "{{ lookup('pipe', 'date +%F-%H%M%S') }}"
backup_dir: "/data/compose/nextcloud/backup-{{ backup_timestamp }}"
docker_prefix: >- docker_prefix: >-
unalias docker 2>/dev/null || true; unalias docker 2>/dev/null || true;
export DOCKER_CLI_HINTS=0; export DOCKER_CLI_HINTS=0;
command docker command docker
# Backup commands executed inside the Nextcloud VM nextcloud_version_command: >-
docker exec -u www-data
{{ nextcloud_container }}
php occ -V
nextcloud_maintenance_off_command: >-
docker exec -u www-data
{{ nextcloud_container }}
php occ maintenance:mode --off
# Commands executed before the container update
nextcloud_backup_commands: nextcloud_backup_commands:
- >- - >-
mkdir -p {{ backup_dir | quote }} mkdir -p {{ backup_dir | quote }}
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec -u www-data {{ nextcloud_container }} exec -u www-data
{{ nextcloud_container }}
php occ maintenance:mode --on php occ maintenance:mode --on
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec {{ nextcloud_container }} exec
{{ nextcloud_container }}
sh -c sh -c
'tar czf /tmp/nextcloud_conf.tgz 'tar czf /tmp/nextcloud_conf.tgz
-C /var/www/html config custom_apps' -C /var/www/html
config custom_apps'
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
cp cp
{{ nextcloud_container }}:/tmp/nextcloud_conf.tgz {{ nextcloud_container }}:/tmp/nextcloud_conf.tgz
{{ (backup_dir + '/nextcloud_conf.tgz') | quote }} {{ (backup_dir ~ '/nextcloud_conf.tgz') | quote }}
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec {{ nextcloud_container }} exec
{{ nextcloud_container }}
rm -f /tmp/nextcloud_conf.tgz rm -f /tmp/nextcloud_conf.tgz
- >- - >-
set -o pipefail; set -o pipefail;
{{ docker_prefix }} {{ docker_prefix }}
exec {{ nextcloud_db_container }} exec
{{ nextcloud_db_container }}
sh -c sh -c
'if command -v mariadb-dump >/dev/null 2>&1; then 'if command -v mariadb-dump >/dev/null 2>&1; then
exec mariadb-dump exec mariadb-dump
@@ -78,22 +91,25 @@
-p"$MYSQL_PASSWORD" -p"$MYSQL_PASSWORD"
"$MYSQL_DATABASE"; "$MYSQL_DATABASE";
fi' fi'
> {{ (backup_dir + '/db.sql') | quote }} > {{ (backup_dir ~ '/db.sql') | quote }}
- >- - >-
test -s {{ (backup_dir + '/nextcloud_conf.tgz') | quote }} test -s
{{ (backup_dir ~ '/nextcloud_conf.tgz') | quote }}
- >- - >-
test -s {{ (backup_dir + '/db.sql') | quote }} test -s
{{ (backup_dir ~ '/db.sql') | quote }}
# Upgrade commands executed while maintenance mode remains enabled # Commands executed while maintenance mode is enabled
nextcloud_upgrade_commands: nextcloud_upgrade_commands:
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
compose compose
-p {{ nextcloud_project | quote }} -p {{ nextcloud_project | quote }}
-f {{ nextcloud_compose_file | quote }} -f {{ nextcloud_compose_file | quote }}
pull {{ nextcloud_service | quote }} pull
{{ nextcloud_service | quote }}
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
@@ -108,29 +124,44 @@
- >- - >-
timeout 180s bash -c timeout 180s bash -c
'until docker inspect 'until docker inspect
--format "{{ '{{' }}.State.Running{{ '}}' }}" --format="{{ '{{' }}.State.Running{{ '}}' }}"
{{ nextcloud_container | quote }} {{ nextcloud_container | quote }}
2>/dev/null | grep -qx true; 2>/dev/null | grep -qx true;
do sleep 3; done' do
sleep 3;
done'
- >-
timeout 180s bash -c
'until docker exec -u www-data
{{ nextcloud_container | quote }}
php occ status >/dev/null 2>&1;
do
sleep 3;
done'
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec -u www-data {{ nextcloud_container }} exec -u www-data
{{ nextcloud_container }}
php occ upgrade php occ upgrade
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec -u www-data {{ nextcloud_container }} exec -u www-data
{{ nextcloud_container }}
php occ app:update --all php occ app:update --all
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec -u www-data {{ nextcloud_container }} exec -u www-data
{{ nextcloud_container }}
php occ maintenance:repair --include-expensive php occ maintenance:repair --include-expensive
- >- - >-
{{ docker_prefix }} {{ docker_prefix }}
exec -u www-data {{ nextcloud_container }} exec -u www-data
{{ nextcloud_container }}
php occ status php occ status
pre_tasks: pre_tasks:
@@ -143,9 +174,10 @@
- vm_user | string | trim | length > 0 - vm_user | string | trim | length > 0
- vm_pass is defined - vm_pass is defined
- vm_pass | string | length > 0 - vm_pass | string | length > 0
- retry_count | int > 0
fail_msg: >- fail_msg: >-
Missing vm_ip, vm_user or vm_pass. Missing or invalid vm_ip, vm_user, vm_pass or RETRIES.
Configure these variables in the attached Semaphore Variable Group. Configure them in the attached Semaphore Variable Group.
quiet: true quiet: true
no_log: true no_log: true
@@ -171,22 +203,12 @@
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- >- - >-
{{ {{
( ('sudo -n bash -lc ' ~
'sudo -n bash -lc ' + (nextcloud_version_command | quote))
(
'docker exec -u www-data nextcloud php occ -V'
| quote
)
)
if vm_use_sudo if vm_use_sudo
else else
( ('bash -lc ' ~
'bash -lc ' + (nextcloud_version_command | quote))
(
'docker exec -u www-data nextcloud php occ -V'
| quote
)
)
}} }}
environment: environment:
SSHPASS: "{{ vm_pass }}" SSHPASS: "{{ vm_pass }}"
@@ -197,12 +219,19 @@
- name: Nextcloud | Print current version - name: Nextcloud | Print current version
ansible.builtin.debug: ansible.builtin.debug:
msg: "{{ nc_version_before.stdout | default('Version unavailable') }}" msg: >-
{{
nc_version_before.stdout
| default('Nextcloud version is unavailable')
| trim
}}
when: debug_level == 1 when: debug_level == 1
# ------------------------------------------------------------------------- - name: Nextcloud | Run backup and upgrade
block:
# ---------------------------------------------------------------------
# Backup # Backup
# ------------------------------------------------------------------------- # ---------------------------------------------------------------------
- name: Nextcloud | Run backup commands on VM - name: Nextcloud | Run backup commands on VM
ansible.builtin.command: ansible.builtin.command:
@@ -216,13 +245,17 @@
- UserKnownHostsFile=/dev/null - UserKnownHostsFile=/dev/null
- -o - -o
- ConnectTimeout=15 - ConnectTimeout=15
- -o
- ServerAliveInterval=10
- -o
- ServerAliveCountMax=3
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- >- - >-
{{ {{
('sudo -n bash -lc ' + (item | quote)) ('sudo -n bash -lc ' ~ (item | quote))
if vm_use_sudo if vm_use_sudo
else else
('bash -lc ' + (item | quote)) ('bash -lc ' ~ (item | quote))
}} }}
environment: environment:
SSHPASS: "{{ vm_pass }}" SSHPASS: "{{ vm_pass }}"
@@ -239,8 +272,10 @@
msg: | msg: |
CMD: {{ item.item }} CMD: {{ item.item }}
RC: {{ item.rc }} RC: {{ item.rc }}
STDOUT: STDOUT:
{{ item.stdout | default('') | trim }} {{ item.stdout | default('') | trim }}
STDERR: STDERR:
{{ item.stderr | default('') | trim }} {{ item.stderr | default('') | trim }}
loop: "{{ nc_backup_cmds.results }}" loop: "{{ nc_backup_cmds.results }}"
@@ -253,10 +288,10 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- item.rc == 0 - item.rc == 0
fail_msg: >- fail_msg: |
Nextcloud backup command failed: Nextcloud backup command failed.
{{ item.item }} Command: {{ item.item }}
(rc={{ item.rc }}) RC: {{ item.rc }}
STDERR: STDERR:
{{ item.stderr | default('') | trim }} {{ item.stderr | default('') | trim }}
@@ -266,9 +301,9 @@
index_var: backup_assert_index index_var: backup_assert_index
label: "backup-cmd-{{ backup_assert_index }}" label: "backup-cmd-{{ backup_assert_index }}"
# ------------------------------------------------------------------------- # ---------------------------------------------------------------------
# Upgrade # Upgrade
# ------------------------------------------------------------------------- # ---------------------------------------------------------------------
- name: Nextcloud | Run upgrade commands on VM - name: Nextcloud | Run upgrade commands on VM
ansible.builtin.command: ansible.builtin.command:
@@ -282,13 +317,17 @@
- UserKnownHostsFile=/dev/null - UserKnownHostsFile=/dev/null
- -o - -o
- ConnectTimeout=15 - ConnectTimeout=15
- -o
- ServerAliveInterval=10
- -o
- ServerAliveCountMax=3
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- >- - >-
{{ {{
('sudo -n bash -lc ' + (item | quote)) ('sudo -n bash -lc ' ~ (item | quote))
if vm_use_sudo if vm_use_sudo
else else
('bash -lc ' + (item | quote)) ('bash -lc ' ~ (item | quote))
}} }}
environment: environment:
SSHPASS: "{{ vm_pass }}" SSHPASS: "{{ vm_pass }}"
@@ -305,8 +344,10 @@
msg: | msg: |
CMD: {{ item.item }} CMD: {{ item.item }}
RC: {{ item.rc }} RC: {{ item.rc }}
STDOUT: STDOUT:
{{ item.stdout | default('') | trim }} {{ item.stdout | default('') | trim }}
STDERR: STDERR:
{{ item.stderr | default('') | trim }} {{ item.stderr | default('') | trim }}
loop: "{{ nc_upgrade_cmds.results }}" loop: "{{ nc_upgrade_cmds.results }}"
@@ -319,10 +360,10 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- item.rc == 0 - item.rc == 0
fail_msg: >- fail_msg: |
Nextcloud upgrade command failed: Nextcloud upgrade command failed.
{{ item.item }} Command: {{ item.item }}
(rc={{ item.rc }}) RC: {{ item.rc }}
STDERR: STDERR:
{{ item.stderr | default('') | trim }} {{ item.stderr | default('') | trim }}
@@ -332,10 +373,7 @@
index_var: upgrade_assert_index index_var: upgrade_assert_index
label: "upgrade-cmd-{{ upgrade_assert_index }}" label: "upgrade-cmd-{{ upgrade_assert_index }}"
# ------------------------------------------------------------------------- always:
# Disable maintenance mode
# -------------------------------------------------------------------------
- name: Nextcloud | Disable maintenance mode - name: Nextcloud | Disable maintenance mode
ansible.builtin.command: ansible.builtin.command:
argv: argv:
@@ -351,24 +389,12 @@
- "{{ vm_user }}@{{ vm_ip }}" - "{{ vm_user }}@{{ vm_ip }}"
- >- - >-
{{ {{
( ('sudo -n bash -lc ' ~
'sudo -n bash -lc ' + (nextcloud_maintenance_off_command | quote))
(
'docker exec -u www-data nextcloud ' +
'php occ maintenance:mode --off'
| quote
)
)
if vm_use_sudo if vm_use_sudo
else else
( ('bash -lc ' ~
'bash -lc ' + (nextcloud_maintenance_off_command | quote))
(
'docker exec -u www-data nextcloud ' +
'php occ maintenance:mode --off'
| quote
)
)
}} }}
environment: environment:
SSHPASS: "{{ vm_pass }}" SSHPASS: "{{ vm_pass }}"
@@ -376,13 +402,25 @@
changed_when: false changed_when: false
failed_when: false failed_when: false
- name: Nextcloud | Show maintenance mode result
ansible.builtin.debug:
msg: |
RC: {{ nc_maint_off.rc }}
STDOUT:
{{ nc_maint_off.stdout | default('') | trim }}
STDERR:
{{ nc_maint_off.stderr | default('') | trim }}
when: debug_level == 1
- name: Nextcloud | Fail if maintenance mode could not be disabled - name: Nextcloud | Fail if maintenance mode could not be disabled
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- nc_maint_off.rc == 0 - nc_maint_off.rc == 0
fail_msg: >- fail_msg: |
Failed to disable Nextcloud maintenance mode Failed to disable Nextcloud maintenance mode.
(rc={{ nc_maint_off.rc }}). RC: {{ nc_maint_off.rc }}
STDERR: STDERR:
{{ nc_maint_off.stderr | default('') | trim }} {{ nc_maint_off.stderr | default('') | trim }}
@@ -392,108 +430,32 @@
# Readiness check # Readiness check
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
- name: Nextcloud | Wait for status.php from controller - name: Nextcloud | Wait for status.php
ansible.builtin.uri: ansible.builtin.uri:
url: "{{ nextcloud_status_url }}" url: "{{ nextcloud_status_url }}"
method: GET method: GET
return_content: true return_content: true
validate_certs: true validate_certs: true
status_code: 200 status_code: 200
register: nc_status_controller register: nc_status
delegate_to: localhost delegate_to: localhost
run_once: true run_once: true
retries: "{{ retry_count }}" retries: "{{ retry_count }}"
delay: 4 delay: 4
until: until:
- nc_status_controller.status | default(0) == 200 - nc_status.status | default(0) == 200
- nc_status_controller.json is defined - nc_status.json is defined
failed_when: false - nc_status.json.installed | default(false) | bool
- not (nc_status.json.maintenance | default(true) | bool)
- not (nc_status.json.needsDbUpgrade | default(true) | bool)
changed_when: false changed_when: false
- name: Nextcloud | Fetch status.php from VM
ansible.builtin.command:
argv:
- sshpass
- -e
- ssh
- -o
- StrictHostKeyChecking=no
- -o
- UserKnownHostsFile=/dev/null
- -o
- ConnectTimeout=15
- "{{ vm_user }}@{{ vm_ip }}"
- >-
{{
(
'sudo -n bash -lc ' +
(
"python3 -c " +
(
"import urllib.request; " +
"print(urllib.request.urlopen(" +
nextcloud_status_url | quote +
", timeout=15).read().decode())"
) | quote
)
)
if vm_use_sudo
else
(
'bash -lc ' +
(
"python3 -c " +
(
"import urllib.request; " +
"print(urllib.request.urlopen(" +
nextcloud_status_url | quote +
", timeout=15).read().decode())"
) | quote
)
)
}}
environment:
SSHPASS: "{{ vm_pass }}"
register: nc_status_vm
changed_when: false
failed_when: false
when:
- nc_status_controller.status | default(0) != 200
or nc_status_controller.json is not defined
- name: Nextcloud | Use controller status JSON
ansible.builtin.set_fact:
nextcloud_status_json: "{{ nc_status_controller.json }}"
when:
- nc_status_controller.status | default(0) == 200
- nc_status_controller.json is defined
- name: Nextcloud | Use VM status JSON
ansible.builtin.set_fact:
nextcloud_status_json: "{{ nc_status_vm.stdout | trim | from_json }}"
when:
- nextcloud_status_json is not defined
- nc_status_vm is defined
- nc_status_vm.stdout | default('') | trim | length > 0
- name: Nextcloud | Validate final status
ansible.builtin.assert:
that:
- nextcloud_status_json is defined
- nextcloud_status_json.installed | default(false) | bool
- not (nextcloud_status_json.maintenance | default(true) | bool)
- not (nextcloud_status_json.needsDbUpgrade | default(true) | bool)
fail_msg: >-
Nextcloud is not ready after upgrade.
Status: {{ nextcloud_status_json | default('unavailable') }}
quiet: true
- name: Nextcloud | Print final status - name: Nextcloud | Print final status
ansible.builtin.debug: ansible.builtin.debug:
msg: >- msg: >-
Nextcloud {{ nextcloud_status_json.version | default('?') }} Nextcloud {{ nc_status.json.version | default('?') }}
(installed={{ nextcloud_status_json.installed | default('?') }}, (installed={{ nc_status.json.installed | default('?') }},
maintenance={{ nextcloud_status_json.maintenance | default('?') }}, maintenance={{ nc_status.json.maintenance | default('?') }},
needsDbUpgrade={{ needsDbUpgrade={{
nextcloud_status_json.needsDbUpgrade | default('?') nc_status.json.needsDbUpgrade | default('?')
}}) }})