File size: 4,564 Bytes
503b0e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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