blob: 53897a30db369041bb0a78a5e4ac427b7769a96c [file] [log] [blame]
#!/bin/bash
get_db_config(){
if test -f "/var/gerrit/etc/secure.config"; then
export DB_USER=$(git config --file /var/gerrit/etc/secure.config --get database.username)
export DB_PASSWORD=$(git config --file /var/gerrit/etc/secure.config --get database.password)
fi
if test -f "/var/gerrit/etc/gerrit.config"; then
export DB_TYPE=$(git config --file /var/gerrit/etc/gerrit.config --get database.type)
export DB_NAME=$(git config --file /var/gerrit/etc/gerrit.config --get database.database)
export DB_HOST=$(git config --file /var/gerrit/etc/gerrit.config --get database.hostname)
export DB_PORT=$(git config --file /var/gerrit/etc/gerrit.config --get database.port)
fi
if test -z "${DB_USER}"; then
echo "Missing database username in Gerrit config."
exit 1
fi
if test -z "${DB_PASSWORD}"; then
echo "Missing database password in Gerrit config."
exit 1
fi
if test -z "${DB_NAME}"; then
export DB_NAME="reviewdb"
fi
if test -z "${DB_HOST}"; then
echo "Missing database host in Gerrit config."
exit 1
fi
if test -z "${DB_PORT}"; then
echo "Missing database port in Gerrit config."
exit 1
fi
return 0
}
test_repositories(){
local EXIT_CODE=0
test -d "/var/gerrit/git/All-Projects.git" || EXIT_CODE=1
return ${EXIT_CODE}
}
test_mysql_db(){
local EXIT_CODE=0
mysql -h ${DB_HOST} -P${DB_PORT} -u ${DB_USER} -p${DB_PASSWORD} ${DB_NAME} >/dev/null 2>&1 </dev/null || EXIT_CODE=1
# Check existence of some tables
for table in accounts changes patch_sets; do # tables expected in Gerrit 2.12
SQL_EXISTS=$(printf 'SHOW TABLES LIKE "%s"' "${table}")
if [[ $(mysql -h ${DB_HOST} -P${DB_PORT} -u ${DB_USER} -p${DB_PASSWORD} -e "${SQL_EXISTS}" ${DB_NAME}) ]]; then
echo "Table ${table} was found."
else
echo "Table ${table} was NOT found. Continuing to wait..."
local EXIT_CODE=1
break
fi
done
return ${EXIT_CODE}
}
wait_for_repositories(){
echo "Waiting for replicated repositories."
while [ true ]; do
test_repositories && break
sleep 10
done
echo "The All-Projects.git repository was found."
}
wait_for_db_schema(){
echo "Waiting for database to be ready."
case "${DB_TYPE^^}" in
MYSQL)
while [ true ]; do
test_mysql_db && break
sleep 10
done
;;
*)
echo "Database type ${DB_TYPE} not supported."
exit 1
;;
esac
echo "Database was found."
}
wait_for_db_connection(){
echo "Waiting for database connection..."
case "${DB_TYPE^^}" in
MYSQL)
while ! mysqladmin ping -h "${DB_HOST}" -P"${DB_PORT}" --silent; do
sleep 1
done
;;
*)
echo "Database type ${DB_TYPE} not supported."
exit 1
;;
esac
}
# cleanup from last start
rm -f /var/gerrit/logs/gerrit.pid
# read db configuration from gerrit.config
get_db_config || exit 1
# wait for db to start
wait_for_db_connection
if [[ "$TEST_MODE" == "true" ]]; then
java -jar /var/gerrit/bin/gerrit.war init \
--batch \
--no-auto-start \
--install-plugin singleusergroup \
-d /var/gerrit
fi
# wait for All-Projects to arrive via replication from master
wait_for_repositories
# wait for db schema to arrive via replication from master
wait_for_db_schema
echo "Gerrit site appears to be initialized. Gerrit slave can be startet."