harbor低版本迁移到高版本

      harbor低版本的数据不能直接导入到高版本,因为其中数据库字段,表等等均会在版本的升级过程中发生变化,需要将老的harbor中的镜像导出到本地,打上tag,再推送到新的harbor服务器,其整体思路如下:

           1.通过harbor的API获取到所有的项目

           2.通过项目获取到所有的repo

           3.通过repo获取到所有的tag

           4.将项目,repo,tag拼接起来,形成一个pull,tag和push的命令列表并依次执行

需要注意点:

         新版本的harbor需要post请求需要带上cookies字段,为了防止csrf攻击

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import  requests
import  json
import  os
import  logging

original_har_srv = "harbor.galaxyclouds.com"
target_har_srv   = "harbor.dev.galaxyclouds.com"
 
def http_get_request(url):
    headers = {'Authorization': 'Basic YWRtaW46SGFyYm9yMTIzNDU='}
    response = requests.request("GET", url, headers=headers,verify=False)
    #print(response.text)
    return json.loads(response.text)
    
def get_projects(original_har_srv):
    project_url = "https://"+ original_har_srv + "/api/projects" 
    projects = http_get_request(project_url)
    list_usrful_projects_data = []
    for  project in projects:
        dict_useful_project_data = {}
        dict_useful_project_data['project_id']  = project['project_id'] 
        dict_useful_project_data['name']        = project['name']
        dict_useful_project_data['repo_count"'] = project['repo_count']
        list_usrful_projects_data.append(dict_useful_project_data)
    #print(list_usrful_projects_data)
    return list_usrful_projects_data
    
def get_repo_by_project(project,original_har_srv):
    repo_url = "https://" + original_har_srv + "/api/repositories?project_id=" + str(project['project_id'])
    repos = http_get_request(repo_url)
    list_repos_useful_data = []
    for repo in repos:
        dict_repo_useful_data = {}
        dict_repo_useful_data['id']          = repo['id']
        dict_repo_useful_data['name']        = repo['name']
        dict_repo_useful_data['tags_count']  = repo['tags_count']
        list_repos_useful_data.append(dict_repo_useful_data)
    return list_repos_useful_data

def get_tags_by_repo(repo,original_har_srv):
    tag_url = "https://" + original_har_srv + "/api/repositories/" + repo['name'] + "/tags"
    tags    = http_get_request(tag_url)
    list_tags_useful_data = []
    for tag in tags:
        list_tags_useful_data.append(tag['name'])
    return list_tags_useful_data

def create_project(project,target_har_srv):
    create_url = "https://" + target_har_srv + "/api/v2.0/projects"
    payload="{\r\n  \"project_name\": \"" + project+ "\",\r\n  \"metadata\": {\r\n    \"public\": \"true\"\r\n  },\r\n  \"storage_limit\": -1,\r\n  \"registry_id\": null\r\n}"
    headers = {
      'Authorization': 'Basic YWRtaW46SGFyYm9yMTIzNDU=',
      'Content-Type': 'application/json',
      'Cookie': 'Cookie_789=value'
    }
    response = requests.request("POST", create_url, headers=headers, data=payload,verify=False)

    
#create_project("liyao3",target_har_srv)
list_usrful_projects_data = get_projects(original_har_srv)
#docker pull harbor.galaxyclouds.com/app/gin_project:master-0302711e
docker_target  = []
project_create = []
for project in list_usrful_projects_data:
    repos = get_repo_by_project(project,original_har_srv)
    project['repos'] = repos
    create_project(project['name'],target_har_srv)
    for repo in project['repos']:
        tags_use_useful_data = get_tags_by_repo(repo,original_har_srv)
        for tag in tags_use_useful_data:
            cur_tag_docker_target ='/'+ repo['name'] + ":" + tag
            docker_target.append(cur_tag_docker_target)

for target in docker_target:
    pull_cmd = "docker pull " + original_har_srv + target 
    tag_cmd  = "docker tag "  + original_har_srv + target + "  " + target_har_srv + target
    push_cmd = "docker push " + target_har_srv   + target
    # print(pull_cmd)
    # print(tag_cmd)
    # print(push_cmd)
    os.system(pull_cmd)
    os.system(tag_cmd)
    os.system(push_cmd)

上一篇:Linux学习


下一篇:docker离线安装使用yum