From 63bdbfba813c17a3543f42ae879898ea5b3b683b Mon Sep 17 00:00:00 2001
From: nono <np@laquadrature.net>
Date: Mon, 5 Dec 2022 19:26:52 +0100
Subject: [PATCH] Update checking for updates

---
 tasks/update-service.yml | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/tasks/update-service.yml b/tasks/update-service.yml
index 62705f7..0beddc0 100644
--- a/tasks/update-service.yml
+++ b/tasks/update-service.yml
@@ -1,14 +1,27 @@
-# Update the keycloak installation
-- name: Checking for pre-existing installation
-  stat:
-    path: "{{ keycloak_base_path }}/keycloak-{{ keycloak_previous_version }}"
-  register: is_there_an_existing_keycloak
+# Check if there is a keycloak directory in the base_path
+- name: Check for existing keycloak installation
+  find:
+    paths: "{{ keycloak_base_path }}/keycloak-*"
+    file_type: "directory"
+    depth: 1
+  register: keycloak_existing_installations
 
-- name: Printing status of pre-existing installation
-  debug:
-    msg: "{{ is_there_an_existing_keycloak }}"
+- name: Checking if there is an existing installation that needs to be updated
+  block:
+    # We need to extract the version of each directory and add them to a list
+    # If so, what is it's version ? ( cut from  "keycloak-$version ")
+    - name: Check existing versions from directorie's names
+      set_fact: keycloak_list_of_versions="{{ keycloak_existing_installations.files | map(attribute='path') | regex_search('-([0-9]+\.[0-9]+\.[0-9]+\b)$') | list }}"
+    
+    # Compare such version to the one given
+    - name: Compare every version with the keycloak_version 
+      set_fact: keycloak_needs_an_update="keycloak_list_of_versions | version(keycloak_list_of_versions, "<") | reject(false, omit) | list"
+    # If the $old_version < $new_version, we need to update
+
+  # They may be multi keycloak directories
+  when: keycloak_existing_installations.matched >= 1
 
-- name: Updating if needed
+- name: Updating if keycloak needs an update
   block:
     # Stop the service
     - name: Stopping the service for the update.
@@ -28,12 +41,7 @@
     # Upgrade the server
     #- name: Upgrading the server with the new files
     # This is actually reinstalling the files.
-  when: is_there_an_existing_keycloak.stat.exists
-
-- name: Update state
-  debug:
-    msg: "No update done."
-  when: not is_there_an_existing_keycloak.stat.exists
+  when: true in keycloak_needs_an_update
 
 - name:  Force systemd to reread configs
   become: yes
-- 
GitLab