Use local server .env via wrapper startup script
This commit is contained in:
@@ -38,7 +38,7 @@ This creates:
|
|||||||
- `/home/bestmidi/chgrid/server/.venv`
|
- `/home/bestmidi/chgrid/server/.venv`
|
||||||
- `/home/bestmidi/chgrid/server/config.toml` (if missing)
|
- `/home/bestmidi/chgrid/server/config.toml` (if missing)
|
||||||
- `/home/bestmidi/chgrid/server/.env` with `CHGRID_AUTH_SECRET` (if missing)
|
- `/home/bestmidi/chgrid/server/.env` with `CHGRID_AUTH_SECRET` (if missing)
|
||||||
- `/etc/sysconfig/chat-grid` with `CHGRID_AUTH_SECRET` for systemd (created/updated automatically)
|
- `/home/bestmidi/chgrid/server/run_server.sh` (loads `.env` then starts server)
|
||||||
- On first run only, if no admin exists, it prompts to create one immediately.
|
- On first run only, if no admin exists, it prompts to create one immediately.
|
||||||
|
|
||||||
Edit `/home/bestmidi/chgrid/server/config.toml`:
|
Edit `/home/bestmidi/chgrid/server/config.toml`:
|
||||||
@@ -88,8 +88,8 @@ cd /home/bestmidi/chgrid
|
|||||||
```
|
```
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
- `install_service.sh` creates a systemd drop-in at `/etc/systemd/system/chat-grid.service.d/env.conf`
|
- Service startup uses `/home/bestmidi/chgrid/server/run_server.sh`, which sources local
|
||||||
pointing `EnvironmentFile` to `/etc/sysconfig/chat-grid`.
|
`/home/bestmidi/chgrid/server/.env` before launching Python.
|
||||||
|
|
||||||
Logs:
|
Logs:
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ set -euo pipefail
|
|||||||
REPO_ROOT="${1:-/home/bestmidi/chgrid}"
|
REPO_ROOT="${1:-/home/bestmidi/chgrid}"
|
||||||
SERVER_DIR="$REPO_ROOT/server"
|
SERVER_DIR="$REPO_ROOT/server"
|
||||||
PYTHON_SPEC="${PYTHON_SPEC:-3.13}"
|
PYTHON_SPEC="${PYTHON_SPEC:-3.13}"
|
||||||
SYS_ENV_FILE="${CHGRID_SYSTEM_ENV_FILE:-/etc/sysconfig/chat-grid}"
|
|
||||||
|
|
||||||
if ! command -v uv >/dev/null 2>&1; then
|
if ! command -v uv >/dev/null 2>&1; then
|
||||||
echo "error: uv is required but not found in PATH" >&2
|
echo "error: uv is required but not found in PATH" >&2
|
||||||
@@ -62,21 +61,6 @@ PY
|
|||||||
echo "created $SERVER_DIR/.env with CHGRID_AUTH_SECRET"
|
echo "created $SERVER_DIR/.env with CHGRID_AUTH_SECRET"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Ensure a systemd-friendly env file exists for service startup.
|
|
||||||
if [[ -z "${CHGRID_AUTH_SECRET:-}" && -f .env ]]; then
|
|
||||||
set -a
|
|
||||||
# shellcheck disable=SC1091
|
|
||||||
source .env
|
|
||||||
set +a
|
|
||||||
fi
|
|
||||||
if [[ -n "${CHGRID_AUTH_SECRET:-}" ]]; then
|
|
||||||
sudo install -d -m 755 /etc/sysconfig
|
|
||||||
sudo sh -c "printf 'CHGRID_AUTH_SECRET=%s\n' \"\$1\" > \"\$2\"" _ "$CHGRID_AUTH_SECRET" "$SYS_ENV_FILE"
|
|
||||||
sudo chmod 600 "$SYS_ENV_FILE"
|
|
||||||
sudo chown root:root "$SYS_ENV_FILE"
|
|
||||||
echo "ensured system env file for service: $SYS_ENV_FILE"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Load generated/shared auth secret for bootstrap checks.
|
# Load generated/shared auth secret for bootstrap checks.
|
||||||
if [[ -f .env ]]; then
|
if [[ -f .env ]]; then
|
||||||
set -a
|
set -a
|
||||||
@@ -135,5 +119,7 @@ PY
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
chmod +x "$SERVER_DIR/run_server.sh"
|
||||||
|
|
||||||
echo "server install complete"
|
echo "server install complete"
|
||||||
echo "next: edit $SERVER_DIR/config.toml (TLS, bind_ip, port)"
|
echo "next: edit $SERVER_DIR/config.toml (TLS, bind_ip, port)"
|
||||||
|
|||||||
@@ -5,10 +5,7 @@ REPO_ROOT="${1:-/home/bestmidi/chgrid}"
|
|||||||
UNIT_NAME="${2:-chat-grid.service}"
|
UNIT_NAME="${2:-chat-grid.service}"
|
||||||
SRC_UNIT="$REPO_ROOT/deploy/systemd/$UNIT_NAME"
|
SRC_UNIT="$REPO_ROOT/deploy/systemd/$UNIT_NAME"
|
||||||
DST_UNIT="/etc/systemd/system/$UNIT_NAME"
|
DST_UNIT="/etc/systemd/system/$UNIT_NAME"
|
||||||
SERVER_ENV_FILE="$REPO_ROOT/server/.env"
|
DROPIN_FILE="/etc/systemd/system/$UNIT_NAME.d/env.conf"
|
||||||
SYS_ENV_FILE="${CHGRID_SYSTEM_ENV_FILE:-/etc/sysconfig/chat-grid}"
|
|
||||||
DROPIN_DIR="/etc/systemd/system/$UNIT_NAME.d"
|
|
||||||
DROPIN_FILE="$DROPIN_DIR/env.conf"
|
|
||||||
|
|
||||||
if [[ ! -f "$SRC_UNIT" ]]; then
|
if [[ ! -f "$SRC_UNIT" ]]; then
|
||||||
echo "error: unit file not found: $SRC_UNIT" >&2
|
echo "error: unit file not found: $SRC_UNIT" >&2
|
||||||
@@ -16,21 +13,9 @@ if [[ ! -f "$SRC_UNIT" ]]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
sudo cp "$SRC_UNIT" "$DST_UNIT"
|
sudo cp "$SRC_UNIT" "$DST_UNIT"
|
||||||
if [[ -f "$SERVER_ENV_FILE" ]]; then
|
if [[ -f "$DROPIN_FILE" ]]; then
|
||||||
SECRET_LINE="$(grep -m1 '^CHGRID_AUTH_SECRET=' "$SERVER_ENV_FILE" || true)"
|
sudo rm -f "$DROPIN_FILE"
|
||||||
if [[ -n "$SECRET_LINE" ]]; then
|
|
||||||
sudo install -d -m 755 /etc/sysconfig
|
|
||||||
sudo sh -c "printf '%s\n' \"\$1\" > \"\$2\"" _ "$SECRET_LINE" "$SYS_ENV_FILE"
|
|
||||||
sudo chmod 600 "$SYS_ENV_FILE"
|
|
||||||
sudo chown root:root "$SYS_ENV_FILE"
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
sudo install -d -m 755 "$DROPIN_DIR"
|
|
||||||
sudo tee "$DROPIN_FILE" >/dev/null <<EOF
|
|
||||||
[Service]
|
|
||||||
EnvironmentFile=
|
|
||||||
EnvironmentFile=-$SYS_ENV_FILE
|
|
||||||
EOF
|
|
||||||
sudo install -d -m 0755 -o bestmidi -g bestmidi "$REPO_ROOT/server/runtime"
|
sudo install -d -m 0755 -o bestmidi -g bestmidi "$REPO_ROOT/server/runtime"
|
||||||
sudo touch "$REPO_ROOT/server/runtime/server.log"
|
sudo touch "$REPO_ROOT/server/runtime/server.log"
|
||||||
sudo chown bestmidi:bestmidi "$REPO_ROOT/server/runtime/server.log"
|
sudo chown bestmidi:bestmidi "$REPO_ROOT/server/runtime/server.log"
|
||||||
|
|||||||
@@ -8,10 +8,8 @@ User=bestmidi
|
|||||||
Group=bestmidi
|
Group=bestmidi
|
||||||
WorkingDirectory=/home/bestmidi/chgrid/server
|
WorkingDirectory=/home/bestmidi/chgrid/server
|
||||||
Environment=PATH=/home/bestmidi/chgrid/server/.venv/bin:/usr/bin:/bin
|
Environment=PATH=/home/bestmidi/chgrid/server/.venv/bin:/usr/bin:/bin
|
||||||
EnvironmentFile=-/etc/sysconfig/chat-grid
|
|
||||||
EnvironmentFile=-/home/bestmidi/chgrid/server/.env
|
|
||||||
ExecStartPre=/usr/bin/mkdir -p /home/bestmidi/chgrid/server/runtime
|
ExecStartPre=/usr/bin/mkdir -p /home/bestmidi/chgrid/server/runtime
|
||||||
ExecStart=/home/bestmidi/chgrid/server/.venv/bin/python main.py --config /home/bestmidi/chgrid/server/config.toml
|
ExecStart=/home/bestmidi/chgrid/server/run_server.sh
|
||||||
StandardOutput=append:/home/bestmidi/chgrid/server/runtime/server.log
|
StandardOutput=append:/home/bestmidi/chgrid/server/runtime/server.log
|
||||||
StandardError=append:/home/bestmidi/chgrid/server/runtime/server.log
|
StandardError=append:/home/bestmidi/chgrid/server/runtime/server.log
|
||||||
Restart=always
|
Restart=always
|
||||||
|
|||||||
14
server/run_server.sh
Executable file
14
server/run_server.sh
Executable file
@@ -0,0 +1,14 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
cd "$SCRIPT_DIR"
|
||||||
|
|
||||||
|
if [[ -f .env ]]; then
|
||||||
|
set -a
|
||||||
|
# shellcheck disable=SC1091
|
||||||
|
source .env
|
||||||
|
set +a
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec .venv/bin/python main.py --config config.toml
|
||||||
Reference in New Issue
Block a user