AkurAI Build
Menu

Public repository / e20fdc20ba73ed38f462643569dcb30aa8cb2187

AkurAI-Drive / backend/deploy.sh

Latest change 6e3a0824f4ec4d2bea2524a20b4960c7ab9e2766 - backend: akurai-drive-api (OIDC RP + per-tenant S3 provisioning) by olibuijr

#!/usr/bin/env bash
set -euo pipefail

# Deploy akurai-drive-api to the AkurAI box (mail.olibuijr.com).
# Mirrors ~/Projects/akurai-idp/deploy.sh. The orchestrator runs this — not the
# build agent. Env vars live in /etc/akurai-drive-api.env (NOT managed here).

SSH_HOST="${AKURAI_MAIL_SSH:-akurai-mail}"
DEPLOY_DIR="/opt/akurai-drive-api"
SERVICE="akurai-drive-api"
TARGET="x86_64-unknown-linux-musl"
PORT="${DRIVE_PORT:-8096}"

log()  { echo "[deploy] $*"; }
step() { echo; echo "── $* ──"; }

step "1/4  Build (musl static)"
CC_x86_64_unknown_linux_musl=musl-gcc cargo build --release --target "$TARGET" 2>&1
BINARY="target/$TARGET/release/akurai-drive-api"
log "Binary: $(du -sh "$BINARY" | cut -f1)"

step "2/4  Upload"
ssh "$SSH_HOST" "sudo mkdir -p $DEPLOY_DIR"
scp -q "$BINARY" "$SSH_HOST:/tmp/akurai-drive-api-bin"
ssh "$SSH_HOST" "sudo install -m 755 /tmp/akurai-drive-api-bin $DEPLOY_DIR/akurai-drive-api && rm /tmp/akurai-drive-api-bin"

step "3/4  Install service"
ssh "$SSH_HOST" "sudo bash -s" <<'INSTALL'
set -euo pipefail

# Ensure the service user exists.
id -u drive >/dev/null 2>&1 || useradd --system --no-create-home --shell /usr/sbin/nologin drive

cat > /etc/systemd/system/akurai-drive-api.service <<EOF
[Unit]
Description=AkurAI Drive API (Rust OIDC RP + S3 provisioning)
After=network.target

[Service]
Type=simple
ExecStart=/opt/akurai-drive-api/akurai-drive-api
WorkingDirectory=/opt/akurai-drive-api
EnvironmentFile=/etc/akurai-drive-api.env
Restart=always
RestartSec=3
User=drive
Group=drive

[Install]
WantedBy=multi-user.target
EOF

# Data dir for the SQLite DB (path comes from DRIVE_DB_PATH in the env file).
mkdir -p /data/akurai-drive
chown drive:drive /data/akurai-drive

systemctl daemon-reload
systemctl enable akurai-drive-api >/dev/null 2>&1 || true
systemctl restart akurai-drive-api
echo "  [done] service restarted"
INSTALL

step "4/4  Healthcheck"
sleep 2
if ssh "$SSH_HOST" "curl -fsS http://127.0.0.1:$PORT/healthz" | grep -q '"ok":true'; then
  log "Healthcheck passed"
else
  echo "ERROR: healthcheck failed"
  echo "Check: ssh $SSH_HOST 'sudo journalctl -u $SERVICE -n 50'"
  exit 1
fi

RSS=$(ssh "$SSH_HOST" "ps -C akurai-drive-api -o rss= 2>/dev/null | head -1 | tr -d ' '" 2>/dev/null || echo "?")
echo
echo "════════════════════════════════════════════════════"
echo "  AkurAI Drive API deployed (Rust, musl)"
echo "════════════════════════════════════════════════════"
echo "  Binary  : $(du -sh "$BINARY" | cut -f1)"
echo "  RSS     : ${RSS} KB"
echo "  Host    : $SSH_HOST"
echo "  Port    : 127.0.0.1:$PORT"
echo "  Public  : https://akurai-drive.olibuijr.com"
echo "════════════════════════════════════════════════════"