From d1e961e606105977601f12af1ff1550d0b37f1d7 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Wed, 19 Jan 2022 11:59:19 +0100
Subject: [PATCH] add delete_all_tenants option to delete every tenants
 (#60811)

---
 delete-tenants.yml       | 77 +++++++++++++++++++++++++---------------
 group_vars/all           |  1 +
 tasks/delete_tenants.yml | 22 ++++++++++++
 3 files changed, 72 insertions(+), 28 deletions(-)
 create mode 100644 tasks/delete_tenants.yml

diff --git a/delete-tenants.yml b/delete-tenants.yml
index 7055d0e..ab32861 100644
--- a/delete-tenants.yml
+++ b/delete-tenants.yml
@@ -1,5 +1,5 @@
 ---
-- name: delete all publik tenants
+- name: delete publik tenants
   hosts: local
   vars:
     venv_py3_bin: "{{venv_py3}}/bin"
@@ -7,38 +7,59 @@
     - facts
 
   tasks:
-    - name: "delete hobo tenant"
-      command: "{{venv_py3_bin}}/{{apps['hobo']['project_name']}}-manage delete_tenant {{tenants_conf['dev-hobo']}}"
-      ignore_errors: yes
 
-    - name: "delete combo user tenant"
-      command: "{{venv_py3_bin}}/{{apps['combo']['project_name']}}-manage delete_tenant {{tenants_conf['user-combo']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: hobo
+        tenant_dir: /var/lib/hobo/tenants
+        tenants: "{{[tenants_conf['dev-hobo']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['hobo']['project_name']}}-manage delete_tenant"
 
-    - name: "delete combo agent tenant"
-      command: "{{venv_py3_bin}}/{{apps['combo']['project_name']}}-manage delete_tenant {{tenants_conf['agent-combo']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: combo
+        tenant_dir: /var/lib/combo/tenants
+        tenants: "{{[tenants_conf['user-combo'], tenants_conf['agent-combo']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['combo']['project_name']}}-manage delete_tenant"
 
-    - name: "delete authentic tenant"
-      command: "{{venv_py3_bin}}/{{apps['authentic']['project_name']}}-manage delete_tenant {{tenants_conf['connexion-authentic']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: authentic
+        tenant_dir: /var/lib/authentic2-multitenant/tenants
+        tenants: "{{[tenants_conf['connexion-authentic']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['authentic']['project_name']}}-manage delete_tenant"
 
-    - name: "delete wcs tenant"
-      command: "{{venv_py3_bin}}/wcsctl.py delete_tenant {{tenants_conf['demarches-wcs']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: wcs
+        tenant_dir: /var/lib/wcs/tenants
+        tenants: "{{[tenants_conf['demarches-wcs']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/wcsctl.py delete_tenant"
 
-    - name: "delete fargo tenant"
-      command: "{{venv_py3_bin}}/{{apps['fargo']['project_name']}}-manage delete_tenant {{tenants_conf['dev-fargo']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: fargo
+        tenant_dir: /var/lib/fargo/tenants
+        tenants: "{{[tenants_conf['dev-fargo']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['fargo']['project_name']}}-manage delete_tenant"
 
-    - name: "delete chrono tenant"
-      command: "{{venv_py3_bin}}/{{apps['chrono']['project_name']}}-manage delete_tenant {{tenants_conf['dev-chrono']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: chrono
+        tenant_dir: /var/lib/chrono/tenants
+        tenants: "{{[tenants_conf['dev-chrono']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['chrono']['project_name']}}-manage delete_tenant"
 
-    - name: "delete passerelle tenant"
-      command: "{{venv_py3_bin}}/{{apps['passerelle']['project_name']}}-manage delete_tenant {{tenants_conf['dev-passerelle']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: passerelle
+        tenant_dir: /var/lib/passerelle/tenants
+        tenants: "{{[tenants_conf['dev-passerelle']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['passerelle']['project_name']}}-manage delete_tenant"
 
-    - name: "delete bijoe tenant"
-      command: "{{venv_py3_bin}}/{{apps['bijoe']['project_name']}}-manage delete_tenant {{tenants_conf['dev-bijoe']}}"
-      ignore_errors: yes
+    - import_tasks: tasks/delete_tenants.yml
+      vars:
+        app: bijoe
+        tenant_dir: /var/lib/bijoe/tenants
+        tenants: "{{[tenants_conf['dev-bijoe']]}}"
+        delete_tenant_cmd: "{{venv_py3_bin}}/{{apps['bijoe']['project_name']}}-manage delete_tenant"
diff --git a/group_vars/all b/group_vars/all
index 646299c..e43148f 100644
--- a/group_vars/all
+++ b/group_vars/all
@@ -1,6 +1,7 @@
 clean_venv: false
 compile_theme: true
 django_version: 2.2.24
+delete_all_tenants: false
 git_ssh: false
 devinst_share: /usr/local/share/publik-devinst
 publik_conf: "/home/{{user}}/.config/publik"
diff --git a/tasks/delete_tenants.yml b/tasks/delete_tenants.yml
new file mode 100644
index 0000000..cae8b7f
--- /dev/null
+++ b/tasks/delete_tenants.yml
@@ -0,0 +1,22 @@
+- name: "Delete {{app}} main tenants"
+  command: "{{delete_tenant_cmd}} {{item}}"
+  ignore_errors: yes
+  loop: "{{tenants}}"
+  when: not delete_all_tenants
+
+- name: "Find all {{app}} tenants"
+  ansible.builtin.find:
+    paths: "{{tenant_dir}}"
+    recurse: no
+    file_type: directory
+    excludes: '*.invalid'
+  register: all_tenants
+  when: delete_all_tenants
+
+- name: "Delete all {{app}} tenants"
+  command: "{{delete_tenant_cmd}} {{item.path}}"
+  ignore_errors: yes
+  loop: "{{all_tenants.files}}"
+  when: delete_all_tenants
+
+
-- 
2.30.2

