您的当前位置:首页正文

gitlab版本升级并docker化

来源:化拓教育网

写在前面:

这两天把公司的gitlab服务从老版本(7.12.0,源码版本)升级到了最新版(8.14.3,docker化版本)。过程中遇到不少坑,最后总算升级成功了。感觉光看官方文档很难升级成功,还需要研究gitlab的一些实现机制,所以特此把�过程总结记录下来,以便日后查阅。

update.png

第一步,备份数据

由于老版本gitlab的目录的owner是git,需要切换到root,才有权限做备份。

[frank gitlab]$ sudo su - root
[root gitlab]$ sudo -u git -H bundle exec rake gitlab:backup:create RAILS_ENV=production

gitlab会将数据备份成一个tar文件,存放在目录:/home/git/gitlab/tmp/backups/

第二步,将数据解压缩到backup目录

mkdir -p /mnt/docker/gitlab/backup
cd /mnt/docker/gitlab/backup
sudo tar xvf /home/git/gitlab/tmp/backups/1481629084_gitlab_backup.tar

会解压出如下几个目录文件:

  • db:存放数据库备份文件database.sql
  • repositories:存放每个git项目的仓库(repo)。每个repo被打包成为扩展名为bundle的文件,其实质上是个tar文件。
  • uploads:存放用户上传的图片等文件。
  • backup_information.yml:记录了backup相关信息,数据库,gitlab版本等。

如果使用gitlab提供的rake:backup:restore命令还原数据,是不可行的。原因比较多,首先在第一次启动gitlab时,会做db migrate,但此时数据库还没有导入备份的数据库数据呢。之后在做restore时,db的备份文件已无法导入到migrate过的数据库中了,会报错;另外repositories下git项目的存储格式从v7.14到v8.0也发生了变化,从.bundle变成了.git,运行restore的时候会提示文件格式错误。
既然知道了备份的各个文件的作用,那么就可以手动恢复。

第三步,新建并配置v8.0.5的docker-compose.yml文件

redis 需修改:

  • volumes: 挂载的磁盘目录,我的配置:/mnt/docker/gitlab/redis:/var/lib/redis

postgresql需修改:

  • volumes:挂载的磁盘目录,我的配置:
    /mnt/docker/gitlab/postgresql:/var/lib/postgresql
  • DB_USER:数据库名称
  • DB_PASS:数据库密码

gitlab需修改:

  • volumes: 挂载的磁盘目录,我的配置:/mnt/docker/gitlab/gitlab:/home/git/data
  • image:修改成 sameersbn/gitlab:8.0.5-1
  • GITLAB_HOST:gitlab服务的ip地址或者域名,会显示在gitlab的web页面上。
  • GITLAB_PORT:gitlab服务的端口号,会显示在gitlab的web页面上。填空的话是默认值80。
  • GITLAB_EMAIL等:发邮件时的显示的一些信息。
  • SMTP_ENABLED等:如果设置成true,会启用smtp邮件服务器。我使用的是qq企业邮箱,具体配置如下:
    - SMTP_ENABLED=true
    - 
    - 
    - SMTP_PORT=25
    - 
    - SMTP_PASS=password
    - SMTP_STARTTLS=true
    - SMTP_AUTHENTICATION=plain
  • GITLAB_BACKUPS,GITLAB_BACKUP_TIME:备份周期和备份时间。如果觉得daily周期太短,可以改成weekly。
  • GITLAB_SECRETS_DB_KEY_BASE:64位密钥。

第四步,数据库恢复

将数据库备份文件放到postgresql可访问到的位置:

cp /mnt/docker/gitlab/backup/db/database.sql /mnt/docker/gitlab/postgresql

database.sql注释掉开头的两行:

vi /mnt/docker/gitlab/postgresql/database.sql
// 注释掉的语句
--CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';

启动postgresql服务。
在docker-compose.yml所在目录运行

docker-compose up postgresql

进入postgresql容器内:

docker exec -ti gitlab_postgresql_1 bash

运行pgsql恢复数据库

psql -U git -h 127.0.0.1 -d gitlabhq_production -f /var/lib/postgresql/database.sql

执行exit退出postgresql容器

至此,数据库恢复完毕。

第五步,将repositories目录下的bundle文件解压,放到以.git作为扩展名的目录下。

将repositories拷贝到gitlab挂载的数据目录下。

mkdir /mnt/docker/gitlab/gitlab
cp -R backup/repositories /mnt/docker/gitlab/gitlab/

写了个简单的脚本用来做bundle to .git的转化:

#!bin/sh

if [ -d $1 ]; then
    echo "begin to change .bundle to .git ..."
else
    echo "$1 must be a dir"
    exit 1
fi

function walk() {
    echo "file path: $1"
    for file in `ls $1`
    do
        file=$1/$file
        echo $file
        if test -d $file; then
            walk ${file}
        fi
        if test -f $file && [ ${file##*.} = "bundle" ]; then
            gitFile=${file%.*}".git"
            echo gitFile: $gitFile
            if [ ! -d $gitFile ]; then
                echo "begin to unpack bundle file: $file"
                echo "make dir: ${gitFile}"
                mkdir -p $gitFile
                tar xvf $file -C $gitFile/
                echo "unpack bundle file: $file success!"
                rm -rf $file
                echo "bundle file removed"
            fi
        fi
    done
}

walk $1

在repositories目录下执行该脚本。

cd /mnt/docker/gitlab/gitlab/repositories
sh bundle-to-git.sh

第六步,拷贝uploads到gitlab数据目录下

cp -R backup/uploads /mnt/docker/gitlab/gitlab/

第七步,运行gitlab容器

docker-compose up (-d)

第八步,在浏览器中登录gitlab,检查是否一切ok。

点击项目,进入详情页,如果出现“No repository”的提示,很可能是repositories目录下的owner和gitlab的不匹配。

no-repo.png

运行命令,将owner赋给gitlab的owner:

chown -R user:user repositories

如果依然有此提示,需要清除一下缓存:

docker exec -ti gitlab bash
bundle exec rake cache:clear RAILS_ENV=production

另外,你会发现,项目概述中显示的commit数量都为0,这显然不正确。

commit-0.png

修改方法是运行更新commit数的命令:

docker exec -ti gitlab bash
bundle exec rake gitlab:update_commit_count RAILS_ENV=production

第九步,升级gitlab到最新版本(本文为8.14.3)

关闭gitlab v8.0.5容器

docker-compose stop

启动gitlab v8.14.3

docker-compose up 

其他问题:

  1. 如果之前使用ssh的方式连接git服务,更新后发现连接不上了,需要修改本地git的remote repo地址。这是因为docker的宿主机上ssh服务一般都用的22端口,所以gitlab的ssh配置在了非22的端口上(在docker-compose.yml的GITLAB_SSH_PORT配置),那么ssh的地址会多出来了“ssh://”和端口号“:10022”。如图所示:
ssh.png

如果想跟原来的ssh地址保持一致,除了修改docker-compose.yml的ssh端口号为22外(ports:"22:22",GITLAB_SSH_PORT不配置);还需要修改宿主机服务器端的ssh服务的端口号,使用22以外的端口。

  1. 使用ssh的方式拉取代码,可能出现权限问题,需要在profile里将原来的ssh-key删除再重新添加一次。
Cloning into 'mytest'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

最后, 本文用到脚本和配置文件在上可以获取。

--

Update 2016-12-15:

在v8.14.3版本上发现一个严重bug,如果我是一个项目的“owner”, 往master分支git push会报错~

Franklin$ git push
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 4.34 KiB | 0 bytes/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
To 
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 
remove-master.png

Update 2017-01-16:

上面的bug已经在8.15.1以上的版本解决掉了。撒花~