部署代码review和CI

公司原先搭了一个代码Review的服务器,因为历史缘由,装的是一个32bit的Ubuntu系统,后来因为须要,须要安装gitlab,因为gitlab须要64位系统,因此临时凑合了个vagrant,本质就是一个纯粹的虚拟机,感受不爽,这两天终于抽出时间来从新整理了一下。基于Ubuntu 18.04 x64版本和Docker来部署,减小后面换机器换系统可能致使的重复安装工做。php

Docker安装

Docker安装仍是比较简单的。html

$ sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - $ sudo apt-key fingerprint 0EBFCD88 # should have something output, key 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" $ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io $ sudo usermod -aG docker $USER # 执行完从新登陆,后面就能够不用sudo执行docker命令了。 

gitlab转移

gitlab转移仍是比较方便的,gitlab自己就提供各个版本的docker镜像,gitlab转移必需要在同一个版本之间,下载该版本对应的docker镜像,以daemon方式运行:python

$ docker run --detach --publish 192.168.30.102:443:443 --publish 192.168.30.102:80:80 --publish 8222:22 --restart always --volume /home/gitlab/config:/etc/gitlab --volume /home/gitlab/logs:/var/log/gitlab --volume /home/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:9.5.4-ce.0 

按照Backing up and restoring GitLab指导,对于Omnibus Package方式的安装,只要在源机器端执行:mysql

$ sudo gitlab-rake gitlab:backup:create 

在/var/opt/gitlab/backups/下找到备份文件,复制到目标机器的/home/gitlab/data/backups/下。注意:这个步骤同时复制源机器的“/etc/gitlab/gitlab-secrets.json”到目标机器对应目录“/home/gitlab/config”下linux

进入docker机器,执行恢复:git

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-rake gitlab:backup:restore BACKUP=ts_yyyy_mm_dd_ver gitlab-ctl status gitlab-ctl restart gitlab-rake gitlab:check SANITIZE=true 

提示一切成功。若是“gitlab-secrets.json”在执行gitlab:backup:restore前没有复制到目标机,恢复的时候可能会提示出错。 ts_yyyy_mm_dd_ver是指gitlab生成的备份文件,文件名最后的“_gitlab_backup.tar”不须要输入。访问“http://192.168.30.102”,一切如旧,数据转移成功。github

Phabricator转移

因为这个有必定的特殊要求,比较合适的“fauria/lamp”映像,php是7.0的,不符合phabricator的要求,因此基于“fauria/lamp”的Dockerfile和“ubuntu 18.04”镜像,本身build了一个,Dockerfile以下:sql

FROM ubuntu:18.04
MAINTAINER zhuhongbing<hongbingzhu@gmail.com>
LABEL Description="My own LAMP stack, based on Ubuntu 18.04 LTS. Based on fauria/docker-lamp." \
        License="Apache License 2.0" \
        Usage="docker run -d ..." \
        Version="1.0"

RUN apt-get update
RUN apt-get upgrade -y

ENV DEBIAN_FRONTEND noninteractive
ENV ALLOW_OVERRIDE All
ENV DATE_TIMEZONE UTC
ENV TERM xterm

RUN apt-get install mysql-server php apache2 libapache2-mod-php -y
RUN apt-get install php-mysql php-gd php-curl php-apcu php-cli php-json php-mbstring -y
RUN apt-get install git vim curl ftp -y
RUN apt-get install python-pygments subversion -y

COPY index.php /var/www/html/
COPY run-lamp.sh /usr/sbin/

RUN a2enmod rewrite
RUN chmod +x /usr/sbin/run-lamp.sh
RUN chown -R www-data:www-data /var/www/html

VOLUME /mnt/host

EXPOSE 80
EXPOSE 443

CMD ["/usr/sbin/run-lamp.sh"]

这里须要注意的是ENV命令,这个会影响build的过程和最终映像运行的。里面的TERM原先是“dumb”,这个会致使vim界面异常,若是基于Ubuntu,测试设置为xterm是比较好的。也是服务器版ubuntu的缺省设置。docker

Phabricator要求的组件在定制版docker里面基本都就绪了,因此后面基本上在运行的容器中执行数据恢复就好了。apache

phabricator/ $ ./bin/storage dump | gzip > backup.sql.gz # 源机器 $ gunzip -c backup.sql.gz | mysql # 目标机器 

同时注意恢复文件“phabricator/conf/local/local.json”。 注意:phabricator的190418版本,彷佛恢复完毕之后,须要先开一下phd,才能正常访问,比较坑。折腾了很久。之前版本的phabricator邮件很好配置,更新版本以后,邮件始终折腾不成功了,比较坑,谁有经验告诉我一下。配置以下:

"metamta.default-address": "phabricator@byhx-china.com",
  "cluster.mailers": [
    {
      "key": "smtp-mailer",
      "type": "smtp",
      "options": {
        "host": "mail.my-company.com",
        "port": 25,
        "user": "phabricator@my-company.com",
        "password": "my-account-pwd",
        "message-id": false
      }
    }
  ],

编译服务器

编译服务器主要用于从代码服务器抓取代码版本并编译,主要是作交叉编译,比较坑的是“/etc/hosts”文件,这个文件运行的时候会自动从新生成,见 /etc/hosts file of a docker container gets overwritten里面指向的discussion Allow customization of /etc/hosts, /etc/resolv.conf, etc. in containers #2267。简而言之,就是须要在运行docker的时候增长参数“–add-host=”<name_host>:<ip_host>"”来解决。