-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathdeploy-production.sh
More file actions
executable file
·288 lines (237 loc) · 7.97 KB
/
Copy pathdeploy-production.sh
File metadata and controls
executable file
·288 lines (237 loc) · 7.97 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
#!/bin/bash
# Production deployment script for Wazuh MCP Remote Server v4.0
# MCP-compliant remote server with Docker Compose v3.9
# OS-agnostic deployment with security hardening and monitoring
# Branch: mcp-remote
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
print_header() {
echo -e "${CYAN}================================================================${NC}"
echo -e "${CYAN} WAZUH MCP REMOTE SERVER - PRODUCTION DEPLOYMENT${NC}"
echo -e "${CYAN} Branch: mcp-remote | MCP-Compliant Remote Server${NC}"
echo -e "${CYAN}================================================================${NC}"
echo
}
print_step() {
echo -e "${BLUE}▶ $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
check_prerequisites() {
print_step "Checking prerequisites..."
# Check Docker
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed"
exit 1
fi
# Check Docker Compose v2
if ! docker compose version &> /dev/null; then
print_error "Docker Compose v2 is not available. Install latest Docker with Compose v2 integrated."
exit 1
fi
# Check Docker daemon
if ! docker info &> /dev/null; then
print_error "Docker daemon is not running"
exit 1
fi
print_success "Prerequisites check passed"
}
setup_environment() {
print_step "Setting up environment configuration..."
if [[ ! -f .env ]]; then
if [[ -f .env.example ]]; then
cp .env.example .env
print_success "Copied .env.example to .env"
else
print_error ".env file not found and no .env.example template available"
exit 1
fi
fi
# Validate required environment variables
required_vars=("WAZUH_HOST" "WAZUH_USER" "WAZUH_PASS")
missing_vars=()
for var in "${required_vars[@]}"; do
if ! grep -q "^${var}=" .env || grep -q "^${var}=your-" .env; then
missing_vars+=("$var")
fi
done
if [[ ${#missing_vars[@]} -gt 0 ]]; then
print_error "Please configure the following variables in .env:"
for var in "${missing_vars[@]}"; do
echo " - $var"
done
exit 1
fi
print_success "Environment configuration validated"
}
generate_api_key() {
print_step "Generating API key for client authentication..."
# Generate a secure API key
API_KEY="wazuh_$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-32)"
echo
print_success "Generated API key: ${API_KEY}"
echo -e "${YELLOW}⚠️ Save this API key securely - it won't be shown again${NC}"
echo -e "${CYAN}Use this key to authenticate with the MCP server${NC}"
echo
# Save to a secure file
echo "API_KEY=${API_KEY}" > .api_key
chmod 600 .api_key
print_success "API key saved to .api_key (secure permissions)"
}
build_and_deploy() {
print_step "Building and deploying MCP server..."
# Set build metadata
export BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
export VERSION=${VERSION:-4.0.6}
export PYTHON_VERSION=${PYTHON_VERSION:-3.13}
# Build with Docker Compose v2 latest features
print_step "Building container image..."
docker compose build --pull --parallel --progress=auto
# Deploy with health check wait
print_step "Starting MCP server with health checks..."
docker compose up -d --wait --wait-timeout 120
print_success "MCP server deployed successfully"
}
wait_for_services() {
print_step "Waiting for services to be ready..."
local max_attempts=30
local attempt=1
while [[ $attempt -le $max_attempts ]]; do
if curl -f -s http://localhost:3000/health > /dev/null 2>&1; then
break
fi
echo -n "."
sleep 2
((attempt++))
done
echo
if [[ $attempt -gt $max_attempts ]]; then
print_error "Services failed to start within timeout"
docker compose logs --tail=50
exit 1
fi
print_success "Services are ready"
}
run_health_checks() {
print_step "Running health checks..."
# Check main service
if curl -f -s http://localhost:3000/health | grep -q '"status":"healthy"'; then
print_success "MCP server is healthy"
else
print_error "MCP server health check failed"
return 1
fi
return 0
}
show_deployment_info() {
print_step "Deployment complete! Here's your service information:"
echo
echo -e "${CYAN}🔗 Service URLs:${NC}"
echo " • SSE Server: http://localhost:3000"
echo " • Health Check: http://localhost:3000/health"
echo
echo -e "${CYAN}🔑 Authentication:${NC}"
if [[ -f .api_key ]]; then
echo " • API Key: $(cat .api_key | cut -d= -f2)"
fi
echo " • Get Token: curl -X POST http://localhost:3000/auth/token -d '{\"api_key\":\"YOUR_KEY\"}'"
echo
echo -e "${CYAN}📊 Monitoring:${NC}"
echo " • Container Status: docker compose ps"
echo " • Logs: docker compose logs -f"
echo " • Metrics: http://localhost:3000/metrics"
echo
echo -e "${CYAN}🔧 Management:${NC}"
echo " • Stop: docker compose down"
echo " • Restart: docker compose restart"
echo " • Update: ./deploy.sh"
echo
}
cleanup_on_error() {
print_error "Deployment failed, cleaning up..."
docker compose down --remove-orphans || true
}
main() {
print_header
# Set up error handling
trap cleanup_on_error ERR
# Run deployment steps
check_prerequisites
setup_environment
generate_api_key
build_and_deploy
wait_for_services
if run_health_checks; then
print_success "All health checks passed"
show_deployment_info
else
print_error "Some health checks failed, check logs for details"
docker compose logs --tail=20
exit 1
fi
print_success "Deployment completed successfully!"
}
# Handle script arguments
case "${1:-deploy}" in
"deploy")
main
;;
"stop")
print_step "Stopping services..."
docker compose down
print_success "Services stopped"
;;
"restart")
print_step "Restarting services..."
docker compose restart
print_success "Services restarted"
;;
"logs")
docker compose logs -f
;;
"status")
echo -e "${CYAN}Service Status:${NC}"
docker compose ps --format table
echo -e "\n${CYAN}Container Health:${NC}"
docker inspect wazuh-mcp-server --format='Health: {{.State.Health.Status}}' 2>/dev/null || echo "Health check not available"
echo -e "\n${CYAN}Resource Usage:${NC}"
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}" wazuh-mcp-server
;;
"cleanup")
print_step "Cleaning up resources..."
docker compose down --volumes --remove-orphans --timeout 30
docker system prune -f --volumes
print_success "Cleanup completed"
;;
"build")
print_step "Building container image..."
export BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
docker compose build --pull --parallel --progress=auto
print_success "Build completed"
;;
*)
echo "Usage: $0 {deploy|stop|restart|logs|status|cleanup|build}"
echo "Commands:"
echo " deploy - Deploy the MCP server (default)"
echo " stop - Stop all services"
echo " restart - Restart the MCP server"
echo " logs - Follow service logs"
echo " status - Show service status and resource usage"
echo " cleanup - Stop services and clean up resources"
echo " build - Build container image only"
exit 1
;;
esac