#!/bin/bash set -e GITLAB_HOST=${GITLAB_HOST:-localhost} GITLAB_PORT=${GITLAB_PORT:-80} GITLAB_HTTPS=${GITLAB_HTTPS:-false} GITLAB_EMAIL=${GITLAB_EMAIL:-gitlab@localhost} GITLAB_SUPPORT=${GITLAB_SUPPORT:-support@localhost} GITLAB_SIGNUP=${GITLAB_SIGNUP:-false} GITLAB_BACKUPS=${GITLAB_BACKUPS:-disable} GITLAB_BACKUP_EXPIRY=${GITLAB_BACKUP_EXPIRY:-} GITLAB_SHELL_SSH_PORT=${GITLAB_SHELL_SSH_PORT:-22} REDIS_HOST=${REDIS_HOST:-localhost} REDIS_PORT=${REDIS_PORT:-6379} UNICORN_WORKERS=${UNICORN_WORKERS:-2} UNICORN_TIMEOUT=${UNICORN_TIMEOUT:-60} SIDEKIQ_CONCURRENCY=${SIDEKIQ_CONCURRENCY:-5} DB_TYPE=${DB_TYPE:-mysql} DB_HOST=${DB_HOST:-localhost} DB_PORT=${DB_PORT:-} DB_NAME=${DB_NAME:-gitlabhq_production} DB_USER=${DB_USER:-root} DB_PASS=${DB_PASS:-} DB_INIT=${DB_INIT:-} DB_POOL=${DB_POOL:-10} SMTP_DOMAIN=${SMTP_DOMAIN:-www.gmail.com} SMTP_HOST=${SMTP_HOST:-smtp.gmail.com} SMTP_PORT=${SMTP_PORT:-587} SMTP_USER=${SMTP_USER:-} SMTP_PASS=${SMTP_PASS:-} SMTP_STARTTLS=${SMTP_STARTTLS:-true} case "${DB_TYPE}" in mysql) DB_PORT=${DB_PORT:-3306} ;; postgres) DB_PORT=${DB_PORT:-5432} ;; *) echo "Unsupported database adapter. Available adapters are mysql and postgres." && exit 1 ;; esac case "${GITLAB_BACKUPS}" in daily|monthly) GITLAB_BACKUP_EXPIRY=${GITLAB_BACKUP_EXPIRY:-604800} ;; disable|*) GITLAB_BACKUP_EXPIRY=${GITLAB_BACKUP_EXPIRY:-0} ;; esac # generate a password for root. ROOT_PASSWORD=$(pwgen -c -n -1 12) echo "root:$ROOT_PASSWORD" | chpasswd echo User: root Password: $ROOT_PASSWORD # start supervisord /usr/bin/supervisord # over write default configuration templates if [ -d /home/git/data/config ]; then chown -R git:git /home/git/data/config cd /home/git/data/config [ -f nginx/gitlab ] && cp nginx/gitlab /etc/nginx/sites-available/gitlab [ -f gitlab-shell/config.yml ] && sudo -u git -H cp gitlab-shell/config.yml /home/git/gitlab-shell/config.yml [ -f gitlabhq/gitlab.yml ] && sudo -u git -H cp gitlabhq/gitlab.yml /home/git/gitlab/config/gitlab.yml [ -f gitlabhq/resque.yml ] && sudo -u git -H cp gitlabhq/resque.yml /home/git/gitlab/config/resque.yml [ -f gitlabhq/database.yml ] && sudo -u git -H cp gitlabhq/database.yml /home/git/gitlab/config/database.yml [ -f gitlabhq/sidekiq.yml ] && sudo -u git -H cp gitlabhq/sidekiq.yml /home/git/gitlab/config/sidekiq.yml [ -f gitlabhq/unicorn.rb ] && sudo -u git -H cp gitlabhq/unicorn.rb /home/git/gitlab/config/unicorn.rb [ -f gitlabhq/rack_attack.rb ] && sudo -u git -H cp gitlabhq/rack_attack.rb /home/git/gitlab/config/initializers/rack_attack.rb [ -f gitlabhq/smtp_settings.rb ] && sudo -u git -H cp gitlabhq/smtp_settings.rb /home/git/gitlab/config/initializers/smtp_settings.rb fi sed 's/{{YOUR_SERVER_FQDN}}/localhost/' -i /etc/nginx/sites-available/gitlab supervisorctl start nginx # start mysql server if ${DB_HOST} is localhost if [ "${DB_HOST}" == "localhost" ]; then if [ "${DB_TYPE}" == "postgres" ]; then echo "DB_TYPE 'postgres' is not supported internally. Please provide DB_HOST." exit 1 fi # fix permissions and ownership of /var/lib/mysql chown -R mysql:mysql /var/lib/mysql chmod 700 /var/lib/mysql # initialize MySQL data directory if [ ! -d /var/lib/mysql/mysql ]; then mysql_install_db --user=mysql fi supervisorctl start mysqld # wait for mysql server to start (max 120 seconds) timeout=120 while ! mysqladmin -uroot ${DB_PASS:+-p$DB_PASS} status >/dev/null 2>&1 do timeout=$(expr $timeout - 1) if [ $timeout -eq 0 ]; then echo "Failed to start mysql server" exit 1 fi sleep 1 done if ! echo "USE ${DB_NAME}" | mysql -uroot ${DB_PASS:+-p$DB_PASS} >/dev/null 2>&1; then DB_INIT="yes" echo "CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;" | mysql -uroot ${DB_PASS:+-p$DB_PASS} echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \`${DB_NAME}\`.* TO 'root'@'localhost';" | mysql -uroot ${DB_PASS:+-p$DB_PASS} fi fi if [ "${REDIS_HOST}" == "localhost" ]; then supervisorctl start redis-server fi # configure git for the 'git' user sudo -u git -H git config --global user.name "GitLab" sudo -u git -H git config --global user.email "${GITLAB_EMAIL}" sudo -u git -H git config --global core.autocrlf input # configure server url sudo -u git -H sed 's/{{GITLAB_HOST}}/'${GITLAB_HOST}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_PORT}}/'${GITLAB_PORT}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_HTTPS}}/'${GITLAB_HTTPS}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_EMAIL}}/'${GITLAB_EMAIL}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_SUPPORT}}/'${GITLAB_SUPPORT}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_BACKUP_EXPIRY}}/'${GITLAB_BACKUP_EXPIRY}'/' -i /home/git/gitlab/config/gitlab.yml sudo -u git -H sed 's/{{GITLAB_SHELL_SSH_PORT}}/'${GITLAB_SHELL_SSH_PORT}'/' -i /home/git/gitlab/config/gitlab.yml # configure gitlab signup configuration sudo -u git -H sed 's/{{GITLAB_SIGNUP}}/'${GITLAB_SIGNUP}'/' -i /home/git/gitlab/config/gitlab.yml # configure database if [ "${DB_TYPE}" == "postgres" ]; then sudo -u git -H sed 's/{{DB_ADAPTER}}/postgresql/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_ENCODING}}/unicode/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/reconnect: false/#reconnect: false/' -i /home/git/gitlab/config/database.yml elif [ "${DB_TYPE}" == "mysql" ]; then sudo -u git -H sed 's/{{DB_ADAPTER}}/mysql2/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_ENCODING}}/utf8/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/#reconnect: false/reconnect: false/' -i /home/git/gitlab/config/database.yml else echo "Invalid database type: '$DB_TYPE'. Supported choices: [mysql, postgres]." fi sudo -u git -H sed 's/{{DB_HOST}}/'${DB_HOST}'/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_PORT}}/'${DB_PORT}'/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_NAME}}/'${DB_NAME}'/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_USER}}/'${DB_USER}'/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_PASS}}/'${DB_PASS}'/' -i /home/git/gitlab/config/database.yml sudo -u git -H sed 's/{{DB_POOL}}/'${DB_POOL}'/' -i /home/git/gitlab/config/database.yml # configure sidekiq sudo -u git -H sed 's/{{SIDEKIQ_CONCURRENCY}}/'${SIDEKIQ_CONCURRENCY}'/' -i /home/git/gitlab/config/sidekiq.yml # configure redis sudo -u git -H sed 's/{{REDIS_HOST}}/'${REDIS_HOST}'/' -i /home/git/gitlab/config/resque.yml sudo -u git -H sed 's/{{REDIS_PORT}}/'${REDIS_PORT}'/' -i /home/git/gitlab/config/resque.yml # configure gitlab-shell sudo -u git -H sed 's/{{REDIS_HOST}}/'${REDIS_HOST}'/' -i /home/git/gitlab-shell/config.yml sudo -u git -H sed 's/{{REDIS_PORT}}/'${REDIS_PORT}'/' -i /home/git/gitlab-shell/config.yml # configure unicorn workers sed 's/{{UNICORN_WORKERS}}/'${UNICORN_WORKERS}'/' -i /home/git/gitlab/config/unicorn.rb # configure unicorn timeout sed 's/{{UNICORN_TIMEOUT}}/'${UNICORN_TIMEOUT}'/' -i /home/git/gitlab/config/unicorn.rb # configure mail delivery sed 's/{{SMTP_HOST}}/'${SMTP_HOST}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb sed 's/{{SMTP_PORT}}/'${SMTP_PORT}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb sed 's/{{SMTP_USER}}/'${SMTP_USER}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb sed 's/{{SMTP_PASS}}/'${SMTP_PASS}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb sed 's/{{SMTP_DOMAIN}}/'${SMTP_DOMAIN}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb sed 's/{{SMTP_STARTTLS}}/'${SMTP_STARTTLS}'/' -i /home/git/gitlab/config/initializers/smtp_settings.rb # take ownership of /home/git/data chown git:git /home/git/data # create the repositories directory and make sure it has the right permissions sudo -u git -H mkdir -p /home/git/data/repositories/ sudo chmod ug+rwX,o-rwx /home/git/data/repositories/ sudo chmod ug-s /home/git/data/repositories/ find /home/git/data/repositories/ -type d -print0 | sudo xargs -0 chmod g+s chown git:git /home/git/data/repositories # create the satellites directory and make sure it has the right permissions sudo -u git -H mkdir -p /home/git/data/gitlab-satellites/ sudo chmod ug+rwX,o-rwx /home/git/data/gitlab-satellites/ sudo chmod ug-s /home/git/data/gitlab-satellites/ find /home/git/data/gitlab-satellites/ -type d -print0 | sudo xargs -0 chmod g+s chown git:git /home/git/data/gitlab-satellites # create the backups directory sudo -u git -H mkdir -p /home/git/data/backups/ chown git:git /home/git/data/backups/ # create the uploads directory sudo -u git -H mkdir -p /home/git/data/uploads/ chmod -R u+rwX /home/git/data/uploads/ chown git:git /home/git/data/uploads/ # create the .ssh directory sudo -u git -H mkdir -p /home/git/data/.ssh/ touch /home/git/data/.ssh/authorized_keys chmod 700 /home/git/data/.ssh chmod 600 /home/git/data/.ssh/authorized_keys chown -R git:git /home/git/data/.ssh cd /home/git/gitlab/ appStart () { echo "Starting gitlab server..." # reset the database if the --db-init switch was given. if [ "$DB_INIT" == "yes" ]; then sudo -u git -H force=yes bundle exec rake gitlab:setup RAILS_ENV=production fi sudo -u git -H bundle exec rake assets:clean RAILS_ENV=production sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production sudo -u git -H bundle exec rake cache:clear RAILS_ENV=production # start the gitlab application # sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production /etc/init.d/gitlab start # create satellite directories sudo -u git -H bundle exec rake gitlab:satellites:create RAILS_ENV=production sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production # setup cron job for automatic backups case "${GITLAB_BACKUPS}" in daily) sudo -u git -H cat > /tmp/cron.git < /tmp/cron.git < - Execute a rake task." echo " app:help - Displays the help" echo " [command] - Execute the specified linux command eg. bash." } case "$1" in app:start) appStart ;; app:rake) shift 1 appRake $@ ;; app:help) appHelp ;; *) if [ -x $1 ]; then $1 else prog=$(which $1) if [ -n "${prog}" ] ; then shift 1 $prog $@ else appHelp fi fi ;; esac exit 0