Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the process to automatically remove the clients data #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions playbook/del_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions playbook/monarcfo-cleanup/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions playbook/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions playbook/update_cleanup_inventory.py
Original file line number Diff line number Diff line change
@@ -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)