2018-11-02 17:08:25 +03:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2019-01-08 12:05:03 +03:00
|
|
|
log() {
|
|
|
|
TIMESTAMP=$(date -u "+%Y-%m-%dT%H:%M:%S.000+0000")
|
2020-08-06 18:48:55 +03:00
|
|
|
LOGKIND=$1
|
|
|
|
MESSAGE=$2
|
|
|
|
echo "{\"timestamp\":\"$TIMESTAMP\",\"level\":\"info\",\"type\":\"startup\",\"detail\":{\"kind\":\"$LOGKIND\",\"info\":\"$MESSAGE\"}}"
|
2019-01-08 12:05:03 +03:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
DEFAULT_MIGRATIONS_DIR="/hasura-migrations"
|
2020-03-19 15:21:53 +03:00
|
|
|
DEFAULT_METADATA_DIR="/hasura-metadata"
|
|
|
|
TEMP_PROJECT_DIR="/tmp/hasura-project"
|
2018-11-02 17:08:25 +03:00
|
|
|
|
2019-12-27 07:30:06 +03:00
|
|
|
# configure the target database for migrations
|
2022-06-14 14:13:57 +03:00
|
|
|
if [ "${HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR}" ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "database url for migrations is set by $HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR"
|
2022-06-14 14:13:57 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL=$(printenv "$HASURA_GRAPHQL_MIGRATIONS_DATABASE_ENV_VAR")
|
2019-12-27 07:30:06 +03:00
|
|
|
elif [ -z ${HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL+x} ]; then
|
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL=$HASURA_GRAPHQL_DATABASE_URL
|
|
|
|
fi
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "database url for migrations is set by HASURA_GRAPHQL_DATABASE_URL"
|
2019-12-27 07:30:06 +03:00
|
|
|
|
2019-12-27 07:05:29 +03:00
|
|
|
# Use 9691 port for running temporary instance.
|
|
|
|
# In case 9691 is occupied (according to docker networking), then this will fail.
|
2019-12-27 07:30:06 +03:00
|
|
|
# override with another port in that case
|
|
|
|
# TODO: Find a proper random port
|
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT+x} ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "migrations server port env var is not set, defaulting to 9691"
|
2019-12-27 07:30:06 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT=9691
|
|
|
|
fi
|
2019-12-27 07:05:29 +03:00
|
|
|
|
2019-05-31 13:37:59 +03:00
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT+x} ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "server timeout is not set, defaulting to 30 seconds"
|
2019-05-31 13:37:59 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT=30
|
|
|
|
fi
|
2019-01-08 12:05:03 +03:00
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
# wait for a port to be ready
|
|
|
|
wait_for_port() {
|
2022-08-23 09:17:19 +03:00
|
|
|
local PORT=$1
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "waiting $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT for $PORT to be ready"
|
2022-06-14 14:13:57 +03:00
|
|
|
for _ in $(seq 1 $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT);
|
2018-11-02 17:08:25 +03:00
|
|
|
do
|
2022-06-14 14:13:57 +03:00
|
|
|
nc -z localhost "$PORT" > /dev/null 2>&1 && log "migrations-startup" "port $PORT is ready" && return
|
2018-11-02 17:08:25 +03:00
|
|
|
sleep 1
|
|
|
|
done
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "failed waiting for $PORT, try increasing HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT (default: 30)" && exit 1
|
2018-11-02 17:08:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "starting graphql engine temporarily on port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT"
|
2018-11-02 17:08:25 +03:00
|
|
|
|
2019-10-17 12:58:27 +03:00
|
|
|
# start graphql engine with metadata api enabled
|
2022-06-14 14:13:57 +03:00
|
|
|
HASURA_GRAPHQL_DATABASE_URL=$HASURA_GRAPHQL_MIGRATIONS_DATABASE_URL $HGE_BINARY \
|
2019-12-27 07:30:06 +03:00
|
|
|
serve --enabled-apis="metadata" \
|
|
|
|
--server-port=${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT} &
|
2018-11-02 17:08:25 +03:00
|
|
|
# store the pid to kill it later
|
|
|
|
PID=$!
|
|
|
|
|
2019-01-08 12:05:03 +03:00
|
|
|
# wait for port to be ready
|
2019-12-27 07:05:29 +03:00
|
|
|
wait_for_port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT
|
2018-11-02 17:08:25 +03:00
|
|
|
|
|
|
|
# check if migration directory is set, default otherwise
|
|
|
|
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_DIR+x} ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "env var HASURA_GRAPHQL_MIGRATIONS_DIR is not set, defaulting to $DEFAULT_MIGRATIONS_DIR"
|
2018-11-02 17:08:25 +03:00
|
|
|
HASURA_GRAPHQL_MIGRATIONS_DIR="$DEFAULT_MIGRATIONS_DIR"
|
|
|
|
fi
|
|
|
|
|
2020-03-19 15:21:53 +03:00
|
|
|
# check if metadata directory is set, default otherwise
|
|
|
|
if [ -z ${HASURA_GRAPHQL_METADATA_DIR+x} ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-startup" "env var HASURA_GRAPHQL_METADATA_DIR is not set, defaulting to $DEFAULT_METADATA_DIR"
|
2020-03-19 15:21:53 +03:00
|
|
|
HASURA_GRAPHQL_METADATA_DIR="$DEFAULT_METADATA_DIR"
|
|
|
|
fi
|
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
# apply migrations if the directory exist
|
|
|
|
if [ -d "$HASURA_GRAPHQL_MIGRATIONS_DIR" ]; then
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-apply" "applying migrations from $HASURA_GRAPHQL_MIGRATIONS_DIR"
|
2020-03-19 15:21:53 +03:00
|
|
|
mkdir -p "$TEMP_PROJECT_DIR"
|
|
|
|
cp -a "$HASURA_GRAPHQL_MIGRATIONS_DIR/." "$TEMP_PROJECT_DIR/migrations/"
|
|
|
|
cd "$TEMP_PROJECT_DIR"
|
|
|
|
echo "version: 2" > config.yaml
|
|
|
|
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" >> config.yaml
|
2018-11-02 17:08:25 +03:00
|
|
|
hasura-cli migrate apply
|
|
|
|
else
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-apply" "directory $HASURA_GRAPHQL_MIGRATIONS_DIR does not exist, skipping migrations"
|
2018-11-02 17:08:25 +03:00
|
|
|
fi
|
|
|
|
|
2020-03-19 15:21:53 +03:00
|
|
|
# apply metadata if the directory exist
|
|
|
|
if [ -d "$HASURA_GRAPHQL_METADATA_DIR" ]; then
|
|
|
|
rm -rf "TEMP_PROJECT_DIR"
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-apply" "applying metadata from $HASURA_GRAPHQL_METADATA_DIR"
|
2020-03-19 15:21:53 +03:00
|
|
|
mkdir -p "$TEMP_PROJECT_DIR"
|
|
|
|
cp -a "$HASURA_GRAPHQL_METADATA_DIR/." "$TEMP_PROJECT_DIR/metadata/"
|
|
|
|
cd "$TEMP_PROJECT_DIR"
|
|
|
|
echo "version: 2" > config.yaml
|
|
|
|
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" >> config.yaml
|
|
|
|
echo "metadata_directory: metadata" >> config.yaml
|
2020-11-12 12:25:48 +03:00
|
|
|
hasura-cli metadata apply
|
2020-03-19 15:21:53 +03:00
|
|
|
else
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-apply" "directory $HASURA_GRAPHQL_METADATA_DIR does not exist, skipping metadata"
|
2020-03-19 15:21:53 +03:00
|
|
|
fi
|
|
|
|
|
2018-11-02 17:08:25 +03:00
|
|
|
# kill graphql engine that we started earlier
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-shutdown" "killing temporary server"
|
2018-11-02 17:08:25 +03:00
|
|
|
kill $PID
|
|
|
|
|
|
|
|
# pass control to CMD
|
2020-08-06 18:48:55 +03:00
|
|
|
log "migrations-shutdown" "graphql-engine will now start in normal mode"
|
2018-11-02 17:08:25 +03:00
|
|
|
exec "$@"
|