From 0654f9dec8c91ed2cd3d2dc288db4e6f538e6b0e Mon Sep 17 00:00:00 2001 From: Ruslan Baidan Date: Fri, 30 Jun 2023 11:04:07 +0200 Subject: [PATCH] Added the process to automatically remove the clients' databases and user, also dedicated floder in case if the client's instance is removed. --- playbook/del_inventory.py | 33 ++++++++++++++ playbook/monarcfo-cleanup/tasks/main.yaml | 32 ++++++++++++++ playbook/update.sh | 6 +++ playbook/update_cleanup_inventory.py | 53 +++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 playbook/monarcfo-cleanup/tasks/main.yaml create mode 100644 playbook/update_cleanup_inventory.py diff --git a/playbook/del_inventory.py b/playbook/del_inventory.py index 45d1cc6..ec80519 100755 --- a/playbook/del_inventory.py +++ b/playbook/del_inventory.py @@ -35,18 +35,51 @@ def run(): path = os.path.join( os.path.abspath(INVENTORY), "host_vars/", to_delete["server"] ) + + path_to_cleanup = os.path.join( + os.path.abspath(INVENTORY), "cleanup/host_vars/", to_delete["server"] + ) + if not os.path.exists(path): print("Folder do no exists: {}".format(path)) exit(1) generated_file = os.path.join(path, "generated.yaml") + deleted_clients_file = os.path.join(path_to_cleanup, "deleted_clients.yaml") + if not os.path.exists(deleted_clients_file): + open(deleted_clients_file, 'a').close() + + # Read the deleted clients yaml file + with open(deleted_clients_file, "r") as deleted_clients_stream: + deleted_clients_yaml = yaml.load(deleted_clients_stream, Loader=yaml.FullLoader) + if deleted_clients_yaml is None: + deleted_clients_yaml = {} + deleted_clients_yaml["clients"] = {} + with open(generated_file, "r") as stream: ymldata = yaml.load(stream, Loader=yaml.FullLoader) client_list = ymldata["clients"] client_name = to_delete["proxy_alias"] try: del client_list[client_name] + + if client_name not in deleted_clients_yaml["clients"]: + # Add the client to the deleted list. + deleted_clients = {} + deleted_clients["clients"] = deleted_clients_yaml["clients"] + deleted_clients["clients"][client_name] = {} + deleted_clients["clients"][client_name]["name"] = client_name + deleted_clients["clients"][client_name]["isProcessed"] = False + + deleted_clients_yaml.update(deleted_clients) + + with open(deleted_clients_file, "w") as stream: + try: + yaml.dump(deleted_clients_yaml, stream) + except yaml.YAMLError as exc: + print(exc) + except Exception: pass diff --git a/playbook/monarcfo-cleanup/tasks/main.yaml b/playbook/monarcfo-cleanup/tasks/main.yaml new file mode 100644 index 0000000..8bf5e63 --- /dev/null +++ b/playbook/monarcfo-cleanup/tasks/main.yaml @@ -0,0 +1,32 @@ +--- + +- name: deleting of client dedicated folder + file: + path: /var/www/{{ item.value.name }}/ + state: absent + when: not item.value.isProcessed + loop: "{{ clients | dict2items }}" + loop_control: + label: "{{ item.key }}" + become: True + +- name: client database removal + mysql_db: + name: "{{ item.value.name }}" + config_file: /etc/mysql/debian.cnf + state: absent + loop: "{{ clients | dict2items }}" + when: not item.value.isProcessed + loop_control: + label: "{{ item.key }}" + become: True + +- name: database user removal + mysql_user: + config_file: /etc/mysql/debian.cnf + state: absent + loop: "{{ clients | dict2items }}" + when: not item.value.isProcessed + loop_control: + label: "{{ item.key }}" + become: True diff --git a/playbook/update.sh b/playbook/update.sh index 3e5e3fe..597bdcd 100755 --- a/playbook/update.sh +++ b/playbook/update.sh @@ -39,5 +39,11 @@ ssh ansible@$BO_ADDRESS sudo -u www-data /usr/local/bin/del_monarc_clients.sh | echo "Running ansible..." $ANSIBLE_PATH --diff -i ../inventory/ monarc.yaml --user ansible +echo "Running ansible cleanup..." +$ANSIBLE_PATH --diff -i ../inventory/cleanup/ monarc-cleanup.yaml --user ansible + +echo "Update the deleted clients if necessary..." +$PYTHON_PATH ./update_cleanup_inventory.py ../inventory/cleanup/ + echo "Synchronizing templates of deliveries..." $PYTHON_PATH ./list_inventory.py ../inventory/ | xargs -n2 ./update_deliveries.sh $BO_ADDRESS diff --git a/playbook/update_cleanup_inventory.py b/playbook/update_cleanup_inventory.py new file mode 100644 index 0000000..0d2a9ba --- /dev/null +++ b/playbook/update_cleanup_inventory.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +import os +import sys +import json +import yaml + +try: + import configparser as configparser +except: + import ConfigParser as configparser + +HOSTS = configparser.ConfigParser(allow_no_value=True) +HOSTS.optionxform = lambda option: option + + +def run(INVENTORY): + if not os.path.exists(INVENTORY): + print("Folder do no exists: {}".format(INVENTORY)) + exit(1) + + fo_servers = [] + try: + HOSTS.read(os.path.join(INVENTORY, "hosts")) + fo_servers = [fo_server for fo_server, _ in HOSTS.items("dev")] + except Exception as e: + exit(1) + + for fo_server in fo_servers: + yaml_file = os.path.join(INVENTORY, "host_vars", fo_server, "generated.yaml") + if not os.path.exists(yaml_file): + continue + with open(yaml_file, "r") as stream: + ymldata = yaml.load(stream, Loader=yaml.FullLoader) + if ymldata is None: + continue + clients_list = ymldata["clients"] + for client in clients_list: + clients_list[client['name']]['isProcessed'] = True + + with open(yaml_file, "w") as stream: + try: + yaml.dump(clients_list, stream) + except yaml.YAMLError as exc: + print(exc) + + +if __name__ == "__main__": + if len(sys.argv) > 1: + INVENTORY = sys.argv[1] + else: + INVENTORY = "../inventory/cleanup/" + run(INVENTORY)