forked from karpulix/gitlab-scan-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan-registry.py
146 lines (108 loc) Β· 3.61 KB
/
scan-registry.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
136
137
138
139
140
141
142
143
144
145
146
import os
import sys
import csv
import math
import getopt
import dotenv
import gitlab
import logging
import progressbar
# methods
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
# variables
version = "0.8"
output_file = ""
# init
dotenv.load_dotenv()
logging.basicConfig(level=logging.INFO, format='%(message)s')
# .env
# gitlab_token = os.getenv('GITLAB_TOKEN',"")
# gitlab_uri = os.getenv('GITLAB_URI',"")
# args
opts, args = getopt.getopt(sys.argv[1:],"hi:o:v",["ifile=","ofile=", "help", "version"])
for opt, arg in opts:
if opt in ("-h", "--help"):
print ('''
scan-registry <options>
Options:
-h, --help - get help
-o, --ofile - output CSV file
-v, --version - output version and settings of the script
You should have variables in .env file (in context directory) or in the environment:
GITLAB_URI=https://<host-of-gitlab-instans>
GITLAB_TOKEN=<gitlab-token>
(!) environment has priority
''')
exit(0)
if opt in ("-v", "--version"):
logging.info('π¦ registry-scanner %s', version)
logging.info('π¦ GITLAB_URI: "%s"', os.getenv('GITLAB_URI',""))
logging.info('π¦ GITLAB_TOKEN: "%s"', "***" if os.getenv('GITLAB_TOKEN',"") else "")
exit(0)
elif opt in ("-o", "--ofile"):
output_file = arg
logging.info('πΎ Result will be saved as CSV to "%s" file', output_file)
gl = gitlab.Gitlab(os.getenv('GITLAB_URI',""), os.getenv('GITLAB_TOKEN',""))
gl.auth()
# gl.enable_debug()
projects = gl.projects.list(all=True)
progressbar.streams.wrap_stderr()
gitlab_total_size = 0
report = []
counter = len(projects)
logging.info('π Start scan %d projects', counter)
logging.info('----------------------')
for p in projects:
logging.info('π¦ Scan project "%s"', p.name)
if p.archived:
logging.info('β© Skip archived "%s"', p.name)
continue
try:
repositories = p.repositories.list(all=True)
except Exception:
logging.info('π¦ Exception reading repositories for "%s"', p.name)
continue
if repositories:
for r in repositories:
tags = r.tags.list(all=True)
total_size = 0
if tags:
for t in progressbar.progressbar(tags):
tag = r.tags.get(id=t.name)
total_size = total_size + tag.total_size
gitlab_total_size = gitlab_total_size + tag.total_size
report_data = [
p.name,
p.path_with_namespace,
p.web_url,
r.location,
total_size,
convert_size(total_size)
]
report.append(report_data)
logging.info('π Project "%s" spent space for registry "%s"', p.name, convert_size(total_size))
else:
logging.info('β© Project "%s" has not registry.', p.name)
continue
logging.info('π’ Total "%s"', gitlab_total_size)
logging.info('π³ Total "%s"', convert_size(gitlab_total_size))
if output_file:
with open(output_file, 'w') as f:
w = csv.writer(f)
w.writerow([
"name" ,
"path_with_namespace",
"web_url",
"location",
"size",
"human_size"
])
logging.info("π¨ Result was saved to CSV file %s", output_file)
w.writerows(report)