gitlab服务作为公司的代码库比较重要,一般可由专门的运维工程师安装配置管理,基本流程大同小异,大概如下:
vim /etc/yum.repos.d/gitlab-ce.repo
添加内容:
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6
Repo_gpgcheck=0
Enabled=1
Gpgkey=https://packages.gitlab.com/gpg.key
更新yum缓存
sudo yum makecache
建议安装GitLab社区版日常够用了,企业版可以找极狐(国内gitlab)洽谈商务,当然个人破解的话,公司使用有风险,后果自负哈
yum install gitlab-ce # 自动安装最新版
yum install gitlab-ce-x.x.x #安装指定版本
安装可参照 https://developer.aliyun.com/article/74395
sudo gitlab-ctl start # 启动所有 gitlab 组件;
sudo gitlab-ctl stop # 停止所有 gitlab 组件;
sudo gitlab-ctl restart # 重启所有 gitlab 组件;
sudo gitlab-ctl status # 查看服务状态;
sudo gitlab-ctl reconfigure # 启动服务;
sudo vim /etc/gitlab/gitlab.rb # 修改默认的配置文件;
gitlab-rake gitlab:check SANITIZE=true --trace # 检查gitlab;
sudo gitlab-ctl tail # 查看日志;
gitlab是用于仓库管理系统的开源项目,使用git作为代码管理工具,并在此基础上搭建起来的web服务。
一般支持token和ssh两种。登录gitlab-》右上角settings:
ssh-keygen -o -t rsa -b 4096 -C "email@com" -f ~/.ssh/id_rsa
cat id_rsa.pub
ssh -T git@gitlab_hostname
pip install python-gitlab
import os
import time
from AuthineAcc.settings import GITLAB_PRIVATE_TOKEN, GITLAB_URL
import gitlab
import logging
# 日志格式设置
logger = logging.getLogger('gitlab')
class GitLabManage(object):
def __init__(self, user_token, gitlab_url):
self.user_token = user_token
self.gitlab_url = gitlab_url
# token登录
self.client = gitlab.Gitlab(url=self.gitlab_url, private_token=self.user_token, timeout=120)
def get_all_projects(self):
# 获取所有项目
return self.client.projects.list(all=True)
def get_all_users(self):
# 获取所有用户
return self.client.users.list(all=True)
def get_all_groups(self):
# 获取所有权限组
return self.client.groups.list(all=True)
def get_user_by_id(self, pk):
# 根据用户id查找用户详情
try:
user = self.client.users.get(pk)
except gitlab.exceptions.GitlabGetError as e:
return None
return user
def get_project_branches(self, name, path_with_namespace):
# 根据项目名称和组名查找项目
pros = self.client.projects.list(search=name, obey_rate_limit=False)
res = []
if not pros:
return res
pro = None
for _pro in pros:
if _pro.path_with_namespace == path_with_namespace:
pro = _pro
if pro:
res = pro.branches.list(all=True)
return res
return res
def import_project(self, data):
# 根据file(xxx.tar.gz)导入项目
logger.info(data)
if not data:
return None
try:
res = self.client.projects.import_project(file=open(data['file'], 'rb'), path=data['path'],
name=data['name'], namespace=data['group'])
except Exception as e:
logger.error(e)
return None
logger.info(res)
return res
def export_project_by_id(self, pk, file_dir, group=None):
# 根据项目id导出项目(xxx.tar.gz)
try:
logger.info(file_dir)
pro = self.client.projects.get(int(pk))
logger.info(pro.name)
export = pro.exports.create()
export.refresh()
while export.export_status != 'finished':
time.sleep(1)
export.refresh()
logger.info(export.export_status)
# Download the result
file_path = os.path.join(file_dir, '%s.tar.gz' % pro.name)
logger.info(file_path)
with open(file_path, 'wb') as f:
export.download(streamed=True, action=f.write)
except Exception as e:
logger.error(e)
return None
return {'file': file_path, 'name': pro.name, 'path': pro.name, 'group': group}
if __name__ == "__main__":
glm = GitLabManage(GITLAB_PRIVATE_TOKEN, GITLAB_URL)
user = glm.get_user_by_id(1)
print(user.name)
import csv
def write_to_csv(file_path, data):
with open(file_path, mode='a', newline='', encoding='utf-8') as f:
write = csv.writer(f, dialect='excel')
write.writerows(data)
def read_csv(file_path):
data = []
f = csv.reader(open(file_path))
logger.info(f)
for line in f:
if line:
data.append({'id': line[0], 'name': line[1], 'group': line[2], 'newgroup': line[3]})
return data