CalejoControl/deploy-onprem.sh

388 lines
9.5 KiB
Bash
Executable File

#!/bin/bash
# Calejo Control Adapter - On-Prem Deployment Script
# This script automates the deployment process for customer on-prem installations
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
DEPLOYMENT_DIR="/opt/calejo-control-adapter"
LOG_DIR="/var/log/calejo"
CONFIG_DIR="/etc/calejo"
BACKUP_DIR="/var/backup/calejo"
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root for system-wide installation"
exit 1
fi
}
# Function to check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
# Check Docker
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
# Check Docker Compose
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
# Check available disk space
local available_space=$(df / | awk 'NR==2 {print $4}')
if [[ $available_space -lt 1048576 ]]; then # Less than 1GB
print_warning "Low disk space available: ${available_space}KB"
fi
print_success "Prerequisites check passed"
}
# Function to create directories
create_directories() {
print_status "Creating directories..."
mkdir -p $DEPLOYMENT_DIR
mkdir -p $LOG_DIR
mkdir -p $CONFIG_DIR
mkdir -p $BACKUP_DIR
mkdir -p $DEPLOYMENT_DIR/monitoring
mkdir -p $DEPLOYMENT_DIR/scripts
mkdir -p $DEPLOYMENT_DIR/database
print_success "Directories created"
}
# Function to copy files
copy_files() {
print_status "Copying deployment files..."
# Copy main application files
cp -r ./* $DEPLOYMENT_DIR/
# Copy configuration files
cp config/settings.py $CONFIG_DIR/
cp docker-compose.yml $DEPLOYMENT_DIR/
cp docker-compose.test.yml $DEPLOYMENT_DIR/
# Copy scripts
cp scripts/* $DEPLOYMENT_DIR/scripts/
cp test-deployment.sh $DEPLOYMENT_DIR/
cp test_dashboard_local.py $DEPLOYMENT_DIR/
# Copy monitoring configuration
cp -r monitoring/* $DEPLOYMENT_DIR/monitoring/
# Set permissions
chmod +x $DEPLOYMENT_DIR/scripts/*.sh
chmod +x $DEPLOYMENT_DIR/test-deployment.sh
print_success "Files copied to deployment directory"
}
# Function to create systemd service
create_systemd_service() {
print_status "Creating systemd service..."
cat > /etc/systemd/system/calejo-control-adapter.service << EOF
[Unit]
Description=Calejo Control Adapter
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=$DEPLOYMENT_DIR
ExecStart=/usr/bin/docker-compose up -d
ExecStop=/usr/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
print_success "Systemd service created"
}
# Function to create backup script
create_backup_script() {
print_status "Creating backup script..."
cat > $DEPLOYMENT_DIR/scripts/backup-full.sh << 'EOF'
#!/bin/bash
# Full backup script for Calejo Control Adapter
BACKUP_DIR="/var/backup/calejo"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="calejo-backup-$TIMESTAMP.tar.gz"
mkdir -p $BACKUP_DIR
# Stop services
echo "Stopping services..."
docker-compose down
# Create backup
echo "Creating backup..."
tar -czf $BACKUP_DIR/$BACKUP_FILE \
--exclude=node_modules \
--exclude=__pycache__ \
--exclude=*.pyc \
.
# Start services
echo "Starting services..."
docker-compose up -d
echo "Backup created: $BACKUP_DIR/$BACKUP_FILE"
echo "Backup size: $(du -h $BACKUP_DIR/$BACKUP_FILE | cut -f1)"
EOF
chmod +x $DEPLOYMENT_DIR/scripts/backup-full.sh
print_success "Backup script created"
}
# Function to create restore script
create_restore_script() {
print_status "Creating restore script..."
cat > $DEPLOYMENT_DIR/scripts/restore-full.sh << 'EOF'
#!/bin/bash
# Full restore script for Calejo Control Adapter
BACKUP_DIR="/var/backup/calejo"
if [ $# -eq 0 ]; then
echo "Usage: $0 <backup-file>"
echo "Available backups:"
ls -la $BACKUP_DIR/calejo-backup-*.tar.gz 2>/dev/null || echo "No backups found"
exit 1
fi
BACKUP_FILE="$1"
if [ ! -f "$BACKUP_FILE" ]; then
echo "Backup file not found: $BACKUP_FILE"
exit 1
fi
# Stop services
echo "Stopping services..."
docker-compose down
# Restore backup
echo "Restoring from backup..."
tar -xzf "$BACKUP_FILE" -C .
# Start services
echo "Starting services..."
docker-compose up -d
echo "Restore completed from: $BACKUP_FILE"
EOF
chmod +x $DEPLOYMENT_DIR/scripts/restore-full.sh
print_success "Restore script created"
}
# Function to create health check script
create_health_check_script() {
print_status "Creating health check script..."
cat > $DEPLOYMENT_DIR/scripts/health-check.sh << 'EOF'
#!/bin/bash
# Health check script for Calejo Control Adapter
set -e
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m'
check_service() {
local service_name=$1
local port=$2
local endpoint=$3
if curl -s "http://localhost:$port$endpoint" > /dev/null; then
echo -e "${GREEN}✓${NC} $service_name is running on port $port"
return 0
else
echo -e "${RED}✗${NC} $service_name is not responding on port $port"
return 1
fi
}
echo "Running health checks..."
# Check main application
check_service "Main Application" 8080 "/health"
# Check dashboard
check_service "Dashboard" 8080 "/dashboard"
# Check API endpoints
check_service "REST API" 8080 "/api/v1/status"
# Check if containers are running
if docker-compose ps | grep -q "Up"; then
echo -e "${GREEN}✓${NC} All Docker containers are running"
else
echo -e "${RED}✗${NC} Some Docker containers are not running"
docker-compose ps
fi
# Check disk space
echo ""
echo "System resources:"
df -h / | awk 'NR==2 {print "Disk usage: " $5 " (" $3 "/" $2 ")"}'
# Check memory
free -h | awk 'NR==2 {print "Memory usage: " $3 "/" $2}'
echo ""
echo "Health check completed"
EOF
chmod +x $DEPLOYMENT_DIR/scripts/health-check.sh
print_success "Health check script created"
}
# Function to build and start services
build_and_start_services() {
print_status "Building and starting services..."
cd $DEPLOYMENT_DIR
# Build the application
docker-compose build
# Start services
docker-compose up -d
# Wait for services to be ready
print_status "Waiting for services to start..."
for i in {1..30}; do
if curl -s http://localhost:8080/health > /dev/null 2>&1; then
print_success "Services started successfully"
break
fi
echo " Waiting... (attempt $i/30)"
sleep 2
if [ $i -eq 30 ]; then
print_error "Services failed to start within 60 seconds"
docker-compose logs
exit 1
fi
done
}
# Function to display deployment information
display_deployment_info() {
print_success "Deployment completed successfully!"
echo ""
echo "=================================================="
echo " DEPLOYMENT INFORMATION"
echo "=================================================="
echo ""
echo "📊 Access URLs:"
echo " Dashboard: http://$(hostname -I | awk '{print $1}'):8080/dashboard"
echo " REST API: http://$(hostname -I | awk '{print $1}'):8080"
echo " Health Check: http://$(hostname -I | awk '{print $1}'):8080/health"
echo ""
echo "🔧 Management Commands:"
echo " Start: systemctl start calejo-control-adapter"
echo " Stop: systemctl stop calejo-control-adapter"
echo " Status: systemctl status calejo-control-adapter"
echo " Health Check: $DEPLOYMENT_DIR/scripts/health-check.sh"
echo " Backup: $DEPLOYMENT_DIR/scripts/backup-full.sh"
echo ""
echo "📁 Important Directories:"
echo " Application: $DEPLOYMENT_DIR"
echo " Logs: $LOG_DIR"
echo " Configuration: $CONFIG_DIR"
echo " Backups: $BACKUP_DIR"
echo ""
echo "📚 Documentation:"
echo " Quick Start: $DEPLOYMENT_DIR/QUICKSTART.md"
echo " Dashboard: $DEPLOYMENT_DIR/DASHBOARD.md"
echo " Deployment: $DEPLOYMENT_DIR/DEPLOYMENT.md"
echo ""
echo "=================================================="
}
# Main deployment function
main() {
echo ""
echo "🚀 Calejo Control Adapter - On-Prem Deployment"
echo "=================================================="
echo ""
# Check if running as root
check_root
# Check prerequisites
check_prerequisites
# Create directories
create_directories
# Copy files
copy_files
# Create systemd service
create_systemd_service
# Create management scripts
create_backup_script
create_restore_script
create_health_check_script
# Build and start services
build_and_start_services
# Display deployment information
display_deployment_info
echo ""
print_success "On-prem deployment completed!"
echo ""
}
# Run main function
main "$@"