#!/usr/bin/env bash set -euo pipefail # Renders JanusGraph runtime properties from secrets and starts Gremlin Server # Env: # DBOPS_ROOT=/data/adaptai/platform/dbops # SECRETS_DIR=/data/adaptai/secrets/dataops # Local ops env: /data/adaptai/platform/dbops/.env (preferred) # START_GREMLIN=1 (default) DBOPS_ROOT=${DBOPS_ROOT:-/data/adaptai/platform/dbops} SECRETS_DIR=${SECRETS_DIR:-/data/adaptai/secrets/dataops} START_GREMLIN=${START_GREMLIN:-1} JANUS_BACKEND=${JANUS_BACKEND:-scylla} PROPS_SRC="$DBOPS_ROOT/configs/janusgraph/janusgraph-cql.properties" RUN_DIR="$DBOPS_ROOT/run/janusgraph" PROPS_OUT="$RUN_DIR/janusgraph-cql.runtime.properties" GREMLIN_SRC="$DBOPS_ROOT/configs/janusgraph/gremlin-server.yaml" GREMLIN_OUT="$RUN_DIR/gremlin-server.runtime.yaml" JANUS_BIN="$DBOPS_ROOT/binaries/janusgraph/bin/janusgraph-server.sh" mkdir -p "$RUN_DIR" # Load ops env first, then secrets (optional) if [ -f "$DBOPS_ROOT/.env" ]; then # shellcheck disable=SC2046 export $(grep -E '^[A-Z0-9_]+=' "$DBOPS_ROOT/.env" | xargs -d'\n' || true) fi if [ -f "$SECRETS_DIR/.env" ]; then # shellcheck disable=SC2046 export $(grep -E '^(SCYLLA_USER|SCYLLA_PASS)=' "$SECRETS_DIR/.env" | xargs -d'\n' || true) fi # Render runtime properties by substituting ${SCYLLA_USER}/${SCYLLA_PASS} if [ "$JANUS_BACKEND" = "inmemory" ]; then cat > "$PROPS_OUT" <<'CONF' storage.backend=inmemory CONF else cp "$PROPS_SRC" "$PROPS_OUT" # Default to policy port 17542 unless overridden : "${SCYLLA_HOSTS:=scylla.dbops.local:17542}" # Override Scylla hosts/datacenter via env if provided if [ -n "${SCYLLA_HOSTS:-}" ]; then sed -i "s#^storage.hostname=.*#storage.hostname=${SCYLLA_HOSTS}#" "$PROPS_OUT" fi if [ -n "${SCYLLA_DC:-}" ]; then sed -i "s#^storage.cql.local-datacenter=.*#storage.cql.local-datacenter=${SCYLLA_DC}#" "$PROPS_OUT" fi # Inject credentials if provided, otherwise drop auth lines if [ -n "${SCYLLA_USER:-}" ]; then sed -i "s#\${SCYLLA_USER}#${SCYLLA_USER}#g" "$PROPS_OUT" else sed -i "/^storage.username=/d" "$PROPS_OUT" fi if [ -n "${SCYLLA_PASS:-}" ]; then sed -i "s#\${SCYLLA_PASS}#${SCYLLA_PASS}#g" "$PROPS_OUT" else sed -i "/^storage.password=/d" "$PROPS_OUT" fi # TLS: enable only if truststore exists TRUSTSTORE="$SECRETS_DIR/ca.pem" if [ -f "$TRUSTSTORE" ]; then sed -i "s#^storage.cql.ssl.enabled=.*#storage.cql.ssl.enabled=true#" "$PROPS_OUT" sed -i "s#^storage.cql.ssl.truststore=.*#storage.cql.ssl.truststore=$TRUSTSTORE#" "$PROPS_OUT" else sed -i "s#^storage.cql.ssl.enabled=.*#storage.cql.ssl.enabled=false#" "$PROPS_OUT" sed -i "/^storage.cql.ssl.truststore=/d" "$PROPS_OUT" fi fi # Point Gremlin graphs to runtime properties (replace entire graphs: block) cp "$GREMLIN_SRC" "$GREMLIN_OUT" awk -v repl="graphs:\n graph: $PROPS_OUT" ' BEGIN{inblock=0} /^graphs:/ {print repl; inblock=1; next} inblock && /^scriptEngines:/ {print; inblock=0; next} inblock {next} {print} ' "$GREMLIN_OUT" > "$GREMLIN_OUT.tmp" && mv "$GREMLIN_OUT.tmp" "$GREMLIN_OUT" # Remove unsupported keys for JanusGraphServer 1.1.x sed -i '/^scripts:/d' "$GREMLIN_OUT" echo "Rendered properties -> $PROPS_OUT" echo "Gremlin config -> $GREMLIN_OUT" if [ "$START_GREMLIN" = "1" ]; then if [ ! -x "$JANUS_BIN" ]; then echo "JanusGraph binary not found: $JANUS_BIN" >&2 echo "Attempting to download JanusGraph 1.1.0 (full)..." >&2 TMPDIR=$(mktemp -d) pushd "$TMPDIR" >/dev/null curl -fsSL -o janusgraph.zip https://github.com/JanusGraph/janusgraph/releases/download/v1.1.0/janusgraph-full-1.1.0.zip mkdir -p janus_tmp && unzip -q janusgraph.zip -d janus_tmp JDIR=$(find janus_tmp -maxdepth 1 -type d -name "janusgraph-*" | head -n1) if [ -z "$JDIR" ]; then echo "Failed to fetch JanusGraph distribution" >&2 exit 2 fi mkdir -p "$DBOPS_ROOT/binaries/janusgraph" cp -r "$JDIR"/* "$DBOPS_ROOT/binaries/janusgraph/" chmod +x "$DBOPS_ROOT/binaries/janusgraph/bin/janusgraph-server.sh" JANUS_BIN="$DBOPS_ROOT/binaries/janusgraph/bin/janusgraph-server.sh" popd >/dev/null rm -rf "$TMPDIR" fi # Choose foreground vs background mode. Under Supervisor, prefer foreground (run). CMD=start if [ "${JG_FOREGROUND:-0}" = "1" ] || [ -n "${SUPERVISOR_ENABLED:-}" ] || [ -n "${SUPERVISOR_PROCESS_NAME:-}" ]; then # JanusGraph 1.1.x uses 'console' for foreground (no 'run' subcommand) CMD=console exec "$JANUS_BIN" "$CMD" "$GREMLIN_OUT" fi exec "$JANUS_BIN" "$CMD" "$GREMLIN_OUT" fi exit 0