侧边栏壁纸
博主头像
惬意小蜗牛博主等级

海内存知己,天涯若比邻!

  • 累计撰写 54 篇文章
  • 累计创建 143 个标签
  • 累计收到 59 条评论

目 录CONTENT

文章目录

harbor 自动清理镜像脚本

惬意小蜗牛
2021-07-16 / 0 评论 / 0 点赞 / 1,554 阅读 / 2,126 字 / 正在检测是否收录...

harbor 自动清理镜像脚本 保留近期5个镜像

#!/bin/bash
HARBOR_PAHT="/usr/local/harbor"
HARBOR_URL="http://localhost:8080/api"
HARBOR_AUTHOR="admin:passw0rd"
HARBOR_CONTENT_TYPE="Content-Type: application/json"
CLEAR_PROJECT_NAME=""

# 获取仓库中项目的数量
lines=$(curl -s -u $HARBOR_AUTHOR -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/projects?" | grep "\"name\"" | awk -F "\"" '{print $4}' | wc -l)

# 展示当前所有项目序号及名称
touch project_names.txt
echo -e "\033[32m ============ 当前Harbor有 $lines 个项目 :  ===================== \033[0m"
echo 0、不清理
for i in $(seq 1 $lines); do
    a=$(curl -s -u $HARBOR_AUTHOR -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/projects?" | grep "\"name\"" | awk -F "\"" '{print $4}' | awk -v b=$i 'NR==b{print $1}')
    echo $i、$a
    echo $a >> project_names.txt
done

####################################################################################################
############################################ 清理所有项目 ############################################
####################################################################################################
echo -e "\033[32m ============ 是否清理所有项目 ===================== \033[0m"
echo -e "\033[32m Y 是 \033[0m"
echo -e "\033[32m N 否 \033[0m"
echo -e "\033[32m ==================================================== \033[0m"
echo -e ""
read -e -p"请选择 : " choise

if [ ! -n "$choise" ]; then
    echo -e "\033[32m 你没有选择任何选项,操作中断 \033[0m"
