Spaces:
Sleeping
Sleeping
File size: 3,590 Bytes
50a8916 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
#!/usr/bin/env bash
# scripts/bootstrap.sh
set -euo pipefail
# ======== CONFIG ========
APP_USER="${APP_USER:-student-admin}"
APP_HOME="/home/${APP_USER}"
APP_DIR="${APP_DIR:-$APP_HOME/Case_study1}"
ENV_DIR="${ENV_DIR:-$APP_HOME/conda/envs/app311}"
GITHUB_REPO="${GITHUB_REPO:-https://github.com/Juju519/Case_study1.git}"
BRANCH="${BRANCH:-main}"
ENV_FILE="/etc/yourapp.env"
UNIT="/etc/systemd/system/gompei.service"
HEALTH_SVC="/etc/systemd/system/gompei-health.service"
HEALTH_TMR="/etc/systemd/system/gompei-health.timer"
PY="$ENV_DIR/bin/python"
PIP="$ENV_DIR/bin/pip"
# ========================
echo "[bootstrap] starting"
# 0) user
if ! id -u "$APP_USER" >/dev/null 2>&1; then
useradd -m "$APP_USER"
fi
# 1) Miniconda
if ! [ -x /opt/miniconda/bin/conda ]; then
echo "[bootstrap] installing Miniconda"
curl -fsSL https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o /tmp/m.sh
bash /tmp/m.sh -b -p /opt/miniconda
fi
# shellcheck disable=SC1091
source /opt/miniconda/etc/profile.d/conda.sh
# 2) Repo
if [ ! -d "$APP_DIR/.git" ]; then
echo "[bootstrap] cloning $GITHUB_REPO"
sudo -u "$APP_USER" git clone -b "$BRANCH" "$GITHUB_REPO" "$APP_DIR"
else
echo "[bootstrap] updating repo"
pushd "$APP_DIR" >/dev/null
sudo -u "$APP_USER" git fetch --all --prune
sudo -u "$APP_USER" git reset --hard "origin/$BRANCH"
popd >/dev/null
fi
chown -R "$APP_USER:$APP_USER" "$APP_DIR"
# 3) Conda env
if ! [ -x "$PY" ]; then
echo "[bootstrap] creating conda env at $ENV_DIR"
conda create -y -p "$ENV_DIR" python=3.11
fi
# 4) Deps
echo "[bootstrap] installing deps"
"$PIP" install -U pip
[ -f "$APP_DIR/requirements.txt" ] && "$PIP" install -r "$APP_DIR/requirements.txt" || true
"$PIP" install "gradio==4.44.1" "huggingface_hub>=0.25" requests
# 5) Runtime env (secrets)
if [ ! -f "$ENV_FILE" ]; then
echo "[bootstrap] writing $ENV_FILE (fill in secrets!)"
cat >"$ENV_FILE" <<'EOF'
# === runtime config (edit values) ===
API_PROVIDER=nebius
NEBIUS_API_KEY=sk_fill_me
NEBIUS_MODEL=meta-llama/Meta-Llama-3.1-8B-Instruct-fast
NEBIUS_BASE_URL=https://api.studio.nebius.ai/v1
# (Optional HF route)
# API_PROVIDER=hf
# HF_TOKEN=hf_fill_me
# HF_MODEL_ID=sshleifer/tiny-gpt2
GRADIO_SERVER_NAME=0.0.0.0
GRADIO_SERVER_PORT=7860
ENABLE_OAUTH=0
SKIP_UI_ON_IMPORT=0
EOF
fi
chmod 600 "$ENV_FILE"
chown root:root "$ENV_FILE"
# 6) systemd unit
echo "[bootstrap] installing systemd unit"
cat >"$UNIT" <<EOF
[Unit]
Description=Chat with Gompei (Gradio) service
Wants=network-online.target
After=network-online.target
[Service]
User=$APP_USER
WorkingDirectory=$APP_DIR
EnvironmentFile=$ENV_FILE
ExecStart=$PY $APP_DIR/app.py
Restart=always
RestartSec=5
NoNewPrivileges=true
ProtectSystem=full
ProtectHome=false
PrivateTmp=true
KillSignal=SIGTERM
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target
EOF
# 7) health timer (self-restart if UI goes down)
cat >"$HEALTH_SVC" <<'EOF'
[Unit]
Description=Gompei healthcheck
[Service]
Type=oneshot
EnvironmentFile=/etc/yourapp.env
ExecStart=/bin/bash -lc 'curl -fsS --max-time 5 "http://127.0.0.1:${GRADIO_SERVER_PORT:-7860}/" >/dev/null || systemctl restart gompei.service'
EOF
cat >"$HEALTH_TMR" <<'EOF'
[Unit]
Description=Run Gompei healthcheck every minute
[Timer]
OnBootSec=2min
OnUnitActiveSec=1min
AccuracySec=10s
Unit=gompei-health.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable gompei.service gompei-health.timer
systemctl restart gompei.service
systemctl start gompei-health.timer
echo "[bootstrap] done"
systemctl --no-pager --full status gompei.service || true
|