Files
plm-backend-service/install.sh
admin 0efeaff88e feat: PLM backend services - auth, user, project, config services
Sprint 0 backend development complete:
- auth-service: JWT authentication, login/logout, token refresh
- user-service: User CRUD, profile management, RBAC
- project-service: Project lifecycle, member management
- config-service: System configuration, audit logging

Technical stack:
- FastAPI async framework
- SQLAlchemy 2.0 async ORM
- JWT authentication with python-jose
- bcrypt password hashing
- Pydantic v2 validation

API endpoints: 31 total
Code lines: 3,551
2026-03-31 00:08:52 +08:00

222 lines
5.8 KiB
Bash
Executable File

#!/bin/bash
# PLM System Installation Script
# Run with sudo or as root for system-wide installation
set -e
echo "========================================"
echo " PLM System - Installation Script"
echo " Domain: aifly.ren"
echo "========================================"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
INSTALL_DIR="/opt/plm-system"
DOMAIN="aifly.ren"
DB_PASSWORD="plm_secure_password_2024"
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo -e "${YELLOW}Warning: Not running as root. Some features may not work.${NC}"
INSTALL_DIR="$HOME/plm-system"
fi
echo ""
echo "Installation directory: $INSTALL_DIR"
echo "Domain: $DOMAIN"
echo ""
# Function to check command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Step 1: Check system requirements
echo "Step 1: Checking system requirements..."
echo "----------------------------------------"
# Check disk space
DISK_AVAIL=$(df -BG / | awk 'NR==2 {print $4}' | sed 's/G//')
if [ "$DISK_AVAIL" -lt 10 ]; then
echo -e "${RED}Error: Insufficient disk space. Need at least 10GB available.${NC}"
exit 1
fi
echo -e "${GREEN}✓ Disk space: ${DISK_AVAIL}GB available${NC}"
# Check Python
if command_exists python3; then
PYTHON_VER=$(python3 --version 2>&1 | awk '{print $2}')
echo -e "${GREEN}✓ Python: $PYTHON_VER${NC}"
else
echo -e "${RED}✗ Python 3 not found${NC}"
fi
# Check Node.js
if command_exists node; then
NODE_VER=$(node --version)
echo -e "${GREEN}✓ Node.js: $NODE_VER${NC}"
else
echo -e "${RED}✗ Node.js not found${NC}"
fi
# Check Docker
if command_exists docker; then
DOCKER_VER=$(docker --version)
echo -e "${GREEN}✓ Docker: $DOCKER_VER${NC}"
else
echo -e "${YELLOW}! Docker not found (optional)${NC}"
fi
echo ""
# Step 2: Install system dependencies
echo "Step 2: Installing system dependencies..."
echo "----------------------------------------"
if [ "$EUID" -eq 0 ]; then
apt-get update
apt-get install -y nginx postgresql-14 postgresql-contrib-14 redis-server
systemctl enable nginx postgresql redis-server
echo -e "${GREEN}✓ System packages installed${NC}"
else
echo -e "${YELLOW}! Skipping system package installation (requires root)${NC}"
fi
echo ""
# Step 3: Configure PostgreSQL
echo "Step 3: Configuring PostgreSQL..."
echo "----------------------------------------"
if [ "$EUID" -eq 0 ]; then
# Start PostgreSQL
systemctl start postgresql
# Create database and user
sudo -u postgres psql <<EOF
CREATE USER plm_admin WITH PASSWORD '$DB_PASSWORD';
CREATE DATABASE plm_database OWNER plm_admin;
GRANT ALL PRIVILEGES ON DATABASE plm_database TO plm_admin;
\q
EOF
echo -e "${GREEN}✓ PostgreSQL configured${NC}"
else
echo -e "${YELLOW}! PostgreSQL configuration skipped (requires root)${NC}"
fi
echo ""
# Step 4: Configure Nginx
echo "Step 4: Configuring Nginx..."
echo "----------------------------------------"
if [ "$EUID" -eq 0 ]; then
# Backup default config
mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak 2>/dev/null || true
# Copy our config
cp "$INSTALL_DIR/nginx/conf.d/aifly.ren.conf" /etc/nginx/conf.d/
cp "$INSTALL_DIR/nginx/nginx.conf" /etc/nginx/nginx.conf
# Test and reload
nginx -t && systemctl reload nginx
echo -e "${GREEN}✓ Nginx configured${NC}"
else
echo -e "${YELLOW}! Nginx configuration skipped (requires root)${NC}"
fi
echo ""
# Step 5: Setup application
echo "Step 5: Setting up application..."
echo "----------------------------------------"
# Create virtual environment
if [ ! -d "$INSTALL_DIR/python/venv" ]; then
python3 -m venv "$INSTALL_DIR/python/venv"
echo -e "${GREEN}✓ Python virtual environment created${NC}"
fi
# Install Python dependencies
source "$INSTALL_DIR/python/venv/bin/activate"
pip install --upgrade pip
pip install -r "$INSTALL_DIR/python/requirements.txt"
echo -e "${GREEN}✓ Python dependencies installed${NC}"
# Setup Node.js dependencies
cd "$INSTALL_DIR/node"
npm install
echo -e "${GREEN}✓ Node.js dependencies installed${NC}"
echo ""
# Step 6: Configure firewall
echo "Step 6: Configuring firewall..."
echo "----------------------------------------"
if [ "$EUID" -eq 0 ]; then
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 8000/tcp
ufw allow 5432/tcp
echo -e "${GREEN}✓ Firewall configured${NC}"
else
echo -e "${YELLOW}! Firewall configuration skipped (requires root)${NC}"
fi
echo ""
# Step 7: Create systemd services
echo "Step 7: Creating systemd services..."
echo "----------------------------------------"
if [ "$EUID" -eq 0 ]; then
cat > /etc/systemd/system/plm-api.service <<EOF
[Unit]
Description=PLM System API
After=network.target postgresql.service
[Service]
Type=simple
User=www-data
WorkingDirectory=$INSTALL_DIR/app
Environment=PATH=$INSTALL_DIR/python/venv/bin
Environment=DOMAIN=$DOMAIN
ExecStart=$INSTALL_DIR/python/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable plm-api
echo -e "${GREEN}✓ Systemd service created${NC}"
else
echo -e "${YELLOW}! Systemd service creation skipped (requires root)${NC}"
fi
echo ""
echo "========================================"
echo " Installation Complete!"
echo "========================================"
echo ""
echo "Domain: $DOMAIN"
echo "Install Directory: $INSTALL_DIR"
echo ""
echo "Next steps:"
echo " 1. Configure DNS for $DOMAIN to point to this server"
echo " 2. Obtain SSL certificates (Let's Encrypt)"
echo " 3. Start the application: sudo systemctl start plm-api"
echo " 4. Check status: sudo systemctl status plm-api"
echo ""
echo "Health check: http://$DOMAIN/health"
echo "API docs: http://$DOMAIN/docs"
echo ""