mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-25 18:22:43 +00:00
67f7d09fe5
## What A trusted, server-side HTTP API so a bot authenticated with a shared secret can **create private games, change their settings, start them, kick players, and pause/resume** — without opening a WebSocket or joining as a player. Two endpoints under `/api/adminbot/`, reaching the owning worker via the existing `/wN/` nginx routing. They reuse the existing Zod schemas and `GameServer` methods, mirroring the WebSocket intent flow rather than inventing a new wire protocol. | Endpoint | Purpose | | --- | --- | | `POST /api/adminbot/create_game` | Create a private game; the worker mints a self-owned id and returns it (body: `GameConfigSchema.partial()`) | | `POST /api/adminbot/game/:id/intent` | Send a lobby-management intent (body: base `IntentSchema`) | ## How it works - **Auth:** `ADMIN_BOT_API_KEY` env var via the `x-admin-bot-key` header (timing-safe compare). The whole API is **disabled — 404 — when the var is unset**, so non-configured environments expose nothing. It's distinct from the per-instance `ADMIN_TOKEN`, which an external bot can't know. - **`GameServer.handleIntent`** is the unified intent dispatch for both the WebSocket `case "intent"` path and the admin-bot HTTP API. An `IntentActor` carries identity + authority (per-connection lobby-creator/role checks for the WS path; admin authority for the bot). It honors `update_game_config`, `toggle_game_start_timer`, `kick_player`, and `toggle_pause` — **on private games only** (`isPublic()` → 403). Gameplay intents and `mark_disconnected` are rejected (400). - **Private games only.** `create_game` rejects any `gameType` other than `Private` (Public *and* Singleplayer → 400); an omitted `gameType` defaults to `Private`. - **The bot is never a player.** It sends no `clientID`; the server stamps a placeholder `ADMIN_BOT_CLIENT_ID = "ADMINBOT"` (collision-proof — contains `I`/`O`, which `generateID()` never emits). A gameplay intent stamped with it would resolve to no player, so puppeteering is structurally impossible on top of the explicit 400. - **Determinism unchanged:** the only intent that reaches the sim is `toggle_pause`, via the same `addIntent` → turn queue → `ServerTurnMessage` path the WS uses. ## Notable details for review - **`hostCheats` is assigned unconditionally — on purpose.** `updateGameConfig` sets `this.gameConfig.hostCheats = gameConfig.hostCheats` unconditionally, unlike its sibling fields (which are guarded on `!== undefined`). The WS host clears cheats by re-sending the *full* config with `hostCheats: undefined`, so here `undefined` must mean "clear", not "leave unchanged". **Caveat for the admin bot**, which is a *partial*-update client: a partial `update_game_config` that omits `hostCheats` will clear it — the bot should send `hostCheats` explicitly (or a full config) when it wants to keep a previously-set value. - **Deploy wiring:** `ADMIN_BOT_API_KEY` is piped through the deploy steps' `env:` in `deploy.yml`/`release.yml` → `deploy.sh` heredoc → container via `update.sh`'s `--env-file`. The remaining manual step is creating the GitHub secret itself. ## Tests 19 new tests: - `GameServer.handleIntent` admin-bot behavior (per-intent, private-only, post-start guards, placeholder clientID, rejected gameplay/`mark_disconnected` intents). - `create_game` gameType guard (Public and Singleplayer both rejected). - `requireAdminBotKey` middleware (404 disabled / 401 missing / 401 wrong / pass). tsc + eslint clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
203 lines
8.1 KiB
YAML
203 lines
8.1 KiB
YAML
name: 🚀 Deploy
|
|
|
|
on:
|
|
# Allow contributors to schedule manual deployments.
|
|
# Permission to deploy can be restricted by requiring approval in environment configuration.
|
|
workflow_dispatch:
|
|
inputs:
|
|
target_domain:
|
|
description: "Deployment Domain"
|
|
required: true
|
|
default: "openfront.dev"
|
|
type: choice
|
|
options:
|
|
- openfront.io
|
|
- openfront.dev
|
|
target_host:
|
|
description: "Deployment Host"
|
|
required: true
|
|
default: "staging"
|
|
type: choice
|
|
options:
|
|
- masters
|
|
- staging
|
|
- falk2
|
|
target_subdomain:
|
|
description: "Deployment Subdomain"
|
|
required: false
|
|
default: ""
|
|
type: string
|
|
|
|
# Automatic deployment on push
|
|
# See https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore
|
|
push:
|
|
branches:
|
|
- "*"
|
|
|
|
# Nightly build: rebuild and deploy main to nightly.openfront.dev
|
|
# 07:00 UTC = 11:00 PM PST (midnight PDT during summer)
|
|
schedule:
|
|
- cron: "0 7 * * *"
|
|
|
|
permissions: {}
|
|
|
|
concurrency:
|
|
group: ${{ github.event_name == 'workflow_dispatch' && inputs.target_host || 'staging' }}
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
deploy:
|
|
# Deploy on push/schedule/workflow_dispatch (see "on:") unless this is a fork
|
|
if: ${{ github.repository == 'openfrontio/OpenFrontIO' }}
|
|
# Use different logic based on event type
|
|
name: Deploy to ${{ inputs.target_domain || 'openfront.dev' }}
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 30
|
|
environment: ${{ inputs.target_domain == 'openfront.io' && 'prod' || '' }}
|
|
env:
|
|
DOMAIN: ${{ inputs.target_domain || 'openfront.dev' }}
|
|
SUBDOMAIN: ${{ github.event_name == 'schedule' && 'nightly' || github.event_name == 'push' && github.ref_name || inputs.target_subdomain || 'main' }}
|
|
steps:
|
|
- uses: actions/checkout@v6
|
|
- name: 📝 Update job summary
|
|
env:
|
|
FQDN: ${{ env.SUBDOMAIN && format('{0}.{1}', env.SUBDOMAIN, env.DOMAIN) || env.DOMAIN || 'openfront.dev' }}
|
|
run: |
|
|
echo "FQDN=$FQDN" >> $GITHUB_ENV
|
|
cat <<EOF >> $GITHUB_STEP_SUMMARY
|
|
### In progress :ship:
|
|
|
|
Deploying from $GITHUB_REF to $FQDN
|
|
EOF
|
|
- uses: actions/create-github-app-token@v3
|
|
id: generate-token
|
|
if: ${{ github.repository == 'openfrontio/OpenFrontIO' }}
|
|
with:
|
|
app-id: ${{ vars.APP_ID }}
|
|
private-key: ${{ secrets.APP_PRIVATE_KEY }}
|
|
- name: Export the token
|
|
if: ${{ github.repository == 'openfrontio/OpenFrontIO' }}
|
|
env:
|
|
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
|
|
run: |
|
|
echo "GH_TOKEN=$GH_TOKEN" >> $GITHUB_ENV
|
|
gh api octocat
|
|
- name: 📝 Create deployment
|
|
if: ${{ github.repository == 'openfrontio/OpenFrontIO' && steps.generate-token.outputs.token != '' }}
|
|
uses: actions/github-script@v9
|
|
id: deployment
|
|
env:
|
|
ENVIRONMENT: ${{ inputs.target_domain == 'openfront.io' && 'prod' || 'staging' }}
|
|
FQDN: ${{ env.FQDN }}
|
|
with:
|
|
github-token: ${{ steps.generate-token.outputs.token }}
|
|
script: |
|
|
const response = await github.rest.repos.createDeployment({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
ref: process.env.GITHUB_SHA,
|
|
environment: process.env.ENVIRONMENT,
|
|
description: 'Deployment to ' + process.env.FQDN,
|
|
auto_merge: false,
|
|
required_contexts: [],
|
|
transient_environment: process.env.ENVIRONMENT === 'staging' && context.ref !== 'refs/heads/main',
|
|
production_environment: process.env.ENVIRONMENT === 'prod'
|
|
});
|
|
const deployment = response.data;
|
|
if (!deployment || !deployment.id) {
|
|
core.setFailed('Failed to create deployment');
|
|
return;
|
|
}
|
|
core.setOutput('deployment_id', deployment.id);
|
|
- name: 🔗 Log in to GHCR
|
|
uses: docker/login-action@v4
|
|
with:
|
|
registry: ghcr.io
|
|
username: ${{ vars.GHCR_USERNAME }}
|
|
password: ${{ secrets.GHCR_TOKEN }}
|
|
- name: 🔑 Create SSH private key
|
|
env:
|
|
SERVER_HOST_MASTERS: ${{ secrets.SERVER_HOST_MASTERS }}
|
|
SERVER_HOST_FALK2: ${{ secrets.SERVER_HOST_FALK2 }}
|
|
SERVER_HOST_STAGING: ${{ secrets.SERVER_HOST_STAGING }}
|
|
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
run: |
|
|
set -euxo pipefail
|
|
mkdir -p ~/.ssh
|
|
echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa
|
|
test -n "$SERVER_HOST_MASTERS" && ssh-keyscan -H "$SERVER_HOST_MASTERS" >> ~/.ssh/known_hosts
|
|
test -n "$SERVER_HOST_FALK2" && ssh-keyscan -H "$SERVER_HOST_FALK2" >> ~/.ssh/known_hosts
|
|
test -n "$SERVER_HOST_STAGING" && ssh-keyscan -H "$SERVER_HOST_STAGING" >> ~/.ssh/known_hosts
|
|
chmod 600 ~/.ssh/id_rsa
|
|
- name: 🚢 Deploy
|
|
env:
|
|
GHCR_REPO: ${{ vars.GHCR_REPO }}
|
|
GHCR_USERNAME: ${{ vars.GHCR_USERNAME }}
|
|
ENV: ${{ inputs.target_domain == 'openfront.io' && 'prod' || 'staging' }}
|
|
HOST: ${{ github.event_name == 'workflow_dispatch' && inputs.target_host || 'staging' }} # schedule and push both use staging
|
|
CDN_BASE: ${{ vars.CDN_BASE }}
|
|
OTEL_EXPORTER_OTLP_ENDPOINT: ${{ secrets.OTEL_EXPORTER_OTLP_ENDPOINT }}
|
|
OTEL_AUTH_HEADER: ${{ secrets.OTEL_AUTH_HEADER }}
|
|
API_KEY: ${{ secrets.API_KEY }}
|
|
ADMIN_BOT_API_KEY: ${{ secrets.ADMIN_BOT_API_KEY }}
|
|
NUM_WORKERS: ${{ vars.NUM_WORKERS }}
|
|
TURNSTILE_SITE_KEY: ${{ vars.TURNSTILE_SITE_KEY }}
|
|
SERVER_HOST_MASTERS: ${{ secrets.SERVER_HOST_MASTERS }}
|
|
SERVER_HOST_FALK2: ${{ secrets.SERVER_HOST_FALK2 }}
|
|
SERVER_HOST_STAGING: ${{ secrets.SERVER_HOST_STAGING }}
|
|
SSH_KEY: ~/.ssh/id_rsa
|
|
VERSION_TAG: latest
|
|
run: |
|
|
echo "::group::deploy.sh"
|
|
./build-deploy.sh "$ENV" "$HOST" "$SUBDOMAIN"
|
|
echo "Deployment created in ${SECONDS} seconds" >> $GITHUB_STEP_SUMMARY
|
|
echo "::endgroup::"
|
|
- name: ⏳ Wait for deployment to start
|
|
env:
|
|
API_KEY: ${{ secrets.API_KEY }}
|
|
run: |
|
|
echo "::group::Wait for deployment to start"
|
|
set -euxo pipefail
|
|
while [ "$(curl -s -H "X-API-Key: ${API_KEY}" https://${FQDN}/commit.txt)" != "${GITHUB_SHA}" ]; do
|
|
if [ "$SECONDS" -ge 300 ]; then
|
|
echo "Timeout: deployment did not start within 5 minutes"
|
|
exit 1
|
|
fi
|
|
sleep 10
|
|
done
|
|
echo "Deployment started in ${SECONDS} seconds" >> $GITHUB_STEP_SUMMARY
|
|
echo "::endgroup::"
|
|
- name: 🔄 Update deployment status
|
|
if: ${{ always() && github.repository == 'openfrontio/OpenFrontIO' && steps.generate-token.outputs.token != '' && steps.deployment.outcome == 'success' && steps.deployment.outputs.deployment_id != '' }}
|
|
uses: actions/github-script@v9
|
|
env:
|
|
FQDN: ${{ env.FQDN }}
|
|
DEPLOYMENT_ID: ${{ steps.deployment.outputs.deployment_id }}
|
|
STATUS: ${{ job.status }}
|
|
with:
|
|
github-token: ${{ steps.generate-token.outputs.token }}
|
|
script: |
|
|
await github.rest.repos.createDeploymentStatus({
|
|
owner: context.repo.owner,
|
|
repo: context.repo.repo,
|
|
deployment_id: process.env.DEPLOYMENT_ID,
|
|
state: process.env.STATUS === 'success' ? 'success' : 'failure',
|
|
environment_url: 'https://' + process.env.FQDN
|
|
});
|
|
- name: ✅ Update job summary
|
|
if: success()
|
|
run: |
|
|
cat <<EOF >> $GITHUB_STEP_SUMMARY
|
|
### Success! :rocket:
|
|
|
|
Deployed from $GITHUB_REF to $FQDN
|
|
EOF
|
|
- name: ❌ Update job summary
|
|
if: failure()
|
|
run: |
|
|
cat <<EOF >> $GITHUB_STEP_SUMMARY
|
|
### Failure! :fire:
|
|
|
|
Unable to deploy from $GITHUB_REF to $FQDN
|
|
EOF
|