-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from innogames/migration_log_cleanup
serveradmin(resources): add management command for migration log cleanup
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
serveradmin/resources/management/commands/clear_migration_log.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""Serveradmin - igvm_migration_log cleanup | ||
We are using igvm_migration_log for calculating the overall CPU usage of a | ||
Hypervisor. This is obsolete after the next prime time because the cpu_util_pct | ||
will be correct again. This task will get executed once in the morning to | ||
clean up the log entries from the previous day. | ||
Copyright (c) 2020 InnoGames GmbH | ||
""" | ||
from datetime import datetime, timedelta, timezone | ||
from django.core.management.base import BaseCommand | ||
|
||
from adminapi.dataset import Query | ||
from adminapi.filters import Not, Empty | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Deletes igvm_migration_log entries from previous day' | ||
|
||
def handle(self, *args, **options): | ||
"""Entry point for command | ||
get all hypervisors which have an entry in igvm_migration_log | ||
and delete all entries which are from the previous day. | ||
""" | ||
|
||
all_hypervisors = Query({'igvm_migration_log': Not(Empty()), | ||
'servertype': 'hypervisor'}, | ||
['igvm_migration_log', 'hostname']) | ||
|
||
now = datetime.now(tz=timezone.utc) | ||
|
||
for hypervisor in all_hypervisors: | ||
|
||
for entry in hypervisor['igvm_migration_log']: | ||
timestamp = entry.split()[0] | ||
|
||
dt_object_sa = datetime.fromtimestamp( | ||
int(timestamp), | ||
tz=timezone.utc) | ||
|
||
if dt_object_sa.date() == now.date(): | ||
continue | ||
|
||
hypervisor['igvm_migration_log'].discard(entry) | ||
hypervisor.commit() | ||
self.stdout.write(self.style.SUCCESS( | ||
'Delete old entries from {} '.format( | ||
hypervisor['hostname']))) |