Skip to content

Commit

Permalink
[fix] Fix creation of checks for existing devices #348
Browse files Browse the repository at this point in the history
Fixes #348
  • Loading branch information
nemesifier committed Aug 4, 2022
1 parent 76942a3 commit e151a9c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 56 deletions.
24 changes: 1 addition & 23 deletions openwisp_monitoring/check/migrations/0003_create_ping.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,10 @@
import swapper
from django.db import migrations

from openwisp_monitoring.check.settings import AUTO_PING
from openwisp_monitoring.check.tasks import auto_create_ping


def create_device_ping(apps, schema_editor):
if AUTO_PING:
ContentType = apps.get_model('contenttypes', 'ContentType')
Check = apps.get_model('check', 'Check')
Device = apps.get_model('config', 'Device')
for device in Device.objects.all():
auto_create_ping(
model=Device.__name__.lower(),
app_label=Device._meta.app_label,
object_id=str(device.pk),
check_model=Check,
content_type_model=ContentType,
)


class Migration(migrations.Migration):

dependencies = [
('check', '0001_initial_squashed_0002_check_unique_together'),
swapper.dependency('monitoring', 'Metric'),
]

operations = [
migrations.RunPython(create_device_ping, reverse_code=migrations.RunPython.noop)
]
operations = []
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
from django.db import migrations

from openwisp_monitoring.check.settings import AUTO_CONFIG_CHECK
from openwisp_monitoring.check.tasks import auto_create_config_check


def add_config_applied_checks(apps, schema_editor):
if not AUTO_CONFIG_CHECK:
return
ContentType = apps.get_model('contenttypes', 'ContentType')
Check = apps.get_model('check', 'Check')
Device = apps.get_model('config', 'Device')
for device in Device.objects.all():
auto_create_config_check(
model=Device.__name__.lower(),
app_label=Device._meta.app_label,
object_id=str(device.pk),
check_model=Check,
content_type_model=ContentType,
)


def remove_config_applied_checks(apps, schema_editor):
Check = apps.get_model('check', 'Check')
Metric = apps.get_model('monitoring', 'Metric')
Check.objects.filter(
check='openwisp_monitoring.check.classes.ConfigApplied'
).delete()
Metric.objects.filter(configuration='config_applied').delete()


class Migration(migrations.Migration):

dependencies = [
('check', '0004_rename_active_to_is_active'),
]

operations = [
migrations.RunPython(
add_config_applied_checks, reverse_code=remove_config_applied_checks
)
]
operations = []
62 changes: 62 additions & 0 deletions openwisp_monitoring/check/migrations/0007_create_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import swapper
from django.db import migrations

from openwisp_monitoring.check.settings import AUTO_CONFIG_CHECK, AUTO_PING
from openwisp_monitoring.check.tasks import auto_create_config_check, auto_create_ping


def create_ping_checks(apps, schema_editor):
if AUTO_PING:
ContentType = apps.get_model('contenttypes', 'ContentType')
Check = apps.get_model('check', 'Check')
Device = apps.get_model('config', 'Device')
for device in Device.objects.all():
auto_create_ping(
model=Device.__name__.lower(),
app_label=Device._meta.app_label,
object_id=str(device.pk),
check_model=Check,
content_type_model=ContentType,
)


def create_config_applied_checks(apps, schema_editor):
if not AUTO_CONFIG_CHECK:
return
ContentType = apps.get_model('contenttypes', 'ContentType')
Check = apps.get_model('check', 'Check')
Device = apps.get_model('config', 'Device')
for device in Device.objects.all():
auto_create_config_check(
model=Device.__name__.lower(),
app_label=Device._meta.app_label,
object_id=str(device.pk),
check_model=Check,
content_type_model=ContentType,
)


def remove_config_applied_checks(apps, schema_editor):
Check = apps.get_model('check', 'Check')
Metric = apps.get_model('monitoring', 'Metric')
Check.objects.filter(
check='openwisp_monitoring.check.classes.ConfigApplied'
).delete()
Metric.objects.filter(configuration='config_applied').delete()


class Migration(migrations.Migration):

dependencies = [
('check', '0006_rename_check_check_check_type'),
swapper.dependency('monitoring', 'Metric'),
]

operations = [
migrations.RunPython(
create_ping_checks, reverse_code=migrations.RunPython.noop
),
migrations.RunPython(
create_config_applied_checks, reverse_code=remove_config_applied_checks
),
]

0 comments on commit e151a9c

Please sign in to comment.