Helm charts and apps for pm-setup
[nonrtric/plt/ranpm.git] / install / install-nrt.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 . scripts/kube_get_controlplane_host.sh
21 . scripts/kube_get_nodeport.sh
22 . scripts/wait_for_server_ok.sh
23 . scripts/get_influxdb2_token.sh
24 . scripts/create_topic.sh
25
26 # Constants
27 SAMELINE="\033[0K\r"
28
29 # Variables
30 export KHOST=$(kube_get_controlplane_host)
31 if [ $? -ne 0 ]; then
32     echo $KHOST
33     echo "Exiting"
34     exit 1
35 fi
36
37 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
38 echo "Kubernetes control plane host: $KHOST"
39 echo "Host obtained from current kubectl context"
40 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
41
42 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
43 echo "Restarting istiod, workaround to refresh jwks cache"
44 kubectl rollout restart deployments/istiod -n istio-system
45 echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
46
47 # Generic error printout function
48 # args: <numeric-response-code> <descriptive-string>
49 check_error() {
50     if [ $1 -ne 0 ]; then
51         echo "Failed: $2"
52         echo "Exiting..."
53         exit 1
54     fi
55 }
56
57 ##################################################################################
58 echo "##### Installing chart: namespaces"
59 ##################################################################################
60
61 helm install --wait namespaces helm/namespaces
62
63 echo ""
64
65 ##################################################################################
66 echo "##### Installing chart: nrt-base-0"
67 ##################################################################################
68 helm install --wait -n nonrtric nrt-base-0 helm/nrt-base-0
69
70 # Create realm in keycloak
71
72 ##export KC_PORT=$(kube_get_nodeport keycloak nonrtric http)
73 . scripts/populate_keycloak.sh
74
75 create_realms nonrtric-realm
76 while [ $? -ne 0 ]; do
77     create_realms nonrtric-realm
78 done
79
80 # Create client for admin calls
81 cid="console-setup"
82 create_clients nonrtric-realm $cid
83 check_error $?
84 generate_client_secrets nonrtric-realm $cid
85 check_error $?
86
87 # retcode=0
88 # while [ $retcode -eq 0 ]; do
89 #     #NRT_REALM_JWKS=$(kubectl exec -n nonrtric client -- curl -f http://keycloak.nonrtric:8080/realms/nonrtric-realm/protocol/openid-connect/certs)
90 #     NRT_REALM_JWKS=$(curl -fs localhost:31788/realms/nonrtric-realm/protocol/openid-connect/certs)
91 #     if [ $? -eq 0 ]; then
92 #         retcode=1
93 #         #echo $NRT_REALM_JWKS
94 #         echo "JWKS for nonrtric-realm obtained"
95 #     else
96 #         sleep 3
97 #         echo "Wating for keycloak to publish JWKS for nonrtric-realm"
98 #     fi
99 # done
100
101 # export NRT_REALM_JWKS
102
103 echo ""
104
105 # ##################################################################################
106 # echo "##### Installing chart httpecho"
107 # ##################################################################################
108
109 #helm install --wait -n nonrtric httpecho helm/httpecho
110
111
112
113 # TSEC=$SECONDS
114 # numok=0
115 # while [ $numok -lt 10 ]; do
116 #     echo ""
117 #     echo "Time: $(($SECONDS-$TSEC))"
118 #     cid="console-setup"
119 #     __get_admin_token
120 #     TOKEN=$(get_client_token nonrtric-realm $cid)
121 #     decode_token "$TOKEN"
122 #     curl -fv localhost:31789/ -H "Authorization: Bearer $TOKEN"
123 #     if [ $? -eq 0 ]; then
124 #         let numok=numok+1
125 #     fi
126 #     sleep 5
127 # done
128
129 # TSEC=$SECONDS
130 # while [ true ]; do
131 #     echo ""
132 #     echo "Time: $(($SECONDS-$TSEC))"
133 #     cid="console-setup"
134 #     __get_admin_token
135 #     TOKEN=$(get_client_token nonrtric-realm $cid)
136 #     decode_token "$TOKEN"
137 #     curl -v localhost:31789/ -H "Authorization: Bearer $TOKEN"
138 #     sleep 5
139 # done
140 cid="console-setup"
141 __get_admin_token
142 TOKEN=$(get_client_token nonrtric-realm $cid)
143 decode_token "$TOKEN"
144
145 ##################################################################################
146 echo "##### Installing charts: strimzi and nrt-base-1"
147 ##################################################################################
148
149 helm repo add strimzi https://strimzi.io/charts/
150
151 helm install --wait strimzi-kafka-crds -n nonrtric strimzi/strimzi-kafka-operator
152
153
154 cp opa-rules/bundle.tar.gz helm/nrt-base-1/charts/opa-rule-db/data
155
156 #envsubst < helm/nrt-base-1/charts/httpecho/values-template.yaml > helm/nrt-base-1/charts/httpecho/values.yaml
157
158 helm install -n nonrtric nrt-base-1 helm/nrt-base-1
159
160
161 retcode=1
162 while [ $retcode -eq 1 ]; do
163     retcode=0
164     CONFIG=$(kubectl exec -n nonrtric influxdb2-0 -- influx config ls --json)
165     if [ $? -ne 0 ]; then
166         retcode=1
167         sleep 1
168     elif [ "$CONFIG" == "{}" ]; then
169         echo "Configuring db"
170         kubectl exec -n nonrtric influxdb2-0 -- influx setup -u bm -p mySuP3rS3cr3tT0keN -o est -b pm-bucket -f
171         if [ $? -ne 0 ]; then
172             retcode=1
173             sleep 1
174         fi
175     else
176         echo "Db user configured, skipping"
177     fi
178 done
179
180 # Save influx user api-token to secret
181 INFLUXDB2_TOKEN=$(get_influxdb2_token influxdb2-0 nonrtric)
182 INFLUXDB2_TOKEN=$(echo -n $INFLUXDB2_TOKEN | base64)
183 PATCHDATA='[{"op": "add", "path": "/data/token", "value": "'$INFLUXDB2_TOKEN'" }]'
184 kubectl patch secret influxdb-api-token -n nonrtric --type json -p "$PATCHDATA"
185
186
187 echo "Wait for kafka"
188 _ts=$SECONDS
189 until $(kubectl exec -n nonrtric kafka-client -- kafka-topics --list --bootstrap-server kafka-1-kafka-bootstrap.nonrtric:9092 1> /dev/null 2> /dev/null); do
190     echo -ne "  $(($SECONDS-$_ts)) sec, retrying at $(($SECONDS-$_ts+5)) sec                        $SAMELINE"
191     sleep 5
192 done
193 echo ""
194
195 # Pre-create known topic to avoid losing data when autocreated by apps
196 __topics_list="file-ready collected-file json-file-ready-kp json-file-ready-kpadp pmreports"
197 for __topic in $__topics_list; do
198     create_topic kafka-1-kafka-bootstrap.nonrtric:9092 $__topic 10
199 done
200
201 echo ""
202
203 ##################################################################################
204 echo "##### Installing: chart ran"
205 ##################################################################################
206
207 ./helm/ran/certs/gen-certs.sh 10
208 check_error $?
209
210 helm install --wait -n ran ran helm/ran
211
212 echo ""
213
214 ##################################################################################
215 echo "##### Installing chart: nrt-pm"
216 ##################################################################################
217
218
219 cwd=$PWD
220 echo "Updating dfc truststore"
221 cd helm/nrt-pm/charts/dfc/truststore
222 cp template-truststore.jks truststore.jks
223 check_error $?
224
225 echo " Adding https ca cert to dfc truststore"
226 cat <<__EOF__ | keytool -importcert -alias pm-https -file $cwd/helm/ran/certs/httpsca.crt -keystore truststore.jks -storetype JKS -storepass $(< truststore.pass)
227 yes
228 __EOF__
229 cd $cwd
230
231 cid="kafka-producer-pm-xml2json"
232 create_clients nonrtric-realm $cid
233 check_error $?
234 generate_client_secrets nonrtric-realm $cid
235 check_error $?
236
237 export APP_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
238
239 envsubst < helm/nrt-pm/charts/kafka-producer-pm-xml2json/values-template.yaml > helm/nrt-pm/charts/kafka-producer-pm-xml2json/values.yaml
240
241
242 cid="kafka-producer-pm-json2kafka"
243 create_clients nonrtric-realm $cid
244 check_error $?
245 generate_client_secrets nonrtric-realm $cid
246 check_error $?
247
248 export APP_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
249
250 envsubst < helm/nrt-pm/charts/kafka-producer-pm-json2kafka/values-template.yaml > helm/nrt-pm/charts/kafka-producer-pm-json2kafka/values.yaml
251
252
253 cid="kafka-producer-pm-json2influx"
254 create_clients nonrtric-realm $cid
255 check_error $?
256 generate_client_secrets nonrtric-realm $cid
257 check_error $?
258
259 export APP_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
260
261 envsubst < helm/nrt-pm/charts/kafka-producer-pm-json2influx/values-template.yaml > helm/nrt-pm/charts/kafka-producer-pm-json2influx/values.yaml
262
263
264 cid="pm-producer-json2kafka"
265 create_clients nonrtric-realm $cid
266 check_error $?
267 generate_client_secrets nonrtric-realm $cid
268 check_error $?
269
270 export APP_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
271
272 envsubst < helm/nrt-pm/charts/pm-producer-json2kafka/values-template.yaml > helm/nrt-pm/charts/pm-producer-json2kafka/values.yaml
273
274
275 cid="dfc"
276 create_clients nonrtric-realm $cid
277 check_error $?
278 generate_client_secrets nonrtric-realm $cid
279 check_error $?
280
281 export APP_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
282
283 envsubst < helm/nrt-pm/charts/dfc/values-template.yaml > helm/nrt-pm/charts/dfc/values.yaml
284
285
286 #envsubst < helm/nrt-pm/charts/ics/values-template.yaml > helm/nrt-pm/charts/ics/values.yaml
287
288 helm install --wait -n nonrtric nrt-pm helm/nrt-pm
289
290 echo ""
291
292 echo "######################################################################"
293 echo "ranpm installed"
294 echo "Wait until all pods are running before installation additional charts"
295 echo "Do: 'kubectl get po -n nonrtric' and verify that all pods are in status Running"
296 echo " and all included containers are Ready"
297 echo "######################################################################"