-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·948 lines (818 loc) · 27.2 KB
/
deploy.sh
File metadata and controls
executable file
·948 lines (818 loc) · 27.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#!/bin/bash
# cit.is Deployment Script
# Manages Gunicorn, Redis, and Celery services
set -e
# Configuration
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_DIR="${PROJECT_DIR}/pids"
LOG_DIR="${PROJECT_DIR}/logs"
# Load environment variables if .env exists
if [ -f "${PROJECT_DIR}/.env" ]; then
export $(grep -v '^#' "${PROJECT_DIR}/.env" | xargs)
fi
# Debug function to show environment variables
debug_env() {
echo "Environment Variables Debug:"
echo " CHANGEDETECTION_ENABLED: '${CHANGEDETECTION_ENABLED:-<not set>}'"
echo " CHANGEDETECTION_INTERNAL_URL: '${CHANGEDETECTION_INTERNAL_URL:-<not set>}'"
echo " CHANGEDETECTION_EXTERNAL_URL: '${CHANGEDETECTION_EXTERNAL_URL:-<not set>}'"
echo " CHANGEDETECTION_EXTERNAL_PORT: '${CHANGEDETECTION_EXTERNAL_PORT:-<not set>}'"
echo " CHANGEDETECTION_API_KEY: '${CHANGEDETECTION_API_KEY:-<not set>}'"
echo ""
echo "Legacy variables (for reference):"
echo " CHANGEDETECTION_BASE_URL: '${CHANGEDETECTION_BASE_URL:-<not set>}'"
echo " CHANGEDETECTION_PORT: '${CHANGEDETECTION_PORT:-<not set>}'"
}
# Service configuration
GUNICORN_HOST="127.0.0.1"
GUNICORN_PORT="8998"
GUNICORN_WORKERS="10"
REDIS_PORT="${REDIS_PORT:-6379}"
POSTGRES_PORT="${POSTGRES_PORT:-5433}" # Use 5433 to avoid conflict with system PostgreSQL
# Create directories
mkdir -p "${PID_DIR}" "${LOG_DIR}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log() { echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"; }
warn() { echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING:${NC} $1"; }
error() { echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR:${NC} $1"; }
# Database detection
get_database_type() {
local db_type="${DB_TYPE:-sqlite}"
case "${db_type,,}" in # Convert to lowercase
postgres|postgresql)
echo "postgresql"
;;
sqlite|sqlite3)
echo "sqlite"
;;
*)
echo "sqlite" # Default fallback
;;
esac
}
# Check if we should use Docker for services
use_docker_services() {
# Use Docker if docker-compose.yaml exists and Docker is available
if [ -f "${PROJECT_DIR}/docker-compose.yaml" ] && command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Murder function for killing processes on ports
murder() {
local port=$1
if [ -z "$port" ]; then
error "Usage: murder <port>"
return 1
fi
log "Killing processes on port $port"
# Find and kill processes using the port
local pids=$(lsof -ti:$port 2>/dev/null || true)
if [ -n "$pids" ]; then
for pid in $pids; do
log "Killing PID $pid on port $port"
kill -9 $pid 2>/dev/null || true
done
else
log "No processes found on port $port"
fi
}
# Check if process is running
is_running() {
local pidfile=$1
if [ -f "$pidfile" ]; then
local pid=$(cat "$pidfile")
if kill -0 "$pid" 2>/dev/null; then
return 0
else
rm -f "$pidfile"
return 1
fi
fi
return 1
}
# Start Redis
start_redis() {
local pidfile="${PID_DIR}/redis.pid"
local logfile="${LOG_DIR}/redis.log"
if is_running "$pidfile"; then
warn "Redis is already running (PID: $(cat $pidfile))"
return 0
fi
# Check if Redis is already running on system
if redis-cli -p $REDIS_PORT ping >/dev/null 2>&1; then
log "Redis is already running on system"
return 0
fi
log "Starting Redis server..."
redis-server --daemonize yes \
--port $REDIS_PORT \
--pidfile "$pidfile" \
--logfile "$logfile" \
--dir "${PROJECT_DIR}" \
--save 900 1 \
--save 300 10 \
--save 60 10000
# Wait for Redis to start
sleep 2
if redis-cli -p $REDIS_PORT ping >/dev/null 2>&1; then
log "Redis started successfully"
else
error "Failed to start Redis"
return 1
fi
}
# Stop Redis
stop_redis() {
local pidfile="${PID_DIR}/redis.pid"
if is_running "$pidfile"; then
local pid=$(cat "$pidfile")
log "Stopping Redis (PID: $pid)..."
kill "$pid"
# Wait for shutdown
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
sleep 1
count=$((count + 1))
done
if kill -0 "$pid" 2>/dev/null; then
warn "Redis didn't stop gracefully, force killing..."
kill -9 "$pid"
fi
rm -f "$pidfile"
log "Redis stopped"
else
# Try to stop Docker or system Redis if it's running
if docker exec redis redis-cli ping >/dev/null 2>&1; then
log "Stopping Docker Redis..."
docker stop redis
elif redis-cli -p $REDIS_PORT ping >/dev/null 2>&1; then
log "Stopping system Redis..."
redis-cli -p $REDIS_PORT shutdown
else
warn "Redis is not running"
fi
fi
}
# Start Gunicorn
start_gunicorn() {
local pidfile="${PID_DIR}/gunicorn.pid"
local logfile="${LOG_DIR}/gunicorn.log"
local access_log="${LOG_DIR}/gunicorn_access.log"
if is_running "$pidfile"; then
warn "Gunicorn is already running (PID: $(cat $pidfile))"
return 0
fi
# Kill any existing processes on the port
murder $GUNICORN_PORT
log "Starting Gunicorn server..."
cd "$PROJECT_DIR"
gunicorn citis.wsgi:application \
--bind "${GUNICORN_HOST}:${GUNICORN_PORT}" \
--workers $GUNICORN_WORKERS \
--daemon \
--pid "$pidfile" \
--log-file "$logfile" \
--access-logfile "$access_log" \
--log-level info \
--worker-class sync \
--worker-connections 1000 \
--max-requests 1000 \
--max-requests-jitter 50 \
--timeout 60 \
--keep-alive 2
# Wait for Gunicorn to start
sleep 3
if is_running "$pidfile"; then
log "Gunicorn started successfully on http://${GUNICORN_HOST}:${GUNICORN_PORT}"
else
error "Failed to start Gunicorn"
return 1
fi
}
# Stop Gunicorn
stop_gunicorn() {
local pidfile="${PID_DIR}/gunicorn.pid"
if is_running "$pidfile"; then
local pid=$(cat "$pidfile")
log "Stopping Gunicorn (PID: $pid)..."
kill -TERM "$pid"
# Wait for graceful shutdown
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 15 ]; do
sleep 1
count=$((count + 1))
done
if kill -0 "$pid" 2>/dev/null; then
warn "Gunicorn didn't stop gracefully, force killing..."
kill -9 "$pid"
fi
rm -f "$pidfile"
log "Gunicorn stopped"
else
warn "Gunicorn is not running"
# Kill any processes on the port anyway
murder $GUNICORN_PORT
fi
}
# Start Celery Worker
start_celery() {
local pidfile="${PID_DIR}/celery.pid"
local logfile="${LOG_DIR}/celery.log"
if is_running "$pidfile"; then
warn "Celery worker is already running (PID: $(cat $pidfile))"
return 0
fi
log "Starting Celery worker..."
cd "$PROJECT_DIR"
celery -A citis worker \
--detach \
--pidfile="$pidfile" \
--logfile="$logfile" \
--loglevel=info \
--concurrency=4 \
--max-tasks-per-child=1000 \
--time-limit=300 \
--soft-time-limit=240 \
--queues=archive,assets,analytics,celery
# Wait for Celery to start
sleep 3
if is_running "$pidfile"; then
log "Celery worker started successfully"
else
error "Failed to start Celery worker"
return 1
fi
}
# Stop Celery Worker
stop_celery() {
local pidfile="${PID_DIR}/celery.pid"
if is_running "$pidfile"; then
local pid=$(cat "$pidfile")
log "Stopping Celery worker (PID: $pid)..."
# Send TERM signal for graceful shutdown
kill -TERM "$pid"
# Wait for graceful shutdown
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 30 ]; do
sleep 1
count=$((count + 1))
done
if kill -0 "$pid" 2>/dev/null; then
warn "Celery didn't stop gracefully, force killing..."
kill -9 "$pid"
fi
rm -f "$pidfile"
log "Celery worker stopped"
else
warn "Celery worker is not running"
fi
# Kill any remaining celery processes
pkill -f "celery.*citis" 2>/dev/null || true
}
# Start Celery Beat (optional)
start_beat() {
local pidfile="${PID_DIR}/celery-beat.pid"
local logfile="${LOG_DIR}/celery-beat.log"
if is_running "$pidfile"; then
warn "Celery beat is already running (PID: $(cat $pidfile))"
return 0
fi
log "Starting Celery beat scheduler..."
cd "$PROJECT_DIR"
celery -A citis beat \
--detach \
--pidfile="$pidfile" \
--logfile="$logfile" \
--loglevel=info \
--scheduler=django_celery_beat.schedulers:DatabaseScheduler
sleep 3
if is_running "$pidfile"; then
log "Celery beat started successfully"
else
error "Failed to start Celery beat"
return 1
fi
}
# Stop Celery Beat
stop_beat() {
local pidfile="${PID_DIR}/celery-beat.pid"
if is_running "$pidfile"; then
local pid=$(cat "$pidfile")
log "Stopping Celery beat (PID: $pid)..."
kill -TERM "$pid"
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
sleep 1
count=$((count + 1))
done
if kill -0 "$pid" 2>/dev/null; then
kill -9 "$pid"
fi
rm -f "$pidfile"
log "Celery beat stopped"
else
warn "Celery beat is not running"
fi
}
# Start PostgreSQL (via Docker)
start_postgres() {
if [ "$(get_database_type)" != "postgresql" ]; then
log "PostgreSQL not configured - using $(get_database_type) database"
return 0
fi
if ! use_docker_services; then
warn "Docker not available or docker-compose.yaml missing"
log "Please start PostgreSQL manually or install Docker"
return 1
fi
# Check if PostgreSQL container is already running
if docker ps --format '{{.Names}}' | grep -q "citis_postgres"; then
log "PostgreSQL is already running (Docker container)"
return 0
fi
log "Starting PostgreSQL via Docker Compose..."
cd "$PROJECT_DIR"
# Start only PostgreSQL service
docker compose up -d postgres
# Wait for PostgreSQL to be ready
log "Waiting for PostgreSQL to be ready..."
local count=0
while [ $count -lt 30 ]; do
if docker exec citis_postgres pg_isready -U "${POSTGRES_USER:-citis}" -d "${POSTGRES_DB:-citis}" >/dev/null 2>&1; then
log "PostgreSQL started successfully"
return 0
fi
sleep 2
count=$((count + 1))
done
error "PostgreSQL failed to start within 60 seconds"
return 1
}
# Stop PostgreSQL (via Docker)
stop_postgres() {
if [ "$(get_database_type)" != "postgresql" ]; then
return 0
fi
if ! use_docker_services; then
warn "Docker not available - cannot stop PostgreSQL container"
return 1
fi
if docker ps --format '{{.Names}}' | grep -q "citis_postgres"; then
log "Stopping PostgreSQL..."
cd "$PROJECT_DIR"
docker compose stop postgres
log "PostgreSQL stopped"
else
warn "PostgreSQL container is not running"
fi
}
# Start Redis (updated to handle Docker Compose)
start_redis_updated() {
if ! use_docker_services; then
# Fall back to original Redis start function
start_redis
return $?
fi
# Check if Redis container is already running
if docker ps --format '{{.Names}}' | grep -q "citis_redis"; then
log "Redis is already running (Docker container)"
return 0
fi
log "Starting Redis via Docker Compose..."
cd "$PROJECT_DIR"
# Start only Redis service
docker compose up -d redis
# Wait for Redis to be ready
sleep 3
if docker exec citis_redis redis-cli ping >/dev/null 2>&1; then
log "Redis started successfully"
else
error "Failed to start Redis"
return 1
fi
}
# Stop Redis (updated to handle Docker Compose)
stop_redis_updated() {
if ! use_docker_services; then
# Fall back to original Redis stop function
stop_redis
return $?
fi
if docker ps --format '{{.Names}}' | grep -q "citis_redis"; then
log "Stopping Redis..."
cd "$PROJECT_DIR"
docker compose stop redis
log "Redis stopped"
else
warn "Redis container is not running"
fi
}
# Start ChangeDetection.io (via Docker Compose)
start_changedetection() {
# Check if ChangeDetection.io is enabled (case-insensitive)
local enabled="${CHANGEDETECTION_ENABLED:-false}"
enabled="${enabled,,}" # Convert to lowercase
if [ "$enabled" != "true" ]; then
log "ChangeDetection.io is disabled (CHANGEDETECTION_ENABLED=${CHANGEDETECTION_ENABLED:-false})"
return 0
fi
if ! use_docker_services; then
warn "Docker not available - cannot start ChangeDetection.io container"
return 1
fi
# Check if ChangeDetection.io container is already running
if docker ps --format '{{.Names}}' | grep -q "citis_changedetection"; then
log "ChangeDetection.io is already running"
return 0
fi
log "Starting ChangeDetection.io via Docker Compose..."
cd "$PROJECT_DIR"
# Start ChangeDetection.io with the changedetection profile (Chrome browser optional)
docker compose --profile changedetection up -d changedetection
# Wait for ChangeDetection.io to be ready
log "Waiting for ChangeDetection.io to be ready..."
local count=0
while [ $count -lt 30 ]; do
if curl -f "http://localhost:${CHANGEDETECTION_EXTERNAL_PORT:-5001}/" >/dev/null 2>&1; then
log "ChangeDetection.io started successfully"
# Automatically run setup if API key is configured
if [ -n "${CHANGEDETECTION_API_KEY:-}" ]; then
log "Running ChangeDetection.io webhook setup..."
if python manage.py setup_changedetection --verify; then
log "ChangeDetection.io setup completed successfully"
else
warn "ChangeDetection.io setup verification failed - check configuration"
fi
else
warn "CHANGEDETECTION_API_KEY not configured - skipping automatic setup"
log "Configure your API key and run: python manage.py setup_changedetection"
fi
return 0
fi
sleep 2
count=$((count + 1))
done
error "ChangeDetection.io failed to start within 60 seconds"
return 1
}
# Stop ChangeDetection.io (via Docker Compose)
stop_changedetection() {
# Check if ChangeDetection.io is enabled (case-insensitive)
local enabled="${CHANGEDETECTION_ENABLED:-false}"
enabled="${enabled,,}" # Convert to lowercase
if [ "$enabled" != "true" ]; then
return 0
fi
if ! use_docker_services; then
warn "Docker not available - cannot stop ChangeDetection.io container"
return 1
fi
if docker ps --format '{{.Names}}' | grep -q "citis_changedetection"; then
log "Stopping ChangeDetection.io..."
cd "$PROJECT_DIR"
docker compose --profile changedetection stop changedetection
log "ChangeDetection.io stopped"
else
log "ChangeDetection.io is not running"
fi
}
# Start Chrome browser for ChangeDetection.io (optional)
start_chrome() {
# Check if ChangeDetection.io is enabled
local enabled="${CHANGEDETECTION_ENABLED:-false}"
enabled="${enabled,,}" # Convert to lowercase
if [ "$enabled" != "true" ]; then
log "ChangeDetection.io is disabled - Chrome browser not needed"
return 0
fi
if ! use_docker_services; then
warn "Docker not available - cannot start Chrome browser container"
return 1
fi
# Check if Chrome container is already running
if docker ps --format '{{.Names}}' | grep -q "citis_browser_chrome"; then
log "Chrome browser is already running"
return 0
fi
log "Starting Chrome browser for ChangeDetection.io..."
cd "$PROJECT_DIR"
# Start Chrome browser with the changedetection profile
docker compose --profile changedetection up -d citis-browser-chrome
# Wait for Chrome to be ready
log "Waiting for Chrome browser to be ready..."
local count=0
while [ $count -lt 20 ]; do
if docker exec citis_browser_chrome curl -f "http://localhost:4444/status" >/dev/null 2>&1; then
log "Chrome browser started successfully"
# Update ChangeDetection.io to use Chrome
log "Configuring ChangeDetection.io to use Chrome browser..."
# This would require updating the container environment or config
# For now, just inform the user
log "Chrome browser is available at http://citis-browser-chrome:4444/wd/hub (internal)"
return 0
fi
sleep 3
count=$((count + 1))
done
error "Chrome browser failed to start within 60 seconds"
return 1
}
# Stop Chrome browser
stop_chrome() {
if docker ps --format '{{.Names}}' | grep -q "citis_browser_chrome"; then
log "Stopping Chrome browser..."
cd "$PROJECT_DIR"
docker compose --profile changedetection stop citis-browser-chrome
log "Chrome browser stopped"
else
log "Chrome browser is not running"
fi
}
# Status check
status() {
echo -e "\n${BLUE}=== cit.is Service Status ===${NC}"
# Database status
local db_type=$(get_database_type)
echo -e "Database: ${BLUE}${db_type}${NC}"
if [ "$db_type" = "postgresql" ]; then
if docker exec citis_postgres pg_isready -U "${POSTGRES_USER:-citis}" -d "${POSTGRES_DB:-citis}" >/dev/null 2>&1; then
echo -e "PostgreSQL: ${GREEN}✓ Running${NC} (Docker)"
else
echo -e "PostgreSQL: ${RED}✗ Stopped${NC} (Start with: ./deploy.sh start-db)"
fi
fi
# Redis status
if docker exec citis_redis redis-cli ping >/dev/null 2>&1; then
echo -e "Redis: ${GREEN}✓ Running${NC} (Docker Compose)"
elif docker exec redis redis-cli ping >/dev/null 2>&1; then
echo -e "Redis: ${GREEN}✓ Running${NC} (Docker)"
elif redis-cli -p $REDIS_PORT ping >/dev/null 2>&1; then
echo -e "Redis: ${GREEN}✓ Running${NC} (System)"
else
echo -e "Redis: ${RED}✗ Stopped${NC} (Start with: ./deploy.sh start)"
fi
# Gunicorn
local gunicorn_pidfile="${PID_DIR}/gunicorn.pid"
if is_running "$gunicorn_pidfile"; then
echo -e "Gunicorn: ${GREEN}✓ Running${NC} (PID: $(cat $gunicorn_pidfile))"
echo -e "URL: ${BLUE}http://${GUNICORN_HOST}:${GUNICORN_PORT}${NC}"
else
echo -e "Gunicorn: ${RED}✗ Stopped${NC}"
fi
# Celery Worker
local celery_pidfile="${PID_DIR}/celery.pid"
if is_running "$celery_pidfile"; then
echo -e "Celery Worker: ${GREEN}✓ Running${NC} (PID: $(cat $celery_pidfile))"
else
echo -e "Celery Worker: ${RED}✗ Stopped${NC}"
fi
# Celery Beat
local beat_pidfile="${PID_DIR}/celery-beat.pid"
if is_running "$beat_pidfile"; then
echo -e "Celery Beat: ${GREEN}✓ Running${NC} (PID: $(cat $beat_pidfile))"
else
echo -e "Celery Beat: ${YELLOW}⚬ Stopped${NC} (optional)"
fi
# ChangeDetection.io
local enabled="${CHANGEDETECTION_ENABLED:-false}"
enabled="${enabled,,}" # Convert to lowercase
if [ "$enabled" = "true" ]; then
if docker ps --format '{{.Names}}' | grep -q "citis_changedetection"; then
echo -e "ChangeDetection: ${GREEN}✓ Running${NC} (Docker)"
echo -e "URL: ${BLUE}http://localhost:${CHANGEDETECTION_EXTERNAL_PORT:-5001}${NC}"
else
echo -e "ChangeDetection: ${RED}✗ Stopped${NC} (Start with: ./deploy.sh start-changedetection)"
fi
# Chrome browser status (optional)
if docker ps --format '{{.Names}}' | grep -q "citis_browser_chrome"; then
echo -e "Chrome Browser: ${GREEN}✓ Running${NC} (Internal)"
else
echo -e "Chrome Browser: ${YELLOW}⚬ Stopped${NC} (Optional - start with: ./deploy.sh start-chrome)"
fi
else
echo -e "ChangeDetection: ${YELLOW}⚬ Disabled${NC} (CHANGEDETECTION_ENABLED=${CHANGEDETECTION_ENABLED:-false})"
fi
echo ""
}
# Start all services
start_all() {
log "Starting all cit.is services..."
# Start database if PostgreSQL is configured
if [ "$(get_database_type)" = "postgresql" ]; then
start_postgres || return 1
else
log "Using SQLite database - no database service to start"
fi
# Start Redis
start_redis_updated || {
warn "Failed to start Redis - trying fallback methods..."
if docker exec redis redis-cli ping >/dev/null 2>&1; then
log "Redis is already running (Docker container)"
elif redis-cli -p $REDIS_PORT ping >/dev/null 2>&1; then
log "Redis is already running (system service)"
else
error "Redis is not running and could not be started!"
error "Please start Redis manually: docker start redis or systemctl start redis"
return 1
fi
}
# Start ChangeDetection.io if enabled
start_changedetection
start_gunicorn
start_celery
status
}
# Stop all services
stop_all() {
log "Stopping all cit.is services..."
stop_beat
stop_celery
stop_gunicorn
# Stop ChangeDetection.io if enabled
stop_changedetection
# Stop Redis if using Docker Compose
if use_docker_services; then
stop_redis_updated
else
log "Note: Redis left running - stop manually if needed: docker stop redis or systemctl stop redis"
fi
# Stop PostgreSQL if configured
if [ "$(get_database_type)" = "postgresql" ]; then
stop_postgres
fi
# Clean up any remaining processes
murder $GUNICORN_PORT
log "All services stopped"
}
# Restart all services
restart_all() {
log "Restarting all cit.is services..."
stop_all
sleep 2
start_all
}
# Show logs
logs() {
local service=${1:-"all"}
case $service in
"gunicorn")
tail -f "${LOG_DIR}/gunicorn.log"
;;
"celery")
tail -f "${LOG_DIR}/celery.log"
;;
"all")
tail -f "${LOG_DIR}"/*.log
;;
*)
error "Unknown service: $service"
echo "Available services: gunicorn, celery, all"
echo "For Redis logs: docker logs redis"
;;
esac
}
# Show usage
usage() {
echo "cit.is Deployment Script"
echo ""
echo "Usage: $0 [COMMAND] [OPTIONS]"
echo ""
echo "Commands:"
echo " start Start all services (database, redis, gunicorn, celery)"
echo " stop Stop all services"
echo " restart Restart all services"
echo " status Show service status"
echo " logs [SERVICE] Show logs (gunicorn|celery|all)"
echo ""
echo "Individual service commands:"
echo " start-web Start Gunicorn web server"
echo " stop-web Stop Gunicorn web server"
echo " start-celery Start Celery worker"
echo " stop-celery Stop Celery worker"
echo " start-beat Start Celery beat scheduler"
echo " stop-beat Stop Celery beat scheduler"
echo ""
echo "Database commands:"
echo " start-db Start database (PostgreSQL if configured)"
echo " stop-db Stop database"
echo " start-redis Start Redis"
echo " stop-redis Stop Redis"
echo " start-changedetection Start ChangeDetection.io (if enabled)"
echo " stop-changedetection Stop ChangeDetection.io"
echo " start-chrome Start Chrome browser for ChangeDetection.io (optional)"
echo " stop-chrome Stop Chrome browser"
echo ""
echo "Docker Compose commands (if available):"
echo " docker compose up -d postgres Start PostgreSQL"
echo " docker compose up -d redis Start Redis"
echo " docker compose down Stop all containers"
echo ""
echo "Utility commands:"
echo " murder PORT Kill all processes on specified port"
echo " debug-env Show ChangeDetection.io environment variables"
echo ""
echo "Configuration:"
echo " Database: $(get_database_type)"
echo " Host: $GUNICORN_HOST"
echo " Port: $GUNICORN_PORT"
echo " Workers: $GUNICORN_WORKERS"
echo " Redis Port: $REDIS_PORT"
echo " Postgres Port: $POSTGRES_PORT"
echo " ChangeDetection.io: ${CHANGEDETECTION_ENABLED:-false} (External Port: ${CHANGEDETECTION_EXTERNAL_PORT:-5001})"
}
# Main command handling
case "${1:-}" in
"start")
start_all
;;
"stop")
stop_all
;;
"restart")
restart_all
;;
"status")
status
;;
"logs")
logs "${2:-all}"
;;
"start-web")
start_gunicorn
;;
"stop-web")
stop_gunicorn
;;
"start-celery")
start_celery
;;
"stop-celery")
stop_celery
;;
"start-beat")
start_beat
;;
"stop-beat")
stop_beat
;;
"start-db")
if [ "$(get_database_type)" = "postgresql" ]; then
start_postgres
else
log "Using SQLite database - no database service to start"
fi
;;
"stop-db")
if [ "$(get_database_type)" = "postgresql" ]; then
stop_postgres
else
log "Using SQLite database - no database service to stop"
fi
;;
"start-redis")
start_redis_updated
;;
"stop-redis")
stop_redis_updated
;;
"start-changedetection")
start_changedetection
;;
"stop-changedetection")
stop_changedetection
;;
"start-chrome")
start_chrome
;;
"stop-chrome")
stop_chrome
;;
"murder")
if [ -n "${2:-}" ]; then
murder "$2"
else
error "Port number required"
echo "Usage: $0 murder <port>"
exit 1
fi
;;
"debug-env")
debug_env
;;
""|"-h"|"--help"|"help")
usage
;;
*)
error "Unknown command: $1"
usage
exit 1
;;
esac