Repository navigation
Expand file tree
/
Copy pathentrypoint-unified.sh
More file actions
executable file
·985 lines (870 loc) · 38.6 KB
/
Copy pathentrypoint-unified.sh
File metadata and controls
executable file
·985 lines (870 loc) · 38.6 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
#!/bin/bash
set -e
# omada controller unified entrypoint script for versions 5.x and 6.x
# supports both root and rootless modes
# ============================================================================
# SHARED FUNCTIONS
# ============================================================================
setup_environment() {
export TZ
TZ="${TZ:-Etc/UTC}"
SHOW_SERVER_LOGS="${SHOW_SERVER_LOGS:-true}"
SHOW_MONGODB_LOGS="${SHOW_MONGODB_LOGS:-false}"
SSL_CERT_NAME="${SSL_CERT_NAME:-tls.crt}"
SSL_KEY_NAME="${SSL_KEY_NAME:-tls.key}"
SKIP_USERLAND_KERNEL_CHECK="${SKIP_USERLAND_KERNEL_CHECK:-false}"
# PORTS CONFIGURATION
MANAGE_HTTP_PORT="${MANAGE_HTTP_PORT:-8088}"
MANAGE_HTTPS_PORT="${MANAGE_HTTPS_PORT:-8043}"
PORTAL_HTTP_PORT="${PORTAL_HTTP_PORT:-8088}"
PORTAL_HTTPS_PORT="${PORTAL_HTTPS_PORT:-8843}"
PORT_ADOPT_V1="${PORT_ADOPT_V1:-29812}"
PORT_APP_DISCOVERY="${PORT_APP_DISCOVERY:-27001}"
PORT_UPGRADE_V1="${PORT_UPGRADE_V1:-29813}"
PORT_MANAGER_V1="${PORT_MANAGER_V1:-29811}"
PORT_MANAGER_V2="${PORT_MANAGER_V2:-29814}"
PORT_DISCOVERY="${PORT_DISCOVERY:-29810}"
PORT_TRANSFER_V2="${PORT_TRANSFER_V2:-29815}"
PORT_RTTY="${PORT_RTTY:-29816}"
PORT_DEVICE_MONITOR="${PORT_DEVICE_MONITOR:-29817}"
# defaults for the upgrade ports are version dependent; set in configure_upgrade_ports
UPGRADE_HTTPS_PORT="${UPGRADE_HTTPS_PORT:-}"
UPGRADE_ES_HTTPS_PORT="${UPGRADE_ES_HTTPS_PORT:-}"
# skip the 6.4 upgrade precheck (only if you REALLY know what you're doing)
SKIP_6_4_UPGRADE_PRECHECK="${SKIP_6_4_UPGRADE_PRECHECK:-false}"
# WEB CONFIG OVERRIDE
WEB_CONFIG_OVERRIDE="${WEB_CONFIG_OVERRIDE:-false}"
# EXTERNAL MONGODB
MONGO_EXTERNAL="${MONGO_EXTERNAL:-false}"
EAP_MONGOD_URI="${EAP_MONGOD_URI:-mongodb://127.0.0.1:27217/omada}"
# escape & for eval
EAP_MONGOD_URI="$(eval echo "${EAP_MONGOD_URI//&/\\&}")"
# escape after eval as well for sed
EAP_MONGOD_URI="${EAP_MONGOD_URI//&/\\&}"
# EXTRA ARGS for embedded MongoDB (appended to every mongod invocation)
MONGOD_EXTRA_ARGS="${MONGOD_EXTRA_ARGS:-}"
# JAVA HEAP OVERRIDES (replace hardcoded -Xmx/-Xms in the CMD)
JAVA_MAX_HEAP_SIZE="${JAVA_MAX_HEAP_SIZE:-}"
JAVA_MIN_HEAP_SIZE="${JAVA_MIN_HEAP_SIZE:-}"
# SPRING BOOT APPLICATION PROPERTIES (written to classpath as application.properties)
APPLICATION_PROPERTIES="${APPLICATION_PROPERTIES:-}"
}
restore_properties_files() {
DEFAULT_FILES="/opt/tplink/EAPController/properties.defaults/*"
for FILE in ${DEFAULT_FILES}
do
BASENAME=$(basename "${FILE}")
if [ ! -f "/opt/tplink/EAPController/properties/${BASENAME}" ]
then
echo "INFO: Properties file '${BASENAME}' missing, restoring default file..."
cp "${FILE}" "/opt/tplink/EAPController/properties/${BASENAME}"
chown "${PUSERNAME}:${PGROUP}" "/opt/tplink/EAPController/properties/${BASENAME}"
fi
done
}
setup_directories() {
# make sure that the html directory exists
if [ ! -d "/opt/tplink/EAPController/data/html" ] && [ -f "/opt/tplink/EAPController/data-html.tar.gz" ]
then
# missing directory; extract from original
echo "INFO: Report HTML directory missing; extracting backup to '/opt/tplink/EAPController/data/html'"
tar zxvf /opt/tplink/EAPController/data-html.tar.gz -C /opt/tplink/EAPController/data
chown -R "${PUSERNAME}:${PGROUP}" /opt/tplink/EAPController/data/html
fi
# make sure that the pdf directory exists
if [ ! -d "/opt/tplink/EAPController/data/pdf" ]
then
# missing directory; extract from original
echo "INFO: Report PDF directory missing; creating '/opt/tplink/EAPController/data/pdf'"
mkdir /opt/tplink/EAPController/data/pdf
chown -R "${PUSERNAME}:${PGROUP}" /opt/tplink/EAPController/data/pdf
fi
# check to see if there is a db directory; create it if it is missing
if [ ! -d "/opt/tplink/EAPController/data/db" ]
then
echo "INFO: Database directory missing; creating '/opt/tplink/EAPController/data/db'"
mkdir /opt/tplink/EAPController/data/db
chown "${PUSERNAME}:${PGROUP}" /opt/tplink/EAPController/data/db
echo "done"
fi
}
print_timezone_info() {
echo "INFO: Time zone set to '${TZ}'"
}
version_ge() {
# return 0 if version ${1} is greater than or equal to version ${2}
[ "$(printf '%s\n' "${1}" "${2}" | sort -V | head -n1)" = "${2}" ]
}
load_image_version() {
# read the version from the image
if [ -f "/opt/tplink/EAPController/IMAGE_OMADA_VER.txt" ]
then
# file found; read the version that is in the image
IMAGE_OMADA_VER="$(cat /opt/tplink/EAPController/IMAGE_OMADA_VER.txt)"
else
echo "ERROR: Missing image version file (/opt/tplink/EAPController/IMAGE_OMADA_VER.txt); this should never happen!"
exit 1
fi
# get version strings that will be useful
IMAGE_MAJOR_VER="$(echo "${IMAGE_OMADA_VER}" | awk -F '.' '{print $1}')"
IMAGE_MINOR_VER="$(echo "${IMAGE_OMADA_VER}" | awk -F '.' '{print $2}')"
}
configure_upgrade_ports() {
# starting with 6.4, upgrade.es.https.port was removed and upgrade.https.port moved from 8043 (shared with the management port) to 8044
if version_ge "${IMAGE_OMADA_VER}" "6.4"
then
if [ -z "${UPGRADE_HTTPS_PORT}" ]
then
# not explicitly set; use the ES upgrade port if it was customized, otherwise the 6.4 default
if [ -n "${UPGRADE_ES_HTTPS_PORT}" ] && [ "${UPGRADE_ES_HTTPS_PORT}" != "8044" ]
then
echo "WARN: UPGRADE_ES_HTTPS_PORT is deprecated as of 6.4 (merged into UPGRADE_HTTPS_PORT); using its value (${UPGRADE_ES_HTTPS_PORT}) for UPGRADE_HTTPS_PORT"
if [ "${WEB_CONFIG_OVERRIDE}" != "true" ]
then
# once the controller has run, it uses the ports stored in its database; when upgrading, 6.4 resets the ES upgrade port to 8044
echo " Note: if upgrading an existing controller, set WEB_CONFIG_OVERRIDE=true for this to apply; otherwise the controller will use 8044"
fi
UPGRADE_HTTPS_PORT="${UPGRADE_ES_HTTPS_PORT}"
else
UPGRADE_HTTPS_PORT="8044"
fi
elif [ -n "${UPGRADE_ES_HTTPS_PORT}" ] && [ "${UPGRADE_ES_HTTPS_PORT}" != "${UPGRADE_HTTPS_PORT}" ]
then
echo "WARN: UPGRADE_ES_HTTPS_PORT (${UPGRADE_ES_HTTPS_PORT}) is ignored as of 6.4; it was merged into UPGRADE_HTTPS_PORT (${UPGRADE_HTTPS_PORT})"
fi
# warn if the upgrade port is shared with the management port (the pre-6.4 default); 6.4 ignores it and falls back to 8044
if [ "${UPGRADE_HTTPS_PORT}" = "${MANAGE_HTTPS_PORT}" ]
then
echo "##############################################################################"
echo "WARN: UPGRADE_HTTPS_PORT (${UPGRADE_HTTPS_PORT}) is the same as MANAGE_HTTPS_PORT!"
echo " Starting with 6.4, the upgrade port can no longer share the management port; the controller will use 8044 instead."
echo " Remove UPGRADE_HTTPS_PORT from your configuration and make sure 8044/tcp is reachable from your devices."
echo "##############################################################################"
fi
else
UPGRADE_HTTPS_PORT="${UPGRADE_HTTPS_PORT:-8043}"
UPGRADE_ES_HTTPS_PORT="${UPGRADE_ES_HTTPS_PORT:-8044}"
fi
}
update_port_configuration() {
# update stored ports when different of enviroment defined ports (works for numbers only)
PORT_ELEMS=( MANAGE_HTTP_PORT MANAGE_HTTPS_PORT PORTAL_HTTP_PORT PORTAL_HTTPS_PORT PORT_ADOPT_V1 PORT_APP_DISCOVERY PORT_UPGRADE_V1 PORT_MANAGER_V1 PORT_MANAGER_V2 PORT_DISCOVERY PORT_TRANSFER_V2 PORT_RTTY PORT_DEVICE_MONITOR UPGRADE_HTTPS_PORT )
# upgrade.es.https.port was removed in 6.4; leave any stale value alone
if ! version_ge "${IMAGE_OMADA_VER}" "6.4"
then
PORT_ELEMS+=( UPGRADE_ES_HTTPS_PORT )
fi
for ELEM in "${PORT_ELEMS[@]}"
do
# convert element to key name
KEY="$(echo "${ELEM}" | tr '[:upper:]' '[:lower:]' | tr '_' '.')"
# get value we want to set from the element
END_VAL=${!ELEM}
# get the current value from the omada.properties file
STORED_PROP_VAL=$(grep -Po "(?<=${KEY}=)([0-9]+)" /opt/tplink/EAPController/properties/omada.properties || true)
# check to see if we need to set the value
if [ "${STORED_PROP_VAL}" = "" ]
then
echo "INFO: Skipping '${KEY}' - not present in omada.properties"
elif [ "${STORED_PROP_VAL}" != "${END_VAL}" ]
then
# check to see if we are trying to bind to privileged port
if [ "${END_VAL}" -lt "1024" ] && [ "$(cat /proc/sys/net/ipv4/ip_unprivileged_port_start)" = "1024" ]
then
echo "ERROR: Unable to set '${KEY}' to ${END_VAL}; 'ip_unprivileged_port_start' has not been set. See https://github.com/mbentley/docker-omada-controller#unprivileged-ports"
exit 1
fi
# update the key-value pair
echo "INFO: Setting '${KEY}' to ${END_VAL} in omada.properties"
sed -i "s~^${KEY}=${STORED_PROP_VAL}$~${KEY}=${END_VAL}~g" /opt/tplink/EAPController/properties/omada.properties
else
# values already match; nothing to change
echo "INFO: Value of '${KEY}' already set to ${END_VAL} in omada.properties"
fi
done
}
update_general_properties() {
# update stored property values when different of environment defined values (works for any value)
for ELEM in WEB_CONFIG_OVERRIDE MONGO_EXTERNAL EAP_MONGOD_URI
do
# convert element to key name
KEY="$(echo "${ELEM}" | tr '[:upper:]' '[:lower:]' | tr '_' '.')"
# get the full key & value to store for checking later
KEY_VALUE="$(grep "^${KEY}=" /opt/tplink/EAPController/properties/omada.properties || true)"
# get value we want to set from the element
END_VAL=${!ELEM}
#value with redacted password for EAP_MONGOD_URI for logs
END_VAL_REDACTED="$(echo "${!ELEM}" | sed -r 's/(^mongodb:\/\/[^:]*:)([^@]*)(@.*$)/\1*****\3/')"
# get the current value from the omada.properties file
STORED_PROP_VAL=$(grep -Po "(?<=${KEY}=)(.*)+" /opt/tplink/EAPController/properties/omada.properties || true)
# check to see if we need to set the value; see if there is something in the key/value first
if [ -z "${KEY_VALUE}" ]
then
echo "INFO: Skipping '${KEY}' - not present in omada.properties"
elif [ "${STORED_PROP_VAL}" != "${END_VAL}" ]
then
# update the key-value pair
echo "INFO: Setting '${KEY}' to ${END_VAL_REDACTED} in omada.properties"
sed -i "s~^${KEY}=${STORED_PROP_VAL}$~${KEY}=${END_VAL}~g" /opt/tplink/EAPController/properties/omada.properties
else
# values already match; nothing to change
echo "INFO: Value of '${KEY}' already set to ${END_VAL_REDACTED} in omada.properties"
fi
done
}
import_ssl_certificate() {
# Import a cert from a possibly mounted secret or file at /cert
if [ -f "/cert/${SSL_KEY_NAME}" ] && [ -f "/cert/${SSL_CERT_NAME}" ]
then
# see where the keystore directory is; check for old location first
if [ -d /opt/tplink/EAPController/keystore ]
then
# rootless isn't supported on versions < 5.3.1
if [ "${ROOTLESS}" = "true" ]
then
echo "ERROR: rootless isn't supported on versions < 5.3.1"
exit 1
fi
# keystore in the parent folder before 5.3.1
KEYSTORE_DIR="/opt/tplink/EAPController/keystore"
else
# keystore directory moved to the data directory in 5.3.1
KEYSTORE_DIR="/opt/tplink/EAPController/data/keystore"
# check to see if the KEYSTORE_DIR exists (it won't on upgrade)
if [ ! -d "${KEYSTORE_DIR}" ]
then
echo "INFO: Creating keystore directory (${KEYSTORE_DIR})"
mkdir "${KEYSTORE_DIR}"
echo "INFO: Setting permissions on ${KEYSTORE_DIR}"
chown "${PUSERNAME}:${PGROUP}" "${KEYSTORE_DIR}"
fi
fi
echo "INFO: Importing cert from /cert/tls.[key|crt]"
# delete the existing keystore
rm -f "${KEYSTORE_DIR}/eap.keystore"
# example certbot usage: ./certbot-auto certonly --standalone --preferred-challenges http -d mydomain.net
openssl pkcs12 -export \
-inkey "/cert/${SSL_KEY_NAME}" \
-in "/cert/${SSL_CERT_NAME}" \
-certfile "/cert/${SSL_CERT_NAME}" \
-name eap \
-out "${KEYSTORE_DIR}/eap.keystore" \
-passout pass:tplink
# set ownership/permission on keystore
chown "${PUSERNAME}:${PGROUP}" "${KEYSTORE_DIR}/eap.keystore"
chmod 400 "${KEYSTORE_DIR}/eap.keystore"
fi
}
check_old_version_files() {
# see if any of these files exist; if so, do not start as they are from older versions
if [ -f /opt/tplink/EAPController/data/db/tpeap.0 ] || [ -f /opt/tplink/EAPController/data/db/tpeap.1 ] || [ -f /opt/tplink/EAPController/data/db/tpeap.ns ]
then
echo "ERROR: The data volume mounted to /opt/tplink/EAPController/data appears to have data from a previous version!"
echo " Follow the upgrade instructions at https://github.com/mbentley/docker-omada-controller#upgrading-to-41"
exit 1
fi
}
check_old_cmd() {
# check to see if the CMD passed contains the text "com.tplink.omada.start.OmadaLinuxMain" which is the old classpath from 4.x
if [ "$(echo "${EXEC_ARGS[@]}" | grep -q "com.tplink.omada.start.OmadaLinuxMain"; echo $?)" = "0" ]
then
echo -e "\n############################"
echo "WARN: CMD from 4.x detected! It is likely that this container will fail to start properly with a \"Could not find or load main class com.tplink.omada.start.OmadaLinuxMain\" error!"
echo " See the note on old CMDs at https://github.com/mbentley/docker-omada-controller/blob/master/README.md#upgrade-issues for details on why and how to resolve the issue."
echo -e "############################\n"
fi
}
validate_versions() {
# compare version from the image (see load_image_version) to the version stored in the persistent data (last ran version)
# load LAST_RAN_OMADA_VER, if file present
if [ -f "/opt/tplink/EAPController/data/LAST_RAN_OMADA_VER.txt" ]
then
# file found; read the version that was last recorded
LAST_RAN_OMADA_VER="$(cat /opt/tplink/EAPController/data/LAST_RAN_OMADA_VER.txt)"
else
# no file found; set version to 0.0.0 as we don't know the last version
LAST_RAN_OMADA_VER="0.0.0"
fi
# get version strings that will be useful
LAST_RAN_MAJOR_VER="$(echo "${LAST_RAN_OMADA_VER}" | awk -F '.' '{print $1}')"
# make sure we are not trying to upgrade from 4.x to 5.14.32.x or greater
if [ "${LAST_RAN_MAJOR_VER}" = "4" ] && [ "${IMAGE_MAJOR_VER}" -ge "5" ]
then
# check to see if we are runnning 5.14 or greater
if [ "${IMAGE_MAJOR_VER}" = "5" ] && [ "${IMAGE_MINOR_VER}" -ge "14" ] || [ "${IMAGE_MAJOR_VER}" -gt "5" ]
then
echo "ERROR: You are attempting to upgrade from 4.x to 5.14.x or greater; the upgrade code was removed in 5.14.x!"
echo " See https://github.com/mbentley/docker-omada-controller/blob/master/README_v3_and_v4.md#upgrade-path for the upgrade path from 4.x to 5.x"
exit 1
fi
fi
}
check_cpu_features() {
# check to make sure we have the supported cpu features for MongoDB included with 6.x when not using an external MongoDB
if [ "${IMAGE_MAJOR_VER}" = "6" ] && [ "${MONGO_EXTERNAL}" != "true" ]
then
# running 6.x and not using external mongodb; get cpu architecture
ARCH="$(uname -m)"
case "${ARCH}" in
x86_64)
# amd64 checks
echo -n "INFO: running hardware prerequisite check for AVX support on ${ARCH} to ensure your system can run MongoDB 8..."
# check for AVX support
if ! grep -qE '^flags.* avx( .*|$)' /proc/cpuinfo
then
echo -e "\nERROR: your system does not support AVX which is a requirement for the v6 and above container image as it only ships with MongoDB 8"
echo " See https://github.com/mbentley/docker-omada-controller/blob/master/README.md#your-system-does-not-support-avx-or-armv82-a for details on what exactly this means and how you can address this"
exit 1
fi
;;
aarch64|aarch64_be|armv8b|armv8l)
# arm64 checks (list of 64 bit arm compatible names from `uname -m`: https://stackoverflow.com/a/45125525)
echo -n "INFO: running hardware prerequisite check for armv8.2-a support on ${ARCH} to ensure your system can run MongoDB 8..."
# check for armv8.2-a support
if ! grep -qE '^Features.* (fphp|dcpop|sha3|sm3|sm4|asimddp|sha512|sve)( .*|$)' /proc/cpuinfo
then
# failed armv8.2-a test
echo -e "\nERROR: your system does not support the armv8.2-a or later microarchitecture which is a requirement for the v6 and above container image as it only ships with MongoDB 8"
echo " See https://github.com/mbentley/docker-omada-controller/blob/master/README.md#your-system-does-not-support-avx-or-armv82-a for details on what exactly this means and how you can address this"
exit 1
fi
;;
*)
echo -e "\nERROR: unknown architecture (${ARCH})"
exit 1
;;
esac
# prerequisite checks successful
echo "done"
fi
}
validate_mongodb_version() {
# see if this is our first run or if we are using an external MongoDB
if [ "${LAST_RAN_OMADA_VER}" = "0.0.0" ]
then
echo "INFO: skipping MongoDB data version check; first time running"
elif [ "${MONGO_EXTERNAL}" = "true" ]
then
echo "INFO: skipping MongoDB data version check; using external MongoDB"
else
# check to see if we are running v6 but have mongodb persistent data from an older mongodb
if [ "${IMAGE_MAJOR_VER}" = "6" ] && [ "${LAST_RAN_MAJOR_VER}" != "6" ]
then
echo "INFO: Comparing your MongoDB version with the persistent data..."
# get wiredtiger version
WT_VERSION="$(grep -o 'WiredTiger [0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*' /opt/tplink/EAPController/data/db/WiredTiger.turtle | cut -d' ' -f2)"
if [ -z "${WT_VERSION}" ]
then
echo "ERROR: Unable to parse the WiredTiger version!"
exit 1
fi
# check if the wiredtiger version is not 11.3.0
if [ "${WT_VERSION}" != "11.3.0" ]
then
echo "ERROR: Your persistent data for MongoDB is using WiredTiger ${WT_VERSION} (an older MongoDB) but this version of the image has MongoDB $(mongod --version | grep "db version" | awk -F 'n v' '{print $2}')!"
echo " You either need to revert back to a previous v5 tag or manually execute the MongoDB database upgrade."
echo " See https://github.com/mbentley/docker-omada-controller/tree/master/mongodb_upgrade#help-my-controller-stopped-working for instructions on what to do"
exit 1
else
echo "INFO: Success! Your MongoDB version matches your persistent data; continuing with entrypoint startup..."
fi
else
echo "INFO: Not updating to v6; skipping MongoDB version check (this is normal)"
fi
fi
}
validate_upgrade_precheck() {
# 6.4 and above can only be upgraded to from 6.3 and refuse to upgrade when managing devices with firmware that is no longer supported
# this mirrors ecspv1_device_count_precheck from the vendor install.sh (which reads a file written by 6.3 and above) but never deletes data
PRECHECK_FILE="/opt/tplink/EAPController/data/db/upgrade_precheck.json"
# only applies when upgrading to 6.4 or above from an older version
if ! version_ge "${IMAGE_OMADA_VER}" "6.4" || version_ge "${LAST_RAN_OMADA_VER}" "6.4"
then
return
fi
if [ "${SKIP_6_4_UPGRADE_PRECHECK}" = "true" ]
then
echo "WARN: SKIP_6_4_UPGRADE_PRECHECK=true; skipping the ${IMAGE_OMADA_VER} upgrade precheck!"
return
fi
# first run with no precheck file means this is a fresh install
if [ "${LAST_RAN_OMADA_VER}" = "0.0.0" ] && [ ! -f "${PRECHECK_FILE}" ]
then
echo "INFO: skipping ${IMAGE_OMADA_VER} upgrade precheck; first time running"
return
fi
echo "INFO: running ${IMAGE_OMADA_VER} upgrade precheck (last version ran: ${LAST_RAN_OMADA_VER})..."
PRECHECK_BYPASS_MSG=" To bypass this check, set SKIP_6_4_UPGRADE_PRECHECK=true only if you REALLY know what you're doing!"
# rule 1: must be upgrading from 6.3 or above (6.3 is what writes the precheck file)
if [ "${LAST_RAN_OMADA_VER}" != "0.0.0" ] && ! version_ge "${LAST_RAN_OMADA_VER}" "6.3"
then
echo "ERROR: Unable to upgrade directly from ${LAST_RAN_OMADA_VER} to ${IMAGE_OMADA_VER}!"
echo " Upgrade to the latest 6.3 first and let it fully start before upgrading to ${IMAGE_OMADA_VER}."
echo "${PRECHECK_BYPASS_MSG}"
exit 1
fi
if [ ! -f "${PRECHECK_FILE}" ]
then
echo "ERROR: Unable to upgrade to ${IMAGE_OMADA_VER}; the upgrade precheck file (${PRECHECK_FILE}) was not found!"
echo " Start 6.3 and let it fully start (it writes this file once startup completes) before upgrading to ${IMAGE_OMADA_VER}."
echo "${PRECHECK_BYPASS_MSG}"
exit 1
fi
# rule 2: the device count must be present and valid
ECSPV1_DEVICE_COUNT="$(grep -o '"ecspv1_device_count"[[:space:]]*:[[:space:]]*[0-9][0-9]*' "${PRECHECK_FILE}" 2>/dev/null | sed 's/.*:[[:space:]]*//' | head -n 1 || true)"
if [ -z "${ECSPV1_DEVICE_COUNT}" ]
then
echo "ERROR: Unable to upgrade to ${IMAGE_OMADA_VER}; the upgrade precheck file (${PRECHECK_FILE}) is invalid!"
echo " TP-Link reports this as an invalid controller database; contact TP-Link technical support."
echo "${PRECHECK_BYPASS_MSG}"
exit 1
fi
# rule 3: no devices with firmware that is no longer supported
if [ "${ECSPV1_DEVICE_COUNT}" != "0" ]
then
echo "ERROR: Unable to upgrade to ${IMAGE_OMADA_VER}; the controller is managing ${ECSPV1_DEVICE_COUNT} device(s) with outdated firmware that ${IMAGE_OMADA_VER} no longer supports!"
echo " Update the firmware of these devices using 6.3 before upgrading to ${IMAGE_OMADA_VER}."
echo "${PRECHECK_BYPASS_MSG}"
exit 1
fi
echo "INFO: ${IMAGE_OMADA_VER} upgrade precheck passed"
}
check_version_downgrade() {
# use sort to check which version is newer; should sort the newest version to the top
if [ "$(printf '%s\n' "${IMAGE_OMADA_VER}" "${LAST_RAN_OMADA_VER}" | sort -rV | head -n1)" != "${IMAGE_OMADA_VER}" ]
then
# version in the image is didn't match newest image version; this means we are trying to start and older version
echo "ERROR: The version from the image (${IMAGE_OMADA_VER}) is older than the last version executed (${LAST_RAN_OMADA_VER})! Refusing to start to prevent data loss!"
echo " To bypass this check, remove /opt/tplink/EAPController/data/LAST_RAN_OMADA_VER.txt only if you REALLY know what you're doing!"
exit 1
else
echo "INFO: Version check passed; image version (${IMAGE_OMADA_VER}) >= the last version ran (${LAST_RAN_OMADA_VER}); writing image version to last ran file..."
echo "${IMAGE_OMADA_VER}" > /opt/tplink/EAPController/data/LAST_RAN_OMADA_VER.txt
fi
}
check_userland_kernel() {
# check to see if we are in a bad situation with a 32 bit userland and 64 bit kernel (fails to start MongoDB on a Raspberry Pi)
if [ "$(dpkg --print-architecture)" = "armhf" ] && [ "$(uname -m)" = "aarch64" ] && [ "${SKIP_USERLAND_KERNEL_CHECK}" = "false" ]
then
echo "##############################################################################"
echo "##############################################################################"
echo "ERROR: 32 bit userspace with 64 bit kernel detected! MongoDB will NOT start!"
echo " See https://github.com/mbentley/docker-omada-controller/blob/master/README.md#mismatched-userland-and-kernel for how to fix the issue"
echo "##############################################################################"
echo "##############################################################################"
exit 1
else
echo "INFO: userland/kernel check passed"
fi
}
show_java_version() {
# show java version
echo -e "INFO: output of 'java -version':\n$(java -version 2>&1)\n"
}
expand_classpath_wildcards() {
# expand classpath wildcard entries (e.g. lib/*) into an explicit, sorted list of JARs
# TP-Link's control.sh starts the controller with jsvc, which expands wildcards itself using glob(3), so the JARs
# are always in alphabetical order. the java launcher instead expands wildcards with readdir() in an unspecified
# order which varies by filesystem, so the same image can load JARs in a different order on different hosts. the
# Omada controller depends on that order where JARs contain conflicting classes, resources, or Spring beans, so
# match jsvc's behavior to load them the same way a native install does (fixes #621 and #835)
# the expanded classpath is passed using the CLASSPATH environment variable instead of -cp so the hundreds of JAR
# paths don't end up on the java command line (as seen in ps, top, etc.); java uses CLASSPATH when -cp is not set
# check if we're actually starting the Omada controller (not just running some other command)
if echo "${EXEC_ARGS[@]}" | grep -q "com.tplink.smb.omada.starter.OmadaLinuxMain"
then
echo "INFO: Omada Controller startup detected; expanding classpath wildcards in sorted order (matches jsvc)"
# parse CMD arguments to find and remove the -cp parameter, keeping its expanded value for CLASSPATH
NEW_ARGS=()
NEXT_IS_CP=false
EXPANDED_CP=""
for ARG in "${EXEC_ARGS[@]}"
do
if [ "${NEXT_IS_CP}" = "true" ]
then
# this is the classpath value; expand each entry ending in an asterisk the same way jsvc does (dir/* -> dir/*.jar)
NEW_CP=()
IFS=':' read -r -a CP_ENTRIES <<< "${ARG}"
for CP_ENTRY in "${CP_ENTRIES[@]}"
do
if [ "${CP_ENTRY%\*}" != "${CP_ENTRY}" ]
then
# glob in a subshell using the C locale so the sort order is byte order regardless of the container's locale
CP_JARS="$(export LC_ALL=C; shopt -s nullglob; JARS=("${CP_ENTRY%\*}"*.jar); IFS=':'; echo "${JARS[*]}")"
if [ -n "${CP_JARS}" ]
then
echo "INFO: Expanded classpath entry '${CP_ENTRY}' to $(echo "${CP_JARS}" | tr ':' '\n' | wc -l) JARs in sorted order"
NEW_CP+=("${CP_JARS}")
else
echo "WARN: Classpath entry '${CP_ENTRY}' did not match any JARs; leaving it unchanged"
NEW_CP+=("${CP_ENTRY}")
fi
else
NEW_CP+=("${CP_ENTRY}")
fi
done
# if -cp is passed more than once, java uses the last one so do the same
EXPANDED_CP="$(IFS=':'; echo "${NEW_CP[*]}")"
NEXT_IS_CP=false
elif [ "${ARG}" = "-cp" ] || [ "${ARG}" = "-classpath" ]
then
# found the classpath flag; drop it from the arguments as the value is passed using CLASSPATH instead
NEXT_IS_CP=true
else
# regular argument
NEW_ARGS+=("${ARG}")
fi
done
if [ -n "${EXPANDED_CP}" ]
then
# replace the global EXEC_ARGS array with modified one and pass the classpath through the environment
EXEC_ARGS=("${NEW_ARGS[@]}")
export CLASSPATH="${EXPANDED_CP}"
echo "INFO: Passing the classpath to java using the CLASSPATH environment variable"
else
echo "INFO: No -cp argument found; leaving the java command unchanged"
fi
else
echo "INFO: Not starting Omada Controller; skipping classpath wildcard expansion"
fi
}
patch_java_heap() {
# replace hardcoded -Xmx / -Xms in EXEC_ARGS with user-supplied values
if [ -z "${JAVA_MAX_HEAP_SIZE}" ] && [ -z "${JAVA_MIN_HEAP_SIZE}" ]
then
return
fi
XMX_REPLACED="false"
XMS_REPLACED="false"
NEW_ARGS=()
for ARG in "${EXEC_ARGS[@]}"
do
case "${ARG}" in
-Xmx*)
if [ -n "${JAVA_MAX_HEAP_SIZE}" ]
then
echo "INFO: replacing '${ARG}' with '-Xmx${JAVA_MAX_HEAP_SIZE}' (JAVA_MAX_HEAP_SIZE)"
NEW_ARGS+=("-Xmx${JAVA_MAX_HEAP_SIZE}")
XMX_REPLACED="true"
else
NEW_ARGS+=("${ARG}")
fi
;;
-Xms*)
if [ -n "${JAVA_MIN_HEAP_SIZE}" ]
then
echo "INFO: replacing '${ARG}' with '-Xms${JAVA_MIN_HEAP_SIZE}' (JAVA_MIN_HEAP_SIZE)"
NEW_ARGS+=("-Xms${JAVA_MIN_HEAP_SIZE}")
XMS_REPLACED="true"
else
NEW_ARGS+=("${ARG}")
fi
;;
*)
NEW_ARGS+=("${ARG}")
;;
esac
done
EXEC_ARGS=("${NEW_ARGS[@]}")
if [ -n "${JAVA_MAX_HEAP_SIZE}" ] && [ "${XMX_REPLACED}" != "true" ]
then
echo "WARN: JAVA_MAX_HEAP_SIZE was set but no existing -Xmx argument was found in EXEC_ARGS; no replacement was made"
fi
if [ -n "${JAVA_MIN_HEAP_SIZE}" ] && [ "${XMS_REPLACED}" != "true" ]
then
echo "WARN: JAVA_MIN_HEAP_SIZE was set but no existing -Xms argument was found in EXEC_ARGS; no replacement was made"
fi
}
warn_autobackup() {
# check for autobackup
if [ ! -d "/opt/tplink/EAPController/data/autobackup" ]
then
echo
echo "##############################################################################"
echo "##############################################################################"
echo "WARN: autobackup directory not found! Please configure automatic backups!"
echo " For instructions, see https://github.com/mbentley/docker-omada-controller#controller-backups"
echo "##############################################################################"
echo "##############################################################################"
echo
sleep 2
fi
}
tail_logs() {
# tail the omada logs if set to true
if [ "${SHOW_SERVER_LOGS}" = "true" ]
then
if [ "${ROOTLESS}" = "true" ]
then
tail -F -n 0 /opt/tplink/EAPController/logs/server.log &
else
gosu "${PUSERNAME}" tail -F -n 0 /opt/tplink/EAPController/logs/server.log &
fi
fi
# tail the mongodb logs if set to true
if [ "${SHOW_MONGODB_LOGS}" = "true" ]
then
if [ "${ROOTLESS}" = "true" ]
then
tail -F -n 0 /opt/tplink/EAPController/logs/mongod.log &
else
gosu "${PUSERNAME}" tail -F -n 0 /opt/tplink/EAPController/logs/mongod.log &
fi
fi
}
# ============================================================================
# ROOT-SPECIFIC FUNCTIONS
# ============================================================================
setup_user_group() {
PUID="${PUID:-508}"
PGID="${PGID:-508}"
PUSERNAME="${PUSERNAME:-omada}"
PGROUP="${PGROUP:-omada}"
# validate user/group exist with correct UID/GID
echo "INFO: Validating user/group (${PUSERNAME}:${PGROUP}) exists with correct UID/GID (${PUID}:${PGID})"
# check to see if group exists; if not, create it
if grep -q -E "^${PGROUP}:" /etc/group > /dev/null 2>&1
then
# existing group found; also make sure the omada group matches the GID
echo "INFO: Group (${PGROUP}) exists; skipping creation"
EXISTING_GID="$(getent group "${PGROUP}" | cut -d: -f3)"
if [ "${EXISTING_GID}" != "${PGID}" ]
then
echo "ERROR: Group (${PGROUP}) has an unexpected GID; was expecting '${PGID}' but found '${EXISTING_GID}'!"
exit 1
fi
else
# make sure the group doesn't already exist with a different name
if awk -F ':' '{print $3}' /etc/group | grep -q "^${PGID}$"
then
# group ID exists but has a different group name
EXISTING_GROUP="$(grep ":${PGID}:" /etc/group | awk -F ':' '{print $1}')"
echo "INFO: Group (${PGROUP}) already exists with a different name; renaming '${EXISTING_GROUP}' to '${PGROUP}'"
groupmod -n "${PGROUP}" "${EXISTING_GROUP}"
else
# create the group
echo "INFO: Group (${PGROUP}) doesn't exist; creating"
groupadd -g "${PGID}" "${PGROUP}"
fi
fi
# check to see if user exists; if not, create it
if id -u "${PUSERNAME}" > /dev/null 2>&1
then
# exiting user found; also make sure the omada user matches the UID
echo "INFO: User (${PUSERNAME}) exists; skipping creation"
EXISTING_UID="$(id -u "${PUSERNAME}")"
if [ "${EXISTING_UID}" != "${PUID}" ]
then
echo "ERROR: User (${PUSERNAME}) has an unexpected UID; was expecting '${PUID}' but found '${EXISTING_UID}'!"
exit 1
fi
else
# make sure the user doesn't already exist with a different name
if awk -F ':' '{print $3}' /etc/passwd | grep -q "^${PUID}$"
then
# user ID exists but has a different user name
EXISTING_USER="$(grep ":${PUID}:" /etc/passwd | awk -F ':' '{print $1}')"
echo "INFO: User (${PUSERNAME}) already exists with a different name; renaming '${EXISTING_USER}' to '${PUSERNAME}'"
usermod -g "${PGID}" -d /opt/tplink/EAPController/data -l "${PUSERNAME}" -s /bin/sh -c "" "${EXISTING_USER}"
else
# create the user
echo "INFO: User (${PUSERNAME}) doesn't exist; creating"
useradd -u "${PUID}" -g "${PGID}" -d /opt/tplink/EAPController/data -s /bin/sh -c "" "${PUSERNAME}"
fi
fi
}
fix_permissions() {
# skip in rootless mode - not needed and may lack permissions
if [ "${ROOTLESS}" = "true" ]
then
return
fi
# make sure permissions are set appropriately on each directory
for DIR in data logs properties
do
OWNER="$(stat -c '%u' /opt/tplink/EAPController/${DIR})"
GROUP="$(stat -c '%g' /opt/tplink/EAPController/${DIR})"
if [ "${OWNER}" != "${PUID}" ] || [ "${GROUP}" != "${PGID}" ]
then
# notify user that uid:gid are not correct and fix them
echo "WARN: Ownership not set correctly on '/opt/tplink/EAPController/${DIR}'; setting correct ownership (${PUSERNAME}:${PGROUP})"
chown -R "${PUSERNAME}:${PGROUP}" "/opt/tplink/EAPController/${DIR}"
fi
done
# validate permissions on /tmp
TMP_PERMISSIONS="$(stat -c '%a' /tmp)"
if [ "${TMP_PERMISSIONS}" != "1777" ]
then
echo "WARN: Permissions are not set correctly on '/tmp' (${TMP_PERMISSIONS}); setting correct permissions (1777)"
chmod -v 1777 /tmp
fi
}
setup_mongodb_wrapper() {
# only applicable when using embedded MongoDB with extra args requested
if [ -z "${MONGOD_EXTRA_ARGS}" ] || [ "${MONGO_EXTERNAL}" = "true" ]
then
return
fi
# rootless mode: bin/ is owned by root; cannot replace symlink without elevated permissions
if [ "${ROOTLESS}" = "true" ]
then
echo "WARN: MONGOD_EXTRA_ARGS is set but rootless mode is enabled; skipping mongod wrapper (bin/ not writable as non-root)"
return
fi
MONGOD_LINK="/opt/tplink/EAPController/bin/mongod"
if [ -L "${MONGOD_LINK}" ]
then
MONGOD_REAL="$(readlink -f "${MONGOD_LINK}")"
else
MONGOD_REAL="$(command -v mongod)"
fi
if [ -z "${MONGOD_REAL}" ] || [ ! -x "${MONGOD_REAL}" ]
then
echo "ERROR: Unable to determine the real mongod binary for MONGOD_EXTRA_ARGS"
exit 1
fi
if [ "${MONGOD_REAL}" = "${MONGOD_LINK}" ]
then
echo "ERROR: Refusing to create mongod wrapper because the resolved binary path points to the wrapper itself"
exit 1
fi
echo "INFO: MONGOD_EXTRA_ARGS='${MONGOD_EXTRA_ARGS}'; creating mongod wrapper (real binary: ${MONGOD_REAL})"
# replace symlink with a wrapper script that appends the extra args
rm -f "${MONGOD_LINK}"
cat > "${MONGOD_LINK}" << EOF
#!/bin/sh
exec "${MONGOD_REAL}" "\$@" ${MONGOD_EXTRA_ARGS}
EOF
chmod +x "${MONGOD_LINK}"
}
inject_application_properties() {
PROPS_FILE="/opt/tplink/EAPController/properties/application.properties"
if [ -z "${APPLICATION_PROPERTIES}" ]
then
if [ -f "${PROPS_FILE}" ]
then
echo "INFO: APPLICATION_PROPERTIES unset; removing leftover ${PROPS_FILE}"
rm -f "${PROPS_FILE}"
fi
return
fi
echo "INFO: APPLICATION_PROPERTIES set; writing Spring Boot properties to ${PROPS_FILE}"
printf '%s\n' "${APPLICATION_PROPERTIES}" > "${PROPS_FILE}"
chmod 640 "${PROPS_FILE}"
# in non-rootless mode this function runs as root before gosu hands off to ${PUSERNAME};
# without chown the file is root:root 640 and Spring Boot silently can't read it
if [ "${ROOTLESS}" != "true" ]
then
chown "${PUSERNAME}:${PGROUP}" "${PROPS_FILE}"
fi
}
enable_tls_1_11() {
TLS_1_11_ENABLED="${TLS_1_11_ENABLED:-false}"
# re-enable disabled TLS versions 1.0 & 1.1
if [ "${TLS_1_11_ENABLED}" = "true" ]
then
if [ "${ROOTLESS}" = "true" ]
then
echo "WARN: TLS_1_11_ENABLED is not supported in rootless mode (insufficient permissions to modify java.security files)"
return
fi
echo "INFO: Re-enabling TLS 1.0 & 1.1"
if [ -f "/etc/java-8-openjdk/security/java.security" ]
then
# openjdk8
sed -i 's#^jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1,#jdk.tls.disabledAlgorithms=SSLv3,#' /etc/java-8-openjdk/security/java.security
elif [ -f "/etc/java-17-openjdk/security/java.security" ]
then
# openjdk17
sed -i 's#^jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1,#jdk.tls.disabledAlgorithms=SSLv3,#' /etc/java-17-openjdk/security/java.security
else
# not running openjdk8 or openjdk17
echo "WARN: Unable to re-enable TLS 1.0 & 1.1; unable to detect openjdk version"
fi
fi
}
# ============================================================================
# ROOTLESS-SPECIFIC FUNCTIONS
# ============================================================================
validate_rootless() {
PUSERNAME="$(id -u)"
PGROUP="$(id -g)"
# make sure we aren't actually running as root
if [ "${PUSERNAME}" = "0" ] || [ "${PGROUP}" = "0" ]
then
echo "ERROR: you're running as root (${PUSERNAME}:${PGROUP}); this defeats the purpose of running rootless!"
exit 1
else
echo "INFO: running as ${PUSERNAME}:${PGROUP}"
fi
}
check_directory_writable() {
# make sure the directories are writable
for DIR in /opt/tplink/EAPController/data /opt/tplink/EAPController/logs /opt/tplink/EAPController/properties /tmp
do
if [ ! -w "${DIR}" ]
then
# notify user that the directory is not writable
echo "ERROR: ${DIR} is not writable!"
exit 1
fi
done
}
# ============================================================================
# MAIN ORCHESTRATION FUNCTIONS
# ============================================================================
# Common setup and validation: all shared setup, configuration, and validation steps
common_setup_and_validation() {
restore_properties_files
setup_directories
print_timezone_info
load_image_version
configure_upgrade_ports
update_port_configuration
update_general_properties
fix_permissions
setup_mongodb_wrapper
inject_application_properties
import_ssl_certificate
enable_tls_1_11
check_old_version_files
check_old_cmd
validate_versions
check_cpu_features
validate_mongodb_version
validate_upgrade_precheck
check_version_downgrade
check_userland_kernel
show_java_version
warn_autobackup
patch_java_heap
expand_classpath_wildcards
}
main_root() {
setup_environment
setup_user_group
common_setup_and_validation
echo "INFO: Starting Omada Controller as user ${PUSERNAME}"
tail_logs
exec gosu "${PUSERNAME}" "${EXEC_ARGS[@]}"
}
main_rootless() {
setup_environment
validate_rootless
check_directory_writable
common_setup_and_validation
echo "INFO: Starting Omada Controller..."
tail_logs
exec "${EXEC_ARGS[@]}"
}
# ============================================================================
# ENTRYPOINT
# ============================================================================
# initialize global EXEC_ARGS array for argument manipulation
EXEC_ARGS=("${@}")
if [ "${ROOTLESS}" = "true" ]
then
main_rootless
else
main_root
fi