2017-07-01 19:25:19 +08:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
set -e
|
|
|
|
|
|
2017-07-02 00:17:18 +08:00
|
|
|
|
WAR_FILE=/diaowen.war
|
2017-07-02 01:51:47 +08:00
|
|
|
|
UNPACK_DIR=/dwsurvey
|
2017-07-02 00:17:18 +08:00
|
|
|
|
WEBAPP_BASE=$CATALINA_HOME/webapps
|
2017-07-01 22:31:28 +08:00
|
|
|
|
|
|
|
|
|
# determine context root path
|
|
|
|
|
if [[ "x${CONTEXT_ROOT}x" == "xx" || "$CONTEXT_ROOT" == "/" ]]; then
|
|
|
|
|
CONTEXT_PATH=ROOT
|
|
|
|
|
else
|
|
|
|
|
CONTEXT_PATH=${CONTEXT_ROOT}
|
|
|
|
|
fi
|
|
|
|
|
WEBAPP_DIR=${WEBAPP_BASE}/${CONTEXT_PATH}
|
2017-07-01 19:25:19 +08:00
|
|
|
|
CONF_DIR=${WEBAPP_DIR}/WEB-INF/classes/conf
|
|
|
|
|
|
|
|
|
|
require_env() {
|
2017-07-02 14:17:17 +08:00
|
|
|
|
local ENV_NAME="$1"
|
|
|
|
|
# if docker provides secret, secret wins env
|
|
|
|
|
if [[ -r "/run/secrets/$ENV_NAME" ]]; then
|
|
|
|
|
eval $ENV_NAME=$(cat /run/secrets/$ENV_NAME)
|
|
|
|
|
fi
|
|
|
|
|
|
2017-07-01 19:25:19 +08:00
|
|
|
|
eval ENV_VALUE=\$$ENV_NAME
|
|
|
|
|
|
|
|
|
|
if [ -z "$ENV_VALUE" ]; then
|
|
|
|
|
echo "Missing required environment variable: $ENV_NAME"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init_run() {
|
|
|
|
|
# check variables required for initialization
|
|
|
|
|
require_env MYSQL_HOST
|
|
|
|
|
require_env MYSQL_PORT
|
|
|
|
|
require_env MYSQL_DATABASE
|
|
|
|
|
require_env MYSQL_USER
|
|
|
|
|
require_env MYSQL_PASSWORD
|
|
|
|
|
|
|
|
|
|
echo "Unpacking war ..."
|
2017-07-02 01:51:47 +08:00
|
|
|
|
mkdir -p "$UNPACK_DIR"
|
|
|
|
|
unzip -q -x "$WAR_FILE" -d "$UNPACK_DIR" \
|
|
|
|
|
-x "WEB-INF/wjHtml/*" \
|
|
|
|
|
-x "WEB-INF/classes/conf/site/*"
|
|
|
|
|
ln -snf "$UNPACK_DIR" "$WEBAPP_DIR"
|
2017-07-01 19:25:19 +08:00
|
|
|
|
|
|
|
|
|
echo "Configuring dwsurvey ..."
|
|
|
|
|
sed -i "s^jdbc.url=.*\$jdbc.url=jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}?useUnicode=true\&characterEncoding=utf8g;
|
|
|
|
|
s^jdbc.username=.*\$jdbc.username=${MYSQL_USER}g;
|
|
|
|
|
s^jdbc.password=.*\$jdbc.password=${MYSQL_PASSWORD}g" \
|
|
|
|
|
${CONF_DIR}/application.properties
|
|
|
|
|
migrate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
migrate() {
|
|
|
|
|
# TODO We should use some migration mechanism.
|
|
|
|
|
# For now, let's do it in a dirty hacky way.
|
|
|
|
|
require_env ADMIN_EMAIL
|
|
|
|
|
require_env ADMIN_PASSWORD
|
|
|
|
|
|
|
|
|
|
# DIRTY hack, check if table t_user exist, if exist, skip migration
|
|
|
|
|
echo "Checking if table t_user exist ..."
|
|
|
|
|
if [[ $(mysql -N -s -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" --password="$MYSQL_PASSWORD" -e \
|
|
|
|
|
"SELECT count(*) FROM information_schema.TABLES
|
|
|
|
|
WHERE (TABLE_SCHEMA='${MYSQL_DATABASE}') AND (TABLE_NAME='t_user')") \
|
|
|
|
|
-eq 1 ]]; then
|
|
|
|
|
echo "table t_user exists, skipping migration."
|
|
|
|
|
return
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
local MIGRATION_FILE=/tmp/migrate.sql
|
|
|
|
|
cp ${WEBAPP_DIR}/WEB-INF/classes/conf/sql/dwsurvey.sql $MIGRATION_FILE
|
|
|
|
|
|
|
|
|
|
# remove "CREATE DATABASE" clouse, db should be created outside, so no root db account required
|
|
|
|
|
sed -i '/CREATE DATABASE/d' $MIGRATION_FILE
|
|
|
|
|
# use the specified database name
|
|
|
|
|
sed -i "s/USE \`dwsurvey\`;/USE \`${MYSQL_DATABASE}\`;/g" $MIGRATION_FILE
|
|
|
|
|
# remove default user
|
|
|
|
|
sed -i '/INSERT INTO `t_user`/d' $MIGRATION_FILE
|
2017-07-02 00:17:18 +08:00
|
|
|
|
# remove example survey (just don't INSERT anything), meaningless for production usage
|
|
|
|
|
sed -i '/INSERT INTO/d' $MIGRATION_FILE
|
2017-07-01 19:25:19 +08:00
|
|
|
|
# apply migration file
|
|
|
|
|
echo "Applying migration ..."
|
|
|
|
|
mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" --password="$MYSQL_PASSWORD" < $MIGRATION_FILE
|
|
|
|
|
|
|
|
|
|
rm -f "$MIGRATION_FILE"
|
|
|
|
|
|
|
|
|
|
# create admin user
|
|
|
|
|
local ADMIN_LOGIN=${ADMIN_EMAIL%%@*} # use email prefix for login
|
|
|
|
|
local ADMIN_SHA_PASSWORD=$(echo -n "$ADMIN_PASSWORD" | sha1sum | cut -d' ' -f 1)
|
|
|
|
|
echo "Creating admin user ..."
|
|
|
|
|
mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" --password="$MYSQL_PASSWORD" "$MYSQL_DATABASE" -e \
|
|
|
|
|
"INSERT INTO \`t_user\`
|
|
|
|
|
(id, email, login_name, name, sha_password, status, version)
|
|
|
|
|
VALUES (1, '$ADMIN_EMAIL', '$ADMIN_LOGIN', '$ADMIN_LOGIN', '$ADMIN_SHA_PASSWORD', 1, 1);"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# if webapp directory does not exist, it's first run
|
|
|
|
|
if [ ! -d "$WEBAPP_DIR" ]; then
|
|
|
|
|
init_run
|
|
|
|
|
fi
|
|
|
|
|
|
2017-07-12 16:43:10 +08:00
|
|
|
|
# On some boxes with low entropy, tomcat will stuck at start up. It often happens with docker-machine VMs on macos and cloud VM hosts.
|
|
|
|
|
# The dirty hack is to use /dev/urandom as entropy source, it is not recommemded as it reduces security.
|
|
|
|
|
# See: https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source
|
|
|
|
|
export JAVA_OPTS="-Djava.security.egd=file:///dev/urandom $JAVA_OPTS"
|
|
|
|
|
|
2017-07-01 19:25:19 +08:00
|
|
|
|
# start tomcat
|
|
|
|
|
if [ -z "$@" ]; then
|
2017-07-02 00:17:18 +08:00
|
|
|
|
exec $CATALINA_HOME/bin/catalina.sh run
|
2017-07-01 19:25:19 +08:00
|
|
|
|
else
|
|
|
|
|
exec "$@"
|
|
|
|
|
fi
|