-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·523 lines (445 loc) · 14.2 KB
/
deploy.sh
File metadata and controls
executable file
·523 lines (445 loc) · 14.2 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
#!/bin/bash
# Unified FFmpeg-RTMP Deployment Script
# Deploys master node, worker/edge nodes, or both
# Usage: ./deploy.sh [--master|--worker|--both] [options]
set -e
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Logging functions
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }
log_step() { echo -e "${CYAN}[STEP]${NC} $1"; }
error_exit() {
log_error "$1"
exit 1
}
# Default values
DEPLOY_MODE=""
MASTER_URL=""
API_KEY=""
WORKER_ID=$(hostname)
NON_INTERACTIVE=false
SKIP_BUILD=false
GENERATE_CERTS=false
MASTER_IP=""
MASTER_HOST=""
# Parse command line arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--master)
DEPLOY_MODE="master"
shift
;;
--worker)
DEPLOY_MODE="worker"
shift
;;
--edge)
DEPLOY_MODE="worker"
shift
;;
--both)
DEPLOY_MODE="both"
shift
;;
--master-url)
MASTER_URL="$2"
shift 2
;;
--api-key)
API_KEY="$2"
shift 2
;;
--worker-id)
WORKER_ID="$2"
shift 2
;;
--non-interactive|-y)
NON_INTERACTIVE=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--generate-certs)
GENERATE_CERTS=true
shift
;;
--master-ip)
MASTER_IP="$2"
shift 2
;;
--master-host)
MASTER_HOST="$2"
shift 2
;;
--help|-h)
show_help
exit 0
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
show_help() {
cat << EOF
FFmpeg-RTMP Unified Deployment Script
Usage: ./deploy.sh [OPTIONS]
Deployment Modes:
--master Deploy master node only
--worker, --edge Deploy worker/edge node only
--both Deploy both master and worker on same machine
Configuration Options:
--master-url URL Master server URL (for worker deployment)
--api-key KEY API key for authentication
--worker-id ID Worker identifier (default: hostname)
TLS/Security Options:
--generate-certs Generate TLS certificates before deployment
--master-ip IP Master IP address (for certificate SAN)
--master-host HOST Master hostname (for certificate SAN)
Script Options:
--non-interactive, -y Run without prompts
--skip-build Skip building binaries
--help, -h Show this help message
Examples:
# Interactive mode (prompts for options)
./deploy.sh
# Deploy master node
./deploy.sh --master
# Deploy worker node
./deploy.sh --worker --master-url https://master.example.com:8080 --api-key abc123
# Deploy both on local machine (development)
./deploy.sh --both
# Non-interactive worker deployment
./deploy.sh --worker --master-url https://10.0.0.1:8080 --api-key xyz789 -y
Documentation:
Master: deployment/README.md
Worker: deployment/WORKER_DEPLOYMENT.md
Watch Daemon: deployment/WATCH_DEPLOYMENT.md
EOF
}
# Interactive mode
interactive_mode() {
echo ""
echo "========================================"
echo " FFmpeg-RTMP Deployment Wizard"
echo "========================================"
echo ""
# Ask deployment type
echo "Select deployment type:"
echo " 1) Master node (orchestration, monitoring)"
echo " 2) Worker/Edge node (transcoding, discovery)"
echo " 3) Both (development/testing)"
echo ""
read -p "Choice [1-3]: " choice
case $choice in
1)
DEPLOY_MODE="master"
;;
2)
DEPLOY_MODE="worker"
;;
3)
DEPLOY_MODE="both"
;;
*)
error_exit "Invalid choice"
;;
esac
# Worker-specific configuration
if [[ "$DEPLOY_MODE" == "worker" ]]; then
echo ""
read -p "Master URL (e.g., https://10.0.0.1:8080): " MASTER_URL
read -p "API Key: " API_KEY
read -p "Worker ID [$WORKER_ID]: " input_id
if [[ -n "$input_id" ]]; then
WORKER_ID="$input_id"
fi
fi
# Confirmation
echo ""
echo "Configuration:"
echo " Mode: $DEPLOY_MODE"
if [[ "$DEPLOY_MODE" == "worker" ]]; then
echo " Master URL: $MASTER_URL"
echo " Worker ID: $WORKER_ID"
fi
echo ""
read -p "Proceed with deployment? [y/N]: " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Deployment cancelled"
exit 0
fi
}
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
error_exit "This script must be run as root (use sudo)"
fi
}
# Check prerequisites
check_prerequisites() {
log_step "Checking prerequisites..."
local missing=0
# Check systemd
if ! command -v systemctl &> /dev/null; then
log_error "systemctl not found (systemd required)"
missing=1
else
log_success "systemd found"
fi
# Check Go (if not skipping build)
if [[ "$SKIP_BUILD" == false ]]; then
if ! command -v go &> /dev/null; then
log_warn "Go not found (required for building binaries)"
log_info "Install Go 1.24+ or use --skip-build with pre-built binaries"
missing=1
else
GO_VERSION=$(go version | awk '{print $3}')
log_success "Go found: $GO_VERSION"
fi
fi
# Check cgroups (for worker)
if [[ "$DEPLOY_MODE" == "worker" ]] || [[ "$DEPLOY_MODE" == "both" ]]; then
if mount | grep -q "cgroup2 on /sys/fs/cgroup"; then
log_success "Cgroup v2 mounted"
else
log_error "Cgroup v2 not mounted (required for resource limits)"
missing=1
fi
fi
if [ $missing -ne 0 ]; then
error_exit "Missing required prerequisites"
fi
log_success "All prerequisites satisfied"
}
# ============================================
# Certificate Generation
# ============================================
generate_certificates() {
local mode="$1"
log_info "Generating TLS certificates..."
if [ ! -f "deployment/generate-certs.sh" ]; then
log_error "Certificate generation script not found"
return 1
fi
local cert_args="--$mode --output certs"
# Add master IP/host if provided
if [ -n "$MASTER_IP" ]; then
cert_args="$cert_args --master-ip $MASTER_IP"
fi
if [ -n "$MASTER_HOST" ]; then
cert_args="$cert_args --master-host $MASTER_HOST"
fi
# Generate CA for production
if [ "$NON_INTERACTIVE" = false ]; then
read -p "Generate CA certificate for mTLS? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cert_args="$cert_args --ca"
fi
fi
log_info "Running: ./deployment/generate-certs.sh $cert_args"
./deployment/generate-certs.sh $cert_args || return 1
log_success "Certificates generated in certs/ directory"
return 0
}
# Deploy master node
deploy_master() {
log_step "Deploying Master Node..."
# Generate certificates if requested
if [ "$GENERATE_CERTS" = true ]; then
generate_certificates "master" || error_exit "Certificate generation failed"
echo ""
fi
# Check if master deployment script exists
if [ -f "master/deployment/install-master.sh" ]; then
log_info "Running master installation script..."
bash master/deployment/install-master.sh
else
log_warn "Master deployment script not found, using manual steps..."
# Build master binary
if [[ "$SKIP_BUILD" == false ]]; then
log_info "Building master binary..."
make build-master || error_exit "Failed to build master"
fi
# TODO: Add master installation steps
log_warn "Master installation not yet automated"
log_info "Please refer to deployment/README.md for manual installation"
fi
log_success "Master deployment complete"
}
# Deploy worker node
deploy_worker() {
log_step "Deploying Worker/Edge Node..."
# Generate certificates if requested
if [ "$GENERATE_CERTS" = true ]; then
generate_certificates "worker" || error_exit "Certificate generation failed"
echo ""
fi
# Run edge installation script
if [ -f "deployment/install-edge.sh" ]; then
log_info "Running edge installation script..."
bash deployment/install-edge.sh
# Configure worker
if [[ -n "$MASTER_URL" ]]; then
log_info "Configuring worker connection..."
configure_worker
fi
else
error_exit "Edge installation script not found: deployment/install-edge.sh"
fi
log_success "Worker deployment complete"
}
# Configure worker with master connection
configure_worker() {
local config_file="/etc/ffrtmp/worker.env"
if [ ! -f "$config_file" ]; then
log_error "Worker config not found: $config_file"
return 1
fi
log_info "Updating worker configuration..."
# Update MASTER_URL
if [[ -n "$MASTER_URL" ]]; then
sed -i "s|^MASTER_URL=.*|MASTER_URL=$MASTER_URL|" "$config_file"
log_success "Set MASTER_URL=$MASTER_URL"
fi
# Update API_KEY
if [[ -n "$API_KEY" ]]; then
if grep -q "^MASTER_API_KEY=" "$config_file"; then
sed -i "s|^MASTER_API_KEY=.*|MASTER_API_KEY=$API_KEY|" "$config_file"
else
echo "MASTER_API_KEY=$API_KEY" >> "$config_file"
fi
log_success "Set MASTER_API_KEY"
fi
# Update WORKER_ID
if [[ -n "$WORKER_ID" ]]; then
sed -i "s|^WORKER_ID=.*|WORKER_ID=$WORKER_ID|" "$config_file"
log_success "Set WORKER_ID=$WORKER_ID"
fi
}
# Start services
start_services() {
local mode=$1
log_step "Starting services..."
if [[ "$mode" == "master" ]] || [[ "$mode" == "both" ]]; then
if systemctl list-unit-files | grep -q "ffrtmp-master.service"; then
systemctl enable ffrtmp-master.service
systemctl start ffrtmp-master.service
log_success "Master service started"
fi
fi
if [[ "$mode" == "worker" ]] || [[ "$mode" == "both" ]]; then
if systemctl list-unit-files | grep -q "ffrtmp-worker.service"; then
systemctl enable ffrtmp-worker.service
systemctl start ffrtmp-worker.service
log_success "Worker service started"
fi
if systemctl list-unit-files | grep -q "ffrtmp-watch.service"; then
systemctl enable ffrtmp-watch.service
systemctl start ffrtmp-watch.service
log_success "Watch daemon service started"
fi
fi
}
# Show post-deployment info
show_post_deployment() {
local mode=$1
echo ""
echo "========================================"
echo " Deployment Complete!"
echo "========================================"
echo ""
if [[ "$mode" == "master" ]] || [[ "$mode" == "both" ]]; then
echo -e "${GREEN}Master Node:${NC}"
echo " Status: systemctl status ffrtmp-master"
echo " Logs: journalctl -u ffrtmp-master -f"
echo " Web UI: http://$(hostname -I | awk '{print $1}'):8080"
echo ""
fi
if [[ "$mode" == "worker" ]] || [[ "$mode" == "both" ]]; then
echo -e "${GREEN}Worker Node:${NC}"
echo " Worker Status: systemctl status ffrtmp-worker"
echo " Watch Daemon: systemctl status ffrtmp-watch"
echo " Worker Logs: journalctl -u ffrtmp-worker -f"
echo " Watch Logs: journalctl -u ffrtmp-watch -f"
echo " Config: /etc/ffrtmp/worker.env"
echo ""
fi
echo -e "${BLUE}Documentation:${NC}"
echo " Master: deployment/README.md"
echo " Worker: deployment/WORKER_DEPLOYMENT.md"
echo " Watch: deployment/WATCH_DEPLOYMENT.md"
echo ""
echo -e "${YELLOW}Next Steps:${NC}"
if [[ "$mode" == "worker" ]] && [[ -z "$MASTER_URL" ]]; then
echo " 1. Edit /etc/ffrtmp/worker.env"
echo " 2. Set MASTER_URL and MASTER_API_KEY"
echo " 3. Restart: systemctl restart ffrtmp-worker"
fi
echo ""
}
# Main deployment flow
main() {
# Banner
echo ""
echo "╔════════════════════════════════════════╗"
echo "║ FFmpeg-RTMP Unified Deployment ║"
echo "╚════════════════════════════════════════╝"
echo ""
# Parse arguments
parse_args "$@"
# Check root
check_root
# Interactive mode if no deployment mode specified
if [[ -z "$DEPLOY_MODE" ]]; then
interactive_mode
fi
# Check prerequisites
check_prerequisites
# Deploy based on mode
case "$DEPLOY_MODE" in
master)
deploy_master
;;
worker)
deploy_worker
;;
both)
deploy_master
deploy_worker
;;
*)
error_exit "Invalid deployment mode: $DEPLOY_MODE"
;;
esac
# Start services
if [[ "$NON_INTERACTIVE" == true ]]; then
start_services "$DEPLOY_MODE"
else
echo ""
read -p "Start services now? [Y/n]: " start_now
if [[ ! "$start_now" =~ ^[Nn]$ ]]; then
start_services "$DEPLOY_MODE"
fi
fi
# Show post-deployment info
show_post_deployment "$DEPLOY_MODE"
log_success "Deployment completed successfully!"
}
# Run main
main "$@"