Browse Source

implement ensure_vms_and_db_are_synced in cron task and add default

sender to readme
forest-wip
forest 2 years ago
parent
commit
e2c51d067e
  1. 1
      README.md
  2. 1
      capsulflask/__init__.py
  3. 35
      capsulflask/cli.py
  4. 1
      capsulflask/console.py
  5. 4
      capsulflask/db_model.py
  6. 2
      capsulflask/virt_model.py

1
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=**************
```

1
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"),

35
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"].
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
)
)

1
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

4
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,):

2
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)

Loading…
Cancel
Save