harbor-v1版本的清理镜像脚本
#!/bin/bash #API usage reference https://github.com/goharbor/harbor/blob/v1.7.5/docs/swagger.yaml Harbor_URL=http://172.31.170.19/ #SSL证书 #cacert="/data/cert/harbor.xxxx.com.crt" #harbor用户密码 user=admin passwd="harbor123456" #删除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}" [ $? -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" |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}" | 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" | 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" |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
提示有证书使用一下脚本
#!/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
分类:
docker
, kubernetes
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