mirror of
https://github.com/Yamusa85/check-in_system.git
synced 2026-11-22 15:46:29 +03:00
Update install-checkin.sh
added systemd write for app.py
This commit is contained in:
+63
-25
@@ -13,6 +13,7 @@ set -euo pipefail
|
||||
REPO_URL="${REPO_URL:-https://github.com/Yamusa85/check-in_system.git}"
|
||||
INSTALL_DIR="${INSTALL_DIR:-$HOME/checkin-system}"
|
||||
PYTHON_BIN="${PYTHON_BIN:-python3}"
|
||||
SERVICE_NAME="${SERVICE_NAME:-checkin-system}"
|
||||
FLASK_HOST="${FLASK_HOST:-127.0.0.1}"
|
||||
FLASK_PORT="${FLASK_PORT:-5000}"
|
||||
|
||||
@@ -25,11 +26,14 @@ die() { printf '\033[1;31m[x] %s\033[0m\n' "$*" >&2; exit 1; }
|
||||
command -v sudo >/dev/null 2>&1 || die "sudo is required."
|
||||
command -v apt-get >/dev/null 2>&1 || die "This script targets Debian/Ubuntu (apt)."
|
||||
|
||||
# Capture the invoking user for the systemd unit
|
||||
RUN_USER="$(id -un)"
|
||||
RUN_GROUP="$(id -gn)"
|
||||
|
||||
# ---------- 1. Install system packages ----------
|
||||
log "Updating apt package index..."
|
||||
sudo apt-get update -y
|
||||
|
||||
# Map Python module -> apt package, with pip fallback names
|
||||
declare -A APT_PKGS=(
|
||||
[sqlite3]="sqlite3"
|
||||
[flask]="python3-flask"
|
||||
@@ -40,14 +44,12 @@ declare -A APT_PKGS=(
|
||||
)
|
||||
|
||||
log "Installing system dependencies via apt..."
|
||||
# Required base packages
|
||||
sudo apt-get install -y --no-install-recommends \
|
||||
git curl ca-certificates \
|
||||
python3 python3-venv python3-pip \
|
||||
sqlite3 libsqlite3-dev \
|
||||
build-essential python3-dev
|
||||
|
||||
# Try each Flask/Pandas/OpenPyXL package; fall back to pip later if unavailable
|
||||
PIP_FALLBACK=()
|
||||
for mod in "${!APT_PKGS[@]}"; do
|
||||
pkg="${APT_PKGS[$mod]}"
|
||||
@@ -107,7 +109,6 @@ pip install --upgrade pip wheel >/dev/null
|
||||
|
||||
if (( ${#PIP_FALLBACK[@]} > 0 )); then
|
||||
log "Installing pip fallbacks: ${PIP_FALLBACK[*]}"
|
||||
# Map module names to their pip distribution names
|
||||
declare -A PIP_NAMES=(
|
||||
[flask]="Flask"
|
||||
[flask_login]="Flask-Login"
|
||||
@@ -118,7 +119,6 @@ if (( ${#PIP_FALLBACK[@]} > 0 )); then
|
||||
)
|
||||
for mod in "${PIP_FALLBACK[@]}"; do
|
||||
name="${PIP_NAMES[$mod]:-$mod}"
|
||||
# sqlite3 is stdlib; only fall back if truly missing
|
||||
if [[ "$mod" == "sqlite3" ]]; then
|
||||
python3 -c 'import sqlite3' 2>/dev/null || pip install "$name"
|
||||
else
|
||||
@@ -127,41 +127,79 @@ if (( ${#PIP_FALLBACK[@]} > 0 )); then
|
||||
done
|
||||
fi
|
||||
|
||||
# ---------- 5. Optional: launch helper script ----------
|
||||
RUNNER="$INSTALL_DIR/run.sh"
|
||||
cat > "$RUNNER" <<EOF
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$INSTALL_DIR"
|
||||
source "$VENV_DIR/bin/activate"
|
||||
exec python3 app.py
|
||||
EOF
|
||||
chmod +x "$RUNNER"
|
||||
|
||||
# ---------- 6. Smoke test ----------
|
||||
# ---------- 5. Smoke test ----------
|
||||
log "Performing import smoke test..."
|
||||
"$VENV_DIR/bin/python" - <<'PY'
|
||||
import flask, flask_login, werkzeug, pandas, openpyxl, sqlite3
|
||||
print("imports OK:", flask.__version__, pandas.__version__, openpyxl.__version__)
|
||||
PY
|
||||
|
||||
# ---------- 6. Install systemd service ----------
|
||||
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
|
||||
log "Installing systemd unit: $SERVICE_FILE"
|
||||
|
||||
sudo tee "$SERVICE_FILE" >/dev/null <<EOF
|
||||
[Unit]
|
||||
Description=Check-in System (Flask)
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=${RUN_USER}
|
||||
Group=${RUN_GROUP}
|
||||
WorkingDirectory=${INSTALL_DIR}
|
||||
Environment=PYTHONUNBUFFERED=1
|
||||
Environment=FLASK_HOST=${FLASK_HOST}
|
||||
Environment=FLASK_PORT=${FLASK_PORT}
|
||||
# app.py already initializes schema.sql / checkin.db itself on startup.
|
||||
ExecStart=${VENV_DIR}/bin/python ${INSTALL_DIR}/app.py
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
|
||||
# Basic hardening
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
log "Reloading systemd and enabling service..."
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable "${SERVICE_NAME}.service"
|
||||
sudo systemctl restart "${SERVICE_NAME}.service"
|
||||
|
||||
# Give it a moment, then report status
|
||||
sleep 2
|
||||
if sudo systemctl is-active --quiet "${SERVICE_NAME}.service"; then
|
||||
log "Service is running."
|
||||
else
|
||||
warn "Service failed to start. Inspect logs with:"
|
||||
warn " sudo journalctl -u ${SERVICE_NAME}.service -n 50 --no-pager"
|
||||
fi
|
||||
|
||||
cat <<EOF
|
||||
|
||||
✅ Installation complete.
|
||||
|
||||
App directory : $INSTALL_DIR
|
||||
Virtualenv : $VENV_DIR
|
||||
Run with : $RUNNER
|
||||
Service unit : $SERVICE_FILE
|
||||
Service name : ${SERVICE_NAME}
|
||||
|
||||
To start the app manually:
|
||||
Service management:
|
||||
|
||||
cd "$INSTALL_DIR"
|
||||
source .venv/bin/activate
|
||||
python3 app.py
|
||||
sudo systemctl status ${SERVICE_NAME}
|
||||
sudo systemctl restart ${SERVICE_NAME}
|
||||
sudo systemctl stop ${SERVICE_NAME}
|
||||
sudo journalctl -u ${SERVICE_NAME} -f # live logs
|
||||
|
||||
If app.py uses Flask's dev server, it will be reachable at
|
||||
http://${FLASK_HOST}:${FLASK_PORT}/
|
||||
The service starts automatically on boot (WantedBy=multi-user.target)
|
||||
and restarts on failure (Restart=on-failure).
|
||||
|
||||
Note: checkin.db and schema.sql initialization are handled by app.py
|
||||
Checkin database (checkin.db) and schema.sql are initialized by app.py
|
||||
on first run — this script does not create the database.
|
||||
|
||||
App reachable at: http://${FLASK_HOST}:${FLASK_PORT}/
|
||||
EOF
|
||||
|
||||
Reference in New Issue
Block a user