#!/bin/bash # Data Visualization Display Wall - Systemd Installer # Requirements: Node.js, NPM, Systemd (Linux) # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}=== Data Visualization Display Wall Installer ===${NC}" # 1. Check permissions if [ "$EUID" -ne 0 ]; then echo -e "${RED}Please run as root (sudo ./Install.sh)${NC}" exit 1 fi # 2. Get current directory and user PROJECT_DIR=$(pwd) REAL_USER=${SUDO_USER:-$USER} USER_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6) echo -e "Project Directory: ${GREEN}$PROJECT_DIR${NC}" echo -e "Running User: ${GREEN}$REAL_USER${NC}" # 3. Check for mandatory files if [ ! -f "server/index.js" ]; then echo -e "${RED}Error: server/index.js not found. Please run this script from the project root.${NC}" exit 1 fi # 4. Check for dependencies echo -e "${BLUE}Checking dependencies...${NC}" check_dep() { if ! command -v "$1" &> /dev/null; then echo -e "${RED}$1 is not installed. Please install $1 first.${NC}" exit 1 fi } check_dep node check_dep npm # 5. Check for .env file if [ ! -f ".env" ]; then echo -e "${YELLOW}Warning: .env file not found.${NC}" if [ -f ".env.example" ]; then echo -e "Creating .env from .env.example..." cp .env.example .env chown "$REAL_USER":"$REAL_USER" .env echo -e "${GREEN}Created .env file. Please ensure values are correct.${NC}" else echo -e "${RED}Error: .env.example not found. Configuration missing.${NC}" fi fi # 6. Install NPM dependencies echo -e "${BLUE}Installing dependencies...${NC}" # Run npm install as the real user to avoid permission issues in node_modules sudo -u "$REAL_USER" npm install if [ $? -ne 0 ]; then echo -e "${RED}NPM install failed.${NC}" exit 1 fi # 7. Create Systemd Service File SERVICE_FILE="/etc/systemd/system/data-wall.service" NODE_PATH=$(command -v node) echo -e "${BLUE}Creating systemd service at $SERVICE_FILE...${NC}" cat < "$SERVICE_FILE" [Unit] Description=Data Visualization Display Wall After=network.target mysql.service redis-server.service valkey-server.service Wants=mysql.service [Service] Type=simple User=$REAL_USER WorkingDirectory=$PROJECT_DIR ExecStart=$NODE_PATH server/index.js Restart=always RestartSec=10 StandardOutput=syslog StandardError=syslog SyslogIdentifier=data-wall # Pass environment via .env file injection EnvironmentFile=-$PROJECT_DIR/.env Environment=NODE_ENV=production # Security Hardening CapabilityBoundingSet= NoNewPrivileges=true LimitNOFILE=65535 [Install] WantedBy=multi-user.target EOF # 8. Reload Systemd and Start echo -e "${BLUE}Reloading systemd and restarting service...${NC}" systemctl daemon-reload systemctl enable data-wall systemctl restart data-wall # 9. Check Status echo -e "${BLUE}Checking service status...${NC}" sleep 2 if systemctl is-active --quiet data-wall; then echo -e "${GREEN}SUCCESS: Service is now running.${NC}" PORT=$(grep "^PORT=" .env | cut -d'=' -f2) PORT=${PORT:-3000} echo -e "Dashboard URL: ${YELLOW}http://localhost:${PORT}${NC}" echo -e "View logs: ${BLUE}journalctl -u data-wall -f${NC}" else echo -e "${RED}FAILED: Service failed to start.${NC}" echo -e "Check logs with: ${BLUE}journalctl -u data-wall -xe${NC}" fi echo -e "${BLUE}================================================${NC}"