-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.sh
executable file
·126 lines (106 loc) · 5.7 KB
/
bootstrap.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/sh
## Defaults
[ -z ${ARTIFACT_ID+x} ] && export ARTIFACT_ID="${UNDEFINED}"
echo "Bootstrapping ${ARTIFACT_ID}..."
# SYS and APP variables
[ -z ${SYS+x} ] && export SYS=$(echo ${ARTIFACT_ID}| cut -d'-' -f 1)
[ -z ${APP+x} ] && export APP=${ARTIFACT_ID:${#SYS}+1}
echo "- ARTIFACT_ID '${ARTIFACT_ID}' parsed as SYS='${SYS}' and APP='${APP}'"
# Microservice
[ -z ${MICROSERVICE+x} ] && export MICROSERVICE="target/microservice.jar"
echo "- Using the '${MICROSERVICE}' jar file"
# Parsing app auth secrets file if it exists...
[ -z ${APP_AUTH+x} ] && export APP_AUTH="/run/secrets/app/auth"
if [ -f "${APP_AUTH}" ]; then
echo "- Parsing credentials from the '${APP_AUTH}' secrets file"
IFS=':' read -ra CRED < ${APP_AUTH}
export APP_USER="${CRED[0]}" APP_PASS="${CRED[1]}"
fi
# Parsing username secrets file if it exists...
[ -z ${APP_USER_FILE+x} ] && export APP_USER_FILE="/run/secrets/app/username"
if [ -f "${APP_USER_FILE}" ]; then
echo "- Parsing username from the '${APP_USER}' secrets file"
export APP_USER=$(cat ${APP_USER_FILE} | xargs echo -n)
fi
# Parsing password secrets file if it exists...
[ -z ${APP_PASS_FILE+x} ] && export APP_PASS_FILE="/run/secrets/app/password"
if [ -f "${APP_PASS_FILE}" ]; then
echo "- Parsing password from the '${APP_USER}' secrets file"
export APP_PASS=$(cat ${APP_PASS_FILE} | xargs echo -n)
fi
export APP_USER="${APP_USER:-CHANGEME}" APP_PASS="${APP_PASS:-CHANGEME}"
echo "- Using the '${APP_USER}' account as the Microservice identity"
# Parsing app secret
[ -z ${SECRET_PATH+x} ] && export SECRET_PATH="/run/secrets/app-secret"
if [ -f "${SECRET_PATH}" ]; then
# Note that we assume that the secret does not contain whitespace as the command below will trim and remove all
# multiple whitespace, e.g. the string " some secret\n" will become "some secret"
echo "- Using the content of the '$SECRET_PATH' as the Microservice secret"
export SECRET=$(cat $SECRET_PATH | xargs echo -n)
else
echo "WARN: The SECRET_PATH envar does not point to a secrets file, defaulting to the value of the SECRET envar"
fi
# Configuring JMX options...
[ -z ${JMXREMOTE_PASSWORD_PATH+x} ] && export JMXREMOTE_PASSWORD_PATH="/run/secrets/jmxremote.password"
if [ -f "${JMXREMOTE_PASSWORD_PATH}" ]; then
## JMX Options
echo "- Enabling remote JMX access using the '$JMXREMOTE_PASSWORD_PATH' remote password file"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.port=8181"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.password.file=${JMXREMOTE_PASSWORD_PATH}"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.access.file=${ETC_DIR}/jmxremote.access"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.ssl=false"
JMX_OPTS="${JMX_OPTS} -Dcom.sun.management.jmxremote.local.only=false"
else
echo "WARN: Remote JMX access disabled as the '${JMXREMOTE_PASSWORD_PATH}' JMX remote password file could not be found"
fi
# Directories
[ -z ${ETC_DIR+x} ] && export ETC_DIR="src/files"
echo "- Configuration files stored in '$ETC_DIR'"
[ -z ${CACHE_DIR+x} ] && export CACHE_DIR="target/microservice/cache"
echo "- Files cached in '${CACHE_DIR}'"
# Spring Cloud Configuration
[ -z ${CONFIG_URI+x} ] && export CONFIG_URI="http://localhost:8888"
[ -z ${CONFIG_LABEL+x} ] && export CONFIG_LABEL="develop"
[ -z ${CONFIG_PROFILES+x} ] && export CONFIG_PROFILES="loc"
# For debugging and non-spring boot apps
[ -z ${CONFIG+x} ] && export CONFIG="${CONFIG_URI}/${ARTIFACT_ID}/${CONFIG_PROFILES}/${CONFIG_LABEL}"
echo "- Config will be loaded from '${CONFIG}'"
# Spring Boot Configuration
[ -z ${SPRING_CLOUD_CONFIG_URI+x} ] && export SPRING_CLOUD_CONFIG_URI="${CONFIG_URI}"
[ -z ${SPRING_CLOUD_CONFIG_LABEL+x} ] && export SPRING_CLOUD_CONFIG_LABEL="${CONFIG_LABEL}"
[ -z ${SPRING_CLOUD_CONFIG_USERNAME+x} ] && export SPRING_CLOUD_CONFIG_USERNAME="${APP_USER}"
[ -z ${SPRING_CLOUD_CONFIG_PASSWORD+x} ] && export SPRING_CLOUD_CONFIG_PASSWORD="${APP_PASS}"
[ -z ${SPRING_PROFILES_ACTIVE+x} ] && export SPRING_PROFILES_ACTIVE="${CONFIG_PROFILES}"
[ -z ${SPRING_APPLICATION_NAME+x} ] && export SPRING_APPLICATION_NAME="${ARTIFACT_ID}"
[ -z ${HTTP_PORT+x} ] && export HTTP_PORT=9000
# TODO configure spring boot http context = /${ARTIFACT_ID}
# TODO configure java trust store
[ -z ${KEYSTORE+x} ] && export KEYSTORE="file://${ETC_DIR}/keystore.jceks"
echo "- Keystore will be loaded from '${KEYSTORE}'"
[ -z ${SECRET+x} ] && export SECRET="CHANGEME" && echo "WARN: Using default secret '${SECRET}'"
## Java Options
JAVA_OPTS="${JAVA_OPTS} -server"
JAVA_OPTS="${JAVA_OPTS} -Xms${XMS:-32M}"
JAVA_OPTS="${JAVA_OPTS} -Xmx${XMX:-128M}"
# The OnOutOfMemoryError fails the JVM on startup with a message "Unrecognized option: -9"
#JAVA_OPTS="${JAVA_OPTS} -XX:OnOutOfMemoryError='kill -9 %p'"
# Using the new CrashOnOutOfMemoryError option from Java 8u92 instead:
JAVA_OPTS="${JAVA_OPTS} -XX:+CrashOnOutOfMemoryError"
# Clean exit without crash files, probably not what we want
#JAVA_OPTS="${JAVA_OPTS} -XX:+ExitOnOutOfMemoryError"
# We do want a heap dump for further analysis
JAVA_OPTS="${JAVA_OPTS} -XX:+HeapDumpOnOutOfMemoryError"
## Application options
APP_OPTS="${APP_OPTS} -Dlogback.configurationFile=${ETC_DIR}/logback.xml"
APP_OPTS="${APP_OPTS} -Dlogging.config=${ETC_DIR}/logback.xml"
APP_OPTS="${APP_OPTS} -Dconfig.url=${CONFIG}"
APP_OPTS="${APP_OPTS} -Dkeystore.url=${KEYSTORE}"
APP_OPTS="${APP_OPTS} -Dkeystore.password=${SECRET}"
APP_OPTS="${APP_OPTS} -Djwt.secret=${SECRET}"
APP_OPTS="${APP_OPTS} -Dcache.dir=${CACHE_DIR}"
echo "Content of ${ETC_DIR}:"
ls -l ${ETC_DIR}
## Start the Microservice
echo "Bootstrapping finished, starting the ${MICROSERVICE} Microservice..."
exec java ${JAVA_OPTS} ${JMX_OPTS} ${APP_OPTS} -jar ${MICROSERVICE} "${@}"