forked from onlydurodola/bash-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·647 lines (545 loc) · 16.3 KB
/
deploy.sh
File metadata and controls
executable file
·647 lines (545 loc) · 16.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/bin/bash
set -euo pipefail
# Script metadata
SCRIPT_NAME="deploy.sh"
VERSION="1.0.0"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Global variables
GIT_REPO_URL=""
GIT_PAT=""
BRANCH="main"
SSH_USER=""
SERVER_IP=""
SSH_KEY_PATH=""
APP_PORT="3000"
REPO_DIR=""
LOG_FILE="deploy_$(date +%Y%m%d_%H%M%S).log"
# Logging functions
setup_logging() {
exec > >(tee -a "$LOG_FILE")
exec 2>&1
}
log() {
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" | tee -a "$LOG_FILE"
}
log_success() {
echo -e "${GREEN}✓${NC} $1" | tee -a "$LOG_FILE"
}
log_warning() {
echo -e "${YELLOW}⚠${NC} $1" | tee -a "$LOG_FILE"
}
log_error() {
echo -e "${RED}✗${NC} $1" | tee -a "$LOG_FILE"
}
# Validation functions
validate_git_url() {
if [[ ! "$1" =~ ^https://.+ ]]; then
log_error "Invalid Git repository URL format. Must start with https://"
exit 1
fi
}
validate_ip() {
if [[ ! "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
log_error "Invalid IP address format"
exit 1
fi
}
validate_ssh_key() {
local key_path="${1/#\~/$HOME}"
if [ ! -f "$key_path" ]; then
log_error "SSH key file not found: $key_path"
exit 1
fi
}
validate_port() {
if [[ ! "$1" =~ ^[0-9]+$ ]] || [ "$1" -lt 1 ] || [ "$1" -gt 65535 ]; then
log_error "Invalid port number: $1. Must be between 1-65535"
exit 1
fi
}
validate_pat() {
if [ -z "$1" ]; then
log_error "Personal Access Token cannot be empty"
exit 1
fi
}
# User input collection
collect_user_input() {
log "Collecting deployment parameters..."
# Git repository details
while true; do
read -p "Enter Git Repository URL: " GIT_REPO_URL
if validate_git_url "$GIT_REPO_URL"; then
break
fi
done
while true; do
read -s -p "Enter Personal Access Token (PAT): " GIT_PAT
echo
if validate_pat "$GIT_PAT"; then
break
fi
done
read -p "Enter branch name [main]: " BRANCH
BRANCH=${BRANCH:-main}
# SSH details
read -p "Enter SSH username: " SSH_USER
if [ -z "$SSH_USER" ]; then
log_error "SSH username cannot be empty"
exit 1
fi
while true; do
read -p "Enter Server IP address: " SERVER_IP
if validate_ip "$SERVER_IP"; then
break
fi
done
read -p "Enter SSH key path [~/.ssh/id_rsa]: " SSH_KEY_PATH
SSH_KEY_PATH=${SSH_KEY_PATH:-~/.ssh/id_rsa}
SSH_KEY_PATH="${SSH_KEY_PATH/#\~/$HOME}"
validate_ssh_key "$SSH_KEY_PATH"
read -p "Enter application port [3000]: " APP_PORT
APP_PORT=${APP_PORT:-3000}
validate_port "$APP_PORT"
log_success "All parameters collected and validated"
}
# Git operations
clone_repository() {
local repo_name=$(basename "$GIT_REPO_URL" .git)
local repo_dir="$repo_name"
log "Processing repository: $repo_name"
if [ -d "$repo_dir" ]; then
log "Repository exists, pulling latest changes..."
cd "$repo_dir"
# Configure Git for PAT authentication
git config --local credential.helper 'store --file=.git-credentials'
echo "https://oauth2:$GIT_PAT@$(echo $GIT_REPO_URL | cut -d'/' -f3-)" > .git-credentials
# Stash any local changes and pull
git stash push -m "auto-stash-by-deploy-script" || true
git fetch origin
git checkout "$BRANCH" 2>/dev/null || git checkout -b "$BRANCH" "origin/$BRANCH"
git pull origin "$BRANCH"
log_success "Repository updated successfully"
else
log "Cloning repository..."
# Clone using PAT authentication
GIT_REPO_WITH_AUTH=$(echo "$GIT_REPO_URL" | sed "s#https://#https://oauth2:${GIT_PAT}@#")
if git clone -b "$BRANCH" "$GIT_REPO_WITH_AUTH" "$repo_dir" 2>/dev/null || \
git clone "$GIT_REPO_WITH_AUTH" "$repo_dir"; then
cd "$repo_dir"
if [ "$BRANCH" != "main" ]; then
git checkout "$BRANCH" 2>/dev/null || log_warning "Branch $BRANCH not found, using default branch"
fi
log_success "Repository cloned successfully"
else
log_error "Failed to clone repository"
exit 1
fi
fi
REPO_DIR=$(pwd)
}
verify_docker_files() {
log "Verifying Docker configuration files..."
if [ -f "Dockerfile" ]; then
log_success "Dockerfile found"
DOCKER_CONFIG="Dockerfile"
elif [ -f "docker-compose.yml" ]; then
log_success "docker-compose.yml found"
DOCKER_CONFIG="compose"
elif [ -f "docker-compose.yaml" ]; then
log_success "docker-compose.yaml found"
DOCKER_CONFIG="compose"
else
log_error "No Dockerfile or docker-compose.yml found in repository"
exit 1
fi
}
# SSH operations
ssh_command() {
ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes \
"$SSH_USER@$SERVER_IP" "$1"
}
scp_transfer() {
local source="$1"
local destination="$2"
scp -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -o ConnectTimeout=10 -r \
"$source" "$SSH_USER@$SERVER_IP:$destination"
}
test_ssh_connection() {
log "Testing SSH connection to $SERVER_IP..."
if ssh_command "echo 'SSH connection successful'"; then
log_success "SSH connection established"
else
log_error "SSH connection failed. Please check credentials and network connectivity"
exit 1
fi
}
# Remote server setup
prepare_remote_environment() {
log "Preparing remote environment on $SERVER_IP..."
local setup_script=$(cat << 'EOF'
#!/bin/bash
set -euo pipefail
log() { echo "$(date): $1"; }
log_success() { echo "✓ $1"; }
log_error() { echo "✗ $1"; }
# Update system packages
log "Updating system packages..."
sudo apt-get update -qq && sudo apt-get upgrade -y -qq
# Install Docker
if ! command -v docker &> /dev/null; then
log "Installing Docker..."
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
log_success "Docker installed"
else
log_success "Docker already installed"
fi
# Install Docker Compose
if ! command -v docker-compose &> /dev/null; then
log "Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
log_success "Docker Compose installed"
else
log_success "Docker Compose already installed"
fi
# Install Nginx
if ! command -v nginx &> /dev/null; then
log "Installing Nginx..."
sudo apt-get install -y -qq nginx
log_success "Nginx installed"
else
log_success "Nginx already installed"
fi
# Start and enable services
sudo systemctl enable docker --now
sudo systemctl enable nginx --now
# Verify installations
echo "Docker: $(docker --version)"
echo "Docker Compose: $(docker-compose --version)"
echo "Nginx: $(nginx -v 2>&1)"
EOF
)
ssh_command "$setup_script"
log_success "Remote environment prepared"
}
# Application deployment
deploy_application() {
log "Deploying application to remote server..."
local remote_project_dir="/home/$SSH_USER/$(basename "$REPO_DIR")"
local project_name=$(basename "$REPO_DIR")
# Transfer project files
log "Transferring project files to remote server..."
if scp_transfer "$REPO_DIR" "/home/$SSH_USER/"; then
log_success "Project files transferred"
else
log_error "Failed to transfer project files"
exit 1
fi
local deploy_script=$(cat << EOF
#!/bin/bash
set -euo pipefail
cd "$remote_project_dir"
log() { echo "$(date): $1"; }
log_error() { echo "✗ $1"; }
# Stop and remove existing containers
log "Stopping any existing containers..."
docker-compose down 2>/dev/null || true
docker stop app_container 2>/dev/null || true
docker rm app_container 2>/dev/null || true
# Build and run based on configuration
if [ -f "docker-compose.yml" ] || [ -f "docker-compose.yaml" ]; then
log "Using Docker Compose for deployment..."
docker-compose up -d --build
log "Waiting for containers to be healthy..."
sleep 15
# Check container status
if docker-compose ps | grep -q "Up"; then
log "Docker Compose containers are running"
else
log_error "Docker Compose containers failed to start"
docker-compose logs
exit 1
fi
elif [ -f "Dockerfile" ]; then
log "Using Dockerfile for deployment..."
docker build -t ${project_name}_image .
docker run -d --name app_container -p $APP_PORT:$APP_PORT ${project_name}_image
sleep 10
# Check container status
if docker ps | grep -q "app_container"; then
log "Docker container is running"
else
log_error "Docker container failed to start"
docker logs app_container
exit 1
fi
else
log_error "No Docker configuration found"
exit 1
fi
# Verify application is running
log "Verifying application health..."
if curl -f -s -o /dev/null --retry 3 --retry-delay 5 http://localhost:$APP_PORT; then
log "Application is healthy and responding"
else
log_error "Application health check failed"
exit 1
fi
EOF
)
ssh_command "$deploy_script"
log_success "Application deployed successfully"
}
# Nginx configuration
configure_nginx() {
log "Configuring Nginx reverse proxy..."
local nginx_config=$(cat << EOF
server {
listen 80;
server_name $SERVER_IP;
# Security headers
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
location / {
proxy_pass http://localhost:$APP_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
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_cache_bypass \$http_upgrade;
proxy_read_timeout 300;
proxy_connect_timeout 300;
}
# Block common exploits
location ~* (\.env|\.git|\.htaccess) {
deny all;
return 404;
}
}
EOF
)
local nginx_setup=$(cat << EOF
#!/bin/bash
set -euo pipefail
# Create nginx config
echo '$nginx_config' | sudo tee /etc/nginx/sites-available/app > /dev/null
# Enable site
sudo ln -sf /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
# Test configuration
if sudo nginx -t; then
sudo systemctl reload nginx
echo "Nginx configuration validated and reloaded"
else
echo "Nginx configuration test failed"
exit 1
fi
EOF
)
ssh_command "$nginx_setup"
log_success "Nginx reverse proxy configured"
}
# Deployment validation
validate_deployment() {
log "Validating deployment..."
local validation_script=$(cat << 'EOF'
#!/bin/bash
set -euo pipefail
log() { echo "$(date): $1"; }
log_error() { echo "✗ $1"; }
# Check Docker service
if ! systemctl is-active --quiet docker; then
log_error "Docker service is not running"
exit 1
fi
log "Docker service is running"
# Check containers
if [ -f "docker-compose.yml" ] || [ -f "docker-compose.yaml" ]; then
if docker-compose ps | grep -q "Up"; then
log "Docker Compose containers are running"
else
log_error "Docker Compose containers are not running"
exit 1
fi
else
if docker ps | grep -q "app_container"; then
log "Docker container is running"
else
log_error "Docker container is not running"
exit 1
fi
fi
# Check Nginx
if ! systemctl is-active --quiet nginx; then
log_error "Nginx is not running"
exit 1
fi
log "Nginx service is running"
# Test application directly
if curl -f -s -o /dev/null -w "%{http_code}" http://localhost:$APP_PORT | grep -q "200"; then
log "Application is responding on port $APP_PORT"
else
log_error "Application is not responding on port $APP_PORT"
exit 1
fi
# Test Nginx proxy
if curl -f -s -o /dev/null -w "%{http_code}" http://localhost; then
log "Nginx proxy is working correctly"
else
log_error "Nginx proxy is not working"
exit 1
fi
# Final end-to-end test from external perspective
if curl -f -s -o /dev/null http://localhost/health 2>/dev/null || \
curl -f -s -o /dev/null http://localhost/ 2>/dev/null || \
curl -f -s -o /dev/null http://localhost; then
log "End-to-end deployment validation successful"
else
log_error "End-to-end deployment validation failed"
exit 1
fi
EOF
)
local remote_project_dir="/home/$SSH_USER/$(basename "$REPO_DIR")"
if ssh_command "cd $remote_project_dir && $validation_script"; then
log_success "Deployment validation successful"
else
log_error "Deployment validation failed"
exit 1
fi
}
# Cleanup function
cleanup_resources() {
log "Starting cleanup of deployment resources..."
local cleanup_script=$(cat << EOF
#!/bin/bash
set -euo pipefail
log() { echo "$(date): $1"; }
local_project_dir="/home/$SSH_USER/$(basename "$REPO_DIR")"
if [ -d "\$local_project_dir" ]; then
cd "\$local_project_dir"
log "Stopping and removing containers..."
docker-compose down 2>/dev/null || true
docker stop app_container 2>/dev/null || true
docker rm app_container 2>/dev/null || true
docker rmi \$(basename "$REPO_DIR")_image 2>/dev/null || true
log "Removing project files..."
cd ..
rm -rf "\$(basename "$REPO_DIR")"
fi
log "Cleaning up Nginx configuration..."
sudo rm -f /etc/nginx/sites-available/app
sudo rm -f /etc/nginx/sites-enabled/app
sudo systemctl reload nginx
log "Cleanup completed"
EOF
)
ssh_command "$cleanup_script"
log_success "Cleanup completed successfully"
}
# Display deployment info
show_deployment_info() {
log_success "=== DEPLOYMENT COMPLETED SUCCESSFULLY ==="
echo
echo "Application Information:"
echo "-----------------------"
echo "URL: http://$SERVER_IP"
echo "App Port: $APP_PORT"
echo "Server: $SSH_USER@$SERVER_IP"
echo "Project: $(basename "$REPO_DIR")"
echo "Log File: $LOG_FILE"
echo
echo "Next steps:"
echo "1. Test the application at: http://$SERVER_IP"
echo "2. Check logs if needed: $LOG_FILE"
echo "3. To cleanup, run: $0 --cleanup"
echo
}
# Main deployment function
run_deployment() {
log "Starting automated deployment process..."
collect_user_input
test_ssh_connection
clone_repository
verify_docker_files
prepare_remote_environment
deploy_application
configure_nginx
validate_deployment
show_deployment_info
}
# Cleanup only function
run_cleanup() {
log "Starting cleanup process..."
collect_user_input
test_ssh_connection
cleanup_resources
log_success "Cleanup completed"
}
# Help function
show_help() {
cat << EOF
Usage: $0 [OPTION]
Automated Deployment Script for Dockerized Applications
Options:
--cleanup Remove all deployed resources from the server
--help Show this help message
--version Show version information
Without options, runs the full deployment process.
Features:
- Automated Git repository cloning with PAT authentication
- Remote server provisioning (Docker, Docker Compose, Nginx)
- Docker container deployment and management
- Nginx reverse proxy configuration
- Comprehensive logging and validation
Example:
$0 # Run full deployment
$0 --cleanup # Remove deployed resources
EOF
}
# Version function
show_version() {
echo "$SCRIPT_NAME version $VERSION"
echo "Production-grade automated deployment script"
}
# Main execution
main() {
setup_logging
case "${1:-}" in
--cleanup)
run_cleanup
;;
--help|-h)
show_help
;;
--version|-v)
show_version
;;
"")
run_deployment
;;
*)
log_error "Unknown option: $1"
echo
show_help
exit 1
;;
esac
}
# Handle script interruption
trap 'log_error "Script interrupted by user"; exit 1' INT TERM
# Run main function
main "$@"