This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartup.sh
More file actions
executable file
·1481 lines (1289 loc) · 61 KB
/
startup.sh
File metadata and controls
executable file
·1481 lines (1289 loc) · 61 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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# -----------------------------------------------------------------------------
# startup.sh - IoT SCADA Stack Startup and Breakdown Script (Resilient Raw Podman)
#
# This script manages the full life-cycle of the SCADA stack. It is resilient,
# attempts to start all containers, and reports a summary of failures at the end.
# Container startup output (pulls, errors) is now displayed directly.
#
# USAGE:
# chmod +x startup.sh
# ./startup.sh # DEFAULT: Stops existing, and starts all systems
# ./startup.sh setup # Same as default - stops existing and starts all
# ./startup.sh start <service_name> # Start a specific service manually
# ./startup.sh breakdown # Stop and remove all containers, unmount SMB (keeps volumes)
# ./startup.sh nuke # DESTRUCTIVE: Stop containers, remove ALL volumes, unmount SMB
# -----------------------------------------------------------------------------
# Removed set -e: The script will now continue execution after failed commands.
# --- Configuration ---
ENV_FILE="secrets.env"
CONFIG_FILE=".stack_config"
NETWORK_NAME="iot_net"
VOLUME_LIST=(
"mosquitto_data"
"frigate_data"
"nodered_data"
"z2m_data"
"grafana_data"
"influxdb_data"
"nginx_cache"
"doubletake_data"
"compreface_data"
"go2rtc_data"
"fuxa_data"
"telegraf_data"
)
# Array to track the startup status of each service
declare -A SERVICE_STATUS
# Global variables for podman socket detection (populated by detect_podman_socket function)
DETECTED_PODMAN_SOCKET=""
PODMAN_SOCKET_AVAILABLE="false"
echo "--- IoT SCADA Stack Management Script (Resilient Raw Podman) ---"
echo "Using environment file: ${ENV_FILE}"
# --- Check for required files ---
if [ ! -f "$ENV_FILE" ]; then
echo "ERROR: Missing required file. Ensure '${ENV_FILE}' is in the same directory."
exit 1
fi
# --- Helper function to read variables from the secrets file ---
# Usage: read_var VAR_NAME [DEFAULT_VALUE]
# If DEFAULT_VALUE is provided and variable is empty/missing, returns the default
read_var() {
local var_name="$1"
local default_value="$2"
local value
# The grep command handles the reading, removing potential leading/trailing spaces
value=$(grep "^${var_name}=" "${ENV_FILE}" 2>/dev/null | cut -d'=' -f2- | tr -d '[:space:]')
if [ -z "$value" ] && [ -n "$default_value" ]; then
echo "$default_value"
else
echo "$value"
fi
}
# --- Helper function to require a variable (abort if missing) ---
# Usage: require_var VAR_NAME VAR_VALUE DESCRIPTION
# Aborts execution if the variable is empty or missing
require_var() {
local var_name="$1"
local var_value="$2"
local description="$3"
if [ -z "$var_value" ]; then
MISSING_VARS+=("${var_name} - ${description}")
fi
}
# Array to track missing required variables
declare -a MISSING_VARS=()
# ----------------------------------------------------------------------
# --- PODMAN SOCKET DETECTION AND VALIDATION ---
# ----------------------------------------------------------------------
# --- Function to detect and validate the podman socket for the current user ---
# This function implements resilient socket detection to prevent Node-RED from crashing
# if the socket is missing or inaccessible.
detect_podman_socket() {
echo ""
echo "--- Podman Socket Detection ---"
# Read the PODMAN_SOCKET_PATH from secrets.env if it exists
local configured_socket=$(read_var PODMAN_SOCKET_PATH 2>/dev/null || echo "")
# Common socket paths to check (in priority order)
# 1. User-provided path from secrets.env
# 2. Rootless socket for current user (most common on modern systems)
# 3. Rootful system socket (less common for home setups)
local socket_candidates=()
if [ -n "$configured_socket" ]; then
socket_candidates+=("$configured_socket")
fi
# Dynamically detect rootless socket path for current user
local current_user_uid=$(id -u)
local rootless_socket="/run/user/${current_user_uid}/podman/podman.sock"
socket_candidates+=("$rootless_socket")
# Add common rootful socket path
socket_candidates+=("/run/podman/podman.sock")
echo "Searching for podman socket in the following locations:"
for candidate in "${socket_candidates[@]}"; do
echo " - ${candidate}"
done
echo ""
# Try each candidate socket and validate it
for socket_path in "${socket_candidates[@]}"; do
# Check if the socket file exists
if [ ! -e "$socket_path" ]; then
echo " [SKIP] Socket not found: ${socket_path}"
continue
fi
# Check if it's actually a socket file (not a regular file or directory)
if [ ! -S "$socket_path" ]; then
echo " [SKIP] Not a socket: ${socket_path}"
continue
fi
# Check if the socket is readable (basic permission check)
if [ ! -r "$socket_path" ]; then
echo " [SKIP] Socket not readable: ${socket_path}"
continue
fi
# More robust permission check: try to stat the socket
# This catches cases where -r passes but actual access fails
if ! stat "$socket_path" >/dev/null 2>&1; then
echo " [SKIP] Socket not accessible (permission denied): ${socket_path}"
continue
fi
# Socket exists, is a socket type, and is accessible - this is our winner!
echo " [SUCCESS] Found valid podman socket: ${socket_path}"
echo ""
# Export the detected socket path for use by other functions
DETECTED_PODMAN_SOCKET="$socket_path"
PODMAN_SOCKET_AVAILABLE="true"
return 0
done
# No valid socket found
echo " [WARNING] No valid podman socket detected."
echo " Node-RED will start WITHOUT podman/docker integration."
echo " To enable podman integration, ensure the podman socket is available:"
echo " - For rootless: systemctl --user enable --now podman.socket"
echo " - For rootful: systemctl enable --now podman.socket"
echo ""
DETECTED_PODMAN_SOCKET=""
PODMAN_SOCKET_AVAILABLE="false"
return 1
}
# --- Read variables for services and SMB mount ---
# Variables with defaults (non-sensitive configuration)
FRIGATE_PORT=$(read_var FRIGATE_PORT "5000")
NODERED_PORT=$(read_var NODERED_PORT "1880")
FRIGATE_RECORDINGS_HOST_PATH=$(read_var FRIGATE_RECORDINGS_HOST_PATH)
SMB_SERVER=$(read_var SMB_SERVER)
SMB_SHARE=$(read_var SMB_SHARE)
SMB_USER=$(read_var SMB_USER)
SMB_PASS=$(read_var SMB_PASS)
ZIGBEE_DEVICE_PATH=$(read_var ZIGBEE_DEVICE_PATH)
PODMAN_SOCKET_PATH=$(read_var PODMAN_SOCKET_PATH)
CURRENT_UID=$(id -u)
INFLUXDB_ADMIN_USER=$(read_var INFLUXDB_ADMIN_USER "influx_admin")
INFLUXDB_ADMIN_PASSWORD=$(read_var INFLUXDB_ADMIN_PASSWORD)
INFLUXDB_ORG=$(read_var INFLUXDB_ORG "home_org")
INFLUXDB_BUCKET=$(read_var INFLUXDB_BUCKET "iot_scada_data")
INFLUXDB_ADMIN_TOKEN=$(read_var INFLUXDB_ADMIN_TOKEN)
GRAFANA_ADMIN_USER=$(read_var GRAFANA_ADMIN_USER "admin")
GRAFANA_ADMIN_PASSWORD=$(read_var GRAFANA_ADMIN_PASSWORD)
GRAFANA_SECRET_KEY=$(read_var GRAFANA_SECRET_KEY)
GRAFANA_ANONYMOUS_ENABLED=$(read_var GRAFANA_ANONYMOUS_ENABLED "false")
GRAFANA_ANONYMOUS_ORG_NAME=$(read_var GRAFANA_ANONYMOUS_ORG_NAME "Main Org.")
GRAFANA_ANONYMOUS_ORG_ROLE=$(read_var GRAFANA_ANONYMOUS_ORG_ROLE "Viewer")
MQTT_USER=$(read_var MQTT_USER "mqtt_user")
MQTT_PASSWORD=$(read_var MQTT_PASSWORD)
TZ=$(read_var TZ "UTC")
BASE_DOMAIN=$(read_var BASE_DOMAIN "home.local")
GRAFANA_HOSTNAME=$(read_var GRAFANA_HOSTNAME "grafana")
FRIGATE_HOSTNAME=$(read_var FRIGATE_HOSTNAME "frigate")
NODERED_HOSTNAME=$(read_var NODERED_HOSTNAME "nodered")
ZIGBEE2MQTT_HOSTNAME=$(read_var ZIGBEE2MQTT_HOSTNAME "zigbee")
COCKPIT_HOSTNAME=$(read_var COCKPIT_HOSTNAME "cockpit")
DOUBLETAKE_HOSTNAME=$(read_var DOUBLETAKE_HOSTNAME "doubletake")
COMPREFACE_HOSTNAME=$(read_var COMPREFACE_HOSTNAME "compreface")
COMPREFACE_API_KEY=$(read_var COMPREFACE_API_KEY)
GO2RTC_HOSTNAME=$(read_var GO2RTC_HOSTNAME "go2rtc")
# --- Validate required variables (passwords/secrets) ---
# These are critical secrets that must be set - abort if missing
require_var "MQTT_PASSWORD" "$MQTT_PASSWORD" "MQTT broker password"
require_var "INFLUXDB_ADMIN_PASSWORD" "$INFLUXDB_ADMIN_PASSWORD" "InfluxDB admin password"
require_var "INFLUXDB_ADMIN_TOKEN" "$INFLUXDB_ADMIN_TOKEN" "InfluxDB API token"
require_var "GRAFANA_ADMIN_PASSWORD" "$GRAFANA_ADMIN_PASSWORD" "Grafana admin password"
require_var "GRAFANA_SECRET_KEY" "$GRAFANA_SECRET_KEY" "Grafana secret key for sessions"
# Check if any required variables are missing and abort if so
if [ ${#MISSING_VARS[@]} -gt 0 ]; then
echo ""
echo "================================================================"
echo " MISSING REQUIRED CONFIGURATION "
echo "================================================================"
echo ""
echo "ERROR: The following required variables are missing from ${ENV_FILE}:"
echo ""
for var in "${MISSING_VARS[@]}"; do
echo " - $var"
done
echo ""
echo "These are security-critical settings that must be configured."
echo "Please edit ${ENV_FILE} and set all required values."
echo ""
echo "TIP: Run './create_secrets.sh' to auto-generate secure passwords."
echo "================================================================"
exit 1
fi
# ----------------------------------------------------------------------
# --- NGINX CONFIGURATION GENERATION ---
# ----------------------------------------------------------------------
# --- Function to generate nginx configuration based on stack type ---
generate_nginx_config() {
local stack_type=$1
local nginx_conf_file="./nginx/nginx.conf"
echo "Generating nginx configuration for stack type: ${stack_type}..."
cat > "${nginx_conf_file}" << 'NGINX_EOF'
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Default server - serve landing page
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.(css|js|jpg|jpeg|png|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
NGINX_EOF
# Add service configurations based on stack type
if [ "$stack_type" == "iot_only" ] || [ "$stack_type" == "iot_nvr" ]; then
cat >> "${nginx_conf_file}" << NGINX_EOF
# Grafana
server {
listen 80;
server_name ${GRAFANA_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://grafana:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
# Node-RED
server {
listen 80;
server_name ${NODERED_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://nodered:1880;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# Zigbee2MQTT
server {
listen 80;
server_name ${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://zigbee2mqtt:8080;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
NGINX_EOF
fi
if [ "$stack_type" == "nvr_only" ] || [ "$stack_type" == "iot_nvr" ]; then
cat >> "${nginx_conf_file}" << NGINX_EOF
# Frigate NVR
server {
listen 80;
server_name ${FRIGATE_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://frigate:5000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
# Double-Take (Facial Recognition for Frigate)
server {
listen 80;
server_name ${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://doubletake:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
NGINX_EOF
fi
# Add Cockpit (openSUSE web console) proxy - assuming it runs on host
cat >> "${nginx_conf_file}" << NGINX_EOF
# openSUSE Cockpit Web Console
server {
listen 80;
server_name ${COCKPIT_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass https://host.containers.internal:9090;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_ssl_verify off;
}
}
}
NGINX_EOF
# Update the services list in the landing page HTML
local services_html=""
if [ "$stack_type" == "iot_only" ] || [ "$stack_type" == "iot_nvr" ]; then
services_html+="<a href=\"http://${GRAFANA_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Grafana</h3>"
services_html+="<p>Data visualization and monitoring dashboards</p>"
services_html+="<span class=\"service-url\">${GRAFANA_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
services_html+="<a href=\"http://${NODERED_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Node-RED</h3>"
services_html+="<p>Flow-based automation and IoT integration</p>"
services_html+="<span class=\"service-url\">${NODERED_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
services_html+="<a href=\"http://${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Zigbee2MQTT</h3>"
services_html+="<p>Zigbee device control and management</p>"
services_html+="<span class=\"service-url\">${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
fi
if [ "$stack_type" == "nvr_only" ] || [ "$stack_type" == "iot_nvr" ]; then
services_html+="<a href=\"http://${FRIGATE_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Frigate NVR</h3>"
services_html+="<p>Network video recorder with object detection</p>"
services_html+="<span class=\"service-url\">${FRIGATE_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
services_html+="<a href=\"http://${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Double-Take</h3>"
services_html+="<p>Facial recognition for Frigate</p>"
services_html+="<span class=\"service-url\">${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
fi
services_html+="<a href=\"http://${COCKPIT_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>openSUSE Cockpit</h3>"
services_html+="<p>System management and monitoring console</p>"
services_html+="<span class=\"service-url\">${COCKPIT_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
# Copy template and update the HTML file with the services list
local html_template="./nginx/index.html.template"
local html_file="./nginx/index.html"
if [ ! -f "${html_template}" ]; then
echo "ERROR: HTML template file not found at ${html_template}"
return 1
fi
cp "${html_template}" "${html_file}"
sed -i "s|SERVICES_LIST|${services_html}|g" "${html_file}"
echo "Nginx configuration generated at ${nginx_conf_file}"
}
# --- Function to generate nginx config based on actually running services ---
generate_nginx_config_from_running_services() {
local nginx_conf_file="./nginx/nginx.conf"
echo "Checking which services are running and generating nginx configuration..."
# Check which services are actually running
local running_services=$(podman ps --format '{{.Names}}' 2>/dev/null || echo "")
# Start building the nginx config
cat > "${nginx_conf_file}" << 'NGINX_EOF'
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Default server - serve landing page
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.(css|js|jpg|jpeg|png|gif|ico|svg)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}
}
NGINX_EOF
local services_html=""
# Check and add Grafana if running
if echo "$running_services" | grep -q "^grafana$"; then
echo " [ok] Grafana is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# Grafana
server {
listen 80;
server_name ${GRAFANA_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://grafana:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
NGINX_EOF
services_html+="<a href=\"http://${GRAFANA_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Grafana</h3>"
services_html+="<p>Data visualization and monitoring dashboards</p>"
services_html+="<span class=\"service-url\">${GRAFANA_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] Grafana is not running - skipping from nginx config"
fi
# Check and add Node-RED if running
if echo "$running_services" | grep -q "^nodered$"; then
echo " [ok] Node-RED is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# Node-RED
server {
listen 80;
server_name ${NODERED_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://nodered:1880;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
NGINX_EOF
services_html+="<a href=\"http://${NODERED_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Node-RED</h3>"
services_html+="<p>Flow-based automation and IoT integration</p>"
services_html+="<span class=\"service-url\">${NODERED_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] Node-RED is not running - skipping from nginx config"
fi
# Check and add Zigbee2MQTT if running
if echo "$running_services" | grep -q "^zigbee2mqtt$"; then
echo " [ok] Zigbee2MQTT is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# Zigbee2MQTT
server {
listen 80;
server_name ${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://zigbee2mqtt:8080;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
NGINX_EOF
services_html+="<a href=\"http://${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Zigbee2MQTT</h3>"
services_html+="<p>Zigbee device control and management</p>"
services_html+="<span class=\"service-url\">${ZIGBEE2MQTT_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] Zigbee2MQTT is not running - skipping from nginx config"
fi
# Check and add Frigate if running
if echo "$running_services" | grep -q "^frigate$"; then
echo " [ok] Frigate is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# Frigate NVR
server {
listen 80;
server_name ${FRIGATE_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://frigate:5000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
NGINX_EOF
services_html+="<a href=\"http://${FRIGATE_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Frigate NVR</h3>"
services_html+="<p>Network video recorder with object detection</p>"
services_html+="<span class=\"service-url\">${FRIGATE_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] Frigate is not running - skipping from nginx config"
fi
# Check and add Double-Take if running
if echo "$running_services" | grep -q "^doubletake$"; then
echo " [ok] Double-Take is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# Double-Take (Facial Recognition for Frigate)
server {
listen 80;
server_name ${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://doubletake:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
NGINX_EOF
services_html+="<a href=\"http://${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>Double-Take</h3>"
services_html+="<p>Facial recognition for Frigate</p>"
services_html+="<span class=\"service-url\">${DOUBLETAKE_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] Double-Take is not running - skipping from nginx config"
fi
# Check and add CompreFace if running (Fixes #5)
if echo "$running_services" | grep -q "^compreface$"; then
echo " [ok] CompreFace is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# CompreFace (Face Recognition API for Double-Take)
server {
listen 80;
server_name ${COMPREFACE_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://compreface:8000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
NGINX_EOF
services_html+="<a href=\"http://${COMPREFACE_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>CompreFace</h3>"
services_html+="<p>Face recognition API for Double-Take</p>"
services_html+="<span class=\"service-url\">${COMPREFACE_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] CompreFace is not running - skipping from nginx config"
fi
# Check and add go2rtc if running (Fixes #15)
if echo "$running_services" | grep -q "^go2rtc$"; then
echo " [ok] go2rtc is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# go2rtc (RTSP to WebRTC/HLS converter for Grafana dashboards)
server {
listen 80;
server_name ${GO2RTC_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://go2rtc:1984;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
NGINX_EOF
services_html+="<a href=\"http://${GO2RTC_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>go2rtc</h3>"
services_html+="<p>RTSP stream converter for Grafana dashboards</p>"
services_html+="<span class=\"service-url\">${GO2RTC_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] go2rtc is not running - skipping from nginx config"
fi
# Check and add FUXA if running
if echo "$running_services" | grep -q "^fuxa$"; then
echo " [ok] FUXA is running - adding to nginx config"
cat >> "${nginx_conf_file}" << NGINX_EOF
# FUXA (HMI/SCADA Interface)
server {
listen 80;
server_name ${FUXA_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass http://fuxa:1881;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
NGINX_EOF
services_html+="<a href=\"http://${FUXA_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>FUXA</h3>"
services_html+="<p>Web-based HMI/SCADA visual interface</p>"
services_html+="<span class=\"service-url\">${FUXA_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
else
echo " [INFO] FUXA is not running - skipping from nginx config"
fi
# Check and add InfluxDB if running
if echo "$running_services" | grep -q "^influxdb$"; then
echo " [ok] InfluxDB is running (available on port 8086)"
else
echo " [INFO] InfluxDB is not running"
fi
# Check and add Mosquitto if running
if echo "$running_services" | grep -q "^mosquitto$"; then
echo " [ok] Mosquitto is running (available on port 1883)"
else
echo " [INFO] Mosquitto is not running"
fi
# Always add Cockpit (openSUSE web console) proxy - assuming it runs on host
cat >> "${nginx_conf_file}" << NGINX_EOF
# openSUSE Cockpit Web Console
server {
listen 80;
server_name ${COCKPIT_HOSTNAME}.${BASE_DOMAIN};
location / {
proxy_pass https://host.containers.internal:9090;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_ssl_verify off;
}
}
}
NGINX_EOF
services_html+="<a href=\"http://${COCKPIT_HOSTNAME}.${BASE_DOMAIN}\" class=\"service-card\">"
services_html+="<h3>openSUSE Cockpit</h3>"
services_html+="<p>System management and monitoring console</p>"
services_html+="<span class=\"service-url\">${COCKPIT_HOSTNAME}.${BASE_DOMAIN}</span>"
services_html+="</a>"
# Copy template and update the HTML file with the services list
local html_template="./nginx/index.html.template"
local html_file="./nginx/index.html"
if [ ! -f "${html_template}" ]; then
echo "ERROR: HTML template file not found at ${html_template}"
return 1
fi
cp "${html_template}" "${html_file}"
sed -i "s|SERVICES_LIST|${services_html}|g" "${html_file}"
echo "Nginx configuration generated at ${nginx_conf_file} based on running services"
}
# --- Function to check nginx.conf permissions ---
check_and_fix_nginx_permissions() {
local nginx_conf_file="./nginx/nginx.conf"
if [ ! -f "${nginx_conf_file}" ]; then
echo "WARNING: nginx.conf not found at ${nginx_conf_file}. It will be generated."
return 0
fi
echo ""
echo "Checking nginx.conf permissions..."
# Check file permissions
local file_perms=$(stat -c "%a" "${nginx_conf_file}" 2>/dev/null || stat -f "%OLp" "${nginx_conf_file}" 2>/dev/null)
local file_owner=$(stat -c "%u" "${nginx_conf_file}" 2>/dev/null || stat -f "%u" "${nginx_conf_file}" 2>/dev/null)
local current_uid=$(id -u)
echo " File permissions: ${file_perms}"
echo " File owner UID: ${file_owner}"
echo " Current user UID: ${current_uid}"
# Warn if permissions are not 644 or more restrictive
if [ "${file_perms}" != "644" ] && [ "${file_perms}" != "444" ] && [ "${file_perms}" != "400" ] && [ "${file_perms}" != "600" ]; then
echo " [WARNING] File permissions are ${file_perms}. Recommended: 644"
echo " To fix: chmod 644 ${nginx_conf_file}"
else
echo " [ok] File permissions are acceptable"
fi
# Warn if file is not owned by current user
if [ "${file_owner}" != "${current_uid}" ]; then
echo " [WARNING] File is owned by UID ${file_owner}, but current user is UID ${current_uid}"
echo " To fix: chown ${current_uid} ${nginx_conf_file}"
else
echo " [ok] File ownership is correct"
fi
echo ""
}
# ----------------------------------------------------------------------
# --- FIRST-RUN CONFIGURATION ---
# ----------------------------------------------------------------------
# --- Function to check available memory ---
check_memory() {
# Get total memory in GiB
local total_mem_gib=$(free -g | awk '/^Mem:/{print $2}')
echo "$total_mem_gib"
}
# --- Function to display memory warning for NVR ---
show_nvr_memory_warning() {
local mem_gib=$(check_memory)
if [ "$mem_gib" -lt 8 ]; then
echo ""
echo "================================================================"
echo " MEMORY WARNING "
echo "================================================================"
echo "WARNING: Your system has ${mem_gib}GiB of total RAM."
echo "Frigate NVR requires a minimum of 8GiB RAM for optimal operation."
echo "Running with insufficient memory may lead to performance issues."
echo "================================================================"
echo ""
sleep 3 # Give user time to read the warning
fi
}
# --- Function to save stack configuration ---
save_stack_config() {
local config_choice=$1
echo "STACK_TYPE=${config_choice}" > "${CONFIG_FILE}"
echo "Configuration saved to ${CONFIG_FILE}"
}
# --- Function to read stack configuration ---
read_stack_config() {
if [ -f "${CONFIG_FILE}" ]; then
grep "^STACK_TYPE=" "${CONFIG_FILE}" | cut -d'=' -f2
else
echo ""
fi
}
# --- First-run configuration menu ---
first_run_configuration() {
echo ""
echo "================================================================"
echo " FIRST-RUN CONFIGURATION "
echo "================================================================"
echo ""
echo "Welcome to the Home IoT SCADA Stack setup!"
echo ""
echo "Please choose your stack configuration:"
echo ""
echo " 1) IoT/SCADA Stack only (Mosquitto, InfluxDB, Grafana, Node-RED, Zigbee2MQTT)"
echo " 2) NVR only (Frigate for camera recording)"
echo " 3) Both IoT/SCADA Stack + NVR (All services)"
echo ""
echo -n "Enter your choice (1, 2, or 3): "
read -r choice
case "$choice" in
1)
echo ""
echo "Selected: IoT/SCADA Stack only"
save_stack_config "iot_only"
;;
2)
echo ""
echo "Selected: NVR only"
show_nvr_memory_warning
save_stack_config "nvr_only"
;;
3)
echo ""
echo "Selected: Both IoT/SCADA Stack + NVR"
show_nvr_memory_warning
save_stack_config "iot_nvr"
;;
*)
echo ""
echo "Invalid choice. Please run the setup again and select 1, 2, or 3."
exit 1
;;
esac
echo ""
echo "Generating secrets automatically..."
if [ -x "./create_secrets.sh" ]; then
./create_secrets.sh
if [ $? -ne 0 ]; then
echo "ERROR: Failed to generate secrets. Please check create_secrets.sh"
exit 1
fi
else
echo "ERROR: create_secrets.sh not found or not executable"
exit 1
fi
echo ""
echo "First-run configuration complete!"
echo ""
}
# --- Check if this is first run ---
check_first_run() {
if [ ! -f "${CONFIG_FILE}" ]; then
# First run - do configuration
first_run_configuration
# Re-read variables after secrets are generated (with defaults for non-sensitive values)
if [ -f "$ENV_FILE" ]; then
FRIGATE_PORT=$(read_var FRIGATE_PORT "5000")
NODERED_PORT=$(read_var NODERED_PORT "1880")
FRIGATE_RECORDINGS_HOST_PATH=$(read_var FRIGATE_RECORDINGS_HOST_PATH)
SMB_SERVER=$(read_var SMB_SERVER)
SMB_SHARE=$(read_var SMB_SHARE)
SMB_USER=$(read_var SMB_USER)
SMB_PASS=$(read_var SMB_PASS)
ZIGBEE_DEVICE_PATH=$(read_var ZIGBEE_DEVICE_PATH)
PODMAN_SOCKET_PATH=$(read_var PODMAN_SOCKET_PATH)
CURRENT_UID=$(id -u)
INFLUXDB_ADMIN_USER=$(read_var INFLUXDB_ADMIN_USER "influx_admin")
INFLUXDB_ADMIN_PASSWORD=$(read_var INFLUXDB_ADMIN_PASSWORD)
INFLUXDB_ORG=$(read_var INFLUXDB_ORG "home_org")
INFLUXDB_BUCKET=$(read_var INFLUXDB_BUCKET "iot_scada_data")
INFLUXDB_ADMIN_TOKEN=$(read_var INFLUXDB_ADMIN_TOKEN)
GRAFANA_ADMIN_USER=$(read_var GRAFANA_ADMIN_USER "admin")
GRAFANA_ADMIN_PASSWORD=$(read_var GRAFANA_ADMIN_PASSWORD)
GRAFANA_SECRET_KEY=$(read_var GRAFANA_SECRET_KEY)
GRAFANA_ANONYMOUS_ENABLED=$(read_var GRAFANA_ANONYMOUS_ENABLED "false")
GRAFANA_ANONYMOUS_ORG_NAME=$(read_var GRAFANA_ANONYMOUS_ORG_NAME "Main Org.")
GRAFANA_ANONYMOUS_ORG_ROLE=$(read_var GRAFANA_ANONYMOUS_ORG_ROLE "Viewer")
MQTT_USER=$(read_var MQTT_USER "mqtt_user")
MQTT_PASSWORD=$(read_var MQTT_PASSWORD)
TZ=$(read_var TZ "UTC")
BASE_DOMAIN=$(read_var BASE_DOMAIN "home.local")
GRAFANA_HOSTNAME=$(read_var GRAFANA_HOSTNAME "grafana")
FRIGATE_HOSTNAME=$(read_var FRIGATE_HOSTNAME "frigate")
NODERED_HOSTNAME=$(read_var NODERED_HOSTNAME "nodered")
ZIGBEE2MQTT_HOSTNAME=$(read_var ZIGBEE2MQTT_HOSTNAME "zigbee")
COCKPIT_HOSTNAME=$(read_var COCKPIT_HOSTNAME "cockpit")
DOUBLETAKE_HOSTNAME=$(read_var DOUBLETAKE_HOSTNAME "doubletake")
COMPREFACE_HOSTNAME=$(read_var COMPREFACE_HOSTNAME "compreface")
COMPREFACE_API_KEY=$(read_var COMPREFACE_API_KEY)
GO2RTC_HOSTNAME=$(read_var GO2RTC_HOSTNAME "go2rtc")
fi
fi
}
# ----------------------------------------------------------------------
# --- CORE FUNCTIONS ---
# ----------------------------------------------------------------------
# --- Unmount SMB Share ---
unmount_smb_share() {
echo ""
echo "Checking and unmounting SMB share..."
if mountpoint -q "${FRIGATE_RECORDINGS_HOST_PATH}"; then
echo "Unmounting ${FRIGATE_RECORDINGS_HOST_PATH}..."
sudo umount "${FRIGATE_RECORDINGS_HOST_PATH}" || { echo "WARNING: Could not unmount SMB share. May be in use."; }
echo "SMB share unmounted."
else
echo "SMB share is not mounted at ${FRIGATE_RECORDINGS_HOST_PATH}. Skipping unmount."
fi
}
# --- Mount SMB Share for Frigate Recordings ---
mount_smb_share() {
echo ""