CI: Add SonarCloud scan GHA workflow
[nonrtric/plt/ranpm.git] / docker-proj / pmlog-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 . scripts/get_influxdb2_token.sh
21 . scripts/populate_keycloak.sh
22
23 print_usage() {
24     echo "Usage: pmlog-setup.sh"
25     exit 1
26 }
27
28 check_error() {
29     if [ $1 -ne 0 ]; then
30         echo "Failed $2"
31         echo "Exiting..."
32         exit 1
33     fi
34 }
35
36 setup_init() {
37 echo "Cleaning previously started containers..."
38
39 ./pmlog-tear-down.sh
40 }
41
42 pull_image() {
43 if [ -z "$(docker images -q $1)" ]; then
44    echo "Pulling image... "$1
45    docker pull $1
46    check_error $?
47 fi
48 }
49
50 check_images(){
51 export INFLUXDB_IMAGE="influxdb:2.6.1"
52 pull_image $INFLUXDB_IMAGE
53
54 export PMLOG_IMAGE="nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-pmlog:1.0.0"
55 pull_image $PMLOG_IMAGE
56
57 export AUTH_TOKEN_IMAGE=nexus3.o-ran-sc.org:10001/o-ran-sc/nonrtric-plt-auth-token-fetch:1.1.1
58 pull_image $AUTH_TOKEN_IMAGE
59 }
60
61 setup_influx() {
62 data_dir=./config/influxdb2/data
63 mkdir -p $data_dir
64
65 export INFLUXDB2_INSTANCE=influxdb2-0
66 export INFLUXDB2_USERNAME=admin
67 export INFLUXDB2_PASSWORD=mySuP3rS3cr3tT0keN
68 export INFLUXDB2_ORG=est
69 export INFLUXDB2_BUCKET=pm-logg-bucket
70
71 envsubst < docker-compose-influxdb.yaml > docker-compose-influxdb_gen.yaml
72 docker-compose -p influx -f docker-compose-influxdb_gen.yaml up -d
73 }
74
75 setup_pmlog() {
76 chmod 666 config/pmlog/token-cache/jwt.txt
77
78 cid="nrt-pm-log"
79 create_clients nonrtric-realm $cid
80 check_error $?
81 generate_client_secrets nonrtric-realm $cid
82 check_error $?
83
84 export PMLOG_CLIENT_SECRET=$(< .sec_nonrtric-realm_$cid)
85 envsubst < docker-compose-pmlog.yaml > docker-compose-pmlog_gen.yaml
86 docker-compose -p pmlog -f docker-compose-pmlog_gen.yaml up -d
87 }
88 ## Main ##
89 setup_init
90
91 check_images
92
93 setup_influx
94 check_error $?
95
96 # Wait for influxdb2 to start
97 echo 'Waiting for influxdb2 to be ready'
98 until [ $(curl -s -w '%{http_code}' -o /dev/null 'http://localhost:8086/health') -eq 200 ];
99 do
100         echo -n '.'
101         sleep 1
102 done
103 echo ""
104
105 INFLUXDB2_TOKEN=$(get_influxdb2_token $INFLUXDB2_INSTANCE)
106 echo $INFLUXDB2_TOKEN
107 export INFLUXDB2_TOKEN
108
109 setup_pmlog
110 check_error $?