Skip to content

Commit

Permalink
Remove sending notifications about instance leftovers
Browse files Browse the repository at this point in the history
  • Loading branch information
asmorodskyi committed Sep 16, 2024
1 parent c2e7b10 commit 684a131
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 43 deletions.
3 changes: 1 addition & 2 deletions ocw/lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 0 additions & 16 deletions ocw/lib/emailnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
1 change: 0 additions & 1 deletion ocw/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
15 changes: 0 additions & 15 deletions ocw/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<img alt="Email notification was send" src="{}" width=20 height=20/>',
static('img/notified.png'))
return ""


class TagsColumn(tables.TemplateColumn):

def __init__(self, template_name=None, **extra):
Expand All @@ -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')
Expand Down
11 changes: 2 additions & 9 deletions tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 684a131

Please sign in to comment.