feat: alert staff on new instance creation

closes: https://gitea.gna.org/Hostea/dashboard/issues/52
wip-gitea
Hostea dashboard 2022-09-13 19:57:37 +05:30
parent 9d2a53f2e2
commit 86fe9a2c59
Signed by untrusted user who does not match committer: realaravinth
GPG Key ID: AD9F0F08E855ED88
3 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,10 @@
Hello {{ username }}!,
A customer has purchased a new instance. Please find the details below:
Gitea: {{ gitea_uri }}
Woodpecker CI: {{ woodpecker_uri }}
Cheers,
Gna! team

View File

@ -17,6 +17,7 @@ from io import StringIO
from django.test import TestCase, Client, override_settings
from django.core.management import call_command
from django.core import mail
from dash.models import Instance, InstanceConfiguration
from accounts.tests import register_util, login_util
@ -37,6 +38,8 @@ class InfraUtilTest(TestCase):
def setUp(self):
self.username = "infrautil_user"
register_util(t=self, username=self.username)
self.user.is_staff = True
self.user.save()
create_configurations(t=self)
# @override_settings(HOSTEA=infra_custom_config(test_name="test_path_util"))
@ -226,3 +229,18 @@ class InfraUtilTest(TestCase):
print(out)
self.assertEqual(instance_name in out, True)
self.assertEqual(f"Payment not fulfilled for instance: {instance}" in out, True)
staff_notification = None
for m in mail.outbox:
if "New instance alert" in m.subject:
staff_notification = m
break
self.assertEqual(staff_notification.to[0], self.email)
self.assertEqual(
"[Gna!] New instance alert" in staff_notification.subject, True
)
self.assertEqual(
"A customer has purchased a new instance. Please find the details below:"
in staff_notification.body,
True,
)

View File

@ -24,6 +24,7 @@ from time import sleep
from django.utils.crypto import get_random_string
from django.template.loader import render_to_string
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
from django.conf import settings
from payments import get_payment_model
@ -83,10 +84,38 @@ def create_vm_if_not_exists(instance: Instance) -> (str, str):
"""
Create VM utility. Gitea password is returned
"""
def notify_staff(instance: Instance):
infra = Infra()
User = get_user_model()
gitea_uri = Infra.get_gitea_uri(instance=instance)
woodpecker = Infra.get_woodpecker_uri(instance=instance)
for staff in User.objects.filter(is_staff=True):
ctx = {
"gitea_uri": gitea_uri,
"woodpecker_uri": woodpecker,
"username": staff.username,
}
body = render_to_string(
"infrastructure/emails/staff-new-instance-alert.txt",
context=ctx,
)
sender = settings.DEFAULT_FROM_EMAIL
send_mail(
subject="[Gna!] New instance alert",
message=body,
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
recipient_list=[staff.email],
)
infra = Infra()
if not InstanceCreated.objects.filter(instance=instance).exists():
(gitea_password, commit) = infra.add_vm(instance=instance)
InstanceCreated.objects.create(instance=instance, created=True)
notify_staff(instance=instance)
job = Job.objects.create(instance=instance, job_type=str(JobType.PING))
Worker(job=job).start()
return (gitea_password, commit)
@ -95,6 +124,7 @@ def create_vm_if_not_exists(instance: Instance) -> (str, str):
infra.translate_size(instance=instance)
):
# Worker.init_global()
notify_staff(instance=instance)
return infra.add_vm(instance=instance)
return None