-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdelete_rf_stub.py
135 lines (117 loc) · 3.96 KB
/
delete_rf_stub.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
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
""" Helper script to delete rfstub tags """
import logging
import os
import time
import docker
import requests
class RegistryHelper:
""" Docker registry helper base class"""
def __init__(self, docker_client, username, password):
self.username = username
self.password = password
self.docker_client = docker_client
class DockerHubHelper(RegistryHelper):
""" Implement dockerhub helper """
BASE_URL = "https://registry.hub.docker.com"
def __init__(self, docker_client):
username = os.environ.get("DOCKERHUB_USERNAME")
password = os.environ.get("DOCKERHUB_PASSWORD")
super(__class__, self).__init__(
docker_client, username, password)
@staticmethod
def registry_url():
""" registry url """
return "docker.io"
def get_rfstub_tag(self, account, repo):
"""
Find rfstub tags using search_str"
"""
tags = self._fetch_tags(account, repo)
tags = list(filter(
lambda tag: "rfstub" in tag["name"],
tags))
return map(lambda x:x.get("name"), tags)
def _fetch_tags(self, account, repo):
"""
Get tags from the dockerhub registry API
"""
tags = []
url = f"{self.BASE_URL}/v2/repositories/{account}/{repo}/tags?page_size=25"
if account == "_":
# Handle official repository here
url = f"{self.BASE_URL}/v2/repositories/{repo}/tags?page_size=25"
while url:
resp = requests.get(url, timeout=60)
logging.debug(f"url : {url}, {resp.status_code}, {resp.text}")
if 200 <= resp.status_code < 300:
tag_objs = resp.json()
tags += tag_objs.get("results", [])
url = tag_objs.get("next")
else:
break
return tags
def get_auth_header(self):
""" get auth header for JWT """
login_url = f"{self.BASE_URL}/v2/users/login"
resp = requests.post(
login_url,
json={
"username": self.username,
"password": self.password
},
timeout=30
)
if resp.status_code == 200:
resp_json = resp.json()
logging.debug(resp_json)
token = resp_json["token"]
return {"Authorization": f"JWT {token}"}
return {}
def delete_tag(self, account, repo, tag):
""" delete tags """
del_url = f"{self.BASE_URL}/v2/repositories/{account}/{repo}/tags/{tag}/"
auth_header = self.get_auth_header()
resp = requests.delete(del_url, headers=auth_header, timeout=30)
print(resp.status_code, resp.text, del_url)
time.sleep(0.1)
return resp.status_code == 200
if __name__ == "__main__":
dc = docker.from_env()
dh = DockerHubHelper(dc)
repos_to_del = ['airflow',
'airflow-scheduler',
'airflow-worker',
'apache',
'apache-official',
'consul',
'curl',
'envoy',
'etcd',
'fluentd',
'haproxy',
'influxdb',
'kong',
'mariadb',
'mariadb-ib',
'memcached',
'mongodb',
'mysql',
'mysql8-ib',
'mysql-official',
'nats',
'nginx',
'nginx-ib',
'nginx-official',
'oncall',
'postgresql',
'postgresql12-ib',
'prometheus',
'rabbitmq',
'redis-cluster',
'redis6-ib',
'redis-official',
'zookeeper']
for repo_to_del in repos_to_del:
del_tags = dh.get_rfstub_tag("rapidfort", repo_to_del)
for del_tag in del_tags:
dh.delete_tag("rapidfort", repo_to_del, del_tag)