文章声明:此文基于木子实操撰写 生产环境:Rocky Linux release 8.3, gitlab-ce-13.9.4-ce, Python 3.6.8 问题关键字:Python,Gitlab API,Python对接Gitlab API,Python批量设置Gitlab镜像仓库
前述
最近几天一直没有发Rocky Linux相关基础技术文章,主要在于木子最近迷上了Golang,事情的起因在于之前写了一篇关于《在Rocky Linux 8.3 RC1上安装GitLab实现代码仓库同步容灾》,有博友反馈公司有上千个仓库,如果需要一个一个去设置镜像仓库,势必会干晕一批人,于是就想着写一个批量设置Gitlab镜像仓库的脚本,写完跑了一遍用时11分多钟,太久了...,之前一直听说Golang很快,抱着学习的态度,试着用Golang重写了一遍(如果刚开始一直写弱类型语言,第一次开始写强类型语言,会比较痛苦,木子也是第一次写Golang @-@),同样的实现方式,发现在Golang下执行只需要30多秒,瞬间秒杀一切。本着以练代学的精神,于是开始使用Golang来实现一套相对完善的Gitlab镜像仓库同步功能,其中包括:创建组、创建仓库、同步组、同步仓库、设置镜像仓库等功能。这也是为什么最近木子一直没有更新博文的原因。
来一个 Golang & Python 速度对比
# Golang ./setting-gitlab-mirror-repo 1.12s user 0.94s system 5% cpu 34.574 total # Python3 python3 ./setting-gitlab-mirror-repo.py 6.83s user 0.50s system 0% cpu 17:11.92 totalPython代码实现
以下Python脚本仅仅实现设置镜像仓库功能,适用于第一次批量设置镜像仓库。
#! /usr/bin/env python3 # -*- coding: UTF-8 -*- import requests # 拥有管理员权限的访问令牌 headers = {"PRIVATE-TOKEN": "xxxxxx"} # 源git服务器仓库地址 fromgitapiaddress = "https://git.oubayun.com/api/v4/projects" # sync为同步服务器前缀,木子这里源服务器api接口地址为https://git.oubayun.com/api/v4/projects,目标服务器api接口地址为:https://sync.oubayun.com/api/v4/projects togitapiaddress = "https://username:password@sync" # 获取所有仓库地址 def get_all_repo(fromgitapiaddress, headers): # 存储所有仓库地址 allrepoaddress = [] # 通过返回的仓库数量确认是否还需要翻页 allreponum = 1 # 当前打开第P页 pg = 1 while allreponum != 0: payload = {"private": "true", "per_page": "100", "page": "%s" % pg} res = requests.get(url=fromgitapiaddress, params=payload, headers=headers) allreop = res.json() allreponum = len(allreop) for i in range(allreponum): allrepoaddress.append("%s,%s" % (allreop[i]["id"], allreop[i]["http_url_to_repo"])) pg = pg + 1 return(allrepoaddress) # 获取仓库是否已经设置了镜像地址 def get_mirrorurl(fromgitapiaddress, repoid, headers): gitpageaddress = "%s/%s/remote_mirrors" % (fromgitapiaddress, repoid) res = requests.get(url=gitpageaddress, headers=headers) return(res.json()) # 设置镜像仓库地址 def set_mirrorurl(fromgitapiaddress, repoid, mirrorurl, headers): gitpageaddress = "%s/%s/remote_mirrors" % (fromgitapiaddress, repoid) payload = {"url": mirrorurl, "enabled": "true"} res = requests.post(url=gitpageaddress, data=payload, headers=headers) return(res.text) if __name__ == "__main__": allrepoaddress = get_all_repo(fromgitapiaddress, headers) print(allrepoaddress) for repoinfo in allrepoaddress: repoid = repoinfo.split(",")[0] repourl = repoinfo.split(",")[1] # 如果您返回的repourl是http就截取10位,如果是https就截取11位(与是否有用反向代理有关) mirrorurl = togitapiaddress + repourl[10:] print(mirrorurl) repo_mirror_url = get_mirrorurl(fromgitapiaddress, repoid, headers) print(repo_mirror_url) if len(repo_mirror_url) == 0: print("源仓库ID: %s, 源仓库地址: %s, 目标仓库地址L: %s" % (repoid, repourl, mirrorurl)) print(set_mirrorurl(fromgitapiaddress, repoid, mirrorurl, headers)) print("所有项目数量为: %s" % len(allrepoaddress))参考文献
[1] Gitlab获取项目地址API: https://docs.gitlab.com/ee/api/projects.html [2] Gitlab仓库镜像配置API: https://docs.gitlab.com/ee/api/remote_mirrors.html#project-remote-mirrors-api
下篇预告:基于Rocky Linux 8.3 RC1搭建Rsync冷备容灾服务器,如果您有任何想学习了解的技术,欢迎在下方留言,木子将根据需求输出对应基础技术博文。
五平台同步更新: 博客: 欧巴云 知乎: 欧巴云 51CTO: 欧巴云 云+社区: 欧巴云 微信公众号: 欧巴云