From 684a1317021160c2ddea3db3ca01b611feb137ce Mon Sep 17 00:00:00 2001 From: Anton Smorodskyi Date: Mon, 16 Sep 2024 21:51:04 +0200 Subject: [PATCH] Remove sending notifications about instance leftovers --- ocw/lib/db.py | 3 +-- ocw/lib/emailnotify.py | 16 -------------- ...stance_notified_alter_instance_provider.py | 22 +++++++++++++++++++ ocw/models.py | 1 - ocw/tables.py | 15 ------------- tests/test_db.py | 11 ++-------- 6 files changed, 25 insertions(+), 43 deletions(-) create mode 100644 ocw/migrations/0013_remove_instance_notified_alter_instance_provider.py diff --git a/ocw/lib/db.py b/ocw/lib/db.py index 3358eac4..8931b461 100644 --- a/ocw/lib/db.py +++ b/ocw/lib/db.py @@ -8,7 +8,7 @@ from ocw.apps import getScheduler from webui.PCWConfig import PCWConfig from ..models import Instance, StateChoice, ProviderChoice, CspInfo -from .emailnotify import send_mail, send_leftover_notification +from .emailnotify import send_mail from .azure import Azure from .ec2 import EC2 from .gce import GCE @@ -155,7 +155,6 @@ def update_run() -> None: traceback.format_exc()) auto_delete_instances() - send_leftover_notification() RUNNING = False if not error_occured: LAST_UPDATE = datetime.now(timezone.utc) diff --git a/ocw/lib/emailnotify.py b/ocw/lib/emailnotify.py index d0141613..fb4c7b26 100644 --- a/ocw/lib/emailnotify.py +++ b/ocw/lib/emailnotify.py @@ -31,22 +31,6 @@ def draw_instance_table(objects): return table.draw() -def send_leftover_notification(): - if PCWConfig.has('notify'): - all_instances = Instance.objects - all_instances = all_instances.filter(active=True, age__gt=timedelta(hours=PCWConfig.get_feature_property( - 'notify', 'age-hours'))).exclude(ignore=True) - body_prefix = f"Message from {build_absolute_uri()}\n\n" - # Handle namespaces - for namespace in PCWConfig.get_namespaces_for('notify'): - receiver_email = PCWConfig.get_feature_property('notify', 'to', namespace) - namespace_objects = all_instances.filter(namespace=namespace) - if namespace_objects.filter(notified=False).count() > 0 and receiver_email: - send_mail(f'CSP left overs - {namespace}', - body_prefix + draw_instance_table(namespace_objects), receiver_email=receiver_email) - all_instances.update(notified=True) - - def send_cluster_notification(namespace, clusters): if len(clusters) and PCWConfig.has('notify'): clusters_str = '' diff --git a/ocw/migrations/0013_remove_instance_notified_alter_instance_provider.py b/ocw/migrations/0013_remove_instance_notified_alter_instance_provider.py new file mode 100644 index 00000000..fe2d4945 --- /dev/null +++ b/ocw/migrations/0013_remove_instance_notified_alter_instance_provider.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.9 on 2024-09-16 19:49 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ocw', '0012_rename_vault_namespace_instance_namespace_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='instance', + name='notified', + ), + migrations.AlterField( + model_name='instance', + name='provider', + field=models.CharField(choices=[('GCE', 'Google'), ('EC2', 'EC2'), ('AZURE', 'Azure')], max_length=8), + ), + ] diff --git a/ocw/models.py b/ocw/models.py index 06d1c10f..9fb79bec 100644 --- a/ocw/models.py +++ b/ocw/models.py @@ -32,7 +32,6 @@ class Instance(models.Model): instance_id = models.CharField(max_length=200) region = models.CharField(max_length=64, default='') namespace = models.CharField(max_length=64, default='') - notified = models.BooleanField(default=False) ignore = models.BooleanField(default=False) TAG_IGNORE = 'pcw_ignore' diff --git a/ocw/tables.py b/ocw/tables.py index ab664c42..728c62e3 100644 --- a/ocw/tables.py +++ b/ocw/tables.py @@ -32,20 +32,6 @@ def render(self, record): link['url'], link['title'], link['title'], static('img/openqa.svg')) return "" - -class MailColumn(tables.BooleanColumn): - @property - def header(self): - return "" - - def render(self, value, record, bound_column): - value = self._get_bool_value(record, value, bound_column) - if value: - return format_html('Email notification was send', - static('img/notified.png')) - return "" - - class TagsColumn(tables.TemplateColumn): def __init__(self, template_name=None, **extra): @@ -58,7 +44,6 @@ def header(self): class InstanceTable(tables.Table): tags = TagsColumn() - notified = MailColumn() type = tables.Column(accessor=A('get_type')) first_seen = tables.DateTimeColumn(format='M d Y') last_seen = tables.DateTimeColumn(format='M d Y') diff --git a/tests/test_db.py b/tests/test_db.py index 47e06573..e2728fdc 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -179,16 +179,13 @@ def mocked__update_provider(arg1, arg2, arg3): def mocked_auto_delete_instances(): call_stack.append('auto_delete_instances') - def mocked_send_leftover_notification(): - call_stack.append('send_leftover_notification') monkeypatch.setattr('ocw.lib.db._update_provider', mocked__update_provider) monkeypatch.setattr('ocw.lib.db.auto_delete_instances', mocked_auto_delete_instances) - monkeypatch.setattr('ocw.lib.db.send_leftover_notification', mocked_send_leftover_notification) update_run() - assert call_stack == ['_update_provider', 'auto_delete_instances', 'send_leftover_notification'] + assert call_stack == ['_update_provider', 'auto_delete_instances'] def test_update_run_update_provider_throw_exception(update_run_patch, monkeypatch): @@ -202,20 +199,16 @@ def mocked__update_provider(arg1, arg2, arg3): def mocked_auto_delete_instances(): call_stack.append('auto_delete_instances') - def mocked_send_leftover_notification(): - call_stack.append('send_leftover_notification') - def mocked_send_mail(arg1, arg2): call_stack.append('send_mail') monkeypatch.setattr('ocw.lib.db._update_provider', mocked__update_provider) monkeypatch.setattr('ocw.lib.db.auto_delete_instances', mocked_auto_delete_instances) - monkeypatch.setattr('ocw.lib.db.send_leftover_notification', mocked_send_leftover_notification) monkeypatch.setattr('ocw.lib.db.send_mail', mocked_send_mail) update_run() - assert call_stack == ['_update_provider', 'send_mail', 'auto_delete_instances', 'send_leftover_notification'] + assert call_stack == ['_update_provider', 'send_mail', 'auto_delete_instances'] def test_delete_instances_azure(monkeypatch):