-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
274 lines (239 loc) · 7.29 KB
/
deploy.sh
File metadata and controls
274 lines (239 loc) · 7.29 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
#!/bin/bash
# WebAIlyzer Lite API - Simple Deployment Script
# This script provides an easy way to deploy the API using Docker
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
# Default values
IMAGE_NAME="webailyzer-lite-api"
CONTAINER_NAME="webailyzer-api"
PORT="8080"
MEMORY_LIMIT="256m"
CPU_LIMIT="0.5"
# 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
print_success "Docker is available and running"
}
# Function to build the Docker image
build_image() {
print_status "Building Docker image: $IMAGE_NAME"
if docker build -t "$IMAGE_NAME" .; then
print_success "Docker image built successfully"
else
print_error "Failed to build Docker image"
exit 1
fi
}
# Function to stop and remove existing container
cleanup_existing() {
if docker ps -a --format 'table {{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
print_warning "Stopping and removing existing container: $CONTAINER_NAME"
docker stop "$CONTAINER_NAME" &> /dev/null || true
docker rm "$CONTAINER_NAME" &> /dev/null || true
print_success "Existing container removed"
fi
}
# Function to run the container
run_container() {
print_status "Starting container: $CONTAINER_NAME"
docker run -d \
--name "$CONTAINER_NAME" \
-p "$PORT:8080" \
--memory="$MEMORY_LIMIT" \
--cpus="$CPU_LIMIT" \
--restart unless-stopped \
--read-only \
--tmpfs /tmp \
"$IMAGE_NAME"
if [ $? -eq 0 ]; then
print_success "Container started successfully"
print_status "Container is running on port $PORT"
else
print_error "Failed to start container"
exit 1
fi
}
# Function to wait for the service to be ready
wait_for_service() {
print_status "Waiting for service to be ready..."
for i in {1..30}; do
if curl -s -f "http://localhost:$PORT/health" &> /dev/null; then
print_success "Service is ready and responding"
return 0
fi
sleep 1
done
print_error "Service did not become ready within 30 seconds"
print_status "Check container logs with: docker logs $CONTAINER_NAME"
return 1
}
# Function to test the API
test_api() {
print_status "Testing API endpoints..."
# Test health endpoint
if curl -s -f "http://localhost:$PORT/health" | grep -q '"status":"ok"'; then
print_success "Health endpoint is working"
else
print_error "Health endpoint test failed"
return 1
fi
# Test analysis endpoint with a simple URL
if curl -s -X POST "http://localhost:$PORT/v1/analyze" \
-H "Content-Type: application/json" \
-d '{"url":"https://httpbin.org/html"}' | grep -q '"detected"'; then
print_success "Analysis endpoint is working"
else
print_warning "Analysis endpoint test failed (this might be due to network issues)"
fi
}
# Function to show usage information
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -p, --port PORT Port to expose (default: 8080)"
echo " -m, --memory LIMIT Memory limit (default: 256m)"
echo " -c, --cpu LIMIT CPU limit (default: 0.5)"
echo " -n, --name NAME Container name (default: webailyzer-api)"
echo " -i, --image NAME Image name (default: webailyzer-lite-api)"
echo " --no-test Skip API testing after deployment"
echo " --cleanup-only Only cleanup existing container and exit"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Deploy with default settings"
echo " $0 -p 9000 # Deploy on port 9000"
echo " $0 -m 512m -c 1.0 # Deploy with more resources"
echo " $0 --cleanup-only # Only remove existing container"
}
# Function to show deployment information
show_deployment_info() {
echo ""
print_success "=== Deployment Complete ==="
echo ""
echo "Service Information:"
echo " • Container Name: $CONTAINER_NAME"
echo " • Image: $IMAGE_NAME"
echo " • Port: $PORT"
echo " • Memory Limit: $MEMORY_LIMIT"
echo " • CPU Limit: $CPU_LIMIT"
echo ""
echo "API Endpoints:"
echo " • Health Check: http://localhost:$PORT/health"
echo " • Analysis: http://localhost:$PORT/v1/analyze"
echo ""
echo "Quick Test Commands:"
echo " curl http://localhost:$PORT/health"
echo " curl -X POST http://localhost:$PORT/v1/analyze \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"url\":\"https://example.com\"}'"
echo ""
echo "Management Commands:"
echo " • View logs: docker logs $CONTAINER_NAME"
echo " • Stop service: docker stop $CONTAINER_NAME"
echo " • Start service: docker start $CONTAINER_NAME"
echo " • Remove service: docker rm -f $CONTAINER_NAME"
echo ""
}
# Parse command line arguments
SKIP_TEST=false
CLEANUP_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
PORT="$2"
shift 2
;;
-m|--memory)
MEMORY_LIMIT="$2"
shift 2
;;
-c|--cpu)
CPU_LIMIT="$2"
shift 2
;;
-n|--name)
CONTAINER_NAME="$2"
shift 2
;;
-i|--image)
IMAGE_NAME="$2"
shift 2
;;
--no-test)
SKIP_TEST=true
shift
;;
--cleanup-only)
CLEANUP_ONLY=true
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Main deployment process
main() {
echo "WebAIlyzer Lite API - Deployment Script"
echo "========================================"
echo ""
# Check prerequisites
check_docker
# Cleanup existing container
cleanup_existing
# If cleanup-only mode, exit here
if [ "$CLEANUP_ONLY" = true ]; then
print_success "Cleanup completed"
exit 0
fi
# Build and deploy
build_image
run_container
# Wait for service to be ready
if wait_for_service; then
# Test the API if not skipped
if [ "$SKIP_TEST" = false ]; then
test_api
fi
# Show deployment information
show_deployment_info
else
print_error "Deployment failed - service is not responding"
print_status "Check container logs with: docker logs $CONTAINER_NAME"
exit 1
fi
}
# Run main function
main "$@"