Merge pull request 'notify staff when new instance is deployed' (#61) from alert-staff-new-instance into master
ci/woodpecker/push/woodpecker Pipeline was successful Details

Reviewed-on: https://gitea.gna.org/Hostea/dashboard/pulls/61
pull/67/head
Loïc Dachary 2022-09-14 02:48:21 -04:00
commit b5afc49f5d
5 changed files with 60 additions and 2 deletions

View File

@ -59,7 +59,7 @@ class Command(BaseCommand):
sender = settings.DEFAULT_FROM_EMAIL
send_mail(
subject="[Gna!] Payment receipt your Hostea VM",
subject="[Gna!] Payment receipt your Gna! VM",
message=body,
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
recipient_list=[email],

View File

@ -128,7 +128,7 @@ def payment_success(request, payment_public_id):
sender = settings.DEFAULT_FROM_EMAIL
send_mail(
subject="[Gna!] Payment receipt your Hostea VM",
subject="[Gna!] Payment receipt your Gna! VM",
message=body,
from_email=f"No reply Gna!<{sender}>", # TODO read from settings.py
recipient_list=[email],

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