Implement auto-generation of ecosystem.config.js and improve container setup
Some checks failed
Cloud Node Container / Build-and-Push (18) (push) Failing after 38s
Cloud Node Container / Build-and-Push (20) (push) Failing after 33s
Cloud Node Container / Build-and-Push (22) (push) Failing after 33s

- Add automatic ecosystem.config.js generation from package.json
- Create app directory automatically if missing
- Copy simple-website example when app directory is empty
- Remove redundant default app files from configs/
- Add HAProxy support with proper real IP forwarding
- Configure nginx to trust proxy headers from private networks
- Simplify entrypoint logic - always use /home/$user/app

This makes the container more user-friendly by eliminating the need for
manual PM2 configuration and ensuring the server always has a working app.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-07-24 09:01:08 -07:00
parent f8edb2b407
commit 9f0aa4b8b1
10 changed files with 123 additions and 196 deletions

View File

@@ -1,17 +0,0 @@
#!/usr/bin/env bash
USER=$1
BACKUP_DIR="/home/$USER/_backups"
DATE=$(date +%Y%m%d_%H%M%S)
# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR
# Backup application files
tar -czf $BACKUP_DIR/app_backup_$DATE.tar.gz -C /home/$USER app/
# Keep only last 10 backups
cd $BACKUP_DIR
ls -t app_backup_*.tar.gz | tail -n +11 | xargs -r rm
echo "Backup completed: app_backup_$DATE.tar.gz"

View File

@@ -35,7 +35,8 @@ server {
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header X-Forwarded-Proto \$http_x_forwarded_proto;
proxy_set_header X-CLIENT-IP \$http_x_client_ip;
proxy_cache_bypass \$http_upgrade;
}

View File

@@ -30,7 +30,6 @@ nginx
if [[ $environment == 'DEV' ]]; then
echo "Starting Dev Deployment"
mkdir -p /home/$user/_backups
# Ensure microdnf is available for installing additional packages in DEV mode
if ! command -v microdnf &> /dev/null; then
@@ -43,25 +42,38 @@ if [[ $environment == 'DEV' ]]; then
# Start memcached with 32MB memory limit
nohup memcached -d -u $user -p 11211 -m 32
# Set up automatic backups
echo "*/30 * * * * root /scripts/backup.sh $user" >> /etc/crontab
fi
# Start cron for log rotation and backups
/usr/sbin/crond
# If there's an app in the user directory, start it with PM2
if [ -f /home/$user/app/package.json ]; then
cd /home/$user/app
su -c "npm install" $user
su -c "pm2 start ecosystem.config.js" $user
else
# Start default app
cd /var/www/html
npm install
su -c "pm2 start ecosystem.config.js" $user
# Create app directory if it doesn't exist
if [ ! -d /home/$user/app ]; then
echo "Creating app directory at /home/$user/app"
mkdir -p /home/$user/app
chown -R $user:$user /home/$user/app
fi
# If app directory is empty, copy the simple-website example
if [ -z "$(ls -A /home/$user/app)" ]; then
echo "App directory is empty, copying simple-website example..."
cp -r /examples/simple-website/* /home/$user/app/
chown -R $user:$user /home/$user/app
echo "Copied simple-website example to provide a working application"
fi
# Now there's always an app in the user directory (either user's or example)
cd /home/$user/app
su -c "npm install" $user
# Check if ecosystem.config.js exists, if not generate it
if [ ! -f /home/$user/app/ecosystem.config.js ]; then
echo "No ecosystem.config.js found, generating from package.json..."
/scripts/generate-ecosystem-config.sh "$user" "/home/$user/app"
fi
su -c "pm2 start ecosystem.config.js" $user
# Follow logs
tail -f /home/$user/logs/nginx/* /home/$user/logs/nodejs/*

View File

@@ -0,0 +1,73 @@
#!/bin/bash
# Generate ecosystem.config.js from package.json
# Usage: ./generate-ecosystem-config.sh <user> <app_path>
user=$1
app_path=$2
if [ -z "$user" ] || [ -z "$app_path" ]; then
echo "Usage: $0 <user> <app_path>"
exit 1
fi
package_json="$app_path/package.json"
ecosystem_config="$app_path/ecosystem.config.js"
# Check if package.json exists
if [ ! -f "$package_json" ]; then
echo "Error: package.json not found at $package_json"
exit 1
fi
# Extract values from package.json
app_name=$(node -p "try { require('$package_json').name || 'node-app' } catch(e) { 'node-app' }")
main_script=$(node -p "try { require('$package_json').main || 'index.js' } catch(e) { 'index.js' }")
start_script=$(node -p "try { const scripts = require('$package_json').scripts; if (scripts && scripts.start) { scripts.start.replace(/^node\s+/, '') } else { null } } catch(e) { null }")
# Use start script if available, otherwise use main field
if [ "$start_script" != "null" ] && [ -n "$start_script" ]; then
script_file="$start_script"
else
script_file="$main_script"
fi
# Clean up the script file name (remove any node command prefix)
script_file=$(echo "$script_file" | sed 's/^node\s\+//')
# Generate ecosystem.config.js
cat > "$ecosystem_config" << EOF
module.exports = {
apps: [{
name: '${app_name}',
script: '${script_file}',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
kill_timeout: 3000,
wait_ready: true,
listen_timeout: 3000,
env: {
NODE_ENV: 'development',
PORT: 3000,
NODE_OPTIONS: '--max-old-space-size=200'
},
env_production: {
NODE_ENV: 'production',
PORT: 3000,
NODE_OPTIONS: '--max-old-space-size=200'
},
log_file: '/home/${user}/logs/nodejs/app.log',
error_file: '/home/${user}/logs/nodejs/error.log',
out_file: '/home/${user}/logs/nodejs/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
log_type: 'json',
merge_logs: true,
max_restarts: 5,
min_uptime: '10s'
}]
};
EOF
echo "Generated ecosystem.config.js for app: $app_name (script: $script_file)"