-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsemreg
260 lines (227 loc) · 7.69 KB
/
semreg
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
auth="--user ${SEMAPHORE_REGISTRY_USERNAME}:${SEMAPHORE_REGISTRY_PASSWORD}"
SkipTags=""
semreg::gc() {
curl -s $auth https://$SEMAPHORE_REGISTRY_HOST/gc --output /dev/null
sleep 3
local counter=0
local status_code
echo "Running garbage collector : "
while [ $counter -lt 30 ]; do
status_code=$(curl -I -s $auth https://$SEMAPHORE_REGISTRY_HOST/v2/ | head -n 1 | cut -d ' ' -f 2)
if [[ "$status_code" == "200" ]]; then
counter=30
else
echo -ne "."
counter=$((counter+1))
sleep 3
fi
done
if [[ "$status_code" != "200" ]]; then
echo "Couldnt access registry, status code : $status_code"
exit 0
fi
echo "Done"
}
semreg::list() {
local image="$1"
echo "Image Tag CreatedAt Size" >/tmp/dockerlist
for i in $(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/_catalog | jq -r '.repositories[]' 2>/dev/null);
do
if [ -z "$image" ] || [ "$image" == "$i" ]; then
tags=$(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/tags/list | jq -r '.tags[]' 2>/dev/null)
total_size=0
for j in $tags; do
size=0
create_date=$(date +"%Y-%m-%d_%Hh-%Mm-%Ss" --date=$(curl -s $auth -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/manifests/$j | jq -r '.history[].v1Compatibility'| jq -r '.created' 2>/dev/null | head -n1))
for k in $(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' https://$SEMAPHORE_REGISTRY_HOST/v2/$i/manifests/$j | grep size | awk '{print $2}' | sed 's|,||g'); do
size=$((size+k))
done
total_size=$((total_size+size))
echo "$i $j $create_date $size" >> /tmp/dockerlist
done
echo "Total: $total_size" >> /tmp/dockerlist
echo "" >> /tmp/dockerlist
fi
done
column -t /tmp/dockerlist
rm -f /tmp/dockerlist
}
semreg::mark() {
local image="$1"
local tag="$2"
SkipTags="$(sed 's/,/\\|/g' <<< $SkipTags)"
grep -q "$SkipTags" <<< "$tag"
if [ $? -eq 0 ] && [ "$SkipTags" != "" ] ; then
echo "Skipping $image:$tag"
else
curl $auth -X DELETE -sI "https://$SEMAPHORE_REGISTRY_HOST/v2/$image/manifests/$(
curl $auth -sI \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"https://$SEMAPHORE_REGISTRY_HOST/v2/$image/manifests/$tag" \
| tr -d '\r' | sed -En 's/^Docker-Content-Digest: (.*)/\1/pi'
)" --output /dev/null
echo "Marking for delete $image:$tag"
fi
}
semreg::leave_last() {
local image="$1"
local count=${2:-1}
if [ "$image" == "all" ]; then image="";fi
for i in $(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/_catalog | jq -r '.repositories[]' 2>/dev/null);
do
if [ -z "$image" ] || [ "$image" == "$i" ]; then
tags=$(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/tags/list | jq -r '.tags[]' 2>/dev/null)
for j in $tags; do
create_date=$(date --date=$(curl -s $auth -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/manifests/$j | jq -r '.history[].v1Compatibility'| jq -r '.created' 2>/dev/null| head -n1 ) +"%s")
echo "$j $create_date" >> /tmp/to_delete_${i}
done
fi
done
for i in $(ls /tmp/to_delete_*); do
tags=$(cat $i|sort -n -k2|awk '{print $1}')
if [ $count -lt 0 ]; then
count=$(echo "${count##*[+-]}")
fi
delete_tags=$(echo "$tags"|head -n -$count)
delete_image=${i#"/tmp/to_delete_"}
for j in $delete_tags; do
semreg::mark $delete_image $j
done
done
semreg::gc
rm -f /tmp/to_delete_*
}
semreg::delete_age() {
local image="$1"
local age="${2:-1}"
age=${2//[!0-9]/}
local max_age=$(($(date +%s) - age*60*60))
if [ "$image" == "all" ]; then image="";fi
for i in $(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/_catalog | jq -r '.repositories[]' 2>/dev/null);
do
if [ -z "$image" ] || [ "$image" == "$i" ]; then
tags=$(curl -s $auth --header 'Accept: application/vnd.docker.distribution.manifest.v2+json' -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/tags/list | jq -r '.tags[]' 2>/dev/null)
for j in $tags; do
create_date=$(date --date=$(curl -s $auth -X GET https://$SEMAPHORE_REGISTRY_HOST/v2/$i/manifests/$j | jq -r '.history[].v1Compatibility'| jq -r '.created' 2>/dev/null| head -n1 ) +"%s")
echo "$j $create_date" >> /tmp/to_delete_${i}
done
fi
done
for i in $(ls /tmp/to_delete_*); do
delete_tags=$(cat $i|awk '$2 <= '$max_age' {print $1}')
delete_image=${i#"/tmp/to_delete_"}
for j in $delete_tags; do
semreg::mark $delete_image $j
done
done
semreg::gc
rm -f /tmp/to_delete_*
}
semreg::usage() {
local json_usage=$(curl -s $auth https://$SEMAPHORE_REGISTRY_HOST/usage)
if [ "$1" == '--json' ]; then
echo $json_usage
else
regsize=$(echo $json_usage|jq '.usage.size')
regused=$(echo $json_usage|jq '.usage.used')
regavail=$(echo $json_usage|jq '.usage.avail')
output="Size Used Available \n $regsize $regused $regavail"
echo -e "$output" | column -t
fi
}
semreg::prune() {
local params="$@"
arr=( $params )
local tags_str="$(awk -F '--skip' '{print $2}' <<< ${params})"
SkipTags="$(echo ${tags_str//\'/})"
params="$(awk -F '--skip' '{print $1}' <<< ${params} | xargs)"
if [ "$params" == "" ] || [ "$params" == 'all' ]; then
if [ "$SkipTags" == "" ]; then
semreg::delete_age 'all' '24h'
else
semreg::delete_age 'all' '0h'
fi
fi
if [ "${arr[1]}" == "--one-week" ]; then
semreg::delete_age ${arr[0]} '168h'
fi
if [ "${arr[1]}" == "--retain" ]; then
count="${arr[2]}"
count=$(echo "${count//[!0-9]/}")
count="${count:-1}"
if [ "$count" == "" ];then
semreg:doc
exit 0;
fi
semreg::leave_last ${arr[0]} $count
fi
SkipTags=""
}
semreg::doc() {
echo -e "
Usage: semreg [prune|list|usage]
list
Displays images in the registry
Bulk removes unused images from the Semaphore Private Docker Registry.
Usage:
semreg prune [all | image-name] [flags…]
Default behaviour:
Deletes all images older than one day.
Flags:
--one-week - Deletes all images older than one week old.
--retain N - Deletes all images, leaves the newest N images in the registry.
--skip TAGS - Deletes all tags, except the ones specified.(to be implemented)
semreg prune all
Delete all images older than one day.
semreg prune all --one-week
Delete all images older than one week.
semreg prune all --retain 3
Delete all images except the last 3.
semreg prune all --retain 1
Delete all images except the last .
semreg prune <image name>
Delete all tags from <image name> older than one day.
semreg prune <image name> --one-week
Delete all tags from <image name> older than one week.
semreg prune <image name> --retain 3
Delete all tags from <image name> except the last 3.
semreg prune <image name> --retain 1
Delete all tags from <image name> except the last one."
}
semreg::err() {
echo -e "\n! [$(version::date)]: $@" >&2
return 1
}
semreg() {
semreg_command=$1
case $semreg_command in
list)
shift
semreg::list $@
;;
usage)
shift
semreg::usage $1
;;
# delete-tag)
# shift
# semreg::mark $1 $2
# ;;
# leave-last)
# shift
# semreg::leave_last $1 $2
# ;;
# delete-age)
# shift
# semreg::delete_age $1 $2
# ;;
prune)
shift
semreg::prune $@
;;
*)
semreg::doc
;;
esac
}