通过选择的方式清理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
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