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("镜像复制完成。")


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

(~ ̄▽ ̄)~凤凰涅槃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值