shell清理harbor镜像:

#!/bin/bash
#需要先安装jq命令,如 yum install -y epel-release && yum install -y jq
HABOR_API_URL="https://hub.test.com/api"
HARBOR_USER=admin 
HARBOR_PASSWORD=123456
MIN_TAG_COUNTS=10

#projects列表
curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X GET -H "Content-Type: application/json" "$HABOR_API_URL/projects" > .projects
HARBOR_PROJECT_LIST=`cat .projects |grep \"name\": |cut -d ":" -f 2 |cut -d '"' -f 2 |xargs`

for project in $HARBOR_PROJECT_LIST;do
    repository_list=`curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X GET --header 'Accept: application/json' "$HABOR_API_URL/search?q=$project" |grep repository_name |cut -d ':' -f 2 |cut -d '"' -f 2 |xargs`
    for repository in $repository_list;do 
        curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X GET --header 'Accept: application/json' "$HABOR_API_URL/repositories/$repository/tags" > .repository
        #满足最小镜像保留数量则不删除该repository
        tag_counts=`cat .repository |jq 'length'`
        if tag_counts<=$MIN_TAG_COUNTS;then
            continue
        fi
        #用字典存储镜像的tag-time对
        declare -A dict_tag
        for((i=0;i<$tag_counts;i++));do
            tag_name=`cat .repository |jq ".[$i]."name`
            tag_time=`cat .repository |jq ".[$i]."created |date +%s `
            echo $tag_name $tag_time
            dict_tag[$tag_time]=$tag_name
              echo ${!dic_tag[*]}
        done
        #排序,取最小的时间戳的MIN_TAG_COUNTS个
        #删除操作
        curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X DELETE -H "Content-Type: application/json" "$HABOR_API_URL/repositories/$repository/tags/$tag"
        done
    done
done

#### 项目个数
lines=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|wc -l`
##### 展示当前有几个项目
echo "当前Harbor有以下几个项目:"
for i in $(seq 1 $lines)
do
   ###########具体是啥项目
   a=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|awk -v b=$i 'NR==b{print $1}'`
   echo $i、$a
done
#######选择具体的项目
read -p  "请输入序号(1~$lines):,查看其下的镜像仓库:" number
if [ $number -ge 1 -a $number -le $lines ]
then
 #########选择的是哪个项目
 c=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "\"name\""|awk -F "\"" '{print $4}'|awk -v b=$number 'NR==b{print $1}'`
#####多少个仓库
  d=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/projects?" |grep "$c" -C 2 |grep "project_id" |awk '{print $2}' |awk -F "," '{print $1}'`
  #echo "\$d-----------$d"
######显示仓库个数
 ## e=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |awk -F "/" '{print $2}'|wc -l`
e=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=2" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|wc -l`
 ####### 简单展示
  echo "项目$c下有以下镜像仓库:"
  for line in $(seq 1 $e)
  do
   #####具体的仓库名
    f=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|awk -v g=$line 'NR==g{print $1}'`
    echo $line、$f
  done
   read -p  "请输入序号(1~$e):,查看其下的镜像格式以及对应的数量:" num
   if [ $num -ge 1 -a $num -le $e ]
   then
  #### 镜像仓库名字
     h=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories?project_id=$d" | grep "\"name\"" |awk -F "\"" '{print $4}' |sed 's/sc\///g'|awk -v g=$num 'NR==g{print $1}'`
     echo "您选择的仓库是$h"
  #### 标签类型种类个数
#     i=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}' | cut -c -6 |sort -n |uniq|wc -l`
    # echo $i
 #### 标签类型以及个数
    echo "##################################"
    echo "镜像格式为:如果是10月,则为201810*"
    echo "##################################"
    #####每种镜像格式以及其数量
    curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}' | cut -c -6 |awk '{count[$1]++}END{for (i in count)print i,count[i]}'
 ######输入镜像格式,进行删除
     echo "如果想删除某种形式的镜像,请输入类型:"
     read image_format
     ##########输入类型的所有镜像
     images=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}'|grep $image_format|awk '{print $1}'`
     #########统计镜像个数
    count_image=`curl -s -u "admin:Harbor12345" -X GET -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/" |grep "\"name\"" |awk -F"\"" '{print $4}'|grep $image_format|wc -l`
    for image_label in $images
    do
    #############执行删除
      curl -u "admin:Harbor12345" -X DELETE -H "Content-Type: application/json" "https://harbor.k8stest.com/api/repositories/$c%2F$h/tags/$image_label"
    done
      if [ $? -eq 0 ]
      then
        echo "删除成功"
        echo "本次共删除$count_image个镜像"
      fi
 fi
fi

declare -A dic1
dic1=(["k1"]="v1" ["k2"]="v2")
list=('v1' 'v2' 'dic1')
eval echo \${${list[2]}["k1"]}

             declare -A dict
            tag_name=`cat .repository |jq ".[0]."name`
            tag_time=`cat .repository |jq ".[0]."created |date +%s `
            echo $tag_name $tag_time
            #dict_tag[$tag_time]=$tag_name
            dict["$tag_time"]=value111
              echo ${!dic[*]}
        dict_tag["1573550783"]="1234"

        dic_tag['key1']=value
        dic['key2']=value
        dic['key3']=value

#必须先声明
declare -A dic
dic=([key1]="value1" [key2]="value2" [key3]="value3")

#打印指定key的value
echo ${dic["key1"]}
#打印所有key值
echo ${!dic[*]}
#打印所有value
echo ${dic[*]}

#遍历key值
for key in $(echo ${!dic[*]})
do
        echo "$key : ${dic[$key]}"
done

        echo ${!dic_tag[*]}

                遍历
        for key in $(echo ${!dic[*]})
        do
            echo "$key : ${dic[$key]}"
        done 

        字典赋初值
        dic=([key1]="value1" [key2]="value2" [key3]="value3")
        字典动态赋值
        dic['key']=value
        打印指定key的value
        echo ${dic['key']}
        打印所有key值
        echo ${!dic[*]}
        打印所有value
        echo ${dic_tag[*]}

#curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X GET --header 'Accept: application/json' "$HABOR_API_URL/repositories/gccx%2Ftomcat/tags"
curl -u "$HARBOR_USER:$HARBOR_PASSWORD" -X GET --header 'Accept: application/json' "$HABOR_API_URL/repositories/gccx/config/tags"

REGISTRY_ID=`docker ps | grep registry| awk '{print $1}'`   # 获取registry的容器运行id
docker exec ${REGISTRY_ID} registry garbage-collect /etc/registry/config.yml  # 释放通过api已删除的镜像空间

curl -X GET --header 'Accept: application/json' '$HABOR_API_URL/api/search?q=gccx'

# 删除镜像的 tags 时,可以通过 curl 获取返回值来判断成功与否
curl -X DELETE -H 'Accept: text/plain' -u admin:Harbor12345  -o /dev/null -s -w %{http_code}  "http://192.168.0.241/api/repositories/library/redis/tags/2018-10-29_17-39"

发表回复

您的电子邮箱地址不会被公开。

Captcha Code