@@ -12,8 +12,12 @@ APP_NAME="hosted-inference-api"
12
12
DELETE_TAG=" "
13
13
HTTPS_USERNAME=" "
14
14
DELETE_SINCE_DAYS=" "
15
+ SSH_KEY=" "
16
+ SSH_USER=" "
17
+ SSH_HOST=" "
18
+ FORCE_DELETE_REPO=false
15
19
16
- while getopts e:g:r:a:t:u:d: flag
20
+ while getopts e:g:r:a:t:u:d:p:s:h:f: flag
17
21
do
18
22
case " ${flag} " in
19
23
e) ENVIRONMENT_NAME=${OPTARG} ;;
23
27
t) DELETE_TAG=${OPTARG} ;;
24
28
u) HTTPS_USERNAME=${OPTARG} ;;
25
29
d) DELETE_SINCE_DAYS=${OPTARG} ;;
30
+ p) SSH_KEY=${OPTARG} ;;
31
+ s) SSH_USER=${OPTARG} ;;
32
+ h) SSH_HOST=${OPTARG} ;;
33
+ f) FORCE_DELETE_REPO=${OPTARG} ;;
26
34
esac
27
35
done
28
36
@@ -38,14 +46,9 @@ function validate_image_tag() {
38
46
fi
39
47
}
40
48
41
- # Delete images older than DELETE_SINCE_DAYS and exit 0
42
- if [ -n " $DELETE_SINCE_DAYS " ]; then
43
- echo " ::notice::Deleting images older than $DELETE_SINCE_DAYS days"
44
- repo_tags=$( curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL /v2/$GITHUB_OWNER /$APP_NAME /tags/list | jq -r ' .tags[]?' )
45
- if [ -z " $repo_tags " ]; then
46
- echo " ::notice::No images found for $APP_NAME "
47
- exit 0
48
- fi
49
+ function delete_since_days() {
50
+ repo_tags=$1
51
+ delete_since_days=$2
49
52
50
53
while IFS= read -r tag; do
51
54
manifest=$( curl -s -u $HTTPS_USERNAME \
@@ -61,7 +64,7 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
61
64
current_date=$( date +%s)
62
65
days_since=$(( (current_date - created_date) / (60 * 60 * 24 ) ))
63
66
64
- if [ $days_since -gt $DELETE_SINCE_DAYS ]; then
67
+ if [ $days_since -gt $delete_since_days ]; then
65
68
digest=$( curl -s -u $HTTPS_USERNAME \
66
69
-H " Accept: application/vnd.docker.distribution.manifest.v2+json" \
67
70
https://$REGISTRY_URL /v2/$GITHUB_OWNER /$APP_NAME /manifests/$tag \
@@ -72,10 +75,43 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
72
75
echo " $APP_NAME :$tag is $days_since days old"
73
76
fi
74
77
done <<< " $repo_tags"
78
+ }
79
+
80
+ function force_delete_repo() {
81
+ echo " $SSH_KEY " | ssh $SSH_USER @$SSH_HOST " cd ci-setup && docker exec -i -u root ci-setup_registry_1 bin/registry garbage-collect --delete-untagged /etc/docker/registry/config.yml"
82
+ echo " $SSH_KEY " | ssh $SSH_USER @$SSH_HOST " cd ci-setup && docker exec -i -u root ci-setup_registry_1 rm -rf /var/lib/registry/docker/registry/v2/repositories/$GITHUB_OWNER /$APP_NAME "
83
+ echo " ::warning::force deleted $REGISTRY_URL /$GITHUB_OWNER /$APP_NAME "
84
+ }
85
+
86
+ # Main thread
87
+
88
+ # # Force delete image repository and exit 0
89
+ if [ " $FORCE_DELETE_REPO " == " true" ]; then
90
+ repo_tags=$( curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL /v2/$GITHUB_OWNER /$APP_NAME /tags/list | jq -r ' .tags[]?' )
91
+ if [ -n " $repo_tags " ]; then
92
+ echo " ::error::found existing manifests for $REGISTRY_URL /$GITHUB_OWNER /$APP_NAME "
93
+ exit 1
94
+ fi
95
+
96
+ delete_since_days " $repo_tags " " 0"
97
+ force_delete_repo
75
98
exit 0
76
99
fi
77
100
78
- # Delete a specific image and exit 0
101
+ # # Delete images older than DELETE_SINCE_DAYS and exit 0
102
+ if [ -n " $DELETE_SINCE_DAYS " ]; then
103
+ echo " ::notice::Deleting images older than $DELETE_SINCE_DAYS days"
104
+ repo_tags=$( curl -s -u $HTTPS_USERNAME https://$REGISTRY_URL /v2/$GITHUB_OWNER /$APP_NAME /tags/list | jq -r ' .tags[]?' )
105
+ if [ -z " $repo_tags " ]; then
106
+ echo " ::notice::No images found for $APP_NAME "
107
+ exit 0
108
+ fi
109
+
110
+ delete_since_days " $repo_tags " " $DELETE_SINCE_DAYS "
111
+ exit 0
112
+ fi
113
+
114
+ # # Delete a specific image and exit 0
79
115
if [ -n $DELETE_TAG ]; then
80
116
manifest=$( curl -s -u $HTTPS_USERNAME \
81
117
-H " Accept: application/vnd.docker.distribution.manifest.v2+json" \
@@ -93,3 +129,5 @@ if [ -n $DELETE_TAG ]; then
93
129
echo " ::warning::deleted $REGISTRY_URL /$GITHUB_OWNER /$APP_NAME :$DELETE_TAG "
94
130
exit 0
95
131
fi
132
+
133
+ # End of main thread
0 commit comments