Skip to content

Instantly share code, notes, and snippets.

@ches
Created June 21, 2023 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ches/33d3566db2d515047c6931b5cfe837a2 to your computer and use it in GitHub Desktop.
Save ches/33d3566db2d515047c6931b5cfe837a2 to your computer and use it in GitHub Desktop.
Java app Docker entrypoint script, with a simple JVM flags file
#!/bin/bash
# Contains a few particularities of $DAYJOB's infra environment, but you get the, erm, gist.
set -o errexit
declare -r app_jar=/opt/myapp/my-app.jar
declare -r consul_template_config=/etc/consul.d/template/config.json
declare -r flags_file="${JVM_FLAGS_FILE:=/etc/myapp/application.ini}"
# For JMX's weird connection negotiation in a container world
declare rmi_hostname=$(hostname)
notice() {
echo "[my-app] $*"
}
# Loads a configuration file containing base command line options for this script.
# Strips comment lines beginning with pound sign.
#
# Idea stolen from sbt-native-packager, eases making and reviewing changes to
# numerous JVM flags, etc.
loadConfigFile() {
sed $'/^\#/d;/^$/d;s/\r$//' "$1"
}
# Renders config with secrets and service discovery if consul-template is in use.
renderAppConfig() {
if [[ -r "$consul_template_config" ]]; then
notice "consul-template rendering..."
templates_root="/etc/myapp/templates"
consul-template -config "$consul_template_config" \
-template "${templates_root}/application.ctmpl:/etc/myapp/application.yml" \
-once
fi
}
# Waits until Vault sidecar is ready
waitForVault() {
notice "waiting for local Vault agent..."
until curl --output /dev/null --silent --head --fail http://localhost:8200/v1/sys/health; do sleep 2; done
}
run() {
local -a flags
readarray -t flags < <(loadConfigFile "$flags_file")
flags+=("-Djava.rmi.server.hostname=$rmi_hostname")
notice "service starting..."
exec java "${flags[@]}" -jar "$app_jar" "$@"
}
if [[ "$DEPLOYMENT" == "privatecloud" ]]; then
rmi_hostname=127.0.0.1 # Work around JMX's dance for port forwarding with Kubernetes internal interface
waitForVault
fi
renderAppConfig
run "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment