deploy.sh: detect JAVA_HOME and inject into systemd unit at deploy time

The pipekit system user has no PATH to java. deploy.sh now detects
JAVA_HOME by searching common locations and injects Environment= lines
into the installed unit file, making deploys portable across machines.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Paul Trowbridge 2026-06-03 23:34:27 -04:00
parent a66488d1f2
commit ed3653f410

View File

@ -182,8 +182,31 @@ step "Systemd unit"
if [ ! -f "$UNIT_SRC" ]; then
echo " WARNING: $UNIT_SRC not found — skipping"
else
# Detect JAVA_HOME — check PATH first, then search each location individually
JAVA_BIN="$(command -v java 2>/dev/null || true)"
if [ -z "$JAVA_BIN" ]; then
for _dir in /opt /usr/lib/jvm /usr/local/lib/jvm; do
[ -d "$_dir" ] || continue
JAVA_BIN="$(find "$_dir" -maxdepth 3 -name java -type f 2>/dev/null | head -1 || true)"
[ -n "$JAVA_BIN" ] && break
done
fi
if [ -z "$JAVA_BIN" ]; then
echo " WARNING: java not found — JAVA_HOME will not be set in unit"
JAVA_HOME_DETECTED=""
else
JAVA_HOME_DETECTED="$(dirname "$(dirname "$(readlink -f "$JAVA_BIN")")")"
echo " Detected JAVA_HOME: $JAVA_HOME_DETECTED"
fi
echo " Installing $UNIT_DST"
if [ -n "$JAVA_HOME_DETECTED" ]; then
# Inject Environment lines after WorkingDirectory= using printf to handle newlines portably
ENV_BLOCK="$(printf 'Environment=JAVA_HOME=%s\nEnvironment=PATH=%s/bin:/usr/local/bin:/usr/bin:/bin' "$JAVA_HOME_DETECTED" "$JAVA_HOME_DETECTED")"
awk -v env="$ENV_BLOCK" '/^WorkingDirectory=/{print; print env; next}1' "$UNIT_SRC" > "$UNIT_DST"
else
cp "$UNIT_SRC" "$UNIT_DST"
fi
echo " Running systemctl daemon-reload"
systemctl daemon-reload
echo " Enabling $SERVICE_NAME service"