Chinaunix首页 | 论坛 | 博客

OPS

  • 博客访问: 490136
  • 博文数量: 117
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1210
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-05 14:50
个人简介

hellow 运维

文章分类

全部博文(117)

文章存档

2019年(1)

2018年(1)

2017年(45)

2016年(38)

2015年(32)

我的朋友

分类: 系统运维

2015-11-12 11:15:22

参考:
一.部署环境  

    系统:CentOS release 6.5 (Final)  

   IP:192.168.3.27

二.安装基础依赖包   

 
[root@nginx ~]#yum install -y gcc-c++ gcc make curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel -y


三.下载git-2.2.0.tar.gz

 
[root@nginx ~]#wget 


四.解压文件到当前目录下,并安装

[root@nginx git-2.2.0]#tar xf git-2.2.0.tar.gz
[root@nginx git-2.2.0]#cd  git-2.2.0/
[root@nginx git-2.2.0]#make prefix=/usr/local/git all
[root@nginx git-2.2.0]#make prefix=/usr/local/git install
 
增加软连接
[root@nginx git-2.2.0]#ln -s /usr/local/git/bin/* /usr/bin/
 
验证结果,显示出版本号,表示安装成功
[root@nginx git-2.2.0]#git --version
git version 2.2.0


五.创建git用户

 
[root@nginx ~]# 
 useradd -c 'git version manage' -m -d /home/git -s bin/bash  git


六.在git用户家目录下安装gitolite

[root@nginx ~]# su git
[git@nginx root]$ cd
[git@nginx ~]$ mkdir bin


七.克隆gitolite源码

[git@nginx ~]$ git clone 
Cloning into 'gitolite'...remote: Counting objects: 8884, done.
remote: Total 8884 (delta 0), reused 0 (delta 0), pack-reused 8884Receiving 
objects: 100% (8884/8884), 3.66 MiB | 179.00 KiB/s, done.
Resolving deltas: 100% (5055/5055), done.Checking connectivity... done.
[git@nginx ~]$ ls
bin  gitolite


八.安装gitolite

[git@nginx ~]$ ./gitolite/install --to /home/git/bin/
[git@nginx ~]$ ls bin/
commands  gitolite  gitolite-shell  lib  syntactic-sugar  triggers  VERSION  VREF


九.配置gitolite管理员

    生成管理员账户的公钥(此处指定本地root用户为管理员,键入回车使用默认值)

[root@nginx ~]# ssh-keygen 
[root@nginx ~]# cp .ssh/id_rsa.pub /tmp/admin.pub
 
切换回git用户,为gitolite配置管理员 
[git@nginx ~]$ bin/gitolite setup -pk /tmp/admin.pub 
Initialized empty Git repository in /home/git/repositories/gitolite-admin.git
/Initialized empty Git repository in /home/git/repositories/testing.git/
WARNING: /home/git/.ssh missing; creating a new one    
(this is normal on a brand new install)
WARNING: /home/git/.ssh/authorized_keys missing; creating a new one    
(this is normal on a brand new install)
 
[git@nginx ~]$ ls
bin  gitolite  projects.list  repositories


十.管理员日常管理

[root@nginx ~]# git clone git@192.168.3.27:gitolite-admin
Cloning into 'gitolite-admin'...The authenticity of host 
'192.168.3.27 (192.168.3.27)' can't be established.
RSA key fingerprint is c4:34:02:55:ad:42:8a:65:ba:94:00:20:48:d7:3c:33.
Are you sure you want to continue connecting (yes/no)? yes
#这里是第一次连接,需要输入yes进行确认
Warning: Permanently added '192.168.3.27' (RSA) to the list of known hosts.
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (6/6), 734 bytes | 0 bytes/s, done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Checking connectivity... done.
 
[root@nginx gitolite-admin]# ls
conf  keydir
[root@nginx gitolite-admin]# pwd
/root/gitolite-admin


验证:

创建库、添加用户 

例如某test用户访问git服务器上的mytest库


test用户向git服务器管理(此处是之前的服务器本地的root用户)提交自己的ssh无密码公钥


管理员将test的公钥复制到 gitolite-admin/keydir/ 下


注:该key需要在test用户机器上使用ssh-keygen生成,再传送到192.168.3.27/tmp目录下

 
[root@nginx ~]# cp /tmp/test.pub /root/gitolite-admin/keydir/

