-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-and-deploy.sh
More file actions
239 lines (211 loc) · 6.55 KB
/
build-and-deploy.sh
File metadata and controls
239 lines (211 loc) · 6.55 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
#!/bin/bash
# RecoStream Build and Deployment Script
# This script helps you build and deploy the RecoStream application using Docker
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if Docker is installed and running
check_docker() {
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker is not running. Please start Docker first."
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
print_success "Docker and Docker Compose are available"
}
# Function to create necessary directories
create_directories() {
print_status "Creating necessary directories..."
mkdir -p data logs backups nginx/ssl nginx/logs
print_success "Directories created"
}
# Function to copy environment file
setup_environment() {
if [ ! -f .env ]; then
if [ -f .env.docker ]; then
print_status "Copying .env.docker to .env..."
cp .env.docker .env
print_warning "Please edit .env file with your actual configuration values"
else
print_error ".env.docker template not found. Please create environment configuration."
exit 1
fi
else
print_success "Environment file (.env) already exists"
fi
}
# Function to build the application
build_app() {
print_status "Building RecoStream application..."
docker-compose build --no-cache
print_success "Application built successfully"
}
# Function to start services in development mode
start_dev() {
print_status "Starting RecoStream in development mode..."
docker-compose up -d postgres redis
print_status "Waiting for database to be ready..."
sleep 10
docker-compose up recostream-app
}
# Function to start services in production mode
start_prod() {
print_status "Starting RecoStream in production mode..."
docker-compose -f docker-compose.prod.yml up -d
print_success "RecoStream started in production mode"
print_status "Application will be available at:"
print_status " - HTTP: http://localhost"
print_status " - Direct API: http://localhost:8000"
}
# Function to stop services
stop_services() {
print_status "Stopping RecoStream services..."
docker-compose down
docker-compose -f docker-compose.prod.yml down 2>/dev/null || true
print_success "Services stopped"
}
# Function to show logs
show_logs() {
local service=${1:-recostream-app}
print_status "Showing logs for $service..."
docker-compose logs -f $service
}
# Function to backup database
backup_database() {
local backup_name="recostream_backup_$(date +%Y%m%d_%H%M%S).sql"
print_status "Creating database backup: $backup_name"
docker-compose exec postgres pg_dump -U recostream_user recostream > "backups/$backup_name"
print_success "Database backup created: backups/$backup_name"
}
# Function to restore database
restore_database() {
local backup_file=$1
if [ -z "$backup_file" ]; then
print_error "Please specify backup file to restore"
exit 1
fi
if [ ! -f "$backup_file" ]; then
print_error "Backup file not found: $backup_file"
exit 1
fi
print_status "Restoring database from: $backup_file"
docker-compose exec -T postgres psql -U recostream_user recostream < "$backup_file"
print_success "Database restored successfully"
}
# Function to show help
show_help() {
echo "RecoStream Build and Deployment Script"
echo ""
echo "Usage: $0 [COMMAND]"
echo ""
echo "Commands:"
echo " build Build the Docker images"
echo " dev Start in development mode"
echo " prod Start in production mode"
echo " stop Stop all services"
echo " restart Restart all services"
echo " logs [svc] Show logs (optional service name)"
echo " backup Backup database"
echo " restore Restore database from backup"
echo " clean Clean up Docker images and containers"
echo " status Show service status"
echo " help Show this help message"
echo ""
echo "Examples:"
echo " $0 build && $0 dev # Build and start in development"
echo " $0 prod # Start in production mode"
echo " $0 logs recostream-app # Show application logs"
echo " $0 backup # Create database backup"
}
# Function to clean up Docker resources
clean_docker() {
print_status "Cleaning up Docker resources..."
docker-compose down --rmi all --volumes --remove-orphans 2>/dev/null || true
docker-compose -f docker-compose.prod.yml down --rmi all --volumes --remove-orphans 2>/dev/null || true
docker system prune -f
print_success "Docker cleanup completed"
}
# Function to show service status
show_status() {
print_status "Service Status:"
docker-compose ps
echo ""
print_status "Production Service Status:"
docker-compose -f docker-compose.prod.yml ps 2>/dev/null || echo "No production services running"
}
# Main script logic
main() {
local command=${1:-help}
case $command in
"build")
check_docker
create_directories
setup_environment
build_app
;;
"dev")
check_docker
create_directories
setup_environment
start_dev
;;
"prod")
check_docker
create_directories
setup_environment
start_prod
;;
"stop")
stop_services
;;
"restart")
stop_services
sleep 2
start_prod
;;
"logs")
show_logs $2
;;
"backup")
backup_database
;;
"restore")
restore_database $2
;;
"clean")
clean_docker
;;
"status")
show_status
;;
"help"|*)
show_help
;;
esac
}
# Run main function with all arguments
main "$@"