-
Notifications
You must be signed in to change notification settings - Fork 32
/
cleanup_agents.py
92 lines (77 loc) · 3.02 KB
/
cleanup_agents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import os
import sys
from base64 import b64encode
import urllib3
from loguru import logger
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
try:
import requests
except ModuleNotFoundError as e:
logger.error("No module 'requests' found. Install: pip install requests")
sys.exit(1)
older_than = "21d"
def code_desc(http_status_code):
return requests.status_codes._codes[http_status_code][0]
def req(method, resource, data=None):
login_headers = {
"Content-Type": "application/json",
"Authorization": f"Basic {b64encode(auth).decode()}",
}
response = requests.get(login_url, headers=login_headers, verify=False) # nosec
token = json.loads(response.content.decode())["data"]["token"]
requests_headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
url = f"{base_url}/{resource}"
try:
requests.packages.urllib3.disable_warnings()
if method.lower() == "post":
r = requests.post(
url, headers=requests_headers, data=json.dumps(data), verify=verify
)
elif method.lower() == "put":
r = requests.put(url, headers=requests_headers, data=data, verify=verify)
elif method.lower() == "delete":
r = requests.delete(url, headers=requests_headers, data=data, verify=verify)
else:
r = requests.get(url, headers=requests_headers, params=data, verify=verify)
code = r.status_code
res_json = r.json()
except Exception as exception:
logger.error(f"Error: {resource} {exception}")
sys.exit(1)
return code, res_json
def cleanup_agent(older):
status_code, response = req(
"delete",
f"agents?pretty=true&older_than={older}&agents_list=all&status=never_connected,"
f"disconnected",
)
for items in response["data"]["affected_items"]:
status_code, response = req(
"delete",
f"agents?pretty=true&older_than=0s&agents_list={items['id']}&status=all",
)
msg = json.dumps(response, indent=4, sort_keys=True)
code = f"Status: {status_code} - {code_desc(status_code)}"
logger.error(f"INFO - DELETE AGENT:\n{code}\n{msg}")
if __name__ == "__main__":
try:
host = os.environ.get("JOIN_MANAGER_MASTER_HOST")
port = os.environ.get("JOIN_MANAGER_API_PORT")
protocol = os.environ.get("JOIN_MANAGER_PROTOCOL")
user = os.environ.get("JOIN_MANAGER_USER")
password = os.environ.get("JOIN_MANAGER_PASSWORD")
node_name = os.environ.get("NODE_NAME")
older_than = os.environ.get("OLDER_THAN")
login_endpoint = "security/user/authenticate"
verify = False
base_url = f"{protocol}://{host}:{port}"
login_url = f"{protocol}://{host}:{port}/{login_endpoint}"
auth = f"{user}:{password}".encode()
cleanup_agent(older_than)
except KeyError as error:
logger.error(f"Please check system variable {error}")
exit(2)