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