@@ -12,8 +12,12 @@ APP_NAME="hosted-inference-api"
1212DELETE_TAG=" "
1313HTTPS_USERNAME=" "
1414DELETE_SINCE_DAYS=" "
15+ SSH_KEY=" "
16+ SSH_USER=" "
17+ SSH_HOST=" "
18+ FORCE_DELETE_REPO=false
1519
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
1721do
1822 case " ${flag} " in
1923 e) ENVIRONMENT_NAME=${OPTARG} ;;
2327 t) DELETE_TAG=${OPTARG} ;;
2428 u) HTTPS_USERNAME=${OPTARG} ;;
2529 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} ;;
2634 esac
2735done
2836
@@ -38,14 +46,9 @@ function validate_image_tag() {
3846 fi
3947}
4048
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
4952
5053 while IFS= read -r tag; do
5154 manifest=$( curl -s -u $HTTPS_USERNAME \
@@ -61,7 +64,7 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
6164 current_date=$( date +%s)
6265 days_since=$(( (current_date - created_date) / (60 * 60 * 24 ) ))
6366
64- if [ $days_since -gt $DELETE_SINCE_DAYS ]; then
67+ if [ $days_since -gt $delete_since_days ]; then
6568 digest=$( curl -s -u $HTTPS_USERNAME \
6669 -H " Accept: application/vnd.docker.distribution.manifest.v2+json" \
6770 https://$REGISTRY_URL /v2/$GITHUB_OWNER /$APP_NAME /manifests/$tag \
@@ -72,10 +75,43 @@ if [ -n "$DELETE_SINCE_DAYS" ]; then
7275 echo " $APP_NAME :$tag is $days_since days old"
7376 fi
7477 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
7598 exit 0
7699fi
77100
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
79115if [ -n $DELETE_TAG ]; then
80116 manifest=$( curl -s -u $HTTPS_USERNAME \
81117 -H " Accept: application/vnd.docker.distribution.manifest.v2+json" \
@@ -93,3 +129,5 @@ if [ -n $DELETE_TAG ]; then
93129 echo " ::warning::deleted $REGISTRY_URL /$GITHUB_OWNER /$APP_NAME :$DELETE_TAG "
94130 exit 0
95131fi
132+
133+ # End of main thread
0 commit comments