-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup-fiware.sh
executable file
·1763 lines (1443 loc) · 41 KB
/
setup-fiware.sh
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
# MIT License
#
# Copyright (c) 2023-2024 Kazuhito Suda
#
# This file is part of FIWARE Small Bang
#
# https://github.com/lets-fiware/FIWARE-Small-Bang
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -Ceuo pipefail
VERSION=0.6.0
#
# Syslog info
#
logging_info() {
${LOGGING_INFO} && echo "setup: $1" 1>&2
${LOGGER} && /usr/bin/logger -i -p "user.info" -t "FI-SB" "setup: $1"
true
}
#
# Syslog err
#
logging_err() {
echo "error: $1" 1>&2
${LOGGER} && /usr/bin/logger -i -p "user.err" -t "FI-SB" "setup error: $1"
true
}
#
# Get IP address
#
get_ip_address() {
logging_info "${FUNCNAME[0]}"
if [ -z "${IP_ADDRESS}" ]; then
if [ "${OS}" = "linux" ]; then
IP_ADDRESS=$(hostname -I | awk '{ print $1 }')
else
IP_ADDRESS=$("${IPCONFIG_CMD}" getifaddr en0)
fi
fi
}
#
# Check data direcotry
#
check_data_direcotry() {
logging_info "${FUNCNAME[0]}"
if [ -d ./data ]; then
echo "FIWARE instance already exists."
echo "If you want to create new FIWARE instance, run 'make clean' before running setup-fiware.sh"
exit "${ERR_CODE}"
fi
}
#
# Check machine architecture
#
check_machine() {
logging_info "${FUNCNAME[0]}"
ARCH=$("${UNAME_CMD}" -m)
if [ "${ARCH}" = "x86_64" ]; then
ARCH=amd64
elif [ "${ARCH}" = "arm64" ] || [ "${ARCH}" = "aarch64" ]; then
ARCH=arm64
else
MSG="Error: ${ARCH} not supported"
logging_err "${FUNCNAME[0]} ${MSG}"
exit "${ERR_CODE}"
fi
OS=$("${UNAME_CMD}" -s)
if [ "${OS:0:5}" = "Linux" ]; then
OS=linux
elif [ "${OS:0:6}" = "Darwin" ]; then
OS=darwin
else
MSG="Error: ${OS} not supported"
logging_err "${FUNCNAME[0]} ${MSG}"
exit "${ERR_CODE}"
fi
return
}
#
# Check Docker
#
check_docker() {
logging_info "${FUNCNAME[0]}"
if ! type "${DOCKER_CMD}" >/dev/null 2>&1; then
MSG="Error: docker not found"
logging_err "${FUNCNAME[0]} ${MSG}"
exit "${ERR_CODE}"
fi
set +e
found=$(sudo "${DOCKER_CMD}" info --format '{{json . }}' | ${GREP_CMD} -ic \"compose\")
set -e
if [ "${found}" -eq 0 ]; then
MSG="Error: docker compose plugin not found"
logging_err "${FUNCNAME[0]} ${MSG}"
exit "${ERR_CODE}"
fi
}
#
# Init command
#
init_cmd() {
logging_info "${FUNCNAME[0]}"
SUDO=sudo
UNAME_CMD="${FISB_TEST_UNAME_CMD:-uname}"
CURL_CMD="${FISB_TEST_CURL_CMD:-curl}"
GREP_CMD="${FISB_TEST_GREP_CMD:-grep}"
DOCKER_CMD="${FISB_TEST_DOCKER_CMD:-docker}"
DOCKER_COMPOSE="${SUDO} /usr/bin/docker compose"
IPCONFIG_CMD="${FISB_TEST_IPCONFIG_CMD:-ipconfig}"
}
#
# Init directories
#
init_directories() {
logging_info "${FUNCNAME[0]}"
mkdir "${CONFIG_DIR}"
mkdir "${DATA_DIR}"
}
#
# Check value
#
check_value() {
logging_info "${FUNCNAME[0]}"
local VAL
eval "VAL=\${${1}}"
# VAL=${VAL,,}
VAL=$(echo "${VAL}" | tr '[:upper:]' '[:lower:]')
if [ "${VAL}" = "false" ] || [ "${VAL}" = "true" ]; then
eval "${1}=\${VAL}"
elif [ "${VAL}" = "y" ]; then
eval "${1}"=true
else
eval "${1}"=false
fi
}
#
# Create config sh
#
create_config_sh() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> config.sh
#!/bin/bash
# Use Cygnus sink (true or false) Default: false
CYGNUS_MONGO=
CYGNUS_MYSQL=
CYGNUS_POSTGRES=
CYGNUS_ELASTICSEARCH=
# Set passowrd for database systems
ELASTICSEARCH_PASSWORD=
MYSQL_ROOT_PASSWORD=
POSTGRES_PASSWORD=
# Use STH-Comet (true or false) Default: false
COMET=
# Use Quantumleap (true or false) Default: false
QUANTUMLEAP=
# Use Perseo (true or false) Default: false
PERSEO=
# Use WireCloud (true or false) Default: false
WIRECLOUD=
# Use IoT Agent for UltraLight 2.0 (true or false) Default: false
IOTAGENT_UL=
# Use IoT Agent for JSON (true or false) Default: false
IOTAGENT_JSON=
# Specify transport protocol for IoT Agent (HTTP and/or MQTT)
# Use HTTP (true or false) Default: true
IOTAGENT_HTTP=
# Use MQTT (true or false) Default: false
IOTAGENT_MQTT=
# Use node-red (true or false) Default: false
NODE_RED=
# Start containers (true or false) Default: true
START_CONTAINER=
EOF
}
#
# Get config sh
#
get_config_sh() {
logging_info "${FUNCNAME[0]}"
if ! [ -e ./config.sh ]; then
create_config_sh
fi
. ./config.sh
IOTAGENT=false
touch .install
local NAME
local VAL
for NAME in CYGNUS_MONGO CYGNUS_MYSQL CYGNUS_POSTGRES CYGNUS_ELASTICSEARCH COMET QUANTUMLEAP PERSEO WIRECLOUD NODE_RED IOTAGENT_UL IOTAGENT_JSON IOTAGENT_MQTT
do
eval "VAL=\${${NAME}}"
if [ -z "${VAL}" ]; then
eval ${NAME}=false
else
check_value ${NAME}
fi
done
if ${CYGNUS_MONGO} || ${CYGNUS_MYSQL} || ${CYGNUS_POSTGRES} || ${CYGNUS_ELASTICSEARCH}; then
CYGNUS=true
else
CYGNUS=false
fi
if ${IOTAGENT_UL} && ${IOTAGENT_JSON}; then
MSG="Error: IoT Agent UL and IoT Agent JSON cannot be set up on the same instance."
logging_err "${FUNCNAME[0]} ${MSG}"
exit "${ERR_CODE}"
fi
if ${IOTAGENT_UL} || ${IOTAGENT_JSON}; then
IOTAGENT=true
if [ -z "${IOTAGENT_HTTP}" ]; then
IOTAGENT_HTTP=true
else
check_value IOTAGENT_HTTP
fi
if "${IOTAGENT_UL}"; then
IOTA_DEFAULT_RESOURCE=/iot/ul
else
IOTA_DEFAULT_RESOURCE=/iot/json
fi
else
IOTAGENT_HTTP=false
fi
if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
MYSQL_ROOT_PASSWORD=mysql
fi
if [ -z "${POSTGRES_PASSWORD}" ]; then
POSTGRES_PASSWORD=postgres
fi
if [ -z "${ELASTICSEARCH_PASSWORD}" ]; then
ELASTICSEARCH_PASSWORD=elasticsearch
fi
if [ -z "${START_CONTAINER}" ]; then
START_CONTAINER=true
else
check_value START_CONTAINER
fi
}
#
# Set amd64(x86_64) images
#
set_amd64_images() {
IMAGE_ORION=telefonicaiot/fiware-orion:4.0.0
IMAGE_WIRECLOUD=quay.io/fiware/wirecloud:1.3.1
IMAGE_NGSIPROXY=quay.io/fiware/ngsiproxy:1.2.2
IMAGE_COMET=telefonicaiot/fiware-sth-comet:2.11.0
IMAGE_CYGNUS=telefonicaiot/fiware-cygnus:3.8.0
IMAGE_IOTAGENT_UL=telefonicaiot/iotagent-ul:3.4.0
IMAGE_IOTAGENT_JSON=telefonicaiot/iotagent-json:3.4.0
IMAGE_QUANTUMLEAP=orchestracities/quantumleap:1.0.0
IMAGE_PERSEO_CORE=telefonicaiot/perseo-core:1.13.0
IMAGE_PERSEO_FE=telefonicaiot/perseo-fe:1.30.0
IMAGE_ELASTICSEARCH=elasticsearch:2.4
}
#
# Set arm64 images
#
set_arm64_images() {
IMAGE_ORION=letsfiware/orion:4.0.0
IMAGE_WIRECLOUD=letsfiware/wirecloud:1.3.1
IMAGE_NGSIPROXY=letsfiware/ngsiproxy:1.2.2
IMAGE_COMET=letsfiware/sth-comet:2.11.0
IMAGE_CYGNUS=letsfiware/fiware-cygnus:3.8.0
IMAGE_IOTAGENT_UL=letsfiware/iotagent-ul:3.4.0
IMAGE_IOTAGENT_JSON=letsfiware/iotagent-json:3.4.0
IMAGE_QUANTUMLEAP=letsfiware/quantumleap:1.0.0
IMAGE_PERSEO_CORE=letsfiware/perseo-core:1.13.0
IMAGE_PERSEO_FE=letsfiware/perseo-fe:1.30.0
IMAGE_ELASTICSEARCH=letsfiware/elasticsearch:2.4
}
#
# Set default values
#
set_default_values() {
logging_info "${FUNCNAME[0]}"
DOCKER_COMPOSE_YML=docker-compose.yml
DOT_ENV=.env
CONFIG_DIR=./config
SCRIPT_DIR="${CONFIG_DIR}"/script
DATA_DIR=./data
MAKEFILE=Makefile
rm -f "${DOT_ENV}"
cat <<EOF > "${DOT_ENV}"
#
# .env file for FIWARE Small Bang
#
VERSION=${VERSION}
IP_ADDRESS=${IP_ADDRESS}
CONFIG_DIR=${CONFIG_DIR}
DATA_DIR=${DATA_DIR}
EOF
IMAGE_MONGO=mongo:6.0
IMAGE_NGINX=nginx:1.25
IMAGE_MEMCACHED=memcached:1
COMET_LOGOPS_LEVEL=INFO
CYGNUS_LOG_LEVEL=INFO
IOTA_UL_DEFAULT_RESOURCE=/iot/ul
IOTA_UL_LOG_LEVEL=INFO
IOTA_UL_TIMESTAMP=true
IOTA_UL_AUTOCAST=true
IOTA_JSON_DEFAULT_RESOURCE=/iot/json
IOTA_JSON_LOG_LEVEL=INFO
IOTA_JSON_TIMESTAMP=true
IOTA_JSON_AUTOCAST=true
IMAGE_MOSQUITTO=eclipse-mosquitto:1.6
MOSQUITTO_LOG_TYPE=error,warning,notice,information
QUANTUMLEAP_LOGLEVEL=INFO
IMAGE_REDIS=redis:6
IMAGE_CRATE=crate:4.6.6
PERSEO_MAX_AGE=6000
PERSEO_LOG_LEVEL=info
IMAGE_MYSQL=mysql:8.1
IMAGE_POSTGRES=postgres:15
IMAGE_ELASTICSEARCH_DB=elasticsearch:7.17.13
IMAGE_NODE_RED=letsfiware/node-red:v0.6.0
MONGO_INSTALLED=false
POSTGRES_INSTALLED=false
if [ "${ARCH}" = "amd64" ]; then
set_amd64_images
else
set_arm64_images
fi
}
#
# Create collect.sh
#
create_collect_sh() {
logging_info "${FUNCNAME[0]}"
cat << 'EOF' > "${1}"
#!/bin/bash
set -eu
LANG=C
LC_TIME=C
check_cmd() {
if type "$1" >/dev/null 2>&1; then
FOUND=true
else
FOUND=false
fi
}
#
# Check distribuiton
#
check_distro() {
DISTRO="Unknown distro"
ARCH=$(uname -m)
OS=$(uname -s)
if [ "${OS:0:5}" = "Linux" ]; then
if [ -e /etc/redhat-release ]; then
DISTRO=$(cat /etc/redhat-release)
elif [ -e /etc/debian_version ] || [ -e /etc/debian_release ]; then
if [ -e /etc/lsb-release ]; then
ver="$(sed -n -e "/DISTRIB_RELEASE=/s/DISTRIB_RELEASE=\(.*\)/\1/p" /etc/lsb-release | awk -F. '{printf "%2d%02d", $1,$2}')"
DISTRO="Ubuntu ${ver}"
else
DISTRO="not Ubuntu"
fi
else
DISTRO="Unknown Linux"
fi
elif [ "${OS:0:6}" = "Darwin" ]; then
OS=Darwin
fi
echo "OS: ${DISTRO}"
echo "Arch: ${ARCH}"
}
check_docker() {
echo -n "Docker: "
if type docker >/dev/null 2>&1; then
sudo docker --version
else
echo "not found"
fi
echo -n "Docker compose: "
set +e
found=$(sudo docker info --format '{{json . }}' | jq -r '.ClientInfo.Plugins | .[].Name' | grep -ic compose)
set -e
if [ "${found}" -eq 1 ]; then
sudo docker compose version
else
echo "not found"
fi
}
cd "$(dirname "$0")"
cd ../..
if [ ! -e .env ]; then
echo ".env file not fuond"
exit 1
fi
. ./.env
echo '```'
echo -n "Date: "
date
echo "Version: ${VERSION}"
check_distro
check_docker
uname -a
hostname -I
id
echo -n "Hash: "
check_cmd shasum
if "$FOUND"; then
shasum -a 256 setup-fiware.sh
fi
check_cmd git
if "$FOUND" && [ -e .git ]; then
echo "git-hash: "
git log -n 3 --pretty=%H
fi
echo -n "Install: "
if [ -e .install ]; then
echo "in progress"
else
echo "completed"
fi
if [ -e docker-compose.yml ]; then
echo -e "\nDocker containers: "
docker compose ps
fi
if [ -e config.sh ]; then
echo -e "\nconfig.sh: "
cat config.sh
fi
echo -e '```'
EOF
chmod +x "${1}"
}
#
# Create clean.sh
#
create_clean_sh() {
logging_info "${FUNCNAME[0]}"
cat << 'EOF' > "${1}"
#!/bin/bash
FORCE_CLEAN=${FORCE_CLEAN:-false}
if [ "${FORCE_CLEAN}" != "true" ]; then
echo "!CAUTION! This command cleans up your FIWARE instance including your all data."
# shellcheck disable=SC2162
read -p "Are you sure you want to run it? (Y/n): " ans
if [ "${ans}" != "Y" ]; then
echo "Canceled"
exit 1
fi
fi
if [ -e docker-compose.yml ]; then
set +e
sudo docker compose down --remove-orphans
set -e
sleep 5
fi
for i in ./data ./config .env setup_ngsi_go.sh docker-compose.yml Makefile
do
if [ -e "${i}" ]; then
sudo rm -fr "${i}"
fi
done
EOF
chmod +x "${1}"
}
#
# Create info.sh
#
create_info_sh() {
logging_info "${FUNCNAME[0]}"
{
echo "#!/bin/bash";
echo "cat <<EOF";
echo "Service URLs:";
echo " Orion: http://${IP_ADDRESS}:1026";
} > "${1}"
${CYGNUS} && echo " Cygnus: http://${IP_ADDRESS}:5080" >> "${1}"
${COMET} && echo " Comet: http://${IP_ADDRESS}:8666" >> "${1}"
${WIRECLOUD} && {
echo " WireCloud: http://${IP_ADDRESS}";
echo " Ngsiproxy: http://${IP_ADDRESS}:3000";
} >> "${1}"
${IOTAGENT} && echo " IoT Agent: http://${IP_ADDRESS}:4041" >> "${1}"
${IOTAGENT_HTTP} && echo " over HTTP: http://${IP_ADDRESS}:7896${IOTA_DEFAULT_RESOURCE}" >> "${1}"
${IOTAGENT_MQTT} && echo " over MQTT: http://${IP_ADDRESS}:1883" >> "${1}"
${QUANTUMLEAP} && echo " Quantumleap: http://${IP_ADDRESS}:8668" >> "${1}"
${PERSEO} && echo " Perseo: http://${IP_ADDRESS}:9090" >> "${1}"
${NODE_RED} && echo " Node-RED: http://${IP_ADDRESS}:1880" >> "${1}"
{
echo "Sanity check:";
echo " ngsi version --host orion.local";
} >> "${1}"
${CYGNUS} && echo " ngsi version --host cygnus.local" >> "${1}"
${COMET} && echo " ngsi version --host comet.local" >> "${1}"
${WIRECLOUD} && echo " ngsi version --host wirecloud.local" >> "${1}"
${IOTAGENT} && echo " ngsi version --host iotagent.local" >> "${1}"
${QUANTUMLEAP} && echo " ngsi version --host quantumleap.local" >> "${1}"
${PERSEO} && echo " ngsi version --host perseo.local" >> "${1}"
{
echo "docs: https://fi-sb.letsfiware.jp/";
echo "Please see the .env file for details.";
echo "EOF";
} >> "${1}"
chmod +x "${1}"
}
#
# Create Makefile
#
create_makefile() {
logging_info "${FUNCNAME[0]}"
mkdir "${SCRIPT_DIR}"
local CLEAN_SH
CLEAN_SH="${SCRIPT_DIR}"/clean.sh
create_clean_sh "${CLEAN_SH}"
local COLLECT_SH
COLLECT_SH="${SCRIPT_DIR}"/collect.sh
create_collect_sh "${COLLECT_SH}"
local INFO_SH
INFO_SH="${SCRIPT_DIR}"/info.sh
create_info_sh "${INFO_SH}"
cat <<EOF > "${MAKEFILE}"
up:
sudo docker compose up -d
down:
sudo docker compose down
ps:
sudo docker compose ps
clean:
@${CLEAN_SH}
collect:
@${COLLECT_SH}
info:
@${INFO_SH}
EOF
if "${WIRECLOUD}"; then
cat <<EOF >> "${MAKEFILE}"
createsuperuser:
sudo docker compose exec wirecloud python manage.py createsuperuser
EOF
fi
}
#
# Init docker compose file
#
init_docker_compose_yml() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' > "${DOCKER_COMPOSE_YML}"
services:
EOF
}
#
# Add to docker_compose.yml
#
add_to_docker_compose_yml() {
sed -i.bak -e "/${1}/s/^/ ${2}${SED_LF}/" "${DOCKER_COMPOSE_YML}"
}
#
# Setup mongo
#
setup_mongo() {
logging_info "${FUNCNAME[0]}"
if ${MONGO_INSTALLED}; then
return
fi
MONGO_INSTALLED=true
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
mongo:
image: ${IMAGE_MONGO}
command: --nojournal
volumes:
- ${CONFIG_DIR}/mongo/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
- ${DATA_DIR}/mongo-data:/data/db
restart: always
ports:
- 27017:27017
EOF
# create mongo-init.js
local MONGO_DIR
MONGO_DIR="${CONFIG_DIR}"/mongo
mkdir "${MONGO_DIR}"
cat << 'EOF' > "${MONGO_DIR}"/mongo-init.js
db = connect("localhost:27017/orion");
db.createCollection("entities");
db.entities.createIndex({"_id.servicePath": 1, "_id.id": 1, "_id.type": 1}, {unique: true});
db.entities.createIndex({"_id.type": 1});
db.entities.createIndex({"_id.id": 1});
EOF
cat << EOF >> "${DOT_ENV}"
# MongoDB
IMAGE_MONGO=${IMAGE_MONGO}
EOF
}
#
# Setup orion
#
setup_orion() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
orion:
image: ${IMAGE_ORION}
depends_on:
- mongo
entrypoint: ["sh", "-c", "if [ -e /tmp/contextBroker.pid ]; then rm /tmp/contextBroker.pid; fi; /usr/bin/contextBroker -fg -multiservice -ngsiv1Autocast -disableFileLog -dbURI mongodb://mongo"]
restart: always
ports:
- 1026:1026
EOF
cat << EOF >> "${DOT_ENV}"
# Orion
IMAGE_ORION=${IMAGE_ORION}
EOF
setup_mongo
}
#
# Setup mysql
#
setup_mysql() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
mysql:
image: ${IMAGE_MYSQL}
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
volumes:
- ${DATA_DIR}/mysql-data:/var/lib/mysql
restart: always
ports:
- 3306:3306
EOF
cat <<EOF >> "${DOT_ENV}"
# MySQL
IMAGE_MYSQL=${IMAGE_MYSQL}
MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
EOF
}
#
# Setup postgress
#
setup_postgres() {
logging_info "${FUNCNAME[0]}"
if "${POSTGRES_INSTALLED}"; then
return
else
POSTGRES_INSTALLED=true
fi
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
postgres:
image: ${IMAGE_POSTGRES}
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- ${DATA_DIR}/postgres-data:/var/lib/postgresql/data
restart: always
ports:
- 5432:5432
EOF
cat <<EOF >> "${DOT_ENV}"
# PostgreSQL
IMAGE_POSTGRES=${IMAGE_POSTGRES}
POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
EOF
}
#
# Setup elasticsearch
#
setup_elasticsearch() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
elasticsearch-db:
image: ${IMAGE_ELASTICSEARCH_DB}
# __ELASTICSEARCH_PORTS__
volumes:
- ${DATA_DIR}/elasticsearch-db:/usr/share/elasticsearch/data
environment:
ES_JAVA_OPTS: ${ELASTICSEARCH_JAVA_OPTS}
ELASTIC_PASSWORD: ${ELASTICSEARCH_PASSWORD}
# Use single node discovery in order to disable production mode and avoid bootstrap checks.
# see: https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
discovery.type: single-node
healthcheck:
test: curl http://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
interval: 30s
timeout: 10s
retries: 5
restart: always
ports:
- 9200:9200
EOF
mkdir -p "${DATA_DIR}"/elasticsearch-db
cat <<EOF >> "${DOT_ENV}"
# Elasticsearch
IMAGE_ELASTICSEARCH_DB=${IMAGE_ELASTICSEARCH_DB}
ELASTICSEARCH_JAVA_OPTS="-Xmx256m -Xms256m"
ELASTICSEARCH_PASSWORD=${ELASTICSEARCH_PASSWORD}
EOF
}
#
# Setup comet
#
setup_comet() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
comet:
image: ${IMAGE_COMET}
init: true
depends_on:
# __COMET_DEPENDS_ON__
- mongo
networks:
- default
environment:
- STH_HOST=0.0.0.0
- STH_PORT=8666
- DB_PREFIX=sth_
- DB_URI=mongo:27017
- LOGOPS_LEVEL=${COMET_LOGOPS_LEVEL}
healthcheck:
test: curl --fail -s http://comet:8666/version || exit 1
ports:
- 8666:8666
restart: always
EOF
if [ "${CYGNUS_MONGO}" = "true" ]; then
add_to_docker_compose_yml "__COMET_DEPENDS_ON__" " - cygnus"
fi
cat <<EOF >> "${DOT_ENV}"
# Comet
IMAGE_COMET=${IMAGE_COMET}
COMET_LOGOPS_LEVEL=${COMET_LOGOPS_LEVEL}
EOF
}
#
# Setup cygnus
#
setup_cygnus() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
cygnus:
image: ${IMAGE_CYGNUS}
depends_on:
# __CYGNUS_DEPENDS_ON__
environment:
- CYGNUS_API_PORT=5080
# __CYGNUS_ENVIRONMENT__
- CYGNUS_LOG_LEVEL=${CYGNUS_LOG_LEVEL}
healthcheck:
test: curl --fail -s http://cygnus:5080/v1/version || exit 1
restart: always
ports:
- 5080:5080
# __CYGNUS_PORTS__
EOF
local sink
sink=0
if ${CYGNUS_MONGO}; then
sink=$((sink+1))
setup_mongo
add_to_docker_compose_yml "__CYGNUS_DEPENDS_ON__" " - mongo"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MONGO_SERVICE_PORT=5051"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MONGO_HOSTS=mongo:27017"
add_to_docker_compose_yml "__CYGNUS_PORTS__" " - 5051:5051"
fi
if ${CYGNUS_MYSQL}; then
sink=$((sink+1))
setup_mysql
add_to_docker_compose_yml "__CYGNUS_DEPENDS_ON__" " - mysql"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MYSQL_SERVICE_PORT=5050"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MYSQL_HOST=mysql"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MYSQL_PORT=3306"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MYSQL_USER=root"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MYSQL_PASS=\${MYSQL_ROOT_PASSWORD}"
add_to_docker_compose_yml "__CYGNUS_PORTS__" " - 5050:5050"
fi
if ${CYGNUS_POSTGRES}; then
sink=$((sink+1))
setup_postgres
add_to_docker_compose_yml "__CYGNUS_DEPENDS_ON__" " - postgres"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_POSTGRESQL_SERVICE_PORT=5055"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_POSTGRESQL_HOST=postgres"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_POSTGRESQL_PORT=5432"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_POSTGRESQL_USER=postgres"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_POSTGRESQL_PASS=\${POSTGRES_PASSWORD}"
add_to_docker_compose_yml "__CYGNUS_PORTS__" " - 5055:5055"
fi
if ${CYGNUS_ELASTICSEARCH}; then
sink=$((sink+1))
setup_elasticsearch
add_to_docker_compose_yml "__CYGNUS_DEPENDS_ON__" " - elasticsearch-db"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_ELASTICSEARCH_HOST=elasticsearch-db:9200"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_ELASTICSEARCH_PORT=5058"
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_ELASTICSEARCH_SSL=false"
add_to_docker_compose_yml "__CYGNUS_PORTS__" " - 5058:5058"
fi
if [ $sink -ge 2 ]; then
add_to_docker_compose_yml "__CYGNUS_ENVIRONMENT__" " - CYGNUS_MULTIAGENT=true"
fi
cat <<EOF >> "${DOT_ENV}"
# Cygnus
IMAGE_CYGNUS=${IMAGE_CYGNUS}
CYGNUS_LOG_LEVEL=${CYGNUS_LOG_LEVEL}
EOF
}
#
# mosquitto
#
setup_mosquitto() {
logging_info "${FUNCNAME[0]}"
cat <<'EOF' >> "${DOCKER_COMPOSE_YML}"
mosquitto:
image: ${IMAGE_MOSQUITTO}
volumes:
- ${CONFIG_DIR}/mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
- ${DATA_DIR}/mosquitto:/mosquitto/data
restart: always
ports:
- 1883:1883
EOF