Files
atlas/installer/fix-templates.sh
Othman H. Suseno f1a344bf6a
Some checks failed
CI / test-build (push) Has been cancelled
update install script
2025-12-16 00:58:02 +07:00

145 lines
4.0 KiB
Bash

#!/bin/bash
#
# Quick fix script to copy web templates and static files to installation directory
# Run this on the remote machine where templates are missing
#
# Usage: sudo ./installer/fix-templates.sh [--install-dir /opt/atlas]
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Default values
INSTALL_DIR="/opt/atlas"
REPO_DIR=""
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--install-dir)
INSTALL_DIR="$2"
shift 2
;;
--repo-dir)
REPO_DIR="$2"
shift 2
;;
-h|--help)
echo "Fix Templates Script"
echo ""
echo "Usage: sudo ./installer/fix-templates.sh [options]"
echo ""
echo "Options:"
echo " --install-dir DIR Installation directory (default: /opt/atlas)"
echo " --repo-dir DIR Repository directory (if not in current dir)"
echo " -h, --help Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
exit 1
fi
# Get script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# If script is in installer/ subdirectory, get parent (repo root)
if [[ "$(basename "$SCRIPT_DIR")" == "installer" ]]; then
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
else
REPO_ROOT="$SCRIPT_DIR"
fi
# Function to check if directory is valid repo root
is_repo_root() {
local dir="$1"
[[ -f "$dir/go.mod" ]] && [[ -d "$dir/internal" ]] && [[ -d "$dir/web/templates" ]]
}
# Find source directory
SOURCE_DIR=""
if [[ -n "$REPO_DIR" ]] && is_repo_root "$REPO_DIR"; then
SOURCE_DIR="$(cd "$REPO_DIR" && pwd)"
elif is_repo_root "$REPO_ROOT"; then
SOURCE_DIR="$REPO_ROOT"
elif is_repo_root "."; then
SOURCE_DIR="$(pwd)"
fi
if [[ -z "$SOURCE_DIR" ]]; then
echo -e "${RED}Error: Cannot find repository root with web/templates directory${NC}"
echo " Current directory: $(pwd)"
echo " Script directory: $SCRIPT_DIR"
echo ""
echo " Please run this script from the atlas repository root"
echo " Or specify path with: --repo-dir /path/to/atlas"
exit 1
fi
echo -e "${GREEN}Copying web files...${NC}"
echo " Source: $SOURCE_DIR/web"
echo " Destination: $INSTALL_DIR/web"
# Create web directories in install directory
mkdir -p "$INSTALL_DIR/web/templates"
mkdir -p "$INSTALL_DIR/web/static"
# Check if templates exist
if [[ ! -d "$SOURCE_DIR/web/templates" ]]; then
echo -e "${RED}Error: web/templates directory not found in $SOURCE_DIR${NC}"
exit 1
fi
# Copy templates
echo " Copying templates..."
if cp -r "$SOURCE_DIR/web/templates"/* "$INSTALL_DIR/web/templates/" 2>/dev/null; then
echo -e "${GREEN} ✓ Templates copied${NC}"
else
echo -e "${RED} ✗ Failed to copy templates${NC}"
exit 1
fi
# Copy static files if they exist
if [[ -d "$SOURCE_DIR/web/static" ]]; then
echo " Copying static files..."
if cp -r "$SOURCE_DIR/web/static"/* "$INSTALL_DIR/web/static/" 2>/dev/null; then
echo -e "${GREEN} ✓ Static files copied${NC}"
else
echo -e "${YELLOW} ⚠ Some static files may not have been copied${NC}"
fi
fi
# Set ownership (use atlas user if it exists, otherwise root)
SERVICE_USER="atlas"
if id "$SERVICE_USER" &>/dev/null; then
chown -R "$SERVICE_USER:$SERVICE_USER" "$INSTALL_DIR/web"
echo " Set ownership to $SERVICE_USER"
else
chown -R root:root "$INSTALL_DIR/web"
echo " Set ownership to root (atlas user not found)"
fi
# Set permissions
chmod -R 755 "$INSTALL_DIR/web"
echo ""
echo -e "${GREEN}Web files copied successfully!${NC}"
echo " Templates: $INSTALL_DIR/web/templates"
echo " Static: $INSTALL_DIR/web/static"
echo ""
echo "You may need to restart the atlas-api service:"
echo " sudo systemctl restart atlas-api"
echo ""