Merge "[VNFSDK] Use common secret template for postgres credentials"
[oom.git] / kubernetes / sdnc / sdnc-prom / resources / bin / ensureSdncActive.sh
1 #!/bin/bash
2
3 # Copyright © 2018 Amdocs
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 debugLog(){
18   if [ "$enableDebugLogging" == true ]; then
19      if [ $# -eq 0 ]; then
20        echo "" >> $LOGFILE
21      else
22        echo $( date ) $@ >> $LOGFILE
23     fi
24   fi
25 }
26
27 failover(){
28   lockFile=/tmp/sdnc.failover.lock
29   # make sure that no failover is currently running
30   if [ -e ${lockFile} ] && kill -0 $(cat ${lockFile}) 2> /dev/null; then
31     debugLog "Currently running sdnc and dns failover"
32     return
33   fi
34   trap "rm -f ${lockFile}" INT TERM RETURN
35   echo $BASHPID > ${lockFile}
36
37   # perform takeover
38   debugLog "Started executing sdnc.failover for $SITE_NAME"
39   takeoverResult=$( /app/bin/sdnc.failover )
40   debugLog "Completed executing sdnc.failover. takeoverResult is: $takeoverResult"
41   if [ "success" = "$takeoverResult" ]; then
42     # update CoreDNS upon successful execution of sdnc.failover script
43     debugLog "Executing sdnc.dnsswitch"
44     /app/bin/sdnc.dnsswitch
45     rc=$?
46     debugLog "Completed executing sdnc.dnsswitch for $SITE_NAME. rc=$rc"
47   else
48     debugLog "Cluster takeover current status: $takeoverResult on $SITE_NAME."
49     rc=1
50   fi
51
52   if [ $rc -ne 0 ];then
53     takeoverResult="failure"
54   fi
55
56   data="{\
57 \"type\": \"failover\",\
58 \"status\": \"$takeoverResult\",\
59 \"site\": \"$SITE_NAME\",\
60 \"deployment\": \"{{.Values.config.deployment}}\",\
61 \"timestamp\": \"$(date '+%F %T')\"\
62 }"
63
64   # notifications are best-effort - ignore any failures
65   curl -H "Content-Type: application/json" -X POST --data "$data" http://$message_router/events/$topic >/dev/null 2>&1
66
67 }
68
69 LOGFILE="/app/geo.log"
70 enableDebugLogging=true
71 message_router=message-router:3904
72 topic={{.Values.config.messageRouterTopic}}
73 SITE_NAME="sdnc01"
74 if [ "$SDNC_IS_PRIMARY_CLUSTER" = "false" ];then
75   SITE_NAME="sdnc02"
76 fi
77
78 debugLog
79 debugLog "Executing ensureSdncActive"
80
81 # query SDN-C cluster status
82 debugLog "Started executing sdnc.cluster"
83 clusterStatus=$( /app/bin/sdnc.cluster )
84 debugLog "Completed executing sdnc.cluster. Cluster status is: $clusterStatus"
85
86 if [ "active" = "$clusterStatus" ]; then
87   # peform health-check
88   debugLog "Started excuting sdnc.monitor"
89   health=$( /app/bin/sdnc.monitor )
90   debugLog "Completed executing sdnc.monitor. Cluster is: $health"
91
92   if [ "healthy" = "$health" ]; then
93     # Cluster is ACTIVE and HEALTHY
94     exit 0
95   fi
96   exit 1
97
98 elif [ "standby" = "$clusterStatus" ]; then
99   # Run failover in background process and allow PROM to continue
100   ( failover & )
101   exit 0
102 fi
103
104 # Unknown cluster status
105 exit 1