Fix the bug of failure to write due to not sufficient good replicas in SA DBAAS
[ric-plt/ric-dep.git] / bin / install
1 #!/bin/bash
2 ################################################################################
3 #   Copyright (c) 2019 AT&T Intellectual Property.                             #
4 #   Copyright (c) 2019 Nokia.                                                  #
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 ################################################################################
18
19 function wait_for_pods() {
20   echo -n "waiting for $1 pods to run"
21
22   STILL_WAITING=true
23   while $STILL_WAITING; do
24       STILL_WAITING=false
25       PODS=$(kubectl get pods -n $2 2>/dev/null | grep $1 | awk '{print $1}')
26       if [ -z $PODS ]; then
27         STILL_WAITING=true
28         sleep 1
29         echo -n "."
30       fi
31       for POD in ${PODS}; do
32         if [[ $(kubectl get pod ${POD} -n $2 -o go-template --template "{{.status.phase}}") != "Running" ]]; then
33             STILL_WAITING=true
34             sleep 1
35             echo -n "."
36             break
37         fi
38       done 
39   done
40
41   echo
42 }
43
44 function wait_for_cats() {
45   echo -n "waiting for $1 daemonset to complete"
46
47   STILL_WAITING=true
48   while $STILL_WAITING; do
49       STILL_WAITING=false
50       PODS=$(kubectl get pods -n $2 | grep $1 | awk '{print $1}')
51       for POD in ${PODS}; do
52         if [[ $(kubectl logs ${POD} -n $2 --tail 1) != "done" ]]; then
53             STILL_WAITING=true
54             sleep 1
55             echo -n "."
56             break
57         fi
58       done 
59   done
60
61   echo
62 }
63
64
65 while [ -n "$1" ]; do # while loop starts
66
67     case "$1" in
68
69     -f) OVERRIDEYAML=$2
70         shift
71         ;;
72     -c) LIST_OF_COMPONENTS=$2
73         shift
74         ;;
75     *) echo "Option $1 not recognized" ;; # In case you typed a different option other than a,b,c
76
77     esac
78
79     shift
80
81 done
82
83 if [ -z "$OVERRIDEYAML" ];then
84     echo "****************************************************************************************************************"
85     echo "                                                     ERROR                                                      "
86     echo "****************************************************************************************************************"
87     echo "RIC deployment without deployment recipe is currently disabled. Please specify an recipe with the -f option."
88     echo "****************************************************************************************************************"
89     exit 1
90 fi
91
92 HAS_COMMON_PACKAGE=$(helm search local/ric-common | grep ric-common)
93
94 if [ -z "$HAS_COMMON_PACKAGE" ];then
95     echo "****************************************************************************************************************"
96     echo "                                                     ERROR                                                      "
97     echo "****************************************************************************************************************"
98     echo "Can't locate the ric-common helm package in the local repo. Please make sure that it is properly installed."
99     echo "****************************************************************************************************************"
100     exit 1
101 fi
102
103 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
104 COMMON_BLOCK=$(cat $OVERRIDEYAML | awk '/^common:/{getline; while ($0 ~ /^ +.*|^ *|^ *#.*$/) {print $0; if (getline == 0) {break}}}')
105 NAMESPACE_BLOCK=$(cat $OVERRIDEYAML | awk '/^  namespace:/{getline; while ($0 ~ /^ +.*|^ *|^ *#.*$/) {print $0; if (getline == 0) {break}}}')
106 PLTNAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *platform:/{print $2}')
107 INFRANAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *infra:/{print $2}')
108 XAPPNAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *xapp:/{print $2}')
109 RELEASE_PREFIX=$(echo "$COMMON_BLOCK" | awk '/^ *releasePrefix:/{print $2}')
110 LOCAL_REPOSITORY=$(echo "$COMMON_BLOCK" | awk '/^ *localregistry:/{print $2}')
111
112 # replace the dbaasha with dbaas1 if deploying non HA DBaaS
113 COMPONENTS=${LIST_OF_COMPONENTS:-"infrastructure dbaas xapp-onboarder appmgr rtmgr e2mgr e2term a1mediator submgr vespamgr jaegeradapter o1mediator alarmadapter"}
114 echo "Deploying RIC infra components [$COMPONENTS]"
115
116
117 if ! kubectl get ns ${PLTNAMESPACE:-ricplt}> /dev/null 2>&1; then
118     kubectl create ns ${PLTNAMESPACE:-ricplt}
119 fi
120 if ! kubectl get ns ${INFRANAMESPACE:-ricinfra}> /dev/null 2>&1; then
121     kubectl create ns ${INFRANAMESPACE:-ricinfra}
122 fi
123 if ! kubectl get ns ${XAPPNAMESPACE:-ricxapp}> /dev/null 2>&1; then
124     kubectl create ns ${XAPPNAMESPACE:-ricxapp}
125 fi
126 FOUND_RECIPE=$(kubectl get configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe 2>/dev/null )
127 if [ ! -z "$FOUND_RECIPE" ]; then
128     kubectl delete configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe
129 fi
130 kubectl create configmap -n ${PLTNAMESPACE:-ricplt} ricplt-recipe --from-file=recipe=$OVERRIDEYAML
131
132 if [ ! -z "$LOCAL_REPOSITORY" ]; then
133     LOCAL_REPOSITORY="$LOCAL_REPOSITORY/"
134 fi
135
136
137 # Add kernel optimization for radis services
138     cat >kernel_optimizer.yaml <<EOF
139 apiVersion: apps/v1
140 kind: DaemonSet
141 metadata:
142   namespace: ${INFRANAMESPACE:-ricinfra}
143   name: redis-kernel-optimizer
144 spec:
145   selector:
146     matchLabels:
147       app: redis-kernel-optimizer
148   template:
149     metadata:
150       labels:
151         app: redis-kernel-optimizer
152     spec:
153       volumes:
154       - name: sys
155         hostPath:
156           path: /sys
157       containers:
158       - name: disable-thp
159         image: ${LOCAL_REPOSITORY}busybox
160         securityContext:
161           runAsNonRoot: false
162           privileged: true
163           runAsUser: 0
164         command: ["sh", "-c"]
165         args:
166         - |-
167           set -e
168           set -o pipefail
169           trap 'exit' TERM
170           echo never > /rootfs/sys/kernel/mm/transparent_hugepage/enabled
171           echo never > /rootfs/sys/kernel/mm/transparent_hugepage/defrag
172           sysctl -w net.core.somaxconn=511
173           grep -q -F [never] /sys/kernel/mm/transparent_hugepage/enabled
174           grep -q -F [never] /sys/kernel/mm/transparent_hugepage/defrag
175           sysctl -n net.core.somaxconn | grep 511 -q
176           echo "done"
177           while true; do sleep 1; done
178         volumeMounts:
179         - name: sys
180           mountPath: /rootfs/sys
181 EOF
182 kubectl apply -f kernel_optimizer.yaml
183 wait_for_pods redis-kernel-optimizer ${INFRANAMESPACE:-ricinfra}
184 wait_for_cats redis-kernel-optimizer ${INFRANAMESPACE:-ricinfra}
185 kubectl delete -f kernel_optimizer.yaml
186 rm kernel_optimizer.yaml
187
188
189 for component in $COMPONENTS; do
190     helm dep up $DIR/../helm/$component
191     helm install -f $OVERRIDEYAML --namespace "${PLTNAMESPACE:-ricplt}" --name "${RELEASE_PREFIX}-$component" $DIR/../helm/$component
192     sleep 3
193 done
194
195
196
197