-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmimages.py
More file actions
55 lines (38 loc) · 1.72 KB
/
rmimages.py
File metadata and controls
55 lines (38 loc) · 1.72 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
import sys, requests, json, configparser, os
def main(argv):
if len(sys.argv) > 3:
delete_repository_by_name(token=sys.argv[1], name=sys.argv[2], project_id=sys.argv[3])
else:
print("Not all parameters are passed. The correct call format: rm_images.py private-token name_image project_id")
def delete_repository_by_name(name, project_id):
config_gitLab = get_conf_params()['Gitlab']
repositories = requests.get(
"{0}/api/v4/projects/{1}/registry/repositories".format(config_gitLab['gitlab_url'], project_id),
headers={'PRIVATE-TOKEN': config_gitLab['token']}
).json()
if 'message' in repositories and repositories['message'] == '404 Project Not Found':
print("Check the access rights of the token owner. Add ci user to members current project")
exit()
need_repositories = list(filter(lambda x: x["name"] == name, repositories))
if need_repositories == []:
print('Repository not exists, or already removed')
exit()
repository_id = need_repositories[0]['id']
delete_response = requests.delete(
"{0}/api/v4/projects/{1}/registry/repositories/{2}".format(config_gitLab['gitlab_url'], project_id, str(repository_id)),
headers={'PRIVATE-TOKEN': config_gitLab['token']}).json()
if delete_response == 202:
print('Success. Repository removed')
else:
print('Errors: ' + json.dumps(delete_response))
def get_conf_params():
try:
f = open('.env')
f.close()
except FileNotFoundError:
print('File .env does not exist.')
config = configparser.ConfigParser()
config.read(".env")
return config
if __name__ == "__main__":
main(sys.argv)