141 lines
3.5 KiB
Bash
Executable File
141 lines
3.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Backstor UI Installer Script
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${GREEN}Starting Backstor UI Installation...${NC}"
|
|
|
|
# Check for root/sudo
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo -e "${RED}Please run as root (use sudo)${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
PROJECT_DIR=$(pwd)
|
|
SERVER_DIR="$PROJECT_DIR/server"
|
|
SERVICE_NAME="backstor"
|
|
|
|
# 1. Install Node.js Dependencies
|
|
echo "Installing Node.js dependencies..."
|
|
if [ -d "$SERVER_DIR" ]; then
|
|
cd "$SERVER_DIR"
|
|
if [ ! -d "node_modules" ]; then
|
|
npm install
|
|
else
|
|
echo "node_modules already exists. Skipping npm install."
|
|
fi
|
|
else
|
|
echo -e "${RED}Error: server directory not found at $SERVER_DIR${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# 2. Setup Environment Variables (Interactive)
|
|
ENV_FILE="$SERVER_DIR/.env"
|
|
CONFIGURE=true
|
|
|
|
if [ -f "$ENV_FILE" ]; then
|
|
echo -e "\n${GREEN}.env file already exists.${NC}"
|
|
read -p "Do you want to reconfigure it? (y/N): " RECONF
|
|
if [[ ! "$RECONF" =~ ^[Yy]$ ]]; then
|
|
CONFIGURE=false
|
|
fi
|
|
fi
|
|
|
|
if [ "$CONFIGURE" = true ]; then
|
|
echo -e "\n${GREEN}Configuring Environment Variables...${NC}"
|
|
echo "Press Enter to accept defaults [in brackets]"
|
|
|
|
# Helper function for prompts
|
|
prompt_val() {
|
|
local var_name=$1
|
|
local prompt_text=$2
|
|
local default_val=$3
|
|
|
|
read -p "$prompt_text [$default_val]: " input
|
|
input=${input:-$default_val}
|
|
|
|
# Escape special characters if necessary, simpler for now
|
|
eval "$var_name=\"$input\""
|
|
}
|
|
|
|
prompt_val DB_HOST "Database Host" "localhost"
|
|
prompt_val DB_USER "Database User" "bacula"
|
|
prompt_val DB_PASSWORD "Database Password" "bacula" # Default might be insecure, but user can change
|
|
prompt_val DB_NAME "Database Name" "bacula"
|
|
prompt_val DB_PORT "Database Port" "5432"
|
|
|
|
echo ""
|
|
prompt_val SSH_HOST "Remote Bacula SSH Host" "10.10.14.82"
|
|
prompt_val SSH_USER "Remote SSH User" "root"
|
|
prompt_val SSH_PASSWORD "Remote SSH Password" ""
|
|
prompt_val SSH_KEY_PATH "Remote SSH Key Path" ""
|
|
prompt_val BACULA_CONF_DIR "Bacula Config Dir" "/etc/bacula/conf.d/clients"
|
|
|
|
echo ""
|
|
prompt_val PORT "Application Port" "3000"
|
|
|
|
# Write to .env
|
|
cat > "$ENV_FILE" <<EOF
|
|
# Database Configuration
|
|
DB_HOST=$DB_HOST
|
|
DB_USER=$DB_USER
|
|
DB_PASSWORD=$DB_PASSWORD
|
|
DB_NAME=$DB_NAME
|
|
DB_PORT=$DB_PORT
|
|
|
|
# Remote Admin (SSH)
|
|
SSH_HOST=$SSH_HOST
|
|
SSH_USER=$SSH_USER
|
|
SSH_PASSWORD=$SSH_PASSWORD
|
|
SSH_KEY_PATH=$SSH_KEY_PATH
|
|
BACULA_CONF_DIR=$BACULA_CONF_DIR
|
|
|
|
# Server Config
|
|
PORT=$PORT
|
|
EOF
|
|
echo -e "${GREEN}Configuration saved to $ENV_FILE${NC}"
|
|
fi
|
|
|
|
|
|
# 3. Create Systemd Service
|
|
echo "Creating systemd service..."
|
|
|
|
# Detect the user who owns the project directory to run the service as
|
|
REAL_USER=$(stat -c '%U' "$PROJECT_DIR")
|
|
NODE_PATH=$(which node)
|
|
|
|
cat > /etc/systemd/system/$SERVICE_NAME.service <<EOF
|
|
[Unit]
|
|
Description=Backstor UI Service
|
|
After=network.target postgresql.service
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=$REAL_USER
|
|
WorkingDirectory=$SERVER_DIR
|
|
ExecStart=$NODE_PATH index.js
|
|
Restart=on-failure
|
|
Environment=PORT=$PORT
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
echo -e "${GREEN}Service file created at /etc/systemd/system/$SERVICE_NAME.service${NC}"
|
|
|
|
# 4. Enable and Start Service
|
|
echo "Enabling and starting service..."
|
|
systemctl daemon-reload
|
|
systemctl enable $SERVICE_NAME
|
|
systemctl restart $SERVICE_NAME
|
|
|
|
echo -e "${GREEN}Installation Complete!${NC}"
|
|
echo -e "You can check the status with: ${GREEN}systemctl status $SERVICE_NAME${NC}"
|
|
echo -e "Access the UI at: ${GREEN}http://$(hostname -I | awk '{print $1}'):$PORT${NC}"
|