管理员创建myFirstRepo库,并给test分配权限

[root@nginx ~]# cd gitolite-admin/conf/
[root@nginx conf]# vim gitolite.conf 
#以下是gitolite.conf的默认内容
repo gitolite-admin    
    RW+     =   admin
repo testing    
    RW+     =   @all
    
下边定义mytest库,并且指定用户权限:
#在gitolite.conf中添加以下内容
@mygroup    = test
repo mytest    
    RW+     = @mygroup
注:此处@mygroup是一个组,给mygroup组赋予对mytest这个库的读、写、推送的权限
(详细规则可参考gitolite的readme.txt)管理员将对gitolite-admin的修改(建库、加用户)
提交到git服务器
 
[root@nginx gitolite-admin]# pwd
/root/gitolite-admin
[root@nginx gitolite-admin]# git status    #查看git库状态
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:  
(use "git add ..." to update what will be committed)  
(use "git checkout -- ..." to discard changes in working directory)  
 
    modified:   conf/gitolite.conf        #表示该文件被修改
Untracked files:  (use "git add ..." to include in what will be committed)
    keydir/test.pub        #增加的文件
no changes added to commit (use "git add" and/or "git commit -a")
 
#将有改动的文件添加到git库
[root@nginx gitolite-admin]# git add keydir/test.pub conf/gitolite.conf 
[root@nginx gitolite-admin]# git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:  (use "git reset HEAD ..." to unstage) 
    modified:   conf/gitolite.conf  
    new file:   keydir/test.pub  
    #这里表示已添加#在第一次执行git commit命令时会有以下提示,需要使用者表明身份
 
[root@nginx gitolite-admin]# git commit -m "add repo mytest; add user test"
*** Please tell me who you are.Run  
    git config --global user.email "you@example.com"  
    git config --global user.name "Your Name"to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@nginx.(none)')
 
#在这里我们执行以下2条命令
[root@nginx gitolite-admin]# git config --global user.email "hanye@163.com"
[root@nginx gitolite-admin]# git config --global user.name "hanye"
 
#再重新执行git commit命令
[root@nginx gitolite-admin]# git commit -m "add repo mytest; add user test"
[master 7b877e7] add repo mytest; 
add user test 2 files changed, 5 insertions(+) 
create mode 100644 keydir/test.pub
 
#执行完以上命令后,修改的文件只是提交在本地了,并没有提交到git server中,还需要执行git push命令
[root@nginx gitolite-admin]# git push origin master    #推送到远端的master分支
Counting objects: 6, done.Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 817 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: Initialized empty Git repository in /home/git/repositories/mytest.git
/To git@192.168.3.27:gitolite-admin   
3554f3d..7b877e7  master -> master


客户端验证:

[root@ipython ~]# git clone git@192.168.3.27:/mytest.git
Initialized empty Git repository in /root/mytest/.git/
warning: You appear to have cloned an empty repository.
[root@ipython ~]# ll
total 3240
drwxr-xr-x 3 root root    4096 Apr 17 14:45 mytest
[root@ipython ~]# cd mytest/
[root@ipython mytest]# git status
# On branch master#
# Initial commit
#nothing to commit (create/copy files and use "git add" to track)
 
#创建1个文件1.txt并添加到git
[root@ipython mytest]# touch 1.txt
[root@ipython mytest]# git add 1.txt 
[root@ipython mytest]# git commit "add file 1.txt"
 
#第一次使用需要表明身份
[root@ipython mytest]# git commit -m "add file 1.txt"
[master (root-commit) 08e9a37] add file 1.txt 
Committer: root 
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:    
    git config --global user.name "Your Name"    
    git config --global user.email you@example.com
If the identity used for this commit is wrong, you can fix it with: 
   git commit --amend --author='Your Name '
    0 files changed, 0 insertions(+), 0 deletions(-) 
    create mode 100644 1.txt
 
[root@ipython mytest]# git config --global user.email test@aaa.com
[root@ipython mytest]# git config --global user.name test
 
[root@ipython mytest]# git commit -m "add file 1.txt"
# On branch masternothing to commit (working directory clean)
 
[root@ipython mytest]# git status
# On branch masternothing to commit (working directory clean)
 
