From e2c51d067e08c701cb02996fe9e00d3a58dd5607 Mon Sep 17 00:00:00 2001 From: forest Date: Thu, 21 May 2020 21:40:41 -0500 Subject: [PATCH] implement ensure_vms_and_db_are_synced in cron task and add default sender to readme --- README.md | 1 + capsulflask/__init__.py | 1 + capsulflask/cli.py | 35 +++++++++++++++++++++++++++++++++-- capsulflask/console.py | 1 - capsulflask/db_model.py | 4 ++-- capsulflask/virt_model.py | 2 +- 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 51483bf..325f6c6 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ nano .env Enter your SMTP credentials like this: ``` MAIL_USERNAME=forest@nullhex.com +MAIL_DEFAULT_SENDER=forest@nullhex.com MAIL_PASSWORD=************** ``` diff --git a/capsulflask/__init__.py b/capsulflask/__init__.py index da4ebd7..715bf6d 100644 --- a/capsulflask/__init__.py +++ b/capsulflask/__init__.py @@ -21,6 +21,7 @@ app.config.from_mapping( SECRET_KEY=os.environ.get("SECRET_KEY", default="dev"), VIRTUALIZATION_MODEL=os.environ.get("VIRTUALIZATION_MODEL", default="mock"), LOG_LEVEL=os.environ.get("LOG_LEVEL", default="INFO"), + ADMIN_EMAIL_ADDRESSES=os.environ.get("ADMIN_EMAIL_ADDRESSES", default="ops@cyberia.club"), DATABASE_URL=os.environ.get("DATABASE_URL", default="sql://postgres:dev@localhost:5432/postgres"), DATABASE_SCHEMA=os.environ.get("DATABASE_SCHEMA", default="public"), diff --git a/capsulflask/cli.py b/capsulflask/cli.py index fc6f024..9c45f7c 100644 --- a/capsulflask/cli.py +++ b/capsulflask/cli.py @@ -243,5 +243,36 @@ def notify_users_about_account_balance(): def ensure_vms_and_db_are_synced(): - db_ids = get_model().all_vm_ids() - #virt_ids = current_app.config["VIRTUALIZATION_MODEL"]. \ No newline at end of file + db_ids = get_model().all_non_deleted_vm_ids() + virt_ids = current_app.config["VIRTUALIZATION_MODEL"].list_ids() + + db_ids_dict = dict() + virt_ids_dict = dict() + + for id in db_ids: + db_ids_dict[id] = True + + for id in virt_ids: + virt_ids_dict[id] = True + + errors = list() + + for id in db_ids_dict: + if id not in virt_ids_dict: + errors.append(f"{id} is in the database but not in the virtualization model") + + for id in virt_ids_dict: + if id not in db_ids_dict: + errors.append(f"{id} is in the virtualization model but not in the database") + + if len(errors) > 0: + email_addresses_raw = current_app.config['ADMIN_EMAIL_ADDRESSES'].split(",") + email_addresses = list(filter(lambda x: len(x) > 6, map(lambda x: x.strip(), email_addresses_raw ) )) + current_app.config["FLASK_MAIL_INSTANCE"].send( + Message( + "Capsul Consistency Check Failed", + body="\n".join(errors), + recipients=email_addresses + ) + ) + diff --git a/capsulflask/console.py b/capsulflask/console.py index 94eeaa4..7d949ed 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -10,7 +10,6 @@ from flask import session from flask import render_template from flask import redirect from flask import url_for -from flask_mail import Message from werkzeug.exceptions import abort from nanoid import generate diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 32fb779..7dd1383 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -33,8 +33,8 @@ class DBModel: return email return None - def all_vm_ids(self,): - self.cursor.execute("SELECT id FROM vms") + def all_non_deleted_vm_ids(self,): + self.cursor.execute("SELECT id FROM vms WHERE deleted IS NULL") return list(map(lambda x: x[0], self.cursor.fetchall())) def operating_systems_dict(self,): diff --git a/capsulflask/virt_model.py b/capsulflask/virt_model.py index 4b0bce7..51db58a 100644 --- a/capsulflask/virt_model.py +++ b/capsulflask/virt_model.py @@ -43,7 +43,7 @@ class MockVirtualization(VirtualizationInterface): return VirtualMachine(id, ipv4="1.1.1.1") def list_ids(self) -> list: - return get_model().all_vm_ids() + return get_model().all_non_deleted_vm_ids() def create(self, email: str, id: str, template_image_file_name: str, vcpus: int, memory_mb: int, ssh_public_keys: list): validate_capsul_id(id)