bca028162c6e69343a9a4d3c7c941ab84b5db92b
[nonrtric/plt/ranpm.git] / docker-proj / docker-setup.sh
1 #!/bin/bash
2
3 #  ============LICENSE_START===============================================
4 #  Copyright (C) 2023 Nordix Foundation. All rights reserved.
5 #  ========================================================================
6 #  Licensed under the Apache License, Version 2.0 (the "License");
7 #  you may not use this file except in compliance with the License.
8 #  You may obtain a copy of the License at
9 #
10 #       http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #  Unless required by applicable law or agreed to in writing, software
13 #  distributed under the License is distributed on an "AS IS" BASIS,
14 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #  See the License for the specific language governing permissions and
16 #  limitations under the License.
17 #  ============LICENSE_END=================================================
18 #
19
20 print_usage() {
21     echo "Usage: docker-setup.sh"
22     exit 1
23 }
24
25 check_error() {
26     if [ $1 -ne 0 ]; then
27         echo "Failed $2"
28         echo "Exiting..."
29         exit 1
30     fi
31 }
32
33 setup_init() {
34 echo "Cleaning previously started containers..."
35
36 ./docker-tear-down.sh
37
38 echo "Docker pruning"
39 docker system prune -f
40 docker volume prune -f
41
42 echo "Creating dir for minio volume mapping"
43
44 mkdir -p /tmp/minio-test
45 mkdir -p /tmp/minio-test/0
46 rm -rf /tmp/minio-test/0/*
47
48 NW="nonrtric-docker-net"
49 echo "Creating docker network"
50 docker network inspect $NW 2> /dev/null 1> /dev/null
51 if [ $? -ne 0 ]; then
52     docker network create $NW
53 else
54     echo "  Network: $NW exits"
55 fi
56 }
57
58 pull_image() {
59 if [ -z "$(docker images -q $1)" ]; then
60    echo "Pulling image... "$1
61    docker pull $1
62    check_error $?
63 fi
64 }
65
66 check_images(){
67 export KEYCLOAK_IMAGE=quay.io/keycloak/keycloak:20.0.1
68 pull_image $KEYCLOAK_IMAGE
69
70 export OPA_IMAGE=openpolicyagent/opa:latest-envoy
71 pull_image $OPA_IMAGE
72
73 export BUNDLE_IMAGE=nginx:1.21
74 pull_image $BUNDLE_IMAGE
75
76 export MINIO_IMAGE=minio/minio
77 pull_image $MINIO_IMAGE
78
79 export REDPANDA_IMAGE=redpandadata/console:v2.2.3
80 pull_image $REDPANDA_IMAGE
81
82 export STRIMZI_IMAGE=quay.io/strimzi/kafka:0.35.0-kafka-3.4.0
83 pull_image $STRIMZI_IMAGE
84
85 export DMAAP_IMAGE=nexus3.onap.org:10002/onap/dmaap/dmaap-mr:1.4.4
86 pull_image $DMAAP_IMAGE
87
88 export VES_COLLECTOR_IMAGE=nexus3.onap.org:10002/onap/org.onap.dcaegen2.collectors.ves.vescollector:1.12.3
89 pull_image $VES_COLLECTOR_IMAGE
90
91 export ICS_IMAGE="nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-informationcoordinatorservice:1.5.0"
92 pull_image $ICS_IMAGE
93
94 export DMAAPADP_IMAGE="nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-pmproducer:1.0.1"
95 pull_image $DMAAPADP_IMAGE
96
97 export DFC_IMAGE="nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-ranpm-datafilecollector:1.0.0"
98 pull_image $DFC_IMAGE
99
100 export KPX_IMAGE="nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-ranpm-pm-file-converter:1.0.1"
101 pull_image $KPX_IMAGE
102
103 export AUTH_TOKEN_IMAGE=nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-auth-token-fetch:1.1.1
104 pull_image $AUTH_TOKEN_IMAGE
105 }
106
107 setup_keycloak() {
108 ./config/keycloak/certs/gen-certs.sh
109 echo "Starting containers for: keycloak, opa"
110 envsubst  '$KEYCLOAK_IMAGE,$OPA_IMAGE,$BUNDLE_IMAGE' < docker-compose-security.yaml > docker-compose-security_gen.yaml
111 docker-compose -p security -f docker-compose-security_gen.yaml up -d
112 }
113
114 populate_keycloak(){
115 # Create realm in keycloak
116 . scripts/populate_keycloak.sh
117
118 create_realms nonrtric-realm
119 while [ $? -ne 0 ]; do
120     create_realms nonrtric-realm
121 done
122
123 # Create client for admin calls
124 cid="console-setup"
125 create_clients nonrtric-realm $cid
126 check_error $?
127 generate_client_secrets nonrtric-realm $cid
128 check_error $?
129
130 echo ""
131
132 cid="console-setup"
133 __get_admin_token
134 TOKEN=$(get_client_token nonrtric-realm $cid)
135
136 cid="kafka-producer-pm-xml2json"
137 create_clients nonrtric-realm $cid
138 check_error $?
139 generate_client_secrets nonrtric-realm $cid
140 check_error $?
141
142 export XML2JSON_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
143
144 cid="pm-producer-json2kafka"
145 create_clients nonrtric-realm $cid
146 check_error $?
147 generate_client_secrets nonrtric-realm $cid
148 check_error $?
149
150 export JSON2KAFKA_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
151
152 cid="dfc"
153 create_clients nonrtric-realm $cid
154 check_error $?
155 generate_client_secrets nonrtric-realm $cid
156 check_error $?
157
158 export DFC_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
159 }
160
161 setup_kafka() {
162 echo "Starting containers for: kafka, zookeeper, kafka client, ics, minio"
163 envsubst  '$DMAAP_IMAGE,$VES_COLLECTOR_IMAGE,$ICS_IMAGE,$REDPANDA_IMAGE,$STRIMZI_IMAGE,$MINIO_IMAGE' < docker-compose-k1.yaml > docker-compose-k1_gen.yaml
164 docker-compose -p common -f docker-compose-k1_gen.yaml up -d
165 }
166
167 create_topics() {
168 echo "Creating topics: $TOPICS, may take a while ..."
169 for t in $TOPICS; do
170     retcode=1
171     rt=43200000
172     echo "Creating topic $t with retention $(($rt/1000)) seconds"
173     while [ $retcode -ne 0 ]; do
174         docker exec -it common-kafka-1-1 ./bin/kafka-topics.sh \
175                 --create --topic $t --config retention.ms=$rt  --bootstrap-server kafka-1:9092
176         retcode=$?
177     done
178 done
179 }
180
181 setup_dfc() {
182 export NUM_DFC=1
183 echo "Starting $NUM_DFC dfc"
184 export DFC_MINIO=http://minio-server:9000
185 export FILES_VOLUME="/pm-files"
186
187 cwd=$PWD
188 for (( i=1; i<=$NUM_DFC; i++ )); do
189     echo "Updating dfc$i truststore"
190     cd $cwd/config/dfc$i
191     cp ../dfc-common/template-truststore.jks truststore.jks
192     check_error $?
193
194     echo " Adding https ca cert to dfc$i truststore"
195     keytool -importcert -alias pm-https -file $cwd/config/https/certs/httpsca.crt -keystore truststore.jks -storetype JKS -storepass $(cat ../dfc-common/truststore.pass) -noprompt
196     check_error $?
197 done
198 cd $cwd
199
200 chmod 666 config/dfc1/token-cache/jwt.txt
201 envsubst < docker-compose-dfc1.yaml > docker-compose-dfc_gen.yaml
202 envsubst < config/dfc1/application-template.yaml > config/dfc1/application.yaml
203 docker-compose -p dfc -f docker-compose-dfc_gen.yaml up -d
204 }
205
206 setup_producers() {
207 echo "Starting producers"
208 chmod 666 config/pmpr/token-cache/jwt.txt
209 export KPADP_MINIO=http://minio-server:9000
210 cp config/pmpr/application_configuration-minio-template.json config/pmpr/application_configuration.json
211 envsubst < config/pmpr/application-minio-template.yaml > config/pmpr/application.yaml
212
213 envsubst < docker-compose-producers.yaml > docker-compose-producers_gen.yaml
214 docker-compose -p prod -f docker-compose-producers_gen.yaml up -d
215 }
216
217 create_http_servers_certs() {
218 export NUM_HTTP=10
219 echo ""
220 ./config/https/certs/gen-certs.sh $NUM_HTTP
221 }
222
223 setup_http_servers() {
224 cp pm-files/pm* ne-files
225
226 echo "Starting http servers"
227 export PM_HTTPSSERVER_IMAGE="pm-https-server:latest"
228
229 total_lines=$(cat docker-compose-pm-https.yaml | wc -l)
230 services_line=$(grep -n "services:" docker-compose-pm-https.yaml| cut -f1 -d:)
231 let remaining_lines=$total_lines-$services_line
232 export START_TIME=$(date +%Y%m%d.%H%M -d '3 hours ago')
233
234 grep -B $services_line "services:" docker-compose-pm-https.yaml > docker-compose-pm-https_gen.yaml
235 for (( i=1; i<=$NUM_HTTP; i++ )); do
236    export CONTAINER_NUM=$i
237    grep -A $remaining_lines "services:" docker-compose-pm-https.yaml | grep -v "services:" | \
238    envsubst  '$CONTAINER_NUM,$PM_HTTPSSERVER_IMAGE,$START_TIME' >> docker-compose-pm-https_gen.yaml
239 done
240 docker-compose -p pm-https -f docker-compose-pm-https_gen.yaml up -d
241 }
242
243 ## Main ##
244 export KAFKA_NUM_PARTITIONS=10
245 export TOPICS="file-ready collected-file json-file-ready-kp json-file-ready-kpadp pmreports"
246
247 setup_init
248
249 check_images
250
251 setup_keycloak
252 check_error $?
253
254 # Wait for keycloak to start
255 echo 'Waiting for keycloak to be ready'
256 until [ $(curl -s -w '%{http_code}' -o /dev/null 'http://localhost:8462') -eq 200 ];
257 do
258         echo -n '.'
259         sleep 2
260 done
261 echo ""
262 populate_keycloak
263
264 setup_kafka
265 check_error $?
266
267 create_topics
268
269 create_http_servers_certs
270 check_error $?
271
272 setup_dfc
273 check_error $?
274
275 setup_producers
276 check_error $?
277
278 setup_http_servers
279 check_error $?
280
281 scripts/clean-shared-volume.sh