|
| 1 | +# Copyright 2021 ETH Zurich |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +""" |
| 17 | +:mod: scripts.deactivate_dormant |
| 18 | +========================= |
| 19 | +
|
| 20 | +Run with python manage.py runscript deactivate_dormant --script-args <list of inactive ases.txt> |
| 21 | +
|
| 22 | +Bulk deactivate inactive ASes for users who have not logged in for over a year. |
| 23 | +Reads a list ASes that have been inactive for a long time, created from data of the monitoring |
| 24 | +system (by inspecting router_interface_up at the attachment points). |
| 25 | +Expects newline separated AS ids, in the form "ffaa:1:abc". |
| 26 | +""" |
| 27 | + |
| 28 | +import datetime |
| 29 | +import pathlib |
| 30 | +from django.db import transaction |
| 31 | +from django.db.models import Q |
| 32 | + |
| 33 | +from scionlab.models.user_as import UserAS, AttachmentPoint |
| 34 | + |
| 35 | +nologin_threshold = datetime.timedelta(days=365) |
| 36 | + |
| 37 | + |
| 38 | +def run(*args): |
| 39 | + if len(args) != 1: |
| 40 | + print("run with --script-args <list of inactive user ases.txt>") |
| 41 | + return |
| 42 | + filename = args[0] |
| 43 | + as_ids = pathlib.Path(filename).read_text().splitlines() |
| 44 | + deactivate_dormant(as_ids) |
| 45 | + |
| 46 | + |
| 47 | +@transaction.atomic |
| 48 | +def deactivate_dormant(as_ids): |
| 49 | + last_login_before = datetime.datetime.utcnow() - nologin_threshold |
| 50 | + q = UserAS.objects.filter( |
| 51 | + Q(as_id__in=as_ids), |
| 52 | + Q(owner__last_login__lt=last_login_before) | Q(owner__last_login=None), |
| 53 | + ) |
| 54 | + for user_as in q: |
| 55 | + print(user_as.as_id) |
| 56 | + user_as.update_active(False) |
| 57 | + |
| 58 | + # Fix-up distribution of user AS interfaces to router instances. |
| 59 | + for ap in AttachmentPoint.objects.all(): |
| 60 | + ap.split_border_routers() |
0 commit comments