|
| 1 | +#!/usr/bin/env python |
| 2 | +# pylint: disable=invalid-name |
| 3 | + |
| 4 | +"""Deletes all instances from the DM namespace that are considered 'old'. |
| 5 | +
|
| 6 | +This utility checks all projects the user has access to |
| 7 | +and then removes instances that are found. The assumption here |
| 8 | +is that the user has admin rights. |
| 9 | +""" |
| 10 | +import argparse |
| 11 | +from datetime import datetime, timedelta |
| 12 | +from typing import List, Optional, Tuple |
| 13 | +import urllib3 |
| 14 | + |
| 15 | +from dateutil.parser import parse |
| 16 | +from squonk2.auth import Auth |
| 17 | +from squonk2.dm_api import DmApi, DmApiRv |
| 18 | + |
| 19 | +from common import Env, get_env |
| 20 | + |
| 21 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 22 | + |
| 23 | + |
| 24 | +def main(c_args: argparse.Namespace) -> None: |
| 25 | + |
| 26 | + env: Optional[Env] = get_env() |
| 27 | + if not env: |
| 28 | + return |
| 29 | + |
| 30 | + token: str = Auth.get_access_token( |
| 31 | + keycloak_url=env.keycloak_url, |
| 32 | + keycloak_realm=env.keycloak_realm, |
| 33 | + keycloak_client_id=env.keycloak_dm_client_id, |
| 34 | + username=env.keycloak_user, |
| 35 | + password=env.keycloak_user_password, |
| 36 | + ) |
| 37 | + |
| 38 | + # To see everything we need to become admin... |
| 39 | + rv: DmApiRv = DmApi.set_admin_state(token, admin=True) |
| 40 | + assert rv.success |
| 41 | + |
| 42 | + # Max age? |
| 43 | + max_stopped_age: timedelta = timedelta(hours=args.age) |
| 44 | + |
| 45 | + # The collection of instances |
| 46 | + old_instances: List[Tuple[str, str]] = [] |
| 47 | + |
| 48 | + p_rv = DmApi.get_available_instances(token) |
| 49 | + now: datetime = datetime.utcnow() |
| 50 | + if p_rv.success: |
| 51 | + for instance in p_rv.msg['instances']: |
| 52 | + i_id: str = instance['id'] |
| 53 | + i_rv = DmApi.get_instance(token, instance_id=i_id) |
| 54 | + if i_rv.success and 'stopped' in i_rv.msg: |
| 55 | + i_stopped: datetime = parse(i_rv.msg['stopped']) |
| 56 | + i_stopped_age: timedelta = now - i_stopped |
| 57 | + if i_stopped_age >= max_stopped_age: |
| 58 | + i_name: str = i_rv.msg['name'] |
| 59 | + print(f"+ Found instance '{i_name}' [{i_id}] (Stopped {i_stopped_age})") |
| 60 | + old_instances.append((i_id, i_rv.msg['owner'])) |
| 61 | + |
| 62 | + num_deleted: int = 0 |
| 63 | + num_failed: int = 0 |
| 64 | + if c_args.do_it: |
| 65 | + print("Deleting...") |
| 66 | + for i_id, i_owner in old_instances: |
| 67 | + # To delete we need to impersonate the owner of the instance... |
| 68 | + rv = DmApi.set_admin_state(token, admin=True, impersonate=i_owner) |
| 69 | + assert rv.success |
| 70 | + rv = DmApi.delete_instance(token, instance_id=i_id) |
| 71 | + if rv.success: |
| 72 | + num_deleted += 1 |
| 73 | + else: |
| 74 | + num_failed += 1 |
| 75 | + print("Deleted") |
| 76 | + |
| 77 | + # Revert to a non-admin state |
| 78 | + # To see everything we need to become admin... |
| 79 | + rv = DmApi.set_admin_state(token, admin=False) |
| 80 | + assert rv.success |
| 81 | + |
| 82 | + print(f"Found {len(old_instances)}") |
| 83 | + print(f"Deleted {num_deleted}") |
| 84 | + print(f"Failed to deleted {num_failed}") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + # Build a command-line parser and parse it... |
| 89 | + parser = argparse.ArgumentParser( |
| 90 | + description='Delete All Old DM Project Instances') |
| 91 | + parser.add_argument( |
| 92 | + '--age', |
| 93 | + nargs='?', |
| 94 | + default=96, |
| 95 | + type=int, |
| 96 | + help='Age (hours) when an instance is considered "old"', |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + '--do-it', |
| 100 | + help='Set to actually delete, if not set the old instances are listed', |
| 101 | + action='store_true', |
| 102 | + ) |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + main(args) |
0 commit comments