consolidate runtime env into .env, drop ecosystem env blocks (#1229)

- Make .env the single source of truth for runtime config. Remove all
  `env:` blocks from SREBOT/ecosystem.config.js and TSSBOT/ecosystem.config.js
  so values can't silently shadow .env. Both ecosystem files load .env via
  `require('dotenv').config()` and PM2 inherits the resolved environment.

- Rename SREBOT_STORAGE_VOL_PATH → STORAGE_VOL_PATH across all readers
  (BOT/utils.py, BOT/receiver_bridge.py, BOT/render_recap.py, server.js,
  web/server.js, dateindex.js, scripts/*, srebot.service, tests/, README,
  and both .env files). STORAGE is shared between SREBOT and TSSBOT, so the
  variable shouldn't carry one bot's prefix.

- Rename per-process PORT env vars to disambiguated names so .env can be
  the source of truth without collisions:
    PORT (api)     → SREBOT_API_PORT     (server.js)
    PORT (web)     → SREBOT_WEB_PORT     (web/server.js)
    WEBHOOK_PORT   → SREBOT_WEBHOOK_PORT (github_webhook_updater.py)
  SREBOT_EXTERNAL_HOST/PORT/UPSTREAM_URL were already uniquely named;
  they just move from ecosystem env to .env.

- TSSBOT/.env: drop GITHUB_WEBHOOK_SECRET (only srebot-webhook consumes it)
  and the stale SREBOT_DEPLOY_PATH. SREBOT/.env: also drop the obsolete
  SREBOT_DEPLOY_PATH (ecosystem now hardcodes __dirname).

- ecosystem.config.js no longer references SREBOT_DEPLOY_PATH; deploy path
  is always __dirname of the ecosystem file.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
NotSoToothless
2026-05-14 00:07:34 -07:00
committed by GitHub
parent 2f6a9687b1
commit 3fb15d6282
15 changed files with 54 additions and 66 deletions
+2 -2
View File
@@ -32,9 +32,9 @@ def _env(name: str, default: str = "") -> str:
return value.strip() return value.strip()
_storage_root_raw = _env("SREBOT_STORAGE_VOL_PATH") _storage_root_raw = _env("STORAGE_VOL_PATH")
if not _storage_root_raw: if not _storage_root_raw:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
_STORAGE_ROOT = Path(_storage_root_raw) _STORAGE_ROOT = Path(_storage_root_raw)
SREBOT_API_BASE_URL = _env("SREBOT_API_BASE_URL", _env("SREBOT_HTTP_URL", "http://127.0.0.1:6000")).rstrip("/") SREBOT_API_BASE_URL = _env("SREBOT_API_BASE_URL", _env("SREBOT_HTTP_URL", "http://127.0.0.1:6000")).rstrip("/")
SREBOT_API_BEARER_TOKEN = _env("SREBOT_API_BEARER_TOKEN") SREBOT_API_BEARER_TOKEN = _env("SREBOT_API_BEARER_TOKEN")
+2 -2
View File
@@ -231,9 +231,9 @@ def _smooth_rolling_curve(series: list, window: int = 8) -> list:
return out return out
_storage_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() _storage_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not _storage_env: if not _storage_env:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
_STORAGE = Path(_storage_env) _STORAGE = Path(_storage_env)
SQUADRONS_DB = _STORAGE / "squadrons.db" SQUADRONS_DB = _STORAGE / "squadrons.db"
SQ_BATTLES_DB = _STORAGE / "sq_battles.db" SQ_BATTLES_DB = _STORAGE / "sq_battles.db"
+2 -2
View File
@@ -12,9 +12,9 @@ import sys
import tempfile import tempfile
from pathlib import Path from pathlib import Path
_storage_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() _storage_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not _storage_env: if not _storage_env:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
STORAGE = Path(_storage_env) STORAGE = Path(_storage_env)
SEASON_START = "1772348400" SEASON_START = "1772348400"
SEASON_END = "1777852799" SEASON_END = "1777852799"
+2 -2
View File
@@ -46,9 +46,9 @@ load_dotenv()
def require_storage_dir() -> Path: def require_storage_dir() -> Path:
"""Return the configured storage root or fail fast if misconfigured.""" """Return the configured storage root or fail fast if misconfigured."""
raw = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() raw = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not raw: if not raw:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
return Path(raw) return Path(raw)
+17 -10
View File
@@ -3,14 +3,14 @@
1. **Clone the repository** 1. **Clone the repository**
```bash ```bash
git clone https://github.com/Sop-rs/SREBOT_MEOW.git git clone git@github.com:FURRO404/BOTS.git
cd SREBOT_MEOW cd BOTS/SREBOT
``` ```
2. **Set up Python virtual environment** 2. **Set up Python virtual environment**
```bash ```bash
python3 -m venv .venv python3 -m venv .venv
source venv/bin/activate # On Windows: venv\Scripts\activate source .venv/bin/activate
``` ```
3. **Install dependencies** 3. **Install dependencies**
@@ -22,18 +22,25 @@
```bash ```bash
nano .env nano .env
``` ```
Edit the existing `.env` in the repo root and keep the storage path there: `.env` is the single source of truth for runtime config — `ecosystem.config.js`
has no `env:` blocks; it loads `.env` via `require('dotenv').config()` and PM2
inherits the variables when spawning each app. Keys SREBOT reads:
```env ```env
SREBOT_DEPLOY_PATH=/absolute/path/to/SREBOT_MEOW
DISCORD_KEY=your_discord_bot_token_here DISCORD_KEY=your_discord_bot_token_here
DEEPL_KEY=your_deepl_api_key_here # Optional DEEPL_KEY=your_deepl_api_key_here # Optional
GITHUB_WEBHOOK_SECRET=your_webhook_secret # For auto-deployment GITHUB_WEBHOOK_SECRET=your_webhook_secret # For auto-deployment
SREBOT_STORAGE_VOL_PATH=/absolute/path/to/storage STORAGE_VOL_PATH=/absolute/path/to/storage # Shared with TSSBOT
SREBOT_API_BEARER_TOKEN=your_internal_api_token # Optional, protects /api/* SREBOT_API_BEARER_TOKEN=your_internal_api_token # Optional, protects /api/*
SREBOT_EXTERNAL_PORT=18081 # External bridge port SREBOT_API_PORT=6000
SREBOT_WEB_PORT=3001
SREBOT_WEBHOOK_PORT=9000
SREBOT_EXTERNAL_HOST=0.0.0.0
SREBOT_EXTERNAL_PORT=18081
SREBOT_EXTERNAL_BEARER_TOKEN=your_external_bridge_token # Optional, protects the bridge API and websocket SREBOT_EXTERNAL_BEARER_TOKEN=your_external_bridge_token # Optional, protects the bridge API and websocket
SREBOT_EXTERNAL_UPSTREAM_URL=http://127.0.0.1:6000 # Internal SREBOT API to proxy SREBOT_EXTERNAL_UPSTREAM_URL=http://127.0.0.1:6000
NODE_ENV=production
PYTHONUNBUFFERED=1
``` ```
5. **Run the bot** 5. **Run the bot**
@@ -47,7 +54,7 @@
It proxies read-only SREBOT queries and broadcasts replay/GOB envelopes over It proxies read-only SREBOT queries and broadcasts replay/GOB envelopes over
websocket on the same external port. websocket on the same external port.
Its outbox/state files live under the shared storage volume configured in Its outbox/state files live under the shared storage volume configured in
`.env` via `SREBOT_STORAGE_VOL_PATH`. `.env` via `STORAGE_VOL_PATH`.
Useful commands: Useful commands:
+2 -2
View File
@@ -1,9 +1,9 @@
require('dotenv').config(); require('dotenv').config();
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose();
const path = require('path'); const path = require('path');
const STORAGE_ROOT = (process.env.SREBOT_STORAGE_VOL_PATH || '').trim(); const STORAGE_ROOT = (process.env.STORAGE_VOL_PATH || '').trim();
if (!STORAGE_ROOT) { if (!STORAGE_ROOT) {
throw new Error('SREBOT_STORAGE_VOL_PATH must be set'); throw new Error('STORAGE_VOL_PATH must be set');
} }
const DB_PATH = path.join(STORAGE_ROOT, 'sq_battles.db'); const DB_PATH = path.join(STORAGE_ROOT, 'sq_battles.db');
+9 -28
View File
@@ -1,6 +1,9 @@
// Single source of truth for runtime config is SREBOT/.env (loaded here).
// Do NOT add `env:` blocks to apps below — they would override the .env values
// and create two parallel config sources to reason about.
require('dotenv').config(); require('dotenv').config();
const DEPLOY_PATH = process.env.SREBOT_DEPLOY_PATH || __dirname; const DEPLOY_PATH = __dirname;
module.exports = { module.exports = {
apps: [ apps: [
@@ -14,10 +17,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '16000M', max_memory_restart: '16000M',
env: {
NODE_ENV: 'production',
PYTHONUNBUFFERED: '1'
},
log_file: './logs/bot_combined.log', log_file: './logs/bot_combined.log',
out_file: './logs/bot_out.log', out_file: './logs/bot_out.log',
error_file: './logs/bot_error.log', error_file: './logs/bot_error.log',
@@ -27,7 +26,7 @@ module.exports = {
restart_delay: 3000 restart_delay: 3000
}, },
// API Server // API Server (reads SREBOT_API_PORT from .env)
{ {
name: 'srebot-api', name: 'srebot-api',
script: 'server.js', script: 'server.js',
@@ -38,10 +37,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '4G', max_memory_restart: '4G',
env: {
NODE_ENV: 'production',
PORT: 6000
},
log_file: './logs/api_combined.log', log_file: './logs/api_combined.log',
out_file: './logs/api_out.log', out_file: './logs/api_out.log',
error_file: './logs/api_error.log', error_file: './logs/api_error.log',
@@ -54,6 +49,7 @@ module.exports = {
// External bridge for AXBot traffic: // External bridge for AXBot traffic:
// - Proxies read-only API queries to the internal SREBOT API // - Proxies read-only API queries to the internal SREBOT API
// - Streams bridge envelopes to AXBot over websocket // - Streams bridge envelopes to AXBot over websocket
// Reads SREBOT_EXTERNAL_HOST/PORT/UPSTREAM_URL from .env.
{ {
name: 'srebot-axbot', name: 'srebot-axbot',
script: 'BOT/srebot_external.py', script: 'BOT/srebot_external.py',
@@ -63,13 +59,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '1G', max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PYTHONUNBUFFERED: '1',
SREBOT_EXTERNAL_HOST: '0.0.0.0',
SREBOT_EXTERNAL_PORT: 18081,
SREBOT_EXTERNAL_UPSTREAM_URL: 'http://127.0.0.1:6000'
},
log_file: './logs/axbot_combined.log', log_file: './logs/axbot_combined.log',
out_file: './logs/axbot_out.log', out_file: './logs/axbot_out.log',
error_file: './logs/axbot_error.log', error_file: './logs/axbot_error.log',
@@ -79,7 +68,8 @@ module.exports = {
restart_delay: 2000 restart_delay: 2000
}, },
// GitHub Webhook Receiver (auto-deploy on push to main) // GitHub Webhook Receiver (auto-deploy on push to main).
// Reads SREBOT_WEBHOOK_PORT from .env.
{ {
name: 'srebot-webhook', name: 'srebot-webhook',
script: 'github_webhook_updater.py', script: 'github_webhook_updater.py',
@@ -89,11 +79,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '500M', max_memory_restart: '500M',
env: {
NODE_ENV: 'production',
PYTHONUNBUFFERED: '1',
WEBHOOK_PORT: 9000
},
log_file: './logs/webhook_combined.log', log_file: './logs/webhook_combined.log',
out_file: './logs/webhook_out.log', out_file: './logs/webhook_out.log',
error_file: './logs/webhook_error.log', error_file: './logs/webhook_error.log',
@@ -103,7 +88,7 @@ module.exports = {
restart_delay: 2000 restart_delay: 2000
}, },
// Website (Next.js) // Website (reads SREBOT_WEB_PORT from .env)
{ {
name: 'srebot-web', name: 'srebot-web',
script: 'server.js', script: 'server.js',
@@ -113,10 +98,6 @@ module.exports = {
autorestart: true, autorestart: true,
watch: false, watch: false,
max_memory_restart: '500M', max_memory_restart: '500M',
env: {
NODE_ENV: 'production',
PORT: 3001
},
log_file: './logs/web_combined.log', log_file: './logs/web_combined.log',
out_file: './logs/web_out.log', out_file: './logs/web_out.log',
error_file: './logs/web_error.log', error_file: './logs/web_error.log',
+1 -1
View File
@@ -270,7 +270,7 @@ def health():
if __name__ == '__main__': if __name__ == '__main__':
port = int(os.environ.get('WEBHOOK_PORT', 9000)) port = int(os.environ.get('SREBOT_WEBHOOK_PORT', 9000))
logger.info(f"Starting webhook server on port {port}") logger.info(f"Starting webhook server on port {port}")
logger.info(f"Repo path: {REPO_PATH}") logger.info(f"Repo path: {REPO_PATH}")
app.run(host='0.0.0.0', port=port) app.run(host='0.0.0.0', port=port)
+2 -2
View File
@@ -26,9 +26,9 @@ load_dotenv()
APPLICATION_ID = "1254679514466877540" APPLICATION_ID = "1254679514466877540"
TARGET_GUILD = "1379510072815779961" TARGET_GUILD = "1379510072815779961"
TOKEN = os.environ.get("DISCORD_KEY", "") TOKEN = os.environ.get("DISCORD_KEY", "")
_storage_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() _storage_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not _storage_env: if not _storage_env:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
STORAGE = Path(_storage_env) STORAGE = Path(_storage_env)
DB_PATH = STORAGE / "entitlements.db" DB_PATH = STORAGE / "entitlements.db"
+3 -3
View File
@@ -33,7 +33,7 @@ def _infer_storage_root(auth_file: Path) -> Path:
def _prepare_env(storage_root: Path) -> None: def _prepare_env(storage_root: Path) -> None:
os.environ["SREBOT_STORAGE_VOL_PATH"] = str(storage_root) os.environ["STORAGE_VOL_PATH"] = str(storage_root)
def _score_hit(clan: dict[str, Any], needle: str) -> bool: def _score_hit(clan: dict[str, Any], needle: str) -> bool:
@@ -72,10 +72,10 @@ async def _main() -> int:
auth_file = Path(args.auth_file).expanduser().resolve() auth_file = Path(args.auth_file).expanduser().resolve()
storage_root = _infer_storage_root(auth_file) storage_root = _infer_storage_root(auth_file)
else: else:
storage_root_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() storage_root_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not storage_root_env: if not storage_root_env:
print( print(
"SREBOT_STORAGE_VOL_PATH is not set. Export it or pass --auth-file.", "STORAGE_VOL_PATH is not set. Export it or pass --auth-file.",
file=sys.stderr, file=sys.stderr,
) )
return 2 return 2
+3 -3
View File
@@ -46,7 +46,7 @@ logging.basicConfig(
log = logging.getLogger("migrate_clan_id") log = logging.getLogger("migrate_clan_id")
# Auto-load the repo's .env so SREBOT_STORAGE_VOL_PATH and friends resolve when # Auto-load the repo's .env so STORAGE_VOL_PATH and friends resolve when
# the script is run directly (PM2/services already get them from .env). # the script is run directly (PM2/services already get them from .env).
try: try:
from dotenv import load_dotenv from dotenv import load_dotenv
@@ -59,9 +59,9 @@ try:
except ImportError: except ImportError:
pass pass
_storage_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() _storage_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not _storage_env: if not _storage_env:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
STORAGE_DIR = Path(_storage_env) STORAGE_DIR = Path(_storage_env)
SQ_BATTLES_DB = STORAGE_DIR / "sq_battles.db" SQ_BATTLES_DB = STORAGE_DIR / "sq_battles.db"
+2 -2
View File
@@ -17,9 +17,9 @@ from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1] REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_SOURCE = REPO_ROOT / "replays" DEFAULT_SOURCE = REPO_ROOT / "replays"
_storage_env = os.environ.get("SREBOT_STORAGE_VOL_PATH", "").strip() _storage_env = os.environ.get("STORAGE_VOL_PATH", "").strip()
if not _storage_env: if not _storage_env:
raise RuntimeError("SREBOT_STORAGE_VOL_PATH must be set") raise RuntimeError("STORAGE_VOL_PATH must be set")
DEFAULT_STORAGE = Path(_storage_env) DEFAULT_STORAGE = Path(_storage_env)
LEGACY_REPLAY_DIR = re.compile(r"^0([0-9a-fA-F]+)$") LEGACY_REPLAY_DIR = re.compile(r"^0([0-9a-fA-F]+)$")
+3 -3
View File
@@ -14,9 +14,9 @@ function parseJsonColumn(data) {
return JSON.parse(data); return JSON.parse(data);
} }
const STORAGE_ROOT = (process.env.SREBOT_STORAGE_VOL_PATH || '').trim(); const STORAGE_ROOT = (process.env.STORAGE_VOL_PATH || '').trim();
if (!STORAGE_ROOT) { if (!STORAGE_ROOT) {
throw new Error('SREBOT_STORAGE_VOL_PATH must be set'); throw new Error('STORAGE_VOL_PATH must be set');
} }
const REPLAYS_PATH = path.join(STORAGE_ROOT, 'REPLAYS'); const REPLAYS_PATH = path.join(STORAGE_ROOT, 'REPLAYS');
const COMPS_PATH = path.join(STORAGE_ROOT, 'COMPS'); const COMPS_PATH = path.join(STORAGE_ROOT, 'COMPS');
@@ -29,7 +29,7 @@ function replayDataPath(sessionId) {
} }
const app = express(); const app = express();
const PORT = process.env.PORT || 6000; const PORT = process.env.SREBOT_API_PORT || 6000;
const API_BEARER_TOKEN = process.env.SREBOT_API_BEARER_TOKEN || ''; const API_BEARER_TOKEN = process.env.SREBOT_API_BEARER_TOKEN || '';
const ADMIN_BEARER_TOKEN = process.env.SREBOT_ADMIN_BEARER_TOKEN || null; const ADMIN_BEARER_TOKEN = process.env.SREBOT_ADMIN_BEARER_TOKEN || null;
const wlDb = new sqlite3.Database(WL_DB_PATH); const wlDb = new sqlite3.Database(WL_DB_PATH);
+1 -1
View File
@@ -17,7 +17,7 @@ SyslogIdentifier=srebot
# Environment # Environment
Environment=PYTHONUNBUFFERED=1 Environment=PYTHONUNBUFFERED=1
Environment=SREBOT_STORAGE_VOL_PATH=/mnt/HC_Volume_105581488/STORAGE Environment=STORAGE_VOL_PATH=/mnt/HC_Volume_105581488/STORAGE
# Security # Security
NoNewPrivileges=true NoNewPrivileges=true
+3 -3
View File
@@ -89,9 +89,9 @@ function getRosterMemberByUid(uid) {
} }
const sqlite3 = require('sqlite3').verbose(); const sqlite3 = require('sqlite3').verbose();
const STORAGE_ROOT = (process.env.SREBOT_STORAGE_VOL_PATH || '').trim(); const STORAGE_ROOT = (process.env.STORAGE_VOL_PATH || '').trim();
if (!STORAGE_ROOT) { if (!STORAGE_ROOT) {
throw new Error('SREBOT_STORAGE_VOL_PATH must be set'); throw new Error('STORAGE_VOL_PATH must be set');
} }
const REPLAYS_ROOT = path.join(STORAGE_ROOT, 'REPLAYS'); const REPLAYS_ROOT = path.join(STORAGE_ROOT, 'REPLAYS');
fs.mkdirSync(REPLAYS_ROOT, { recursive: true }); fs.mkdirSync(REPLAYS_ROOT, { recursive: true });
@@ -175,7 +175,7 @@ function resolveReplaySessionDir(sessionId) {
} }
const app = express(); const app = express();
const PORT = process.env.PORT || 3001; const PORT = process.env.SREBOT_WEB_PORT || 3001;
const IS_PRIMARY_WORKER = !process.env.NODE_APP_INSTANCE || process.env.NODE_APP_INSTANCE === '0'; const IS_PRIMARY_WORKER = !process.env.NODE_APP_INSTANCE || process.env.NODE_APP_INSTANCE === '0';
// Enable compression for all responses // Enable compression for all responses