[root@ipython mytest]# ll
total 0-rw-r--r-- 1 root root 0 Apr 17 14:47 1.txt
 
[root@ipython mytest]# git push origin master
Counting objects: 3, done.Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)To git@192.168.3.27:/mytest.git
 * [new branch]      master -> master

Git是个分布式版本控制系统,可以随心所欲的设定提交者的用户名和邮件地址(如用下命令)。在团队协作时,这太不安全了。如果团队成员冒名他人向服务器版本库推送新提交时,将无从查起。

git config user.name

git config user.email

       目前搭建Git服务器,常用的有Gitolite、Gitosis和Gerrit。其中Google开发的Gerrit审核服务器对提交者的邮件地址进行了审核(但对user.name没有审核)。而Gitolit和Gitosis压根就没审核。

       我最近搭建了Gitolite + SSH认作为Git服务器,配合Redmine作为需求管理和缺陷跟踪,实现跟Git的整合。Redmine中的用户用一个ID标识,而Git的提交作者 包含用户名和邮件地址,可以将Redmine的用户ID和Git的提交作者相关联。

显然,如果在Git提交时随意变更提交者的姓名和邮件地址,就会破坏Remine软件中设置好的用户对应关系。

 

3. 实现Gitolite服务器对提交作者信息进行审核

       首先,Gitolite本身不带有这样的功能,那如何实现Gitolite服务器对提交作者信息进行审核呢?立刻闪过我脑袋的想法就是git的hooks,到Gitolite官方文档中找到了答案。

 

gitolite documentation官网:

 

       使用pre-receive钩子,pre-receive文件内容如下:

  1. #!/bin/sh  
  2. #  
  3. mismatch=0  
  4. while read old new ref; do  
  5.   author=`git show --pretty=format:%an $new | head -1`  
  6.   email=`git show --pretty=format:%ae $new | head -1`  
  7.     
  8. #  echo "     email = \"$email\", author = \"$author\", GL_USER = \"$GL_USER\""  
  9.     
  10.   if test "$GL_USER" != "$author"; then  
  11.     echo  
  12.     echo "ERROR: Invalid user name on object $new:"  
  13.     echo "       Expecting \"$GL_USER\", got \"$author\""  
  14.     mismatch=1  
  15.   fi  
  16.     
  17.   if test "$GL_USER@yaxon.com" != "$email"; then  
  18.     echo  
  19.     echo "ERROR: Invalid user email on object $new:"  
  20.     echo "       Expecting \"$GL_USER@yaxon.com\", got \"$email\""  
  21.     mismatch=1  
  22.   fi  
  23. done  
  24.   
  25. if test $mismatch -eq 1; then  
  26.   echo  
  27.   echo "Please run the following commands and try again:"  
  28.   echo "> git config user.name \"$GL_USER\""  
  29.   echo "> git config user.email \"$GL_USER@yaxon.com\""  
  30.   echo "> git commit --amend --author=\"$GL_USER <$GL_USER@yaxon.com>\""  
  31.   echo  
  32.   exit 1;  
  33. fi  
  34.   
  35. exit 0;  

 稍加说明,你应该就能明白怎么实现的了:

◆用git show解析出commit的提交作者(author)和邮件地址(email)。

◆用Gitolite自带的环境变量$GL_USER,得到git push时用的SSH用户信息。这个$GL_USER其实就是Gitolit服务器上安装目录\.gitolite\keydir下的SHH公钥文件名 (对应着一个SSH连接用户,一般命名为团队成员的姓名)。

 

4. pre-receive钩子放哪里?

       有两种选择:

◆如果要对某一个版本库进行审核,只要将pre-receive文件放在Gitolite服务器上指定的版本库的中的hooks目录下即可。

◆如果要对Gitolite服务器上所有的版本库进行审核。可以按如下步骤进行:

①、以安装gitolite的用户身份(如git)登陆gitolite服务器。(可以用SSH登陆);

②、如将pre-receive拷贝至gitlolite安装目录下/home/git/.gitolite/hooks/common/

③、执行./bin/gitolite setup --hooks-only


有次安装完了,git push时,老提示我"error: cannot run hooks/pre-receive: No such file or directory",最后发现时因为pre-receive文件换行符是windows的\r\n搞的鬼,改成\n即可。











阅读(2072) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~