-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
86 lines (72 loc) · 3.26 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
set -e
PG_DATABASE_URL=${PG_DATABASE_URL:-postgres://postgres:postgres@postgres:5432/cic_graph}
/bin/tern migrate --migrations /service/migrations --config /service/migrations/tern.conf
log() {
TIMESTAMP=$(date -u "+%Y-%m-%dT%H:%M:%S.000+0000")
LOGKIND=$1
MESSAGE=$2
echo "{\"timestamp\":\"$TIMESTAMP\",\"level\":\"info\",\"type\":\"startup\",\"detail\":{\"kind\":\"$LOGKIND\",\"info\":\"$MESSAGE\"}}"
}
DEFAULT_METADATA_DIR="/service/metadata"
TEMP_PROJECT_DIR="/tmp/hasura-project"
if [ -z ${HGE_BINARY+x} ]; then
log "migrations-startup" "validation failed: expected HGE_BINARY environment variable to be set"
exit 1
fi
# Use 9691 port for running temporary instance.
# In case 9691 is occupied (according to docker networking), then this will fail.
# override with another port in that case
# TODO: Find a proper random port
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT+x} ]; then
log "migrations-startup" "migrations server port env var is not set, defaulting to 9691"
HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT=9691
fi
if [ -z ${HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT+x} ]; then
log "migrations-startup" "server timeout is not set, defaulting to 30 seconds"
HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT=30
fi
# wait for a port to be ready
wait_for_port() {
local PORT=$1
log "migrations-startup" "waiting $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT for $PORT to be ready"
for _ in $(seq 1 $HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT);
do
nc -z localhost "$PORT" > /dev/null 2>&1 && log "migrations-startup" "port $PORT is ready" && return
sleep 1
done
log "migrations-startup" "failed waiting for $PORT, try increasing HASURA_GRAPHQL_MIGRATIONS_SERVER_TIMEOUT (default: 30)" && exit 1
}
log "migrations-startup" "starting graphql engine temporarily on port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT"
# start graphql engine with metadata api enabled
$HGE_BINARY serve --enabled-apis="metadata" \
--server-port=${HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT} &
# store the pid to kill it later
PID=$!
# wait for port to be ready
wait_for_port $HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT
# check if metadata directory is set, default otherwise
if [ -z ${HASURA_GRAPHQL_METADATA_DIR+x} ]; then
log "migrations-startup" "env var HASURA_GRAPHQL_METADATA_DIR is not set, defaulting to $DEFAULT_METADATA_DIR"
HASURA_GRAPHQL_METADATA_DIR="$DEFAULT_METADATA_DIR"
fi
# apply metadata if the directory exist
if [ -d "$HASURA_GRAPHQL_METADATA_DIR" ]; then
rm -rf "TEMP_PROJECT_DIR"
log "migrations-apply" "applying metadata from $HASURA_GRAPHQL_METADATA_DIR"
mkdir -p "$TEMP_PROJECT_DIR"
cp -a "$HASURA_GRAPHQL_METADATA_DIR/." "$TEMP_PROJECT_DIR/metadata/"
cd "$TEMP_PROJECT_DIR"
echo "version: 3" > config.yaml
echo "endpoint: http://localhost:$HASURA_GRAPHQL_MIGRATIONS_SERVER_PORT" >> config.yaml
echo "metadata_directory: metadata" >> config.yaml
hasura-cli metadata apply
else
log "migrations-apply" "directory $HASURA_GRAPHQL_METADATA_DIR does not exist, skipping metadata"
fi
# kill graphql engine that we started earlier
log "migrations-shutdown" "killing temporary server"
kill $PID
# pass control to CMD
log "migrations-shutdown" "graphql-engine will now start in normal mode"
exec "$@"