erp-test-suite / start.sh
cronjob-python's picture
Deploy bc3ca8b2da9e8965989188ae78089a0cf381833f
dc510b0 verified
Raw History Blame Contribute Delete
4.81 kB
#!/bin/sh
# start.sh - HF Space entrypoint for the Spring Boot ERP seeder.
# POSIX sh (Alpine has no bash). Single source of truth = the `ENV_FILE`
# deployment secret (multi-line KEY=VALUE document, pasted from .env.hf.clean).
# Precedence (highest first): pre-existing process env, ENV_FILE, .env file.
# Never logs values - key names only.
set -eu
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
# trim leading/trailing whitespace (spaces/tabs) - $1 -> stdout
_trim() {
_t="$1"
_t="${_t#"${_t%%[! \t]*}"}"
_t="${_t%"${_t##*[! \t]}"}"
printf '%s' "$_t"
}
# Parse one multi-line KEY=VALUE document and export each entry.
# Handles: CRLF endings, blank lines, '#' comments, 'export ' prefixes,
# surrounding single/double quotes, values containing '=' (split on first '=').
load_env_document() {
_content="$1"
_tmp="$(mktemp 2>/dev/null || echo /tmp/envdoc.tmp)"
printf '%s\n' "$_content" > "$_tmp"
_ok=0 _skip=0 _fail=0
while IFS= read -r _line || [ -n "$_line" ]; do
_line="${_line%$'\r'}"
_line="$(printf '%s' "$_line" | tr -d '\r')"
_key="$(_trim "$_line")"
[ -n "$_key" ] || continue
case "$_key" in \#*) continue ;; esac
case "$_key" in export\ *) _key="$(_trim "${_key#export }")" ;; esac
case "$_key" in
*=*)
_val="${_key#*=}"
_key="${_key%%=*}"
;;
*)
log "ENV FAIL: malformed line, no '=' found"
_fail=$((_fail + 1))
continue
;;
esac
_key="$(_trim "$_key")"
case "$_key" in
'' | *[!A-Za-z0-9_]* | [0-9]*)
log "ENV FAIL: invalid variable name '${_key}'"
_fail=$((_fail + 1))
continue
;;
esac
_val="$(_trim "$_val")"
case "$_val" in
\"*\") _val="${_val#\"}"; _val="${_val%\"}" ;;
\'*\') _val="${_val#\'}"; _val="${_val%\'}" ;;
esac
# _ = indirect "is set?" test (set even if empty counts as set)
if eval "[ -n \"\${${_key}+x}\" ]"; then
log "ENV SKIP: $_key (already set in process environment - keeping existing value)"
_skip=$((_skip + 1))
elif export "${_key}=${_val}" 2>/dev/null; then
log "ENV OK: $_key"
_ok=$((_ok + 1))
else
log "ENV FAIL: $_key (could not export)"
_fail=$((_fail + 1))
fi
done < "$_tmp"
rm -f "$_tmp"
log "ENV_FILE load summary: $_ok loaded, $_skip skipped, $_fail failed, $((_ok + _skip + _fail)) total"
}
# Fallback for local development: flat .env file, same rules (first '=' split).
load_dotenv_file() {
_file="$1"
[ -f "$_file" ] || return 0
log "Loading fallback file: $_file"
while IFS= read -r _line || [ -n "$_line" ]; do
_line="$(printf '%s' "$_line" | tr -d '\r')"
_key="$(_trim "$_line")"
[ -n "$_key" ] || continue
case "$_key" in \#*) continue ;; esac
case "$_key" in *=*) ;; *) continue ;; esac
_val="${_key#*=}"
_key="$(_trim "${_key%%=*}")"
_val="$(_trim "$_val")"
case "$_val" in
\"*\") _val="${_val#\"}"; _val="${_val%\"}" ;;
\'*\') _val="${_val#\'}"; _val="${_val%\'}" ;;
esac
if eval "[ -n \"\${${_key}+x}\" ]"; then
log "ENV SKIP: $_key (already set)"
else
export "${_key}=${_val}" 2>/dev/null && log "ENV OK: $_key" || log "ENV FAIL: $_key"
fi
done < "$_file"
}
if [ -n "${ENV_FILE:-}" ]; then
log "ENV_FILE secret detected - loading embedded environment"
load_env_document "$ENV_FILE"
else
log "ENV_FILE secret not set - falling back to .env file (local dev)"
load_dotenv_file ".env"
fi
# Post-load sanity check for critical keys (warnings only - never fatal,
# Spring falls back to built-in defaults in application.yml).
_crit_missing=""
for _v in MONGODB_URI MYSQL_JDBC_URL POSTGRES_JDBC_URL; do
if eval "[ -z \"\${${_v}:-}\" ]"; then
_crit_missing="$_crit_missing $_v"
fi
done
if [ -n "$_crit_missing" ]; then
log "WARNING: critical environment keys not set (using application.yml defaults):$_crit_missing"
else
log "Critical environment keys: all present"
fi
unset _crit_missing _v
PORT="${PORT:-${SERVER_PORT:-7860}}"
export SERVER_PORT="$PORT"
log "=== Starting ERP seeder (port=$PORT) ==="
log "Java: $(java -version 2>&1 | head -n 1)"
JAR="$(ls -t /app/target/erp-system-*.jar 2>/dev/null | head -n 1)"
if [ -z "$JAR" ]; then
log "ERROR: no jar found under /app/target - build failed?"
ls /app/target/ 2>/dev/null || log "ERROR: /app/target missing"
exit 1
fi
log "Jar: $JAR"
exec java -jar "$JAR"