-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·204 lines (174 loc) · 5.5 KB
/
deploy.sh
File metadata and controls
executable file
·204 lines (174 loc) · 5.5 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
#!/bin/bash
###############################################################################
# European News Intelligence Hub - Deployment Script
#
# Usage:
# ./deploy.sh [environment]
#
# Environments:
# production - Deploy to production with SSL
# staging - Deploy to staging environment
# dev - Deploy development environment
###############################################################################
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
# Functions
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if environment argument is provided
if [ -z "$1" ]; then
log_error "Environment not specified"
echo "Usage: ./deploy.sh [production|staging|dev]"
exit 1
fi
ENVIRONMENT=$1
log_info "Starting deployment for environment: $ENVIRONMENT"
# Validate environment
if [[ ! "$ENVIRONMENT" =~ ^(production|staging|dev)$ ]]; then
log_error "Invalid environment: $ENVIRONMENT"
echo "Valid environments: production, staging, dev"
exit 1
fi
# Check if .env file exists
if [ "$ENVIRONMENT" == "production" ] && [ ! -f ".env.production" ]; then
log_error ".env.production file not found"
echo "Please create .env.production file with required environment variables"
exit 1
fi
# Load environment variables
if [ "$ENVIRONMENT" == "production" ]; then
log_info "Loading production environment variables"
export $(cat .env.production | grep -v '^#' | xargs)
elif [ "$ENVIRONMENT" == "staging" ]; then
log_info "Loading staging environment variables"
export $(cat .env.staging | grep -v '^#' | xargs)
else
log_info "Loading development environment variables"
export $(cat .env | grep -v '^#' | xargs)
fi
# Check required environment variables
required_vars=("POSTGRES_PASSWORD" "REDIS_PASSWORD" "SECRET_KEY" "GEMINI_API_KEY")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
log_error "Required environment variable $var is not set"
exit 1
fi
done
log_success "Environment variables loaded"
# Stop existing containers
log_info "Stopping existing containers..."
if [ "$ENVIRONMENT" == "production" ]; then
docker-compose -f docker-compose.prod.yml down
else
docker-compose down
fi
log_success "Containers stopped"
# Pull latest code (if deploying from git)
if [ "$ENVIRONMENT" == "production" ] && [ -d ".git" ]; then
log_info "Pulling latest code from git..."
git pull origin main
log_success "Code updated"
fi
# Build and start containers
log_info "Building Docker images..."
if [ "$ENVIRONMENT" == "production" ]; then
docker-compose -f docker-compose.prod.yml build --no-cache
else
docker-compose build
fi
log_success "Docker images built"
log_info "Starting containers..."
if [ "$ENVIRONMENT" == "production" ]; then
docker-compose -f docker-compose.prod.yml up -d
else
docker-compose up -d
fi
log_success "Containers started"
# Wait for services to be healthy
log_info "Waiting for services to be healthy..."
sleep 10
# Check backend health
log_info "Checking backend health..."
if [ "$ENVIRONMENT" == "production" ]; then
max_attempts=30
attempt=0
while [ $attempt -lt $max_attempts ]; do
if curl -f http://localhost/health > /dev/null 2>&1; then
log_success "Backend is healthy"
break
fi
attempt=$((attempt + 1))
echo -n "."
sleep 2
done
if [ $attempt -eq $max_attempts ]; then
log_error "Backend health check failed"
exit 1
fi
else
if curl -f http://localhost:8000/health > /dev/null 2>&1; then
log_success "Backend is healthy"
else
log_warning "Backend health check failed (this is expected for first deployment)"
fi
fi
# Run database migrations (if needed)
log_info "Running database migrations..."
if [ "$ENVIRONMENT" == "production" ]; then
docker-compose -f docker-compose.prod.yml exec -T backend alembic upgrade head || log_warning "Migration skipped"
else
docker-compose exec -T backend alembic upgrade head || log_warning "Migration skipped"
fi
# Show running containers
log_info "Running containers:"
if [ "$ENVIRONMENT" == "production" ]; then
docker-compose -f docker-compose.prod.yml ps
else
docker-compose ps
fi
# Deployment complete
echo ""
log_success "========================================"
log_success " Deployment completed successfully! "
log_success "========================================"
echo ""
if [ "$ENVIRONMENT" == "production" ]; then
log_info "Application URLs:"
echo " Frontend: https://yourdomain.com"
echo " Backend API: https://yourdomain.com/api"
echo " API Docs: https://yourdomain.com/docs"
echo ""
log_info "View logs:"
echo " docker-compose -f docker-compose.prod.yml logs -f"
else
log_info "Application URLs:"
echo " Frontend: http://localhost:3000"
echo " Backend API: http://localhost:8000"
echo " API Docs: http://localhost:8000/docs"
echo ""
log_info "View logs:"
echo " docker-compose logs -f"
fi
echo ""
log_info "Useful commands:"
echo " Stop all services: docker-compose down"
echo " View backend logs: docker-compose logs -f backend"
echo " View celery logs: docker-compose logs -f celery_worker"
echo " Database backup: ./scripts/backup.sh"
echo " Health check: ./scripts/health_check.sh"
echo ""