|
| 1 | +#!/bin/sh |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +set -eu |
| 6 | + |
| 7 | +usage() { |
| 8 | + echo "Usage: init-gateway-config.sh <deb|homebrew|rpm|snap> <config-file> [package args...]" >&2 |
| 9 | + exit 2 |
| 10 | +} |
| 11 | + |
| 12 | +profile="${1:-}" |
| 13 | +CONFIG_FILE="${2:-}" |
| 14 | +if [ -z "$profile" ] || [ -z "$CONFIG_FILE" ]; then |
| 15 | + usage |
| 16 | +fi |
| 17 | + |
| 18 | +if [ -f "$CONFIG_FILE" ]; then |
| 19 | + exit 0 |
| 20 | +fi |
| 21 | + |
| 22 | +toml_escape() { |
| 23 | + printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' |
| 24 | +} |
| 25 | + |
| 26 | +toml_string() { |
| 27 | + printf '"%s"' "$(toml_escape "$1")" |
| 28 | +} |
| 29 | + |
| 30 | +emit_string_field() { |
| 31 | + key="$1" |
| 32 | + value="$2" |
| 33 | + if [ -n "$value" ]; then |
| 34 | + printf '%s = %s\n' "$key" "$(toml_string "$value")" |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +write_desktop_config() { |
| 39 | + pki_dir="${1:-}" |
| 40 | + driver_dir="${2:-}" |
| 41 | + vm_state_dir="${3:-}" |
| 42 | + docker_supervisor_image="${4:-}" |
| 43 | + docker_tls_dir="${5:-}" |
| 44 | + if [ -z "$pki_dir" ] || [ -z "$driver_dir" ] || [ -z "$vm_state_dir" ]; then |
| 45 | + usage |
| 46 | + fi |
| 47 | + |
| 48 | + mkdir -p "$(dirname "$CONFIG_FILE")" "$vm_state_dir" |
| 49 | + |
| 50 | + tmp="${CONFIG_FILE}.tmp" |
| 51 | + { |
| 52 | + cat <<EOF |
| 53 | +[openshell] |
| 54 | +version = 1 |
| 55 | +
|
| 56 | +[openshell.gateway] |
| 57 | +bind_address = "127.0.0.1:17670" |
| 58 | +# Leave unset to auto-detect the compute driver. |
| 59 | +# compute_drivers = ["vm"] |
| 60 | +default_image = "ghcr.io/nvidia/openshell-community/sandboxes/base:latest" |
| 61 | +supervisor_image = "ghcr.io/nvidia/openshell/supervisor:latest" |
| 62 | +guest_tls_ca = $(toml_string "${pki_dir}/ca.crt") |
| 63 | +guest_tls_cert = $(toml_string "${pki_dir}/client/tls.crt") |
| 64 | +guest_tls_key = $(toml_string "${pki_dir}/client/tls.key") |
| 65 | +
|
| 66 | +[openshell.gateway.tls] |
| 67 | +cert_path = $(toml_string "${pki_dir}/server/tls.crt") |
| 68 | +key_path = $(toml_string "${pki_dir}/server/tls.key") |
| 69 | +client_ca_path = $(toml_string "${pki_dir}/ca.crt") |
| 70 | +
|
| 71 | +[openshell.drivers.vm] |
| 72 | +state_dir = $(toml_string "$vm_state_dir") |
| 73 | +driver_dir = $(toml_string "$driver_dir") |
| 74 | +grpc_endpoint = "https://127.0.0.1:17670" |
| 75 | +
|
| 76 | +[openshell.drivers.docker] |
| 77 | +grpc_endpoint = "https://127.0.0.1:17670" |
| 78 | +EOF |
| 79 | + |
| 80 | + emit_string_field supervisor_image "$docker_supervisor_image" |
| 81 | + if [ -n "$docker_tls_dir" ]; then |
| 82 | + emit_string_field guest_tls_ca "${docker_tls_dir}/ca.crt" |
| 83 | + emit_string_field guest_tls_cert "${docker_tls_dir}/client/tls.crt" |
| 84 | + emit_string_field guest_tls_key "${docker_tls_dir}/client/tls.key" |
| 85 | + fi |
| 86 | + } > "$tmp" |
| 87 | + |
| 88 | + chmod 600 "$tmp" |
| 89 | + mv "$tmp" "$CONFIG_FILE" |
| 90 | +} |
| 91 | + |
| 92 | +write_snap_config() { |
| 93 | + supervisor_bin="${1:-}" |
| 94 | + if [ -z "$supervisor_bin" ]; then |
| 95 | + usage |
| 96 | + fi |
| 97 | + |
| 98 | + mkdir -p "$(dirname "$CONFIG_FILE")" |
| 99 | + |
| 100 | + tmp="${CONFIG_FILE}.tmp" |
| 101 | + { |
| 102 | + cat <<EOF |
| 103 | +[openshell] |
| 104 | +version = 1 |
| 105 | +
|
| 106 | +[openshell.gateway] |
| 107 | +bind_address = "127.0.0.1:17670" |
| 108 | +disable_tls = true |
| 109 | +# Leave unset to auto-detect the compute driver. |
| 110 | +# compute_drivers = ["docker"] |
| 111 | +default_image = "ghcr.io/nvidia/openshell-community/sandboxes/base:latest" |
| 112 | +
|
| 113 | +[openshell.drivers.docker] |
| 114 | +image_pull_policy = "IfNotPresent" |
| 115 | +sandbox_namespace = "docker-snap" |
| 116 | +grpc_endpoint = "http://host.openshell.internal:17670" |
| 117 | +supervisor_bin = $(toml_string "$supervisor_bin") |
| 118 | +network_name = "openshell-snap" |
| 119 | +EOF |
| 120 | + } > "$tmp" |
| 121 | + |
| 122 | + chmod 600 "$tmp" |
| 123 | + mv "$tmp" "$CONFIG_FILE" |
| 124 | +} |
| 125 | + |
| 126 | +write_rpm_config() { |
| 127 | + pki_dir="${1:-}" |
| 128 | + supervisor_image="${2:-}" |
| 129 | + if [ -z "$pki_dir" ] || [ -z "$supervisor_image" ]; then |
| 130 | + usage |
| 131 | + fi |
| 132 | + |
| 133 | + mkdir -p "$(dirname "$CONFIG_FILE")" |
| 134 | + |
| 135 | + tmp="${CONFIG_FILE}.tmp" |
| 136 | + { |
| 137 | + cat <<EOF |
| 138 | +[openshell] |
| 139 | +version = 1 |
| 140 | +
|
| 141 | +[openshell.gateway] |
| 142 | +bind_address = "127.0.0.1:17670" |
| 143 | +# Leave unset to auto-detect the compute driver. |
| 144 | +# compute_drivers = ["podman"] |
| 145 | +default_image = "ghcr.io/nvidia/openshell-community/sandboxes/base:latest" |
| 146 | +supervisor_image = $(toml_string "$supervisor_image") |
| 147 | +guest_tls_ca = $(toml_string "${pki_dir}/ca.crt") |
| 148 | +guest_tls_cert = $(toml_string "${pki_dir}/client/tls.crt") |
| 149 | +guest_tls_key = $(toml_string "${pki_dir}/client/tls.key") |
| 150 | +
|
| 151 | +[openshell.gateway.tls] |
| 152 | +cert_path = $(toml_string "${pki_dir}/server/tls.crt") |
| 153 | +key_path = $(toml_string "${pki_dir}/server/tls.key") |
| 154 | +client_ca_path = $(toml_string "${pki_dir}/ca.crt") |
| 155 | +
|
| 156 | +[openshell.drivers.podman] |
| 157 | +image_pull_policy = "missing" |
| 158 | +network_name = "openshell" |
| 159 | +stop_timeout_secs = 10 |
| 160 | +EOF |
| 161 | + } > "$tmp" |
| 162 | + |
| 163 | + chmod 600 "$tmp" |
| 164 | + mv "$tmp" "$CONFIG_FILE" |
| 165 | +} |
| 166 | + |
| 167 | +case "$profile" in |
| 168 | + deb) |
| 169 | + write_desktop_config "${3:-}" "${4:-}" "${5:-}" "" "" |
| 170 | + ;; |
| 171 | + homebrew) |
| 172 | + write_desktop_config "${3:-}" "${4:-}" "${5:-}" "${6:-}" "${7:-}" |
| 173 | + ;; |
| 174 | + rpm) |
| 175 | + write_rpm_config "${3:-}" "${4:-}" |
| 176 | + ;; |
| 177 | + snap) |
| 178 | + write_snap_config "${3:-}" |
| 179 | + ;; |
| 180 | + *) |
| 181 | + usage |
| 182 | + ;; |
| 183 | +esac |
0 commit comments