This repository was archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_outdated_wall_e_resources.py
More file actions
executable file
·140 lines (123 loc) · 4.87 KB
/
clear_outdated_wall_e_resources.py
File metadata and controls
executable file
·140 lines (123 loc) · 4.87 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python3
import json
import re
import subprocess
import sys
import time
import requests
# commands to run to setup python on jenkins docker container
# sudo apt-get install -y python3
# sudo apt-get install wget
# wget https://bootstrap.pypa.io/get-pip.py
# sudo apt install python3-distutils
# python3 get-pip.py
DISCORD_TOKEN = sys.argv[1]
header = {
"Authorization": f"Bot {DISCORD_TOKEN}"
}
DISCORD_CHANNEL_SUFFIXES = ["_logs", "_bot_channel", "_council", "_mod_channel", "_announcements"]
_, output = subprocess.getstatusoutput("docker images 'test_*'")
docker_image_names = {
docker_image_name.split(" ")[0].strip(): docker_image_name.split(" ")[-3].strip()
for docker_image_name in output.split("\n")[1:]
}
_, output = subprocess.getstatusoutput("docker ps -a --filter 'name=TEST_' --format 'table {{.Names}}'")
docker_container_names = output.split("\n")[1:]
current_prs = [
f"pr-{pr['number']}"
for pr in json.loads(requests.get("https://api.github.com/repos/CSSS/wall_e/pulls?state=open").text)
]
current_branches = [
branch['name']
for branch in json.loads(requests.get("https://api.github.com/repos/CSSS/wall_e/branches").text)
]
branches_to_remove = []
channels_to_keep = ["Text Channels", "Voice Channels", "general", "observation-room", "builds"]
for docker_container_name in docker_container_names:
print(f"iterating through container name [{docker_container_name}]")
if re.search(r'_wall_e_db$', docker_container_name):
print(f"processing container name {docker_container_name}")
branch_name = docker_container_name[5:-10]
branch_name_lower_case = branch_name.lower()
print(f"branch is set to [{branch_name}]")
if branch_name not in current_prs and branch_name not in current_branches:
print(f"branch [{branch_name}] is valid to remove")
branches_to_remove.append(branch_name)
else:
channels_to_keep.append(branch_name_lower_case)
channels_to_keep.extend([
f"{branch_name_lower_case}{discord_channel_suffix}"
for discord_channel_suffix in DISCORD_CHANNEL_SUFFIXES
])
successful = False
retry_limit = 5
attempt = 0
guild_id = None
while (not successful) and (attempt < retry_limit):
url = "https://discordapp.com/api/users/@me/guilds"
response = requests.get(url, headers=header)
status_code = response.status_code
response_body = json.loads(response.text)
if status_code == 200:
successful = True
guild_id = response_body[0]['id']
elif status_code == 429:
attempt += 1
print(f"GET {url}")
print(response_body.get("message", None))
retry_after = response_body.get('retry_after', None)
if retry_after is not None:
time.sleep(float(retry_after))
else:
print(f"GET {url}")
print(json.dumps(response.json(), indent=4))
attempt += 1
time.sleep(5)
successful = False
retry_limit = 5
attempt = 0
channels = []
while (not successful) and (attempt < retry_limit):
url = f"https://discordapp.com/api/guilds/{guild_id}/channels"
response = requests.get(url, headers=header)
status_code = response.status_code
response_body = json.loads(response.text)
if status_code == 200:
successful = True
channels = response_body
elif status_code == 429:
attempt += 1
print(f"GET {url}")
print(response_body.get("message", None))
retry_after = response_body.get('retry_after', None)
if retry_after is not None:
time.sleep(float(retry_after))
else:
print(f"GET {url}")
print(json.dumps(response.json(), indent=4))
attempt += 1
time.sleep(5)
for channel in channels:
channel_name = channel.get("name", None)
if channel_name not in channels_to_keep:
url = f"https://discordapp.com/api/channels/{channel['id']}"
response = requests.delete(url, headers=header)
print(f"DELETE {url}")
print(json.dumps(response.json(), indent=4))
for branch_to_remove in branches_to_remove:
with open("clear_docker_resources.sh", "w") as f:
f.write(f"""
#!/bin/bash
export COMPOSE_PROJECT_NAME="TEST_{branch_to_remove}"
export container_name=$(echo "${{COMPOSE_PROJECT_NAME}}"_wall_e)
export db_container_name=$(echo "${{COMPOSE_PROJECT_NAME}}"_wall_e_db)
export image_name=$(echo "${{COMPOSE_PROJECT_NAME}}"_wall_e | awk '{{print tolower($0)}}')
export network_name=$(echo "${{COMPOSE_PROJECT_NAME}}"_wall_e_network | awk '{{print tolower($0)}}')
export volume_name="${{COMPOSE_PROJECT_NAME}}_logs"
docker rm -f "${{container_name}}" "${{db_container_name}}"
docker volume rm "${{volume_name}}" || true
docker image rm "${{image_name}}" || true
docker network rm "${{network_name}}" || true
""")
_, output = subprocess.getstatusoutput("bash clear_docker_resources.sh")
print(output)