|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# Start a standalone openshell-gateway backed by the Kubernetes compute driver. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 11 | +# shellcheck source=tasks/scripts/gateway-common.sh |
| 12 | +source "${ROOT}/tasks/scripts/gateway-common.sh" |
| 13 | +# shellcheck source=tasks/scripts/gateway-pull-policy.sh |
| 14 | +source "${ROOT}/tasks/scripts/gateway-pull-policy.sh" |
| 15 | + |
| 16 | +PORT="${OPENSHELL_SERVER_PORT:-8080}" |
| 17 | +GATEWAY_NAME="${OPENSHELL_GATEWAY_NAME:-kubernetes-dev}" |
| 18 | +STATE_DIR="${OPENSHELL_GATEWAY_STATE_DIR:-${ROOT}/.cache/gateway-kubernetes}" |
| 19 | +SANDBOX_NAMESPACE="${OPENSHELL_SANDBOX_NAMESPACE:-kubernetes-dev}" |
| 20 | +SANDBOX_IMAGE="${OPENSHELL_SANDBOX_IMAGE:-ghcr.io/nvidia/openshell-community/sandboxes/base:latest}" |
| 21 | +SANDBOX_IMAGE_PULL_POLICY="$(normalize_image_pull_policy "${OPENSHELL_SANDBOX_IMAGE_PULL_POLICY:-if_not_present}")" |
| 22 | +GRPC_ENDPOINT="${OPENSHELL_GRPC_ENDPOINT:-}" |
| 23 | +LOG_LEVEL="${OPENSHELL_LOG_LEVEL:-info}" |
| 24 | +PRIMARY_BIND_IP="${OPENSHELL_BIND_ADDRESS:-127.0.0.1}" |
| 25 | +GATEWAY_BIN="${OPENSHELL_GATEWAY_BIN:-${ROOT}/target/debug/openshell-gateway}" |
| 26 | + |
| 27 | +validate_gateway_name "${GATEWAY_NAME}" OPENSHELL_GATEWAY_NAME |
| 28 | + |
| 29 | +if port_is_in_use "${PORT}"; then |
| 30 | + echo "ERROR: port ${PORT} is already in use; free it or set OPENSHELL_SERVER_PORT" >&2 |
| 31 | + exit 2 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "Building openshell-gateway..." |
| 35 | +run_mise_task build:gateway |
| 36 | + |
| 37 | +if [[ ! -x "${GATEWAY_BIN}" ]]; then |
| 38 | + echo "ERROR: expected gateway binary at ${GATEWAY_BIN}" >&2 |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +TLS_DIR="${STATE_DIR}/tls" |
| 43 | +echo "Generating local gateway credentials..." |
| 44 | +"${GATEWAY_BIN}" generate-certs \ |
| 45 | + --output-dir "${TLS_DIR}" \ |
| 46 | + --server-san "127.0.0.1" \ |
| 47 | + --server-san "localhost" \ |
| 48 | + --server-san "host.openshell.internal" |
| 49 | + |
| 50 | +mkdir -p "${STATE_DIR}" |
| 51 | +CONFIG_PATH="${STATE_DIR}/gateway.toml" |
| 52 | +install -m 600 /dev/null "${CONFIG_PATH}" |
| 53 | +cat >"${CONFIG_PATH}" <<EOF |
| 54 | +[openshell] |
| 55 | +version = 2 |
| 56 | +
|
| 57 | +[openshell.gateway] |
| 58 | +name = "${GATEWAY_NAME}" |
| 59 | +compute_driver = "kubernetes" |
| 60 | +disable_tls = true |
| 61 | +
|
| 62 | +[openshell.gateway.otlp] |
| 63 | +endpoint = "http://127.0.0.1:4317" |
| 64 | +
|
| 65 | +[openshell.gateway.auth] |
| 66 | +allow_unauthenticated_users = true |
| 67 | +
|
| 68 | +[openshell.gateway.gateway_jwt] |
| 69 | +signing_key_path = "${TLS_DIR}/jwt/signing.pem" |
| 70 | +public_key_path = "${TLS_DIR}/jwt/public.pem" |
| 71 | +kid_path = "${TLS_DIR}/jwt/kid" |
| 72 | +gateway_id = "${GATEWAY_NAME}" |
| 73 | +ttl_secs = 3600 |
| 74 | +
|
| 75 | +[openshell.drivers.kubernetes] |
| 76 | +namespace = "${SANDBOX_NAMESPACE}" |
| 77 | +default_image = "${SANDBOX_IMAGE}" |
| 78 | +image_pull_policy = "${SANDBOX_IMAGE_PULL_POLICY}" |
| 79 | +EOF |
| 80 | +if [[ -n "${GRPC_ENDPOINT}" ]]; then |
| 81 | + printf 'grpc_endpoint = "%s"\n' "${GRPC_ENDPOINT}" >>"${CONFIG_PATH}" |
| 82 | +fi |
| 83 | + |
| 84 | +GATEWAY_ENDPOINT="http://127.0.0.1:${PORT}" |
| 85 | +register_local_gateway "${GATEWAY_NAME}" "${GATEWAY_ENDPOINT}" "${PORT}" true |
| 86 | + |
| 87 | +echo "Starting standalone Kubernetes gateway..." |
| 88 | +echo " gateway: ${GATEWAY_NAME}" |
| 89 | +echo " endpoint: ${GATEWAY_ENDPOINT}" |
| 90 | +echo " bind: ${PRIMARY_BIND_IP}:${PORT}" |
| 91 | +echo " namespace: ${SANDBOX_NAMESPACE}" |
| 92 | +echo " state dir: ${STATE_DIR}" |
| 93 | +echo |
| 94 | +echo "Active gateway set to '${GATEWAY_NAME}'. The CLI now targets this gateway by default." |
| 95 | +echo |
| 96 | + |
| 97 | +exec "${GATEWAY_BIN}" \ |
| 98 | + --config "${CONFIG_PATH}" \ |
| 99 | + --bind-address "${PRIMARY_BIND_IP}" \ |
| 100 | + --port "${PORT}" \ |
| 101 | + --log-level "${LOG_LEVEL}" \ |
| 102 | + --compute-driver kubernetes \ |
| 103 | + --disable-tls \ |
| 104 | + --db-url "sqlite:${STATE_DIR}/gateway.db?mode=rwc" |
0 commit comments