| 1 | #!/usr/bin/env bash |
| 2 | # Starts self-hosted g1t inside its container: makes the keys it needs once, |
| 3 | # writes the Wrangler configs, brings every database up to date, and runs |
| 4 | # every Worker in one workerd on :8787. |
| 5 | set -euo pipefail |
| 6 | |
| 7 | cd "$(dirname "$0")" |
| 8 | DATA="${G1T_DATA:-/data}" |
| 9 | STATE="$DATA/state" |
| 10 | KEYS="$DATA/keys.env" |
| 11 | GENERATED="$DATA/generated" |
| 12 | WRANGLER="$(cd ../.. && pwd)/node_modules/.bin/wrangler" |
| 13 | mkdir -p "$STATE" |
| 14 | |
| 15 | # Keys that seal secrets at rest (actions, integrations, webhooks). Made on |
| 16 | # first start and kept on the volume: losing them loses those secrets. |
| 17 | if [ ! -f "$KEYS" ]; then |
| 18 | umask 077 |
| 19 | { |
| 20 | echo "ACTIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 21 | echo "INTEGRATIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 22 | echo "WEBHOOKS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 23 | } > "$KEYS" |
| 24 | fi |
| 25 | set -a |
| 26 | # shellcheck disable=SC1090 |
| 27 | . "$KEYS" |
| 28 | set +a |
| 29 | |
| 30 | # The git store's shared secret: given, or the file the git store made. |
| 31 | if [ -z "${GITSTORE_SECRET:-}" ] && [ -n "${GITSTORE_SECRET_FILE:-}" ]; then |
| 32 | for _ in $(seq 1 60); do [ -s "$GITSTORE_SECRET_FILE" ] && break; sleep 1; done |
| 33 | GITSTORE_SECRET="$(cat "$GITSTORE_SECRET_FILE")" |
| 34 | export GITSTORE_SECRET |
| 35 | fi |
| 36 | [ -n "${GITSTORE_SECRET:-}" ] || { echo "No GITSTORE_SECRET or GITSTORE_SECRET_FILE." >&2; exit 1; } |
| 37 | |
| 38 | node configs.mjs "$GENERATED" |
| 39 | cd "$GENERATED" |
| 40 | |
| 41 | # Migrations: the same files D1 gets, applied to the SQLite files on the |
| 42 | # volume. Applied ones are recorded, so this is safe on every start. |
| 43 | for config in $(cat workers.txt); do |
| 44 | for database in $(node -e "for (const d of require('./$config').d1_databases ?? []) console.log(d.database_name)"); do |
| 45 | echo "Migrating $database" |
| 46 | "$WRANGLER" d1 migrations apply "$database" --local --persist-to "$STATE" -c "$config" >/dev/null |
| 47 | done |
| 48 | done |
| 49 | |
| 50 | args=() |
| 51 | while read -r config; do args+=(-c "$config"); done < workers.txt |
| 52 | echo "g1t is starting on ${PUBLIC_URL:-http://localhost:8787}" |
| 53 | exec "$WRANGLER" dev "${args[@]}" \ |
| 54 | --ip 0.0.0.0 --port 8787 \ |
| 55 | --persist-to "$STATE" \ |
| 56 | --show-interactive-dev-session=false |