blob: feb666219609364d800338cbb576191e224714c6 [file] [log] [blame]
Mohammadreza Pasandidehb642ee52018-06-19 15:19:53 -04001#!/bin/bash
Jakub Latuseke0e8ca72020-10-21 13:36:29 +02002{{/*
Mohammadreza Pasandidehb642ee52018-06-19 15:19:53 -04003
4# Copyright © 2018 Amdocs
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.
Jakub Latuseke0e8ca72020-10-21 13:36:29 +020017*/}}
Mohammadreza Pasandidehb642ee52018-06-19 15:19:53 -040018
19debugLog(){
Guillaume Lambert5f4af052021-03-09 21:52:32 +010020 if [ "$enableDebugLogging" = true ]; then
Mohammadreza Pasandidehb642ee52018-06-19 15:19:53 -040021 if [ $# -eq 0 ]; then
22 echo "" >> $LOGFILE
23 else
24 echo $( date ) $@ >> $LOGFILE
25 fi
26 fi
27}
28
29failover(){
30 lockFile=/tmp/sdnc.failover.lock
31 # make sure that no failover is currently running
32 if [ -e ${lockFile} ] && kill -0 $(cat ${lockFile}) 2> /dev/null; then
33 debugLog "Currently running sdnc and dns failover"
34 return
35 fi
36 trap "rm -f ${lockFile}" INT TERM RETURN
37 echo $BASHPID > ${lockFile}
38
39 # perform takeover
40 debugLog "Started executing sdnc.failover for $SITE_NAME"
41 takeoverResult=$( /app/bin/sdnc.failover )
42 debugLog "Completed executing sdnc.failover. takeoverResult is: $takeoverResult"
43 if [ "success" = "$takeoverResult" ]; then
44 # update CoreDNS upon successful execution of sdnc.failover script
45 debugLog "Executing sdnc.dnsswitch"
46 /app/bin/sdnc.dnsswitch
47 rc=$?
48 debugLog "Completed executing sdnc.dnsswitch for $SITE_NAME. rc=$rc"
49 else
50 debugLog "Cluster takeover current status: $takeoverResult on $SITE_NAME."
51 rc=1
52 fi
53
54 if [ $rc -ne 0 ];then
55 takeoverResult="failure"
56 fi
57
58 data="{\
59\"type\": \"failover\",\
60\"status\": \"$takeoverResult\",\
61\"site\": \"$SITE_NAME\",\
62\"deployment\": \"{{.Values.config.deployment}}\",\
63\"timestamp\": \"$(date '+%F %T')\"\
64}"
65
66 # notifications are best-effort - ignore any failures
67 curl -H "Content-Type: application/json" -X POST --data "$data" http://$message_router/events/$topic >/dev/null 2>&1
68
69}
70
71LOGFILE="/app/geo.log"
72enableDebugLogging=true
73message_router=message-router:3904
74topic={{.Values.config.messageRouterTopic}}
75SITE_NAME="sdnc01"
76if [ "$SDNC_IS_PRIMARY_CLUSTER" = "false" ];then
77 SITE_NAME="sdnc02"
78fi
79
80debugLog
81debugLog "Executing ensureSdncActive"
82
83# query SDN-C cluster status
84debugLog "Started executing sdnc.cluster"
85clusterStatus=$( /app/bin/sdnc.cluster )
86debugLog "Completed executing sdnc.cluster. Cluster status is: $clusterStatus"
87
88if [ "active" = "$clusterStatus" ]; then
89 # peform health-check
90 debugLog "Started excuting sdnc.monitor"
91 health=$( /app/bin/sdnc.monitor )
92 debugLog "Completed executing sdnc.monitor. Cluster is: $health"
93
94 if [ "healthy" = "$health" ]; then
95 # Cluster is ACTIVE and HEALTHY
96 exit 0
97 fi
98 exit 1
99
100elif [ "standby" = "$clusterStatus" ]; then
101 # Run failover in background process and allow PROM to continue
102 ( failover & )
103 exit 0
104fi
105
106# Unknown cluster status
107exit 1