else
    echo -e "\033[32m 你的选择是: \033[0m" $choise
    if [[ "$choise" == 'Y' ]]; then
        PROJECT_NAME_FILE=project_names.txt
        cat $PROJECT_NAME_FILE | while read PRO_NAME; do
            echo "name : ${PRO_NAME}"
            # 赋值要清理的项目名称
            CLEAR_PROJECT_NAME=${PRO_NAME}

            # 获取所有 tags_count
            COUNT=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/search?q=$CLEAR_PROJECT_NAME" | grep "\"tags_count\"" | awk -F "\"" '{print $3$4}' | awk -F "" '{print $3$4}')

            # 获取 当前项目 中 所有 repository_name
            REPS=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/search?q=$CLEAR_PROJECT_NAME" | grep "\"repository_name\"" | awk -F "\"" '{print $4}')

            # 接下来就是根据需求造文件,我这里想要的格式是 tags_count ,repository_name在同一行一一对应关联起来。
            touch test_count.txt
            touch test_server.txt

            for i in $COUNT; do
                echo $i >> test_count.txt
            done

            for y in $REPS; do
                echo $y >> test_server.txt
            done

            # 合并俩个文件拼接出新的文件,新的文件count_and_server.txt里面存放的就是我们造好的数据
            paste test_count.txt test_server.txt > count_and_server.txt

            # 删除不需要的文件
            rm -rf test_count.txt
            rm -rf test_server.txt

            # 循环读取每一行并判断,这里根据tab切割第一个值是tags_count,第二个值对应repository_name,判断大于5将服务读取到server.txt,这样server.txt里面就是我们所有需要清除的镜像
            touch server.txt
            FILENAME=count_and_server.txt
            cat $FILENAME | while read LINE; do
                new_str=$(echo $LINE | cut -d " " -f1)
                if [ -z "$new_str" ]; then
                    echo "new_str is empty"
                else
                    # echo "new_str : $new_str"
                    new_str1=$(echo $LINE | cut -d " " -f2)
                    # echo "new_str1 : $new_str1"
                    # echo "[$new_str -gt 5]"
                    if [ $new_str -gt 5 ]; then
                        echo $new_str1 >> server.txt
                    fi
                fi
            done

            # 判断所有需要清除的镜像文件是否为空文件
            if [ -s server.txt ]; then
                echo "server.txt not empty"
                # cat server.txt

                # 镜像仓库地址格式化切割,将"/"换成"%2F"
                sed -i 's@\/@%2F@g' server.txt
                # cat server.txt

                SERVER_NAME=server.txt
                cat $SERVER_NAME | while read LINE; do
                    name_date=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/repositories/${LINE}/tags" | egrep "\"created|name\"" | awk -F "\"" '{print $4}')
                    touch date.txt
                    touch new_date.txt
                    touch new1_date.txt
                    touch del_tag.txt
                    for date in $name_date; do
                        # echo "date: $date"
                        echo $date >> date.txt
                    done

                    awk '{if(NR%2!=0)ORS=" ";else ORS="\n"}1' date.txt >> new_date.txt
                    sort -r new_date.txt >new1_date.txt
                    rm -rf new_date.txt
                    rm -rf date.txt

                    NEW_DEL=new1_date.txt
                    awk '{print $1}' $NEW_DEL >> del_tag.txt
                    DEL_LINE=del_tag.txt
                    del_tag=$(cat new1_date.txt | tail -n +6 | awk -F' ' '{print $1}')

                    cat $DEL_LINE | while read TAG_LINE; do
                        echo "LINE : ${LINE}"
                        echo "del_tag : $TAG_LINE"
                        URL="$HARBOR_URL/repositories/${LINE}/tags/$TAG_LINE"
                        echo $URL
                        # curl -s -u $HARBOR_AUTHOR -k -v -X DELETE $URL
                        curl -s -u $HARBOR_AUTHOR -k -X DELETE $URL
                    done
                done
                rm -rf date.txt
                rm -rf new_date.txt
                rm -rf new1_date.txt
                rm -rf del_tag.txt
            else
                echo "empty"
            fi
            rm -rf count_and_server.txt
            rm -rf server.txt
        done
    elif [[ "$choise" == 'N' ]]; then
        echo -e ""
    else
        echo -e "\033[32m 输入错误,操作中断 \033[0m"
    fi
fi

####################################################################################################
############################################ 清理单个项目 ############################################
####################################################################################################
# 选择具体的项目,进行清理
echo -e "\033[32m ============ 请输入需要清理的仓库序号 (1~$lines) ===================== \033[0m"
read -p "请输入 : " number
if [[ $number == 0 ]]; then
    echo -e "\033[32m 不清理任何仓库 \033[0m"
elif [ $number -ge 1 -a $number -le $lines ]; then
    # 赋值要清理的项目名称
    CLEAR_PROJECT_NAME=$(curl -s -u $HARBOR_AUTHOR -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/projects?" | grep "\"name\"" | awk -F "\"" '{print $4}' | awk -v b=$number 'NR==b{print $1}')

    # 获取所有 tags_count
    COUNT=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/search?q=$CLEAR_PROJECT_NAME" | grep "\"tags_count\"" | awk -F "\"" '{print $3$4}' | awk -F "" '{print $3$4}')

    # 获取 当前项目 中 所有 repository_name
    REPS=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/search?q=$CLEAR_PROJECT_NAME" | grep "\"repository_name\"" | awk -F "\"" '{print $4}')

    # 接下来就是根据需求造文件,我这里想要的格式是 tags_count ,repository_name在同一行一一对应关联起来。
    touch test_count.txt
    touch test_server.txt

    for i in $COUNT; do
        echo $i >> test_count.txt
    done

    for y in $REPS; do
        echo $y >> test_server.txt
    done

    # 合并俩个文件拼接出新的文件,新的文件count_and_server.txt里面存放的就是我们造好的数据
    paste test_count.txt test_server.txt > count_and_server.txt

    # 删除不需要的文件
    rm -rf test_count.txt
    rm -rf test_server.txt

    # 循环读取每一行并判断,这里根据tab切割第一个值是tags_count,第二个值对应repository_name,判断大于5将服务读取到server.txt,这样server.txt里面就是我们所有需要清除的镜像
    touch server.txt
    FILENAME=count_and_server.txt
    cat $FILENAME | while read LINE; do
        new_str=$(echo $LINE | cut -d " " -f1)
        if [ -z "$new_str" ]; then
            echo "new_str is empty"
        else
            # echo "new_str : $new_str"
            new_str1=$(echo $LINE | cut -d " " -f2)
            # echo "new_str1 : $new_str1"
            # echo "[$new_str -gt 5]"
            if [ $new_str -gt 5 ]; then
                echo $new_str1 >> server.txt
            fi
        fi
    done

    # 判断所有需要清除的镜像文件是否为空文件
    if [ -s server.txt ]; then
        echo "server.txt not empty"
        # cat server.txt

        # 镜像仓库地址格式化切割,将"/"换成"%2F"
        sed -i 's@\/@%2F@g' server.txt
        # cat server.txt

        SERVER_NAME=server.txt
        cat $SERVER_NAME | while read LINE; do
            name_date=$(curl -s -u $HARBOR_AUTHOR -k -X GET -H $HARBOR_CONTENT_TYPE "$HARBOR_URL/repositories/${LINE}/tags" | egrep "\"created|name\"" | awk -F "\"" '{print $4}')
            touch date.txt
            touch new_date.txt
            touch new1_date.txt
            touch del_tag.txt
            for date in $name_date; do
                # echo "date: $date"
                echo $date >> date.txt
            done

            awk '{if(NR%2!=0)ORS=" ";else ORS="\n"}1' date.txt >> new_date.txt
            sort -r new_date.txt >new1_date.txt
            rm -rf new_date.txt
            rm -rf date.txt

            NEW_DEL=new1_date.txt
            awk '{print $1}' $NEW_DEL >> del_tag.txt
            DEL_LINE=del_tag.txt
            del_tag=$(cat new1_date.txt | tail -n +6 | awk -F' ' '{print $1}')

            cat $DEL_LINE | while read TAG_LINE; do
                echo "LINE : ${LINE}"
                echo "del_tag : $TAG_LINE"
                URL="$HARBOR_URL/repositories/${LINE}/tags/$TAG_LINE"
                echo $URL
                # curl -s -u $HARBOR_AUTHOR -k -v -X DELETE $URL
                curl -s -u $HARBOR_AUTHOR -k -X DELETE $URL
            done
        done
        rm -rf date.txt
        rm -rf new_date.txt
        rm -rf new1_date.txt
        rm -rf del_tag.txt
    else
        echo "empty"
    fi
    rm -rf count_and_server.txt
    rm -rf server.txt
fi

rm -rf project_names.txt

####################################################################################################
############################################ 立即清空 GC ############################################
####################################################################################################
echo -e "\033[32m ============ 是否立即清空 GC 释放资源 ===================== \033[0m"
echo -e "\033[32m Y 是 \033[0m"
echo -e "\033[32m N 否 \033[0m"
echo -e "\033[32m ==================================================== \033[0m"
echo -e ""
read -e -p"请选择 : " choise

if [ ! -n "$choise" ]; then
    echo -e "\033[32m 你没有选择任何选项,操作中断 \033[0m"
else
    echo -e "\033[32m 你的选择是: \033[0m" $choise
    if [[ "$choise" == 'Y' ]]; then
        # 前面只是在ui中删除并不是真正意义的删除,还需要执行harbor命令清空Gc才能真正释放资源
        cd ${HARBOR_PAHT}
        docker-compose stop
        docker run -it --name gc --rm --volumes-from registry vmware/registry:2.6.2-photon garbage-collect --dry-run /etc/registry/config.yml
        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
    elif [[ "$choise" == 'N' ]]; then
        echo -e ""
    else
        echo -e "\033[32m 输入错误,操作中断 \033[0m"
        returnSh
    fi
fi
0

评论区