This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry-cleanup
executable file
·121 lines (98 loc) · 3.04 KB
/
registry-cleanup
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
#!/bin/bash
# Check arguments.
if [ "x$1" == "x" ] || [ "x$2" == "x" ] || [ "x$3" == "x" ]
then
# Print error.
echo "Error: Missing arguments."
# Print usage.
echo "Usage: $(basename "$0") registry max_images max_age"
# Exit.
exit 1
fi
# Get registry, maximum images and maximum age.
registry="$1/v2"
max_images="$(( $2 + 1 ))"
max_age="$(( $(date "+%s") - $3 * 86400 ))"
# Gets repositories.
function get_repositories {
# Get repositories.
curl --silent "$registry/_catalog" | jq -r ".repositories[]"
}
# Gets tags by repository.
function get_tags {
# Get repository.
repository="$1"
# Get tags by repository.
curl --silent "$registry/$repository/tags/list" | jq -r ".tags[]"
}
# Gets image by repository and tag.
function get_image {
# Get repository and tag.
repository="$1"
tag="$2"
# Get image by repository and tag.
curl --silent "$registry/$repository/manifests/$tag" --header "Accept: application/vnd.docker.distribution.manifest.v2+json" | jq -r ".config.digest"
}
# Gets created by repository and image.
function get_created {
# Get repository and image.
repository="$1"
image="$2"
# Get created by repository and image.
created="$(curl --silent "$registry/$repository/blobs/$image" | jq -r ".created")"
# Convert to seconds.
date --date "$created" "+%s"
}
# Deletes tag by repository and tag.
function delete_tag {
# Get repository and tag.
repository="$1"
tag="$2"
# Get digest by repository and tag.
digest="$(curl --silent "$registry/$repository/manifests/$tag" --header "Accept: application/vnd.docker.distribution.manifest.v2+json" --head | grep "^Docker-Content-Digest\:.*$" | awk '{print $2}' | tr --delete '\r')"
# Delete tag by repository and digest.
curl --silent "$registry/$repository/manifests/$digest" --request DELETE
}
# Get repositories.
repositories=$(get_repositories)
# Iterate repositories.
for repository in $repositories
do
# Get tags by repository.
tags=$(get_tags "$repository")
# Iterate tags.
for tag in $tags
do
# Get image by repository and tag.
image="$(get_image "$repository" "$tag")"
# Get created by repository and image.
created="$(get_created "$repository" "$image")"
echo "$created $image $tag"
done > /tmp/tags
# Get images to delete.
awk '{print $1" "$2}' /tmp/tags | sort --numeric-sort --reverse --unique | tail --lines "+$max_images" > /tmp/images
# Iterate images.
while read line
do
# Get created.
created="$(echo "$line" | awk '{print $1}')"
# Check age.
if [ "$created" -lt "$max_age" ]
then
# Get image.
image="$(echo "$line" | awk '{print $2}')"
# Get tags to delete.
tags=$(grep "$image" /tmp/tags | awk '{print $3}')
# Iterate tags to delete.
for tag in $tags
do
# Log deletion.
echo "DELETE $repository:$tag [image: $image] [created: $created]"
# Delete tag.
delete_tag "$repository" "$tag"
done
fi
done < /tmp/images
done
# Run Docker Registry garbage collection.
registry garbage-collect /etc/docker/registry/config.yml