Skip to content

Commit b4db278

Browse files
author
Alan Christie
committed
fix: template
Adds delete-old-instances Support for client 1.7.1 (env-based SSL verification control)
1 parent ace4f51 commit b4db278

File tree

6 files changed

+118
-2
lines changed

6 files changed

+118
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ display the tool's help.
4242
You should find the following tools in this repository: -
4343

4444
- `delete-all-instances`
45+
- `delete-old-instances`
4546
- `delete-test-projects`
4647

4748
---

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
im-squonk2-client ~= 1.0
2+
python-dateutil == 2.8.2
3+
rich == 12.5.1

setenv-template.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
# Copy it to setenv.sh, edit and install with
44
# "source setenv.sh".
55
export SQUONK2_ASAPI_URL="https://example.com/account-server-api"
6+
export SQUONK2_ASAPI_VERIFY_SSL_CERT="yes"
67
export SQUONK2_DMAPI_URL="https://example.com/data-manager-api"
8+
export SQUONK2_DMAPI_VERIFY_SSL_CERT="yes"
79
export SQUONK2_KEYCLOAK_URL="https://example.com/auth"
810
export SQUONK2_KEYCLOAK_REALM="squonk"
9-
export SQUONK2_KEYCLOAK_AS_CLIENT_ID="data-manager-api-test"
10-
export SQUONK2_KEYCLOAK_DM_CLIENT_ID="account-server-api-test"
11+
export SQUONK2_KEYCLOAK_AS_CLIENT_ID="account-server-api-test"
12+
export SQUONK2_KEYCLOAK_DM_CLIENT_ID="data-manager-api-test"
1113
export SQUONK2_KEYCLOAK_USER="dmit-user-admin"
1214
export SQUONK2_KEYCLOAK_USER_PASSWORD="SetMe"

tools/delete-all-instances.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
"""
1313
import argparse
1414
from typing import Dict, List, Optional, Tuple
15+
import urllib3
1516

1617
from common import Env, get_env
1718

1819
from squonk2.auth import Auth
1920
from squonk2.dm_api import DmApi, DmApiRv
2021

22+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
23+
2124

2225
def main(c_args: argparse.Namespace) -> None:
2326

tools/delete-old-instances.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)

tools/delete-test-projects.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
"""
66
import argparse
77
from typing import Any, Dict, List, Optional
8+
import urllib3
89

910
from common import Env, get_env, TEST_UNIT, TEST_USER_NAMES
1011

1112
from squonk2.auth import Auth
1213
from squonk2.dm_api import DmApi, DmApiRv
1314

15+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
16+
1417

1518
def main(c_args: argparse.Namespace) -> None:
1619
"""Main function."""

0 commit comments

Comments
 (0)