-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
347 lines (296 loc) · 12.1 KB
/
setup.sh
File metadata and controls
347 lines (296 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/bin/bash
################################################################################
# Elite Auto Server Setup - Main Entry Point
# Version: 1.0.0
# Description: Professional-grade server provisioning system
# Author: naveed-gung (https://github.com/naveed-gung)
# License: MIT
################################################################################
set -euo pipefail
IFS=$'\n\t'
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
# Global variables (must be defined before sourcing libraries)
readonly VERSION="1.0.0"
readonly LOG_DIR="/var/log/elite-setup"
readonly CONFIG_DIR="/etc/elite-setup"
readonly BACKUP_DIR="/var/lib/elite-setup/backups"
# Source all library modules
source "${SCRIPT_DIR}/lib/colors.sh"
source "${SCRIPT_DIR}/lib/logger.sh"
source "${SCRIPT_DIR}/lib/utils.sh"
source "${SCRIPT_DIR}/lib/preflight.sh"
source "${SCRIPT_DIR}/lib/installer.sh"
source "${SCRIPT_DIR}/lib/config.sh"
source "${SCRIPT_DIR}/lib/security.sh"
source "${SCRIPT_DIR}/lib/reporting.sh"
source "${SCRIPT_DIR}/lib/notifications.sh"
# Default configuration
MODE="interactive"
PROFILE="production"
CONFIG_FILE=""
SILENT=false
DRY_RUN=false
TEST_MODE=false
WITH_DOCKER=false
SKIP_FIREWALL=false
DOMAIN=""
SSL_EMAIL=""
NODE_VERSION="20"
MONGODB_VERSION="7.0"
SLACK_WEBHOOK=""
DISCORD_WEBHOOK=""
################################################################################
# Display banner
################################################################################
show_banner() {
if [[ "${SILENT}" == false ]]; then
clear
echo -e "${CYAN}"
cat << 'EOF'
╔═══════════════════════════════════════════════════════════════════════╗
║ ║
║ ███████╗██╗ ██╗████████╗███████╗ ███████╗███████╗████████╗ ║
║ ██╔════╝██║ ██║╚══██╔══╝██╔════╝ ██╔════╝██╔════╝╚══██╔══╝ ║
║ █████╗ ██║ ██║ ██║ █████╗ ███████╗█████╗ ██║ ║
║ ██╔══╝ ██║ ██║ ██║ ██╔══╝ ╚════██║██╔══╝ ██║ ║
║ ███████╗███████╗██║ ██║ ███████╗ ███████║███████╗ ██║ ║
║ ╚══════╝╚══════╝╚═╝ ╚═╝ ╚══════╝ ╚══════╝╚══════╝ ╚═╝ ║
║ ║
║ Elite Auto Server Setup - Version ${VERSION} ║
║ Professional Server Provisioning System ║
║ Created by: naveed-gung | naveed-gung.dev ║
║ ║
╚═══════════════════════════════════════════════════════════════════════╝
EOF
echo -e "${NC}"
echo ""
fi
}
################################################################################
# Display help
################################################################################
show_help() {
cat << EOF
${BOLD}Elite Auto Server Setup${NC} - Zero-touch production server provisioning
${BOLD}USAGE:${NC}
sudo ./setup.sh [OPTIONS]
${BOLD}OPTIONS:${NC}
${GREEN}--mode=MODE${NC} Mode: interactive, silent, or production (default: interactive)
${GREEN}--profile=PROFILE${NC} Profile: development, staging, production (default: production)
${GREEN}--config=FILE${NC} Load configuration from JSON file
${GREEN}--domain=DOMAIN${NC} Domain name for SSL certificate
${GREEN}--ssl-email=EMAIL${NC} Email for Let's Encrypt notifications
${GREEN}--node-version=VERSION${NC} Node.js version to install (default: 20)
${GREEN}--mongodb-version=VERSION${NC} MongoDB version to install (default: 7.0)
${GREEN}--with-docker${NC} Install Docker and docker-compose
${GREEN}--skip-firewall${NC} Skip firewall configuration
${GREEN}--slack-webhook=URL${NC} Slack webhook for notifications
${GREEN}--discord-webhook=URL${NC} Discord webhook for notifications
${GREEN}--silent${NC} Run in silent mode (no interactive prompts)
${GREEN}--dry-run${NC} Simulate installation without making changes
${GREEN}--test${NC} Test mode - validates script without installing anything
${GREEN}--help${NC} Display this help message
${GREEN}--version${NC} Display version information
${BOLD}EXAMPLES:${NC}
${CYAN}# Interactive installation${NC}
sudo ./setup.sh
${CYAN}# Production server with SSL${NC}
sudo ./setup.sh --mode=production --domain=example.com --ssl-email=admin@example.com
${CYAN}# Development environment with Docker${NC}
sudo ./setup.sh --profile=development --with-docker --skip-firewall
${CYAN}# CI/CD deployment${NC}
sudo ./setup.sh --config=production.json --silent
${BOLD}CONFIGURATION PROFILES:${NC}
${YELLOW}development${NC} - Local development environment
${YELLOW}staging${NC} - Pre-production testing environment
${YELLOW}production${NC} - Production server with full security
${BOLD}DOCUMENTATION:${NC}
GitHub: https://github.com/naveed-gung/elite-server-setup
Portfolio: https://naveed-gung.dev
Created by: naveed-gung
EOF
}
################################################################################
# Display version
################################################################################
show_version() {
echo "Elite Auto Server Setup v${VERSION}"
echo "Created by: naveed-gung (https://github.com/naveed-gung)"
echo "Portfolio: https://naveed-gung.dev"
echo "License: MIT"
}
################################################################################
# Parse command-line arguments
################################################################################
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
show_help
exit 0
;;
--version|-v)
show_version
exit 0
;;
--mode=*)
MODE="${1#*=}"
;;
--profile=*)
PROFILE="${1#*=}"
;;
--config=*)
CONFIG_FILE="${1#*=}"
;;
--domain=*)
DOMAIN="${1#*=}"
;;
--ssl-email=*)
SSL_EMAIL="${1#*=}"
;;
--node-version=*)
NODE_VERSION="${1#*=}"
;;
--mongodb-version=*)
MONGODB_VERSION="${1#*=}"
;;
--slack-webhook=*)
SLACK_WEBHOOK="${1#*=}"
;;
--discord-webhook=*)
DISCORD_WEBHOOK="${1#*=}"
;;
--with-docker)
WITH_DOCKER=true
;;
--skip-firewall)
SKIP_FIREWALL=true
;;
--silent)
SILENT=true
;;
--dry-run)
DRY_RUN=true
;;
--test)
TEST_MODE=true
DRY_RUN=true
;;
*)
echo -e "${RED}Error: Unknown option: $1${NC}" >&2
echo "Run './setup.sh --help' for usage information."
exit 1
;;
esac
shift
done
}
################################################################################
# Main installation flow
################################################################################
main() {
# Parse arguments
parse_arguments "$@"
# Display banner
show_banner
# Initialize logging
init_logging
log_info "=== Elite Auto Server Setup v${VERSION} ==="
log_info "Mode: ${MODE}"
log_info "Profile: ${PROFILE}"
# Check if running as root
if [[ "${EUID}" -ne 0 ]]; then
log_error "This script must be run as root or with sudo"
echo -e "${RED}Error: Please run with sudo${NC}"
exit 1
fi
# Load configuration
if [[ -n "${CONFIG_FILE}" ]]; then
log_info "Loading configuration from: ${CONFIG_FILE}"
load_config "${CONFIG_FILE}"
fi
# Run preflight checks
log_info "Running preflight system checks..."
if ! run_preflight_checks; then
log_error "Preflight checks failed"
send_notification "❌ Elite Setup Failed" "Preflight checks failed on $(hostname)"
exit 1
fi
# Display installation plan
if [[ "${SILENT}" == false ]]; then
display_installation_plan
read -p "Continue with installation? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Installation cancelled by user"
exit 0
fi
fi
# Create backup point for rollback
log_info "Creating system backup point..."
create_backup_point
# Start installation
local start_time=$(date +%s)
if [[ "${DRY_RUN}" == true ]]; then
log_info "DRY RUN MODE - No changes will be made"
fi
# Install components
log_info "Installing components..."
# Update system packages
install_component "System Update" update_system_packages
# Install Node.js
install_component "Node.js ${NODE_VERSION}" install_nodejs
# Install MongoDB
install_component "MongoDB ${MONGODB_VERSION}" install_mongodb
# Install Nginx
install_component "Nginx" install_nginx
# Install PM2
install_component "PM2" install_pm2
# Install Docker (optional)
if [[ "${WITH_DOCKER}" == true ]]; then
install_component "Docker" install_docker
fi
# Configure security
if [[ "${SKIP_FIREWALL}" == false ]]; then
install_component "Security Hardening" configure_security
fi
# Post-installation health checks
log_info "Running post-installation health checks..."
if ! run_health_checks; then
log_warning "Some health checks failed"
fi
# Generate deployment report
log_info "Generating deployment report..."
generate_deployment_report
# Calculate installation time
local end_time=$(date +%s)
local duration=$((end_time - start_time))
# Display summary
display_installation_summary "${duration}"
# Send notifications
send_notification "✅ Elite Setup Complete" "Server $(hostname) provisioned successfully in ${duration}s"
log_info "=== Installation Complete ==="
log_info "Installation logs: ${LOG_DIR}/install.log"
log_info "Deployment report: ${LOG_DIR}/report.html"
exit 0
}
################################################################################
# Error handler
################################################################################
error_handler() {
local line_number=$1
log_error "Error occurred in setup.sh at line ${line_number}"
log_error "Rolling back changes..."
if [[ "${DRY_RUN}" == false ]]; then
rollback_installation
fi
send_notification "❌ Elite Setup Failed" "Installation failed on $(hostname) at line ${line_number}"
echo -e "${RED}Installation failed. Check logs at ${LOG_DIR}/error.log${NC}"
exit 1
}
# Set error trap
trap 'error_handler ${LINENO}' ERR
# Run main function
main "$@"