harbor仓库镜像迁移脚本

import subprocess
import json
import logging

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# 替换这里的Harbor仓库地址和凭据
harbor_url = "https://harbor.test.com"
harbor_name = "harbor.test.com"
username = "admin"
password = "Harbor12345"

# 替换为新仓库的地址和凭据
new_harbor_url = "https://harbor.test.com"
new_harbor_name = "harbor.test.com"
new_harbor_pro = "test"
new_username = "admin"
new_password = "Harbor12345"

# 执行Curl命令获取项目列表
try:
    curl_output = subprocess.check_output(
        f'curl --insecure -u "{
     username}:{
     password}" -X GET -H "Content-Type: application/json" {
     harbor_url}/api/v2.0/projects|jq', 
        shell=True
    ).decode('utf-8')
except subprocess.CalledProcessError as e:
    logging.error(f"获取项目列表时出错: {
     e}")
    exit(1)

# 将Curl命令的输出解析为JSON
try:
    projects = json.loads(curl_output)
except json.JSONDecodeError as e:
    logging.error(f"无法解析JSON数据: {
     e}")
    exit(1)

# 遍历项目列表
for project in projects:
    project_name = project["name"]
    
    # 执行Curl命令获取项目下的镜像列表
    try:
        curl_output = subprocess.check_output(
            f'curl --insecure -u "{
     username}:{
     password}" -X GET -H "Content-Type: application/json" {
     harbor_url}/api/v2.0/projects/{
     project_name}/repositories|jq', 
            shell=True
        ).decode('utf-8')
    except subprocess.CalledProcessError as e:
        logging.error(f"获取项目 {
     project_name} 的镜像列表时出错: {
     e}")
        continue

    # 将Curl命令的输出解析为JSON
    try:
        repositories = json.loads(curl_output)
    except json.JSONDecodeError as e:
        logging.error(f"无法解析项目 {
     project_name} 的镜像列表: {
     e}")
        continue

    # 遍历镜像列表
    for repo in repositories:
        repo_name = repo["name"]
        repo_name_new_repo_name = repo_name.split("/", 1)

        if len(repo_name_new_repo_name) > 1:
            # 如果成功分割出两部分,repo_name_new_repo_name[1] 将包含第一个斜杠之后的部分
            new_repo_name = repo_name_new_repo_name[1]
        else:
            # 如果没有斜杠或只有一个斜杠,将保持原始 repo_name
            new_repo_name = repo_name
        
        # 执行Curl命令获取镜像标签列表
        try:
            curl_output = subprocess.check_output(
                f'curl --insecure -u "{
     username}:{
     password}" -X GET -H "Content-Type: application/json" {
     harbor_url}/api/v2.0/projects/{
     project_name}/repositories/{
     new_repo_name}/artifacts', 
                shell=True
            ).decode('utf-8')
        except subprocess.CalledProcessError as e:
            logging.error(f"获取镜像 {
     repo_name} 标签列表时出错: {
     e}")
            continue

        # 提取标签信息
        try:
            artifacts = json.loads(curl_output)
            for image_info in artifacts:
                digest = image_info["digest"]
                tags = image_info["tags"]
                # 遍历每个标签并构建镜像地址
                for tag in tags:
                    tag_name = tag["name"]
                    source_image_url = f"{
     harbor_name}/{
     project_name}/{
     new_repo_name}:{
     tag_name}"
                    new_image_url = f"{
     new_harbor_name}/{
     new_harbor_pro}/{
     new_repo_name}:{
     tag_name}"
                    
                    # 拉取源镜像
                    try:
                        subprocess.check_call(
                            f'docker pull {
     source_image_url}',
                            shell=True
                        )
                    except subprocess.CalledProcessError as e:
                        logging.error(f"无法拉取镜像 {
     source_image_url}: {
     e}")
                        continue
                    
                    # 标记新的镜像地址
                    try:
                        subprocess.check_call(
                            f'docker tag {
     source_image_url} {
     new_image_url}',
                            shell=True
                        )
                    except subprocess.CalledProcessError as e:
                        logging.error(f"无法标记镜像 {
     new_image_url}: {
     e}")
                        continue
                    
                    # 推送新镜像到新仓库
                    try:
                        subprocess.check_call(
                            f'docker push {
     new_image_url}',
                            shell=True
                        )
                    except subprocess.CalledProcessError as e:
                        logging.error(f"无法推送镜像 {
     new_image_url}: {
     e}")
                        continue
                    
                    # 删除本地源镜像
                    try:
                        subprocess.check_call(
                            f'docker rmi {
     source_image_url}',
                            shell=True
                        )
                    except subprocess.CalledProcessError as e:
                        logging.error(f"无法删除本地镜像 {
     source_image_url}: {
     e}")
                        continue
                    
        except (json.JSONDecodeError) as e:
            logging.error(f"无法解析镜像 {
     repo_name} 的标签信息: {
     e}")
            continue

logging.info("镜像复制完成。")


相关推荐

  1. harbor仓库镜像迁移脚本

    2023-12-09 07:12:02       23 阅读
  2. docker 镜像仓库harbor安装

    2023-12-09 07:12:02       47 阅读
  3. Docker Harbor私有镜像image仓库安装

    2023-12-09 07:12:02       39 阅读
  4. docker: 搭建 harbor 镜像仓库

    2023-12-09 07:12:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 07:12:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 07:12:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 07:12:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 07:12:02       18 阅读

热门阅读

  1. C_15练习题

    2023-12-09 07:12:02       31 阅读
  2. ubuntu22.04安装过程记录

    2023-12-09 07:12:02       46 阅读
  3. blender 数字键盘上的快捷键

    2023-12-09 07:12:02       45 阅读
  4. 我的创作纪念日

    2023-12-09 07:12:02       38 阅读
  5. Vue2学习(组件的使用)

    2023-12-09 07:12:02       43 阅读
  6. 有关CSS选择器

    2023-12-09 07:12:02       39 阅读
  7. C++_对C数据类型的扩展

    2023-12-09 07:12:02       29 阅读
  8. ChatGPT的常识

    2023-12-09 07:12:02       42 阅读
  9. 蓝桥杯ACwing习题

    2023-12-09 07:12:02       33 阅读
  10. 美国Linux服务器的iptables防火墙介绍

    2023-12-09 07:12:02       40 阅读
  11. iClient3D 加载天地图服务

    2023-12-09 07:12:02       28 阅读
  12. MySQL七 | 存储引擎

    2023-12-09 07:12:02       40 阅读
  13. Visual Studio 2015 中 FFmpeg 开发环境的搭建

    2023-12-09 07:12:02       34 阅读