首页 > 系统相关 >shell 实现harbor 指定仓库镜像tags 查询

shell 实现harbor 指定仓库镜像tags 查询

时间:2023-10-08 18:07:05浏览次数:33  
标签:count project shell tags harbor get echo repo 033

1.需求背景

1.1 容器发布ci 成功以后 我们希望CD时候可以选择相关镜像
1.2 统计指定仓库镜像有多少个版本


2.shell代码

#!/bin/bash

#harbor 关键环境变量
HARBOR_URL=""
USERNAME=""
PASSWORD=""
PAGE_SIZE=50

imges_head=$(echo "$HARBOR_URL"|awk -F // '{print $2}' )
projects_endpoint="$HARBOR_URL/api/v2.0/projects?page_size=$PAGE_SIZE"
out_file="./images_tag_list.txt"

checkSysEnv() {
if [ -z "$HARBOR_URL" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]
then
   echo -e "\033[31m缺少harbor登录关联环境变量,请检查并填写正确配置....\033[0m"
   exit 1
fi
}

getProjectList(){
checkSysEnv
projects=$(curl -u $USERNAME:$PASSWORD -skX 'GET' $projects_endpoint |jq -r '.[].name')
if  [ -z "$projects" ]
then
    echo -e "\033[31m 获取harbor项目仓库出现异常境,请检查....\033[0m"
    exit 1
fi
for project in $projects
do
    echo "$project"
done
}

getRepoCountPages(){
checkSysEnv
project_name=$1
if [ -z "$project_name" ]
then
    projects=`getProjectList`
    for project in $projects
    do
       repo_count_endpoint="$HARBOR_URL/api/v2.0/projects?name=$project"
       repo_count=$(curl -u $USERNAME:$PASSWORD -skX 'GET'   $repo_count_endpoint |jq -r '.[].repo_count')
       pages=$(expr $repo_count / 10 + 1)
       echo "$pages"
     done
else
     repo_count_endpoint="$HARBOR_URL/api/v2.0/projects?name=$project_name"
     repo_count=$(curl -u $USERNAME:$PASSWORD -skX 'GET'   $repo_count_endpoint |jq -r '.[].repo_count')
     pages=$(expr $repo_count / 10 + 1)
     echo "$pages"
fi
}

get_appoint_repo_tags(){
    echo "" > ./images_tag_list.txt
    project=$2
    repo_count=`getRepoCountPages $project`
    if [ -z "$repo_count" ]; then
      continue
    fi
    pages=$(expr "$repo_count" / 10 + 1)
    for ((i=1; i<=$pages; i++)); do
        images_endpoint="$HARBOR_URL/api/v2.0/projects/$project/repositories?page_size=10&page=$i"
        repos_json=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$images_endpoint")
        if [ -z "$repos_json" ] || [ "$repos_json" == "null" ]; then
            continue
        fi
        repos=$(echo "$repos_json" | jq -r '.[].name' | awk -F / '{print $2}')
        for repo in $repos; do
            tag_count_url="$HARBOR_URL/api/v2.0/projects/$project/repositories/$repo"
            tag_count=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$tag_count_url" | jq '.artifact_count')

            if [ -z "$tag_count" ] || [ "$tag_count" == "null" ]; then
                continue
            fi

            pages1=$(expr "$tag_count" / 10 + 1)

            if [ -z "$pages1" ]; then
                continue
            fi

            for ((j=1; j<=$pages1; j++)); do
                tags_endpoint="$HARBOR_URL/api/v2.0/projects/$project/repositories/$repo/artifacts?page_size=10&page=$j"
                tags_json=$(curl -u "$USERNAME:$PASSWORD" -skX 'GET' "$tags_endpoint")

                if [ -z "$tags_json" ] || [ "$tags_json" == "null" ] ; then
                    continue
                fi

                tags=$(echo "$tags_json" |jq -r '.[].tags[].name')
                if [ -z "$tags" ] || [ "$tags" == "null" ]; then
                     continue
                fi
                for tag in $tags; do
                   if [ -z "$tag" ] || [ "$tag" == "null" ]; then
                      continue
                   fi
                       printf "$HARBOR_URL/$project/$repo:$tag \n" >> images_tag_list.txt 2>&1
                       if [ ! -f "$out_file" ]; then
                           echo "文件$out_file 不存在"
                           exit 1
                       fi
                       while IFS= read -r line; do
                          echo "$line"
                       done < "$out_file"
                 done
            done
        done
    done
}

case $1 in
    "get_project_list")
        getProjectList
        ;;
    "get_repo_count_one_page")
        getRepoCountPages $*
        ;;
    "get_appoint_repo_tags")
         checkSysEnv
         get_appoint_repo_tags $*
     ;;
    *)
        echo -e "\033[32m 参数如下: \033[0m"
        echo -e "\033[32m get_project_list \033[0m 获取harbor项目列表 默认查询最大50个 其他修改 PAGE_SIZE 参数"
        echo -e "\033[32m get_repo_count_one_page \033[0m 获取项目列表有多少页数据 后面指定harbor项目名称,不指定查询全部"
        echo -e "\033[32m get_appoint_repo_tags \033[0m get_appoint_repo_tags 指定harbor仓库名称 后面跟 harbor存在的仓库名称可以通过get_project_list查询"
        ;;
esac





标签:count,project,shell,tags,harbor,get,echo,repo,033
From: https://blog.51cto.com/breaklinux/7759578

相关文章

  • Python入门示例系列04 使用 IDLE Shell
    启动IDLEShell安装Python之后,点击开始菜单,找到P开头的菜单项,点开某个版本的Python,如下图:  点击IDLE    输入代码之后按回车Enter可以运行代码【上图,输入x=1+2,然后按回车,即开始执行】输入变量(对象)名可以查看值【上图,输入x,可以查看x的值】Alt+P上一条代码(st......
  • Linux系列---【shell脚本-模拟手动跑每天的定时任务】
    问题背景上线的时候经常会遇到这样的问题,上线一个每天跑的定时任务,一般跑最近一年的数据,上线的时候需要手动跑过去最近一年的数据,手动肯定不方便,于是这里写了一个好用的shell脚本,来降本增效。shell脚本#!/bin/bash#设置循环的日期范围start_date="20230801"end_date="20......
  • 完整创建数据库,shell脚本
    #1.安装依赖[root@db03~]#yuminstall-yautoconflibaio-devel#1.解压[root@db02~]#tarxfmysql-5.6.50-linux-glibc2.12-x86_64.tar.gz#2.创建app目录[root@db02~]#mkdir/app#3.移动目录[root@db02~]#mv/root/mysql-5.6.50-linux-glibc2.12-x86_64/a......
  • shell脚本执行make不一定调用Makefile
    今天在研究安卓编译流程时发现,shell脚本内执行make并不会调用当前路径下的Makefile,这有点让我大吃一惊。一番定位过后,发现执行make时调用的是另外一个shell脚本内定义的make函数,所以猜想可能GNUMake工具发现make有定义,所以直接调用该函数去了。于是我做了如下实验进行猜想验......
  • harbor登录时报错error parsing HTTP 404 response body: invalid character
    **harbor登录时报如下错误: **[root@localhostTDFW-publish-tar]#dockerlogin--username=user1192.168.2.10:8090--passworduser1_passWARNING!Using--passwordviatheCLIisinsecure.Use--password-stdin.Errorresponsefromdaemon:Get"http://192.168.2.......
  • Python IDLE和Shell的快捷键
     PythonIDLEShell的快捷键自动补全          Alt+/(查找编辑器内已经写过的代码来补全)上一条命令          Alt+P下一条命令          Alt+N PythonIDLE编辑器的快捷键自动补全代码        Alt+/(查找编辑器内已经写过的代码来补......
  • web DevOps / shell d3 / case
    s案例1:中断及退出案例2:基于case分支编写脚本案例3:编写一键部署软件脚本案例4:启动脚本案例5:使用Shell函数案例6:字符串处理案例7:字符串初值的处理1案例1:中断及退出1.1问题本案例要求编写两个Shell脚本,相关要求如下:从键盘循环取整数(0结束)并求和,输出最终结果1.2方......
  • 【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
    问题描述编写PythonFunction,并且在Function中通过 subprocess 调用powershell.exe执行powershell脚本。importazure.functionsasfuncimportloggingimportsubprocessapp=func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)defrun(cmd):completed......
  • 【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
    问题描述编写PythonFunction,并且在Function中通过 subprocess 调用powershell.exe执行powershell脚本。importazure.functionsasfuncimportloggingimportsubprocessapp=func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)defrun(cmd):compl......
  • 编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
    expect脚本:[10:40:45root@centos8~]#catexpect{1..3}#!/usr/bin/expectspawnssh10.0.0.151expect{    "yes/no"{send"yes\n";exp_continue}    "password"{send"123456\n"}}interact​######################......