| 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 | HERE="$(pwd)" |
| 9 | DATA="${G1T_DATA:-/data}" |
| 10 | STATE="$DATA/state" |
| 11 | KEYS="$DATA/keys.env" |
| 12 | GENERATED="$DATA/generated" |
| 13 | WRANGLER="$(cd ../.. && pwd)/node_modules/.bin/wrangler" |
| 14 | mkdir -p "$STATE" |
| 15 | |
| 16 | # Keys that seal secrets at rest (actions, integrations, webhooks). Made on |
| 17 | # first start and kept on the volume: losing them loses those secrets. |
| 18 | if [ ! -f "$KEYS" ]; then |
| 19 | umask 077 |
| 20 | { |
| 21 | echo "ACTIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 22 | echo "INTEGRATIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 23 | echo "WEBHOOKS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" |
| 24 | } > "$KEYS" |
| 25 | fi |
| 26 | # Identity's key seals the GitHub tokens of linked accounts; added to keys |
| 27 | # made before it existed. |
| 28 | if ! grep -q '^IDENTITY_KEY=' "$KEYS"; then |
| 29 | echo "IDENTITY_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS" |
| 30 | fi |
| 31 | # The packages service's key, which signs registry tokens. |
| 32 | if ! grep -q '^PACKAGES_TOKEN_SECRET=' "$KEYS"; then |
| 33 | echo "PACKAGES_TOKEN_SECRET=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS" |
| 34 | fi |
| 35 | set -a |
| 36 | # shellcheck disable=SC1090 |
| 37 | . "$KEYS" |
| 38 | set +a |
| 39 | |
| 40 | # The git store's shared secret: given, or the file the git store made. |
| 41 | if [ -z "${GITSTORE_SECRET:-}" ] && [ -n "${GITSTORE_SECRET_FILE:-}" ]; then |
| 42 | for _ in $(seq 1 60); do [ -s "$GITSTORE_SECRET_FILE" ] && break; sleep 1; done |
| 43 | GITSTORE_SECRET="$(cat "$GITSTORE_SECRET_FILE")" |
| 44 | export GITSTORE_SECRET |
| 45 | fi |
| 46 | [ -n "${GITSTORE_SECRET:-}" ] || { echo "No GITSTORE_SECRET or GITSTORE_SECRET_FILE." >&2; exit 1; } |
| 47 | |
| 48 | node configs.mjs "$GENERATED" |
| 49 | cd "$GENERATED" |
| 50 | |
| 51 | # Migrations: the same files D1 gets, applied to the SQLite files on the |
| 52 | # volume. Applied ones are recorded, so this is safe on every start. |
| 53 | for config in $(cat workers.txt); do |
| 54 | for database in $(node -e "for (const d of require('./$config').d1_databases ?? []) console.log(d.database_name)"); do |
| 55 | echo "Migrating $database" |
| 56 | "$WRANGLER" d1 migrations apply "$database" --local --persist-to "$STATE" -c "$config" >/dev/null |
| 57 | done |
| 58 | done |
| 59 | |
| 60 | args=() |
| 61 | while read -r config; do args+=(-c "$config"); done < workers.txt |
| 62 | |
| 63 | # workerd fires no cron triggers: the scheduler runs the services' crons |
| 64 | # (schedules.json, from configs.mjs) once a minute through Wrangler's local |
| 65 | # API, which answers only here, on localhost. |
| 66 | node "$HERE/scheduler.mjs" schedules.json http://127.0.0.1:8787 & |
| 67 | echo "g1t is starting on ${PUBLIC_URL:-http://localhost:8787}" |
| 68 | exec "$WRANGLER" dev "${args[@]}" \ |
| 69 | --ip 0.0.0.0 --port 8787 \ |
| 70 | --persist-to "$STATE" \ |
| 71 | --show-interactive-dev-session=false |