Omnibus GitLabからGitLab.comに移行した話

はじめに

VPSに置いてあったOmnibus GitLabをアップデートしたら起動しなくなったので、これを期にGitLab.comに移行しました。

環境

移行元

  • Omnibus GitLab Version: 10.4.3, Revision: 183dd5d
  • CentOS 6.9 x86_64

移行先

  • GitLab.com (API v4)

手順

1. GitLab上のbareリポジトリをnon-bareリポジトリにする

普通なら git clone して origin を差し替えて git push するだけなのですが、今回はGitLabが起動しなくなっていたので、サーバ上のbareリポジトリからnon-bareリポジトリを作ります。

Omnibus GitLabのリポジトリは、デフォルト設定であれば /var/opt/gitlab/git-data/repositories/<ユーザ名> にあります。

というわけで、こんな感じの雑なスクリプトでnon-bareリポジトリを作ります*1

DIRNAME='/var/opt/gitlab/git-data/repositories/<ユーザ名>'

for f in $(ls $DIRNAME | grep -E '.git$'); do
    echo $f
    cd $DIRNAME;
    f2=`echo $f | sed -e 's/.git$//'`;

    # ワーキングディレクトリを作り、bareリポジトリを .git にリネームして入れる
    mkdir $f2;
    mv ${f2}.git ${f2}/.git 

    # bareリポジトリじゃなくする。
    cd ${f2}/.git
    git config core.bare false
    cd ..
    git checkout-index -a
    git reset --hard HEAD
done;

…よく考えたら git daemon すれば普通にcloneできたかもしれませんが、気にしないことにします。

2. スニペットのデータを取り出す

スニペットのデータはPostgreSQLの中に入っているので、gitlab-psql コマンドで取り出します。

$ su -
# /usr/bin/gitlab-ctl start postgresql
# /usr/bin/gitlab-psql gitlabhq_production -c 'select title,file_name,content from snippets;'

テーブル定義は以下のような感じなので、他にも必要な列があれば、適当に取得します。

gitlabhq_production=# \d snippets;
                                            Table "public.snippets"
         Column          |            Type             |                       Modifiers                       
-------------------------+-----------------------------+-------------------------------------------------------
 id                      | integer                     | not null default nextval('snippets_id_seq'::regclass)
 title                   | character varying(255)      | 
 content                 | text                        | 
 author_id               | integer                     | not null
 project_id              | integer                     | 
 created_at              | timestamp without time zone | 
 updated_at              | timestamp without time zone | 
 file_name               | character varying(255)      | 
 type                    | character varying(255)      | 
 visibility_level        | integer                     | not null default 0
 title_html              | text                        | 
 content_html            | text                        | 
 cached_markdown_version | integer                     | 
 description             | text                        | 
 description_html        | text                        | 

3. GitLab.comのアカウントを作る

https://gitlab.com にアクセスしてアカウントを作ります。

4. GitLab APIでプロジェクトを作る

1つずつ手作業でプロジェクトを作るのはつらいので、APIを使いましょう。 まずはアクセストークンを https://gitlab.com/profile/personal_access_tokens から作ります。面倒なのでスコープは全部つけました。

f:id:ser1zw:20180503034223p:plain

プロジェクトを作るには、POST /projects してやればよいようです。

docs.gitlab.com

単発のcurlでやるなら、こんな感じ。

$ curl -X POST --header 'Private-Token: <アクセストークン>' https://gitlab.com/api/v4/projects?name=<プロジェクト名>

1つずつやるのは面倒なので、シェル芸で一気にやります。

$ ls /var/opt/gitlab/git-data/repositories/<ユーザ名> | xargs -I@ curl -X POST --header 'Private-Token: <アクセストークン>' https://gitlab.com/api/v4/projects?name=@

5. プロジェクトにpushする

1で作ったリポジトリの内容をpushしていきます。 これも雑なスクリプトで一気にやります。

DIRNAME='/var/opt/gitlab/git-data/repositories/<ユーザ名>'

for d in $(ls $DIRNAME); do
    echo $d
    cd $DIRNAME/$d;
    git remote add origin git@gitlab.com:<ユーザ名>/${d}.git
    git push -u origin --all
    git push -u origin --tags
done;

スニペットは少ししか無かったので、今回は手動でやってしまいました。

まとめ

というわけで、Omnibus GitLabからGitLab.comへの移行をする話でした。

参考

*1:やってる内容は http://qnighy.hatenablog.com/entry/2017/02/25/154752 を参考にしました