From 48e965bb640bbee7d1b253f94c1d8ea96a8298eb Mon Sep 17 00:00:00 2001
From: Franck Bebel <franck.bebel@ouidou.fr>
Date: Mon, 25 May 2020 21:47:50 +0200
Subject: [PATCH] [DEVOPS] Add vitamui uninstallation

---
 deployment/roles/uninstall/tasks/consul.yml   |  29 +++
 deployment/roles/uninstall/tasks/main.yml     |   6 +
 .../roles/uninstall/tasks/repository.yml      |  23 ++
 deployment/roles/uninstall/tasks/rsyslog.yml  |   6 +
 .../roles/uninstall/tasks/vitamui-service.yml |  28 +++
 deployment/uninstall.yml                      | 224 ++++++++++++++----
 6 files changed, 267 insertions(+), 49 deletions(-)
 create mode 100644 deployment/roles/uninstall/tasks/consul.yml
 create mode 100644 deployment/roles/uninstall/tasks/main.yml
 create mode 100644 deployment/roles/uninstall/tasks/repository.yml
 create mode 100644 deployment/roles/uninstall/tasks/rsyslog.yml
 create mode 100644 deployment/roles/uninstall/tasks/vitamui-service.yml

diff --git a/deployment/roles/uninstall/tasks/consul.yml b/deployment/roles/uninstall/tasks/consul.yml
new file mode 100644
index 00000000..7d105372
--- /dev/null
+++ b/deployment/roles/uninstall/tasks/consul.yml
@@ -0,0 +1,29 @@
+
+---
+# tasks file for vitam-consul-uninstall
+- name: Stop vitam service
+  systemd:
+    name: "vitam-{{ service_name }}"
+    state: stopped
+  ignore_errors: yes
+
+- name: Uninstall vitam service
+  yum:
+    name: "vitam-{{ service_name }}"
+    state: absent
+
+- name: Delete file system of consul
+  file:
+    name: "{{ item }}"
+    state: absent
+  with_items:
+    - "/vitam/app/{{ service_name }}"
+    - "/vitam/bin/{{ service_name }}"
+    - "/vitam/conf/{{ service_name }}"
+    - "/vitam/data/{{ service_name }}"
+    - "/vitam/defaults/{{ service_name }}"
+    - "/vitam/lib/{{ service_name }}"
+    - "/vitam/log/{{ service_name }}"
+    - "/vitam/run/{{ service_name }}"
+    - "/vitam/script/{{ service_name }}"
+    - "/vitam/tmp/{{ service_name }}"
diff --git a/deployment/roles/uninstall/tasks/main.yml b/deployment/roles/uninstall/tasks/main.yml
new file mode 100644
index 00000000..e1d9ce19
--- /dev/null
+++ b/deployment/roles/uninstall/tasks/main.yml
@@ -0,0 +1,6 @@
+---
+# tasks file for dlab-uninstall
+
+- name: "Execute uninstall for {{ uninstaller_type }}" 
+  include_tasks: "{{ uninstaller_type }}.yml"
+  when: "{{ lookup('pipe', 'test -f {{ role_path }}/tasks/{{ uninstaller_type }}.yml || echo nofile') == \"\" }}"
\ No newline at end of file
diff --git a/deployment/roles/uninstall/tasks/repository.yml b/deployment/roles/uninstall/tasks/repository.yml
new file mode 100644
index 00000000..e8b24854
--- /dev/null
+++ b/deployment/roles/uninstall/tasks/repository.yml
@@ -0,0 +1,23 @@
+# remove the dlab repository
+- name: Reload systemd
+  command: systemctl daemon-reload
+
+- name: Clean systemd
+  command: systemctl reset-failed
+
+- name: Remove yum repository
+  file:
+    path: "/etc/yum.repos.d/{{ item }}.repo"
+    state: absent
+  with_items:
+    - vitamui-repositories
+    - logstash
+    - mongo
+
+- name: Clean repository metadata
+  shell: yum clean metadata
+  ignore_errors: yes
+
+- name: Ensure yum cache is reset
+  command: yum makecache
+  ignore_errors: yes
\ No newline at end of file
diff --git a/deployment/roles/uninstall/tasks/rsyslog.yml b/deployment/roles/uninstall/tasks/rsyslog.yml
new file mode 100644
index 00000000..ee2618d7
--- /dev/null
+++ b/deployment/roles/uninstall/tasks/rsyslog.yml
@@ -0,0 +1,6 @@
+# Task for rsyslog uninstallation
+
+- name: Remove vitamui template
+  file:
+    path: /etc/rsyslog.d/vitamui_transport.conf
+    state: absent
\ No newline at end of file
diff --git a/deployment/roles/uninstall/tasks/vitamui-service.yml b/deployment/roles/uninstall/tasks/vitamui-service.yml
new file mode 100644
index 00000000..b39c170d
--- /dev/null
+++ b/deployment/roles/uninstall/tasks/vitamui-service.yml
@@ -0,0 +1,28 @@
+---
+# tasks file for vitamui-uninstall
+- name: Stop vitamui service
+  systemd:
+    name: "vitamui-{{ service_name }}"
+    state: stopped
+  ignore_errors: yes
+
+- name: Uninstall vitamui service
+  yum:
+    name: "vitamui-{{ service_name }}"
+    state: absent
+
+- name: Delete file system of the service
+  file:
+    name: "{{ item }}"
+    state: absent
+  with_items:
+    - "/vitamui/app/{{ service_name }}"
+    - "/vitamui/bin/{{ service_name }}"
+    - "/vitamui/conf/{{ service_name }}"
+    - "/vitamui/data/{{ service_name }}"
+    - "/vitamui/defaults/{{ service_name }}"
+    - "/vitamui/lib/{{ service_name }}"
+    - "/vitamui/log/{{ service_name }}"
+    - "/vitamui/run/{{ service_name }}"
+    - "/vitamui/script/{{ service_name }}"
+    - "/vitamui/tmp/{{ service_name }}"
\ No newline at end of file
diff --git a/deployment/uninstall.yml b/deployment/uninstall.yml
index 74da7b51..ff548628 100644
--- a/deployment/uninstall.yml
+++ b/deployment/uninstall.yml
@@ -1,54 +1,180 @@
-- hosts: localhost
-  connection: local
-  become: false
+# ########################################################
+# # ZONE Common vitamui
+# ########################################################
+- hosts: hosts_vitamui
   roles:
-    - bootstrap_ansible
+    - uninstall
+  vars:
+    uninstaller_type: rsyslog
+
+
+
+########################################################
+# ZONE Infra
+########################################################
+
+## Pas de consul pour vitamui
 
 - hosts: hosts_vitamui
-  gather_facts: false
+  roles:
+    - uninstall
+  vars:
+    service_name: consul
+    uninstaller_type: consul
+  tags:
+    - consul
+    - zone-vitamui-infra
+
+- hosts: hosts_vitamui_logstash
+  roles:
+    - uninstall
+  vars:
+    service_name: logstash
+    uninstaller_type: vitamui-service
+  tags:
+    - logstash
+    - zone-vitamui-infra
 
+#######################################################
+# ZONE Data
+#######################################################
+
+- hosts: hosts_vitamui_mongod
+  roles:
+    - uninstall
+  vars:
+    service_name: mongod
+    uninstaller_type: vitamui-service
+  tags:
+    - mongod
+
+- hosts: hosts_vitamui_mongod
+  roles:
+    - uninstall
+  vars:
+    service_name: mongo-express
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-data
+    - mongo-express
+
+########################################################
+# ZONE APP
+########################################################
+
+- hosts: hosts_vitamui_security_internal
+  roles:
+    - uninstall
+  vars:
+    service_name: security-internal
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-app
+    - security-internal
+
+- hosts: hosts_vitamui_iam_internal
+  roles:
+    - uninstall
+  vars:
+    service_name: iam-internal
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-app
+    - iam-internal
+
+- hosts: hosts_vitamui_iam_external
+  roles:
+    - uninstall
+  vars:
+    service_name: iam-external
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-app
+    - iam-external
+
+########################################################
+# ZONE CAS
+########################################################
+- hosts: hosts_cas_server
+  roles:
+    - uninstall
+  vars:
+    service_name: cas-server
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-cas
+    - cas-server
+
+########################################################
+# ZONE UI
+########################################################
+- hosts: hosts_ui_identity
+  roles:
+    - uninstall
+  vars:
+    service_name: ui-identity
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-ui
+    - ui-identity
+
+- hosts: hosts_ui_portal
+  roles:
+    - uninstall
+  vars:
+    service_name: ui-portal
+    uninstaller_type: vitamui-service
+  tags:
+    - zone-vitamui-ui
+    - ui-portal
+
+
+########################################################
+# ZONE ADMIN
+########################################################
+- hosts: hosts_ui_identity_admin
+  roles:
+    - uninstall
+  vars:
+    service_name: ui-identity-admin
+    uninstaller_type: vitamui-service
+  tags:
+    - ui-identity-admin
+    - zone-vitamui-admin
+
+########################################################
+# ZONE REVERSE
+########################################################
+- hosts: hosts_vitamui_reverseproxy
+  roles:
+    - uninstall
+  vars:
+    service_name: nginx
+    uninstaller_type: vitamui-service
+  tags:
+   - reverseproxy
+   - zone-vitamui-rp
+   - nginx
+
+############################
+# Delete empty dir
+############################
+
+- hosts: hosts_vitamui
   tasks:
-    - name: Stop all vitamui services
-      shell: "systemctl stop vitam*"
-      ignore_errors: yes
-
-    - name: Stop all dlab services
-      shell: "systemctl stop dlab*"
-      ignore_errors: yes
-
-    # Uninstalling package will remove systemd unit files
-    - name: Uninstall vitamui packages
-      yum:
-        name: "vitam*"
-        state: removed
-      ignore_errors: yes
-
-    - name: Reload systemd
-      command: systemctl daemon-reload
-
-    - name: Clean systemd
-      command: systemctl reset-failed
-
-#    - name: Remove yum repository
-#      file:
-#        path: "/etc/yum.repos.d/{{ item }}.repo"
-#        state: absent
-#      with_items:
-#        - logstash
-#        - mongo
-
-    - name: Clean repository metadata
-      shell: yum clean metadata
-      ignore_errors: yes
-
-    - name: Ensure yum cache is reset
-      command: yum makecache
-      ignore_errors: yes
-
-    - name: Remove vitamui file system
-      command: rm -Rf /vitamui
-      ignore_errors: yes
-
-    - name: Remove vitam file system
-      command: rm -Rf /vitam
-      ignore_errors: yes
+    - name: delete empty dir vitamui
+      shell: "find /vitamui -type d -empty -delete"
+    - name: delete empty dir vitam
+      shell: "find /vitam -type d -empty -delete"
+
+############################
+# Remove repository and yum cache
+############################
+
+- hosts: hosts_vitamui
+  roles:
+    - uninstall
+  vars:
+    uninstaller_type: repository
+  tags:
+   - repository
\ No newline at end of file
-- 
GitLab