Files
cloud-node-container/scripts/generate-ecosystem-config.sh

74 lines
2.1 KiB
Bash
Raw Permalink Normal View History

#!/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}',
exec_mode: 'fork',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
kill_timeout: 3000,
wait_ready: false,
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)"