gitlab-unicorn-startup.in 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#!{{ bash_bin }}
# start up gitlab's unicorn with first making sure db is properly setup and all
# migrations are up as pre-condition.

RAKE={{ gitlab_rake }}

die() {
    echo "$*" 1>&2
    exit 1
}

12 13 14 15 16 17 18 19 20
# run psql on gitlab db
psql() {
    {{ psql_bin }}  \
        -h {{ pgsql['pgdata-directory'] }}  \
        -U {{ pgsql.superuser }}            \
        -d {{ pgsql.dbname }}               \
        "$@"
}

21 22 23 24 25 26
# 1. what to do when instance is initially setup
# see
#   https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-cookbooks/gitlab/recipes/database_migrations.rb
#   https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-cookbooks/gitlab/recipes/postgresql.rb

# initial db setup
27 28 29
# ( first quering PG several times waiting a bit till postgresql is started and ready )
tpgwait=5
while true; do
30
    pgtables="$(psql -c '\d')" && break
31 32 33 34 35 36
    tpgwait=$(( $tpgwait - 1 ))
    test $tpgwait = 0 && die "pg query problem"
    echo "I: PostgreSQL is not ready (yet ?); will retry $tpgwait times..." 1>&2
    sleep 1
done
echo "I: PostgreSQL ready." 1>&2
37

38 39 40
# make sure pg_trgm extension is enabled for gitlab db
psql -c 'CREATE EXTENSION IF NOT EXISTS pg_trgm;'   || die "pg_trgm setup failed"

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
if echo "$pgtables" | grep -q '^No relations found' ; then
    $RAKE db:schema:load db:seed_fu  || die "initial db setup failed"
fi

# re-build ssh keys
# (we do not use them - just for cleannes)
force=yes $RAKE gitlab:shell:setup   || die "gitlab:shell:setup failed"


# 2. what to do when instance is upgraded
# see
#   https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/support/deploy/deploy.sh
#   https://gitlab.com/gitlab-org/gitlab-ce/blob/master/lib/gitlab/upgrader.rb
#   https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-cookbooks/gitlab/recipes/gitlab-rails.rb
#   https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-ctl-commands/upgrade.rb
#
# Assets compilation is handled at instance deployment time. We do everything else here.

# make sure all migrations are up
migrate_log="{{ log_dir }}/db-migrate-`date +%s`.log"
$RAKE db:migrate >$migrate_log 2>&1  || die "db:migrate failed"
# if it was a no-op "migration" - we don't need info about that - only keep
# logs of actual migration run.
test -s $migrate_log || rm $migrate_log


# clear cache
$RAKE cache:clear   || die "cache:clear failed"



# 3. finally exec to unicorn
exec {{ gitlab_unicorn }}   \
    -E production   \
    -c {{ unicorn_rb.rendered }}    \
    {{ gitlab_work.location }}/config.ru