-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheasy-setup
executable file
·939 lines (849 loc) · 28.4 KB
/
easy-setup
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
#!/usr/bin/env bash
[ "${debug}" == "1" ] && set -o xtrace
[ -z "${EVA_REPOSITORY_URL}" ] && EVA_REPOSITORY_URL=https://pub.bma.ai/eva3
export EVA_REPOSITORY_URL
PYTHON3_MIN=6
UC_ENABLED=
LM_ENABLED=
SFA_ENABLED=
FORCE=0
CLEAR=0
APPEND=0
APPEND_MQTT=0
INTERACTIVE=1
INSTALL_UC=0
INSTALL_LM=0
INSTALL_SFA=0
MQTT_HOST=
MQTT_PORT=
MQTT_USER=
MQTT_PASSWORD=
MQTT_SPACE=
MQTT_CAFILE=
MQTT_CERT=
MQTT_KEY=
MQTT_ANNOUNCE_ENABLED=0
MQTT_DISCOVERY_ENABLED=0
MQTT_NO_RETAIN=0
LINK=0
BENCHMARK=0
CLOUDMANAGER=0
DEFAULT_CA_FILE=/etc/ssl/certs/ca-certificates.crt
DEPLOY_FILE=
FEATURES=
REMOTES='0.0.0.0/0'
VALUE=
REQUIRED="realpath python3 curl jq hostname"
MAX_START_WAIT=180
function on_exit {
local err=$?
if [ $err -ne 0 ]; then
echo
FORCE_ALL=1 ./sbin/eva-control stop
./sbin/registry-control stop
echo
echo "SETUP FAILED, CODE: $err"
fi
}
function usage {
cat <<EOF
Usage: easy-setup [--force] [--clear] [--auto] [--local-only] [-u USER]
[--link] [--mqtt user:password@host:port/space] [-p {uc,lm,sfa,all}]
[--cloud-manager]
Options:
--clear clear runtime and configs (recommended with --force.
WARNING!!! destroys all data)
--force force install even if configs are already present
--skip-check skip virtualenv check
--auto perform automatic (unattended) setup
Options for the automatic setup:
--local-only accept API keys (except operator) only from local host
-u USER set up all controllers to run under specified user
--link link all controllers statically
WARNING: it's not possible to change controller id
after linking. IDs of all controllers are automatically
set to the current host name. If you want to use different
IDs, prepare etc/uc.ini and etc/lm.ini files first and
set 'name' key there
--mqtt user:password@host:port/space
specify MQTT server access
--mqtt-cafile FILE MQTT CA file to enable MQTT via SSL
--mqtt-cert FILE MQTT authorization cert
--mqtt-key FILE MQTT authorization key
--mqtt-announce Enable MQTT announces and API calls via MQTT
--mqtt-discovery Enable autodiscovery of other nodes via MQTT
--mqtt-disable-retain Disable MQTT retain (to work with Amazon IoT)
-p {uc|lm|sfa|all} specify which controller to set up or all, can be specified
several times
--cloud-manager set up SFA cloud manager
--feature NAME:PARAMS Install additional feature, can be specified several times
--deploy FILE deploy config from YAML file (requires cloud manager)
Env variables:
EVA_REPOSITORY_URL Alternative EVA ICS repository URL
VENV_CONFIG Alternative VENV config YAML to use/import
EOF
}
function option_error {
usage
exit 2
}
function start_controller() {
"./sbin/eva-control" start "$1" || return 5
I=0
sleep 0.5
./sbin/eva-tinyapi -C "$1" -F test > /dev/null 2>&1
R=$?
if [ $R -eq 9 ]; then
echo " configs missing!"
return 1
fi
echo -n "$1: ." | tr "[:lower:]" "[:upper:]"
while [ $R -ne 0 ]; do
if [ $((I++)) -gt ${MAX_START_WAIT} ]; then
echo " failed to start!"
return 2
fi
sleep 0.5
echo -n "."
./sbin/eva-tinyapi -C "$1" -F test > /dev/null 2>&1
R=$?
done
echo " started"
return 0
}
function save_controller_state {
./sbin/eva-tinyapi -C "$1" -F save > /dev/null 2>&1 || return 1
}
function create_operator_key {
local P=$1
if ./sbin/eva-tinyapi -C "$P" -F list_key_props i=operator > /dev/null 2>&1; then
echo "Key 'operator' already exists, recreating"
./sbin/eva-tinyapi -C "$P" -F destroy_key i=operator > /dev/null 2>&1 || return 1
fi
./sbin/eva-tinyapi -C "$P" -F create_key \
i=operator > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=operator p=key v="$OPKEY" > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=operator p=groups v='#' > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=operator p=pvt v='#' > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=operator p=allow v=lock > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=operator p=sysfunc v=1 > /dev/null 2>&1 || return 1
return 0
}
function create_operator_user {
local P=$1
./sbin/eva-tinyapi -C "$P" -F destroy_user u=operator > /dev/null 2>&1
./sbin/eva-tinyapi -C "$P" -F create_user \
u=operator p="${OPPASSWD}" a=operator > /dev/null 2>&1 || return 1
return 0
}
function create_default_key {
local P=$1
local ALLOW=$2
if ./sbin/eva-tinyapi -C "$P" -F list_key_props i=default > /dev/null 2>&1; then
echo "Key 'default' already exists, recreating"
./sbin/eva-tinyapi -C "$P" -F destroy_key i=default > /dev/null 2>&1 || return 1
fi
./sbin/eva-tinyapi -C "$P" -F create_key \
i=default > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=default p=key v="$DEFAULTKEY" > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=default p=groups v='#' > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=default p=allow v="$ALLOW" > /dev/null 2>&1 || return 1
./sbin/eva-tinyapi -C "$P" -F set_key_prop \
i=default p=hosts_allow v="$REMOTES" > /dev/null 2>&1 || return 1
return 0
}
function create_notifier {
local T=$1
local BATCH=
if [ "$MQTT_HOST" ]; then
echo "Creating notifier for ${T}"
local sp=
[ "${MQTT_SPACE}" ] && local sp="-s ${MQTT_SPACE}"
local auth=
[ "$MQTT_USER" ] && local auth="${MQTT_USER}"
[ "$MQTT_PASSWORD" ] && local auth="$auth:${MQTT_PASSWORD}"
[ "$auth" ] && auth="${auth}@"
local port=
[ "${MQTT_PORT}" ] && port=":${MQTT_PORT}"
BATCH="create eva_1 mqtt:${auth}${MQTT_HOST}${port} ${sp} -y"
if [ "$MQTT_CAFILE" ]; then
BATCH="$BATCH;set eva_1 ca_certs $MQTT_CAFILE"
fi
if [ "$MQTT_CERT" ]; then
BATCH="$BATCH;set eva_1 certfile $MQTT_CERT"
fi
if [ "$MQTT_KEY" ]; then
BATCH="$BATCH;set eva_1 keyfile $MQTT_KEY"
fi
BATCH="$BATCH;subscribe state eva_1 -p '#' -g '#'"
BATCH="$BATCH;subscribe log eva_1"
BATCH="$BATCH;subscribe server eva_1"
if [ "$MQTT_ANNOUNCE_ENABLED" == "1" ] && [ "$T" != 'sfa' ]; then
BATCH="$BATCH;set eva_1 api_enabled 1"
BATCH="$BATCH;set eva_1 announce_interval 30"
fi
[ "$MQTT_DISCOVERY_ENABLED" == "1" ] && [ "$T" != 'uc' ] && \
BATCH="$BATCH;set eva_1 discovery_enabled 1"
[ "$MQTT_NO_RETAIN" = "1" ] && BATCH="$BATCH;set eva_1 retain_enabled 0"
BATCH="$BATCH;test eva_1"
if [ "$T" = "sfa" ]; then
[ "$BATCH" ] && BATCH="$BATCH;"
BATCH="${BATCH}set eva_1 collect_logs 1"
fi
fi
[ -z "${BATCH}" ] && return 0
echo "$BATCH" | "./bin/$T-notifier" -R --exec-batch=stdin
return $?
}
function create_db_notifier {
local T=$1
local BATCH="create db_1 db:runtime/db/sfa_history.db -y;subscribe state db_1 -p '#' -g '#'"
echo "$BATCH" | "./bin/$T-notifier" -R --exec-batch=stdin
return $?
}
function check_required_exec {
local p=$1
echo -n "Checking $p => "
if ! RESULT=$(command -v "$p" 2>&1); then
echo "Missing! Please install"
return 1
fi
echo "${RESULT}"
return 0
}
function askYN {
if [ "$INTERACTIVE" != "1" ]; then
VALUE=2
return
fi
local v=
if [ "$2" == "N" ]; then
local default=N
local default_val=0
else
local default=Y
local default_val=1
fi
while [ -z "$v" ]; do
local P="$1 (Y/N, default ${default})? "
read -rp "${P}" a
if [ -z "$a" ]; then
v=${default_val}
echo -en "\\033[1A\\033[2K"
echo "${P}${default}"
else
case $a in
y|Y)
v=1
;;
n|N)
v=0
;;
esac
fi
done
VALUE=$v
}
function askMQTT {
while true; do
if [ "$INTERACTIVE" == "1" ]; then
echo -n "MQTT host, required for cloud setup (use empty value to skip MQTT connection for now): "
read -r MQTT_HOST
if [ -z "${MQTT_HOST}" ]; then
return
fi
echo -n "MQTT port [1883]: "
read -r MQTT_PORT
[ -z "${MQTT_PORT}" ] && MQTT_PORT=1883
MQTT_USER=
echo -n "MQTT user (empty for none): "
read -r MQTT_USER
MQTT_PASSWORD=
if [ "$MQTT_USER" ]; then
echo -n "MQTT password (empty for none): "
read -rs MQTT_PASSWORD
echo
fi
echo -n "MQTT space (empty for the root hive): "
read -r MQTT_SPACE
askYN "Enable MQTT SSL" N
MQTT_CAFILE=
if [ "$VALUE" = "1" ]; then
while true; do
echo -n "MQTT CA file ($DEFAULT_CA_FILE): "
read -r MQTT_CAFILE
[ -z "$MQTT_CAFILE" ] && MQTT_CAFILE=$DEFAULT_CA_FILE
if [ ! -f "$MQTT_CAFILE" ]; then
echo "No such file: $MQTT_CAFILE"
else
break
fi
done
while true; do
echo -n "MQTT cert file (empty for none): "
read -r MQTT_CERT
[ -z "$MQTT_CERT" ] || [ -f "$MQTT_CERT" ] ; break
done
if [ "$MQTT_CERT" ]; then
while true; do
echo -n "MQTT key file (empty for none): "
read -r MQTT_KEY
[ -z "$MQTT_KEY" ] || [ -f "$MQTT_KEY" ] ; break
done
fi
fi
else
if [ -z "${MQTT_HOST}" ]; then
return
fi
fi
SSL_OPTS=
if [ "$MQTT_CAFILE" ]; then
SSL_OPTS="--cafile $MQTT_CAFILE"
[ "$MQTT_CERT" ] && SSL_OPTS="$SSL_OPTS --cert $MQTT_CERT"
[ "$MQTT_KEY" ] && SSL_OPTS="$SSL_OPTS --key $MQTT_KEY"
fi
if ! ./sbin/check-mqtt ${SSL_OPTS} \
"${MQTT_USER}:${MQTT_PASSWORD}@${MQTT_HOST}:${MQTT_PORT}/${MQTT_SPACE}" > /dev/null 2>&1
then
if [ "${INTERACTIVE}" != "1" ]; then
echo "MQTT test failed"
exit 5
fi
else
if [ "$INTERACTIVE" == "1" ]; then
askYN "Enable node announcements and API calls via MQTT"
MQTT_ANNOUNCE_ENABLED=$VALUE
askYN "Enable autodiscovery of other nodes via MQTT"
MQTT_DISCOVERY_ENABLED=$VALUE
askYN "Disable MQTT retain topics (to work with Amazon IoT)" N
MQTT_NO_RETAIN=$VALUE
fi
return
fi
echo "MQTT test failed"
done
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--link)
LINK=1
shift
;;
--mqtt)
M=$2
if (echo "$M"|grep '@' > /dev/null); then
MQTT_AUTH=$(echo "$M"|cut -d@ -f1)
MQTT_USER=$(echo "$MQTT_AUTH"|cut -d: -f1)
( echo "$MQTT_AUTH" |grep ':' ) > /dev/null && \
MQTT_PASSWORD=$(echo "$M"|cut -d@ -f1|cut -d: -f2)
fi
MQTT_HOST=$(echo "$M"|cut -d@ -f2|cut -d: -f1|cut -d/ -f1)
MQTT_PORT=$(echo "$M"|cut -d@ -f2 |cut -d/ -f1|awk -F: '{ print $2 }')
[ -z "$MQTT_PORT" ] && MQTT_PORT=1883
( echo "$M" |grep '/' > /dev/null ) && MQTT_SPACE=$(echo "$M"|cut -d/ -f2-)
shift
shift
;;
--mqtt-cafile)
if [ ! -f "$2" ]; then
echo "No such MQTT CA file: $2"
exit 5
fi
MQTT_CAFILE=$2
shift
shift
;;
--mqtt-cert)
if [ ! -f "$2" ]; then
echo "No such MQTT cert file: $2"
exit 5
fi
MQTT_CERT=$2
shift
shift
;;
--mqtt-key)
if [ ! -f "$2" ]; then
echo "No such MQTT key file: $2"
exit 5
fi
MQTT_KEY=$2
shift
shift
;;
--mqtt-announce)
MQTT_ANNOUNCE_ENABLED=1
shift
;;
--mqtt-discovery)
MQTT_DISCOVERY_ENABLED=1
shift
;;
--mqtt-disable-retain)
MQTT_NO_RETAIN=1
shift
;;
--feature)
FEATURES="$FEATURES $2"
shift
shift
;;
-p)
case $2 in
uc|UC)
INSTALL_UC=1
;;
lm|LM)
INSTALL_LM=1
;;
sfa|SFA)
INSTALL_SFA=1
;;
all)
INSTALL_UC=1
INSTALL_LM=1
INSTALL_SFA=1
;;
*)
option_error
;;
esac
shift
shift
;;
--local-only)
echo "This node will be installed with local-only key access"
REMOTES="127.0.0.1"
shift
;;
--force)
echo "WARNING: using force installation, will stop EVA and remove old configs"
FORCE=1
shift
;;
--clear)
echo "WARNING: asked to clear config. Will remove all files in runtime"
CLEAR=1
shift
;;
--skip-check)
SKIP_CHECK=1
shift
;;
--skip-venv)
SKIP_VENV=1
shift
;;
-h|--help)
usage
exit 0
;;
--auto)
INTERACTIVE=0
echo "Will perform automatic install"
shift
;;
--cloud-manager)
CLOUDMANAGER=1
shift
;;
--deploy)
DEPLOY_FILE=$2
shift
shift
;;
--benchmark)
echo "Will setup UC and do benchmark"
INTERACTIVE=0
INSTALL_UC=1
BENCHMARK=1
shift
;;
*)
option_error
;;
esac
done
trap on_exit exit
if [ -z $SKIP_CHECK ]; then
e=0
for r in ${REQUIRED}; do
check_required_exec "$r" || e=1
done
[ $e -ne 0 ] && exit 1
fi
EVA_DIR=$(dirname "$(realpath "$0")")
cd "${EVA_DIR}" || exit 11
./install/mklinks || exit 1
# Install YEDB
mkdir -p var
./install/install-yedb || exit 3
./sbin/registry-control restart || exit 1
REGISTRY_INI=${EVA_DIR}/etc/eva_config
[ -f "${REGISTRY_INI}" ] && source "${REGISTRY_INI}"
[ -z "${SYSTEM_NAME}" ] && SYSTEM_NAME=$(hostname)
# Create VENV key if not present
PYTHON=
if [ "$VENV_CONFIG" ] || ! AUTO_PREFIX=1 ./sbin/eva-registry-cli get config/venv >& /dev/null; then
[ "$VENV_CONFIG" ] && VENV_CFG=$VENV_CONFIG || VENV_CFG=./lib/eva/registry/setup-defaults/config/venv.yml
./sbin/eva-registry-cli set ".schema/eva3/${SYSTEM_NAME}/config/venv" \
- -p yaml < ./lib/eva/registry/schema-venv.yml > /dev/null || exit 8
AUTO_PREFIX=1 ./sbin/eva-registry-cli set \
config/venv - -p yaml < $VENV_CFG > /dev/null || exit 8
fi
source <(./sbin/key-as-source config/venv 2>/dev/null)
PYTHON_MINOR=$($PYTHON --version|cut -d. -f2)
if [ "$PYTHON_MINOR" -lt "$PYTHON3_MIN" ]; then
echo "Python 3.$PYTHON3_MIN is required"
exit 1
fi
echo
source <(./sbin/key-as-source config/uc/service UC 2>/dev/null)
source <(./sbin/key-as-source config/lm/service LM 2>/dev/null)
source <(./sbin/key-as-source config/sfa/service SFA 2>/dev/null)
if [ "$UC_ENABLED" == "1" ] || [ "${LM_ENABLED}" == "1" ] || [ "${SFA_ENABLED}" == "1" ]; then
if [ "$CLEAR" != "1" ]; then
echo "Installed system detected"
source <(./sbin/key-as-source data/easy_setup 2>/dev/null)
[ "$UC_ENABLED" == "1" ] && PRD=uc
[ "$LM_ENABLED" == "1" ] && PRD=lm
[ "$SFA_ENABLED" == "1" ] && PRD=sfa
echo "Will use settings from $PRD"
eval "$(./sbin/get-setup-options $PRD|grep -A999 ^MASTERKEY)" || exit 1
APPEND=1
fi
fi
[ "$MASTERKEY" ] || MASTERKEY=$( (tr -cd '[:alnum:]' < /dev/urandom | head -c64) 2>/dev/null)
[ "$DEFAULTKEY" ] || DEFAULTKEY=$( (tr -cd '[:alnum:]' < /dev/urandom | head -c64) 2>/dev/null)
[ "$OPKEY" ] || OPKEY=$( (tr -cd '[:alnum:]' < /dev/urandom | head -c64) 2>/dev/null)
[ "$OPPASSWD" ] || OPPASSWD=$( (tr -cd '[:alnum:]' < /dev/urandom | head -c16) 2>/dev/null)
if [ "$INTERACTIVE" == "1" ]; then
echo
echo "Your masterkey: ${MASTERKEY}"
echo "Your controller default key: ${DEFAULTKEY}"
echo "Your operator key: ${OPKEY}"
echo
cat << EOF
WARNING: If you want to run some components under restricted users, create
"var" and "log" folders in EVA installation dir and make sure the restricted
users have an access to these folders before continuing easy-setup. If you've
customized ini files in etc, make sure the restricted user has an access to
both <component>.ini and <component>_apikeys.ini.
Press ENTER to continue easy-setup, Ctrl+C to abort.
EOF
read -r
echo
fi
[ "$FORCE" == "1" ] || [ "$CLEAR" == "1" ] && (./sbin/eva-control stop > /dev/null 2>&1 )
if [ "$CLEAR" == "1" ]; then
KEYS_TO_CLEAR=(config/common config/clouds config/uc config/lm config/sfa config/watchdog data inventory)
for k in "${KEYS_TO_CLEAR[@]}"; do
AUTO_PREFIX=1 ./sbin/eva-registry-cli delete "${k}" -r > /dev/null || exit 8
done
rm -rf ./runtime/db || exit 1
./sbin/eva-registry-cli delete ".schema/${SYSTEM_NAME}" -r > /dev/null || exit 8
echo "WARNING: all configs are deleted"
fi
# check mods, build venv
if [ -z $SKIP_VENV ]; then
echo "Building VENV"
./install/build-venv || exit 1
echo
fi
./install/import-registry-schema || exit 8
./install/import-registry-defaults || exit 8
if [ "$PREPARE_REGISTRY_SCRIPT" ]; then
$PREPARE_REGISTRY_SCRIPT || exit $?
fi
# ASK SOME QUESTIONS
if [ "$APPEND" != "1" ]; then
echo
askYN "Should this node grant API access for the remote instances"
[ $VALUE == "0" ] && REMOTES="127.0.0.1"
askMQTT
elif [ ! "$MQTT_HOST" ]; then
askMQTT
echo
fi
[ "$MQTT_HOST" ] && APPEND_MQTT=1
if [ "$APPEND" != "1" ]; then
echo
askYN "Link controllers statically"
[ $VALUE == "1" ] && LINK=1
fi
# INSTALL UC
if [ "$UC_ENABLED" != "1" ] || [ "$FORCE" == "1" ]; then
echo
askYN "Install EVA Universal Controller on this host"
[ $VALUE == "1" ] && INSTALL_UC=1
elif [ "$APPEND_MQTT" == "1" ]; then
if ! ./bin/uc-notifier config eva_1 > /dev/null 2>&1; then
echo "Setting up MQTT for UC"
./sbin/eva-control stop uc || exit 1
create_notifier uc || exit 1
start_controller uc || exit 1
fi
else
echo "UC is already installed. Skipping"
INSTALL_UC=0
fi
echo
if [ "$INSTALL_UC" == "1" ]; then
echo "Installing EVA Universal Controller"
bash install/install-uc || exit 1
./sbin/key-set-flag config/uc/service:setup 1 > /dev/null || exit 8
./sbin/key-set-flag config/uc/service:enabled 1 > /dev/null || exit 8
echo
if [ "$BENCHMARK" == "1" ]; then
echo "Setting benchmark config"
AUTO_PREFIX=1 ./sbin/eva-registry-cli set config/uc/main \
- -p yaml < ./etc/setup/uc-benchmark.yml > /dev/null || exit 8
fi
echo "Setting UC master key"
(cat <<EOF
key: "${MASTERKEY}"
master: true
hosts-allow:
- ${REMOTES}
EOF
)| AUTO_PREFIX=1 ./sbin/eva-registry-cli set config/uc/apikeys/masterkey - -p yaml > /dev/null || exit 8
create_notifier uc || exit 1
if ! start_controller uc; then
echo "Unable to test UC"
./sbin/eva-control stop
exit 5
fi
echo "Creating UC keys"
create_operator_key uc || exit 21
create_default_key uc cmd,device || exit 22
save_controller_state uc || exit 23
echo
fi
# INSTALL LM
if [ "$LM_ENABLED" != "1" ] || [ "$FORCE" == "1" ]; then
askYN "Install EVA Logical Manager PLC on this host"
[ $VALUE == "1" ] && INSTALL_LM=1
elif [ "$APPEND_MQTT" == "1" ]; then
if ! ./bin/lm-notifier config eva_1 > /dev/null 2>&1; then
echo "Setting up MQTT for LM PLC"
./sbin/eva-control stop lm || exit 1
create_notifier lm || exit 1
start_controller lm || exit 1
fi
else
echo "LM is already installed. Skipping"
INSTALL_LM=0
fi
echo
if [ "$INSTALL_LM" == "1" ]; then
echo "Installing EVA LM PLC"
bash install/install-lm || exit 1
./sbin/key-set-flag config/lm/service:setup 1 > /dev/null || exit 8
./sbin/key-set-flag config/lm/service:enabled 1 > /dev/null || exit 8
echo
echo "Setting LM master key"
(cat <<EOF
key: "${MASTERKEY}"
master: true
hosts-allow:
- ${REMOTES}
EOF
)| AUTO_PREFIX=1 ./sbin/eva-registry-cli set config/lm/apikeys/masterkey - -p yaml > /dev/null || exit 8
create_notifier lm || exit 1
if ! start_controller lm; then
echo "Unable to test LM"
./sbin/eva-control stop
exit 5
fi
echo "Creating LM keys"
create_operator_key lm || exit 21
create_default_key lm lock || exit 22
save_controller_state lm || exit 23
if [ "$LINK" == "1" ]; then
if [ "$INSTALL_UC" == "1" ] || [ "$UC_ENABLED" == "1" ]; then
echo "Linking local UC to LM PLC"
SNAME=uc/$(./sbin/eva-tinyapi -C uc -F test|jq -Mr .system)
./sbin/eva-tinyapi -C lm -F setup_mode setup=1 > /dev/null 2>&1
./sbin/eva-tinyapi -C lm -F remove_controller i="${SNAME}" > /dev/null 2>&1
if ! ./sbin/eva-tinyapi -C lm -F append_controller \
u=http://localhost:8812 a=\$default save=1 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
if ! ./sbin/eva-tinyapi -C lm -F setup_mode setup=0 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
fi
fi
echo
fi
# INSTALL SFA
if [ "$SFA_ENABLED" != "1" ] || [ "$FORCE" == "1" ]; then
askYN "Install SCADA Final Aggregator on this host"
[ $VALUE == "1" ] && INSTALL_SFA=1
elif [ "$APPEND_MQTT" == "1" ]; then
if ! ./bin/sfa-notifier config eva_1 > /dev/null 2>&1; then
echo "Setting up MQTT for SFA"
./sbin/eva-control stop sfa || exit 1
create_notifier sfa || exit 1
start_controller sfa || exit 1
fi
else
echo "SFA is already installed. Skipping"
INSTALL_SFA=0
fi
echo
if [ "$INSTALL_SFA" == "1" ]; then
echo "Installing EVA SFA"
bash install/install-sfa || exit 1
askYN "Enable SFA Cloud Manager"
[ $VALUE == "1" ] && CLOUDMANAGER=1
./sbin/key-set-flag config/sfa/service:setup 1 > /dev/null || exit 8
./sbin/key-set-flag config/sfa/service:enabled 1 > /dev/null || exit 8
echo
if [ "$CLOUDMANAGER" == "1" ]; then
./sbin/key-set-flag config/sfa/main:cloud/cloud-manager 1 > /dev/null || exit 8
fi
echo "Setting SFA master key"
(cat <<EOF
key: "${MASTERKEY}"
master: true
hosts-allow:
- ${REMOTES}
EOF
)| AUTO_PREFIX=1 ./sbin/eva-registry-cli set config/sfa/apikeys/masterkey - -p yaml > /dev/null || exit 8
create_notifier sfa || exit 1
create_db_notifier sfa || exit 1
if ! start_controller sfa; then
echo "Unable to test SFA"
./sbin/eva-control stop
exit 5
fi
echo "Creating SFA keys"
create_operator_key sfa || exit 21
create_default_key sfa || exit 22
echo "Creating SFA operator user"
create_operator_user sfa || exit 21
save_controller_state sfa || exit 23
if [ "$LINK" == "1" ]; then
if [ "$INSTALL_UC" == "1" ] || [ "$UC_ENABLED" == "1" ]; then
echo "Linking local UC to SFA"
SNAME=uc/$(./sbin/eva-tinyapi -C uc -F test|jq -Mr .system)
./sbin/eva-tinyapi -C sfa -F setup_mode setup=1 > /dev/null 2>&1
./sbin/eva-tinyapi -C sfa -F remove_controller i="${SNAME}" > /dev/null 2>&1
if ! ./sbin/eva-tinyapi -C sfa -F append_controller \
g=uc u=http://localhost:8812 a=\$default save=1 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
if ! ./sbin/eva-tinyapi -C sfa -F setup_mode setup=0 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
if [ "$CLOUDMANAGER" == "1" ]; then
if ! ./sbin/eva-tinyapi -C sfa -F set_controller_prop \
i="${SNAME}" p=masterkey v=\$masterkey save=1 > /dev/null 2>&1; then
echo "Management API linking failed!"
./sbin/eva-control stop
exit 5
fi
fi
fi
if [ "$INSTALL_LM" == "1" ]; then
echo "Linking local LM PLC to SFA"
SNAME=lm/$(./sbin/eva-tinyapi -C lm -F test|jq -Mr .system)
./sbin/eva-tinyapi -C sfa -F setup_mode setup=1 > /dev/null 2>&1
./sbin/eva-tinyapi -C sfa -F remove_controller i="${SNAME}" > /dev/null 2>&1
if ! ./sbin/eva-tinyapi -C sfa -F append_controller \
g=lm u=http://localhost:8817 a=\$default save=1 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
if ! ./sbin/eva-tinyapi -C sfa -F setup_mode setup=0 > /dev/null 2>&1; then
echo "Linking failed!"
./sbin/eva-control stop
exit 5
fi
if [ "$CLOUDMANAGER" == "1" ]; then
if ! ./sbin/eva-tinyapi -C sfa -F set_controller_prop \
i="${SNAME}" p=masterkey v=\$masterkey save=1 > /dev/null 2>&1; then
echo "Management API linking failed!"
./sbin/eva-control stop
exit 5
fi
fi
fi
fi
echo
fi
# FEATURES
if [ "$FEATURES" ]; then
for f in $FEATURES; do
if [[ "$f" == *":"* ]]; then
feature=$(echo "$f" | cut -d: -f1)
params=$(echo "$f" | cut -d: -f2-)
else
feature=$f
params=
fi
./bin/eva feature setup "${feature}" ${params} || exit 12
done
fi
# COMPLETED
if [ "$APPEND_MQTT" == "1" ]; then
echo "MQTT connection is set up"
elif [ "$INSTALL_UC" != "1" ] && [ "$INSTALL_LM" != "1" ] && [ "$INSTALL_SFA" != "1" ]; then
echo "No products selected, nothing was installed"
exit 0
fi
ln -sf eva bin/eva-shell
echo "Setup completed!"
echo
echo "EVA ICS v$(./sbin/eva-tinyapi -V) build $(./sbin/eva-tinyapi -B)"
echo
echo "MASTERKEY: ${MASTERKEY}"
echo "Controller default key: ${DEFAULTKEY}"
echo "Operator key: ${OPKEY}"
echo
if [ "$INSTALL_SFA" == "1" ]; then
echo "Default user: operator"
echo "Password: ${OPPASSWD}"
echo
fi
mkdir -p backup
chmod 700 backup
./sbin/key-set-flag data/easy_setup:mqtt-discovery-enabled $MQTT_DISCOVERY_ENABLED > /dev/null || exit 8
./sbin/key-set-flag data/easy_setup:link $LINK > /dev/null || exit 8
if [ "${DEPLOY_FILE}" ]; then
if ! [[ ${DEPLOY_FILE} == http://* ]] && ! [[ ${DEPLOY_FILE} == https://* ]]; then
P=$(realpath "${DEPLOY_FILE}")
cd "$(dirname "${P}")" || exit 13
DEPLOY_FILE="$(basename "$DEPLOY_FILE")"
fi
"${EVA_DIR}"/bin/eva-shell sfa cloud deploy "${DEPLOY_FILE}" -s -y -c "srv=$(hostname)"
cd "${EVA_DIR}" || exit 13
fi
if [ "$BENCHMARK" == "1" ]; then
echo "Starting CRT benchmark"
echo
./tests/benchmark-uc-crt
./sbin/eva-control stop
fi
exit 0