Shell脚本清理harbor镜像
我们经常会遇到harbor服务器磁盘不足的情况,要是删除的镜像多,通过web界面选择的方式会比较慢,于是写了下面脚本,可以通过API清理镜像,这样就方便多了。若有瑕疵请各位大佬留言。#!/bin/bash#API usage referencehttps://github.com/goharbor/harbor/blob/v1.7.5/docs/swagger.yamlHarbor_URL=htt
·
通过选择的方式清理harbor镜像库要删除的镜像版本。
#!/bin/bash
#API usage reference https://github.com/goharbor/harbor/blob/v1.7.5/docs/swagger.yaml
Harbor_URL=https://harbor.xxxxx.com
#SSL证书
cacert="/data/cert/harbor.xxxx.com.crt"
#harbor用户密码
user=admin
passwd="Harbor12345"
#删除tags
delete_tags(){
read -p "直接批量复制粘贴要删除的镜像即可: " input
for i in $input
do
i=`expr $i + 0`
#echo "${Harbor_URL}/api/repositories/${repo_name}/tags/${i} is deleted"
curl -s -u "$user:$passwd" -X DELETE -H "Content-Type: application/json" "${Harbor_URL}/api/repositories/${repo_name}/tags/${i}" --cacert "$cacert"
[ $? -eq 0 ] && echo "${Harbor_URL}/api/repositories/${repo_name}/tags/${i} is deleted" || exit 1
done
}
#获取tags
get_tags(){
tags_msg=`curl -s -u "$user:$passwd" -X GET -H "Content-Type: application/json" "${Harbor_URL}/api/repositories/${repo_name}/tags" --cacert "$cacert" |grep -w '"name"' |awk -F \" '{print $(NF-1)}' |sort -n`
echo "=========================tags清单==================================="
echo $tags_msg
delete_tags
}
#获取镜像
get_mirror(){
mirror_name=`curl -s -u "$user:$passwd" -X GET -H "Content-Type: application/json" "${Harbor_URL}/api/repositories?project_id=${project_id}" --cacert "$cacert" | grep -w "name" | awk -F \" '{print $(NF-1)}'`
echo "=========================镜像清单===================================="
select repo_name in $mirror_name
do
case $REPLY in
[1-9]|[1-9][0-9])
#mirror_id=`echo "$mirror_msg" |awk -F "[:,]" 'NR==1{print int($2)}'`
echo $repo_name
get_tags
;;
*)
echo "please input number"
;;
esac
break
done
}
#获取项目
get_project() {
project_name=`curl -s -u "$user:$passwd" -X GET -H "Content-Type: application/json" "${Harbor_URL}/api/projects" --cacert "$cacert" | grep -w "name" | awk -F \" '{print $(NF-1)}'`
cat <<EOF
=====================================================================================
#请先选择要清理哪个项目下的镜像
#然后选择要清理哪个镜像下的哪些tag
#清理tags
EOF
select name in $project_name
do
project_msg=`curl -s -u "$user:$passwd" -X GET -H "Content-Type: application/json" "${Harbor_URL}/api/projects" --cacert "$cacert" |egrep -w "project_id|name" |grep -w -B 1 "$name"`
case $REPLY in
[0-9]|[1][0-9])
project_id=`echo "$project_msg" |awk -F "[:,]" 'NR==1{print int($2)}'`
#echo ${project_id}
get_mirror
;;
*)
echo "please input number"
;;
esac
break
done
}
get_project
回收存储空间
#要启用垃圾回收(GC),首先要关闭Harbor服务,然后再执行清理命令
#停止Harbor相关服务
cd ./harbor
docker-compose stop
#使用–dry-run参数运行容器,预览运行效果,但不删除任何数据
docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect --dry-run /etc/registry/config.yml
#NOTE: The above option “–dry-run” will print the progress without removing any data.
Verify the result of the above test, then use the below commands to perform garbage collection and restart Harbor.
#不使用–dry-run参数,将删除相关的文件和镜像,
docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect /etc/registry/config.yml
#重新启动Harbor相关服务
docker-compose start
更多推荐
已为社区贡献1条内容
所有评论(0)