netgescon-day0/scripts/repair-ui.sh

100 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# NetGescon UI repair script: rebuild CSS/JS, clear caches, and relink storage after reboot/crash.
# Usage: sudo bash scripts/repair-ui.sh
set -euo pipefail
APP_DIR="/var/www/netgescon"
USER_WEB="www-data"
GROUP_WEB="www-data"
BUILD_USER="${SUDO_USER:-$(whoami)}"
log(){ echo -e "[repair-ui] $*"; }
if [[ ! -d "$APP_DIR" ]]; then
echo "App dir not found: $APP_DIR" >&2
exit 1
fi
cd "$APP_DIR"
log "Working dir: $(pwd)"
# 1) Ensure .env exists
if [[ ! -f .env ]]; then
log ".env missing, copying from example"
cp .env.example .env || true
fi
# 2) PHP deps (composer)
if command -v composer >/dev/null 2>&1; then
export COMPOSER_ALLOW_SUPERUSER=1
log "Installing composer deps (no-dev)"
composer install --no-interaction --prefer-dist --optimize-autoloader --no-progress || composer install --no-interaction
else
log "composer not found, skipping"
fi
# 3) Node deps and build (Vite)
if command -v npm >/dev/null 2>&1; then
# Fix potential permission issues from previous root runs
if [[ -d node_modules ]]; then
log "Fixing node_modules ownership for build user ($BUILD_USER)"
chown -R "$BUILD_USER":"$GROUP_WEB" node_modules 2>/dev/null || true
if [[ -d node_modules/.bin ]]; then
chmod -R u+rwX node_modules/.bin 2>/dev/null || true
fi
fi
# Ensure build output dir is writable or clean before build
if [[ -d public/build ]]; then
log "Cleaning public/build to avoid permission issues"
chown -R "$BUILD_USER":"$GROUP_WEB" public/build 2>/dev/null || true
rm -rf public/build 2>/dev/null || true
fi
log "Installing node deps"
(npm ci --no-audit --no-fund || npm install)
log "Building assets"
npm run build || true
else
log "npm not found, skipping build"
fi
# 4) Remove any Vite HMR hotfile to avoid dev mode on remote
if [[ -f public/hot ]]; then
log "Removing public/hot (disable HMR)"
rm -f public/hot
fi
# 5) Storage link
log "Linking storage"
php artisan storage:link >/dev/null 2>&1 || true
# 6) Clear and warm caches
log "Clearing caches"
php artisan optimize:clear || true
php artisan config:cache || true
php artisan route:cache || true
php artisan view:cache || true
# 7) Optional DB migrations (guarded by MIGRATE=1)
if [[ "${MIGRATE:-0}" == "1" ]]; then
log "Running migrations (force)"
php artisan migrate --force || true
fi
# 8) Permissions (best-effort)
log "Adjusting permissions (best-effort)"
chown -R "$USER_WEB":"$GROUP_WEB" storage bootstrap/cache public/build 2>/dev/null || true
# Keep node_modules owned by build user to avoid EACCES on next builds
chown -R "$BUILD_USER":"$GROUP_WEB" node_modules 2>/dev/null || true
chmod -R ug+rwX storage bootstrap/cache 2>/dev/null || true
# 9) Try restarting services if present (optional)
if command -v systemctl >/dev/null 2>&1; then
if systemctl list-unit-files | grep -q php; then
log "Restarting PHP service (if any)"; sudo systemctl restart php*-fpm 2>/dev/null || true; fi
if systemctl list-unit-files | grep -q nginx.service; then
log "Restarting nginx"; sudo systemctl restart nginx 2>/dev/null || true; fi
fi
log "Done. If serving via artisan, you can start: php artisan serve --host=0.0.0.0 --port=8000"