目录
前言
一提起 Git,大家基本上都会想到 GitHub, GitLab 这一类在线托管平台。
我们可能习惯了在这些平台上创建仓库、推拉代码、创建 Pull/Merge Request、发起 Issue。但这些特性都不是 Git 提供的,Git 能跟踪的只有代码信息。这类平台是在 Git 之上重新构建了一套 Web 系统来保存诸如用户、Pull/Merge Request、Issue 等信息的。
如果你只是想简单控制一下用户权限,则不需要这么复杂的系统,gitolite 就够用。我也是偶然发现 gitolite 的,很喜欢这种轻量级的设计。但我没想到的是好多著名的开源项目也在使用 gitolite:
- linux 内核
- gentoo linux 发行版
- fedora linux 发行版
gitolite 的原理很简单。首先,它提供了一个 gitolite-shell。当你使用 ssh 访问 gitolite 时 ssh 服务会启动 gitolite-shell。这个脚本会检查当前用户是否有权限访问对应的仓库。如果用户通过 git push 修改了仓库内容,则会触发 gitolite 预先配置 git hook,这些 hook 会执行创建仓库、添加用户之类的操作。[1]
本文将详细介绍如何搭建一个gitolite服务器。
一、创建虚拟机
确保局域网的机器所有的IP地址都在一个网段192.168.163.***
二、修改虚拟机用户名
确保/etc/hosts, /etc/sysconfig/network, gitolite.rc文件中的hostname一致,本文统一为gitserver1,gitserver2, gitserver3 …
查看用户名
[root@localhost ~]$ hostname
[root@localhost ~]$ vim /etc/hosts
hosts文件,给127.0.0.1添加hostname
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 gitserver1
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.163.110 gitserver1
192.168.163.111 gitserver2
192.168.163.112 gitserver3
重启服务器
[root@localhost ~]$ vim /etc/sysconfig/network # 编辑network文件修改hostname行
[root@centos6 ~]$ cat /etc/sysconfig/network # 检查修改
NETWORKING=yes
HOSTNAME= gitserver1
重启系统
三、创建用户
一般都将新建的用户放到/home文件路径下,切换到root用户下,安装所需要的依赖文件:
[root@localhost ~]# yum install zlib-devel perl-CPAN perl-devel curl-devel gettext expat-devel gettext-devel openssl-devel perl-Time-HiRes git -y
创建git用户
[root@localhost ~]# adduser –d /home/git git
给git用户创建密码
[root@localhost ~]# echo ‘*****’ | passwd –stdin git
创建bin文件夹,配置环境变量
[root@localhost ~]# vim /etc/profile
在最后,添加:
#gitolite
export PATH="~/bin:$PATH"
保存,退出,然后运行:
[root@localhost ~]# source /etc/profile
不报错则成功。
[root@localhost ~]# vim /usr/lib/tmpfiles.d/tmp.conf
添加如下一条
X /tmp/*.pub
切换到git用户,创建公钥和私钥对,在服务器端生成公钥和私钥对。
[git@localhost ~]$ ssh-keygen -t rsa
将服务器的公钥发送到客户端的ssh目录(/tmp/)并命名为gitserver1.pub。gitserver1上创建的公钥复制到gitserver2上,命名为:gitserver1.pub。
[git@localhost gitserver1]$ scp ~/.ssh/id_rsa.pub git@gitserver2:/tmp/gitserver1.pub
gitserver2上创建的公钥复制到gitserver1上,命名为:gitserver2.pub。
[git@localhost gitserver2]$ scp ~/.ssh/id_rsa.pub git@gitserver1:/tmp/gitserver2.pub
修改tmp文件删除规则,禁止centos 10天后自动删除pub文件
四、通过源码安装gitolite
[git@localhost ~]$ mkdir bin
使用改进后的Gitolite版本库github.com/ossxp-com/gitolite.git
[git@localhost ~]$ git clone git://github.com/ossxp-com/gitolite.git
安装Gitolite
[git@localhost ~]$ cd gitolite
[git@localhost gitolite]$ ./gitolite/src/gl-system-install
运行: gl-setup完成设置。
[git@localhost ~]$ vim ~/bin/gl-setup
修改GL_PACKAGE_CONF=$HOME/share/gitolite/conf,将客户机的id_rsa.pub文件上传到gitserver1的目录下/tmp/admin.pub。运行下面的命令进行设置
[git@localhost bin]$ ./gl-setup /tmp/admin.pub
五、版本库镜像
打开gitserver1上Gitolite的配置文件:file:`~/.gitolite.rc`,进行如下设置:
[git@localhost ~]$ vim ~/.gitolite.rc
$GL_HOSTNAME = 'gitserver1';
$GL_GITCONFIG_KEYS = "gitolite.mirror.*";
$GL_PACKAGE_CONF = $ENV{HOME} . "/share/gitolite/conf";
$GL_PACKAGE_HOOKS = $ENV{HOME } . "/share/gitolite/hooks";
$SVNSERVE = $ENV{HOME} . "/bin/svnserve -r /opt/svn/svnroot/ -t --tunnel-user=%u";
同理对gitserver2等机器进行同样的设置,在gitserver1上运行gl-tool设置其他服务器到本服务器上的公钥认证。
[git@localhost bin]$ ./gl-tool add-mirroring-peer /tmp/gitserver2.pub
在gitserver2上运行gl-tool设置其他服务器到本服务器上的公钥认证。
[git@localhost bin]$ ./gl-tool add-mirroring-peer /tmp/gitserver1.pub
验证gitserver1和gitserver2的连接情况:
[git@localhost gitserver1]$ ssh git@gitserver2 info
Hello gitserver1, I am gitserver2
出现上述欢迎语,说明连接成功
转到客户端,创建文件夹gitserver_1,存放来自gitserver_1的gitolite配置文件
[git@localhost ~]$ mkdir gitserver_1
[git@localhost ~]$ cd gitserver_1
[git@localhost ~]$ git clone git@gitserver_1:gitolite-admin.git
[git@localhost gitserver_1]$ cd gitolite-admin
设置gitserver_1下的文件conf/gitolite.conf
repo testing
config gitolite.mirror.master = "gitserver1"
config gitolite.mirror.slaves = "gitserver2 gitserver3"
上传改动到gitserver1,对gitserver2、gitserver3…进行相同的设置,并上传到相应的服务器。
六、验证镜像是否成功
登录到gitserver1, gitserver1,gitserver1上,查看testing.git库下的config的内容如下,说明镜像配置成功
[core]
repositoryformatversion = 0
filemode = true
bare = true
[gitweb]
owner = admin
[gitolite "mirror"]
master = gitserver1
slaves = gitserver2 gitserver3
在客户机下,增加一个文件gitserver1/testing/readme.txt,并将修改推送到master,在gitserver2/testing下运行如下命令
[git@localhost testing]$ git pull
如果testing下出现了readme.txt更新文件,说明镜像功能成功运行。
七、新增加用户
将新用户的公钥复制到gitserver1、gitserver2、gitserver3的gitolite-amin/keydir目录下,并命名为webpack.pub
[git@localhost keydir]$ scp wp@192.168.163.22:/home/wp/.ssh/id_rsa.pub webpack.pub
将公钥提交公钥到各个服务器。
[git@localhost keydir]$ git add –A
[git@localhost keydir]$ git commit –m “add new user webpack”
[git@localhost keydir]$ git push origin master
更改用户授权:
[git@localhost keydir]$ vim /gitserver1/gitolite-admin/conf/gitolite.conf
@admin = admin
@team = gitclient1 webpack
repo gitolite-admin
RW+ = @admin
R = @all
repo users/CREATOR/[a-zA-Z].*
C = @all
RW+ = CREATOR
RW = WRITERS
R = READERS @admin
# repo description for testing repo on gitweb
testing "admin" = "sandbox, everyone can push"
repo testing
#config hooks.mailinglist = testing-commit@list.foo.bar
#config hooks.announcelist = testing-commit@list.foo.bar
RW = @admin
RW = @team
config gitolite.mirror.master = "gitserver1"
config gitolite.mirror.slaves = "gitserver2 gitserver3"
提交授权到各个服务器
[git@localhost keydir]$ git add –A
[git@localhost keydir]$ git commit –m “authorize webpack”
[git@localhost keydir]$ git push origin master
八、创建远程仓库
在gitadmin机器上修改每个gitserver的conf文件直接推送之后,自动创建远程仓库。
repo pipeline
C = @team
RW = @team
R = @admin
这个表示可以创建固定路径的仓库如:/pipeline.git
[git@localhost conf]$ git add -A
[git@localhost conf]$ git commit –m “create pipeline repository”
[git@localhost conf]$ git push origin master
参考资料
[1] https://zhuanlan.zhihu.com/p/75921691
[2] https://blog.csdn.net/hnhuangyiyang/article/details/50993467#t2