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
复制代码

 

posted @   awks  阅读(123)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
点击右上角即可分享
微信分享提示