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("镜像复制完成。")
kubernetes镜像仓库harbor
2023-12-09 07:12:02 31 阅读