blob: fb246531298cf0fd1d8619dbb541be8fbbb136ef [file] [log] [blame]
Mohammadreza Pasandidehb642ee52018-06-19 15:19:53 -04001#!/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
17debugLog(){
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
27failover(){
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
69LOGFILE="/app/geo.log"
70enableDebugLogging=true
71message_router=message-router:3904
72topic={{.Values.config.messageRouterTopic}}
73SITE_NAME="sdnc01"
74if [ "$SDNC_IS_PRIMARY_CLUSTER" = "false" ];then
75 SITE_NAME="sdnc02"
76fi
77
78debugLog
79debugLog "Executing ensureSdncActive"
80
81# query SDN-C cluster status
82debugLog "Started executing sdnc.cluster"
83clusterStatus=$( /app/bin/sdnc.cluster )
84debugLog "Completed executing sdnc.cluster. Cluster status is: $clusterStatus"
85
86if [ "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
98elif [ "standby" = "$clusterStatus" ]; then
99 # Run failover in background process and allow PROM to continue
100 ( failover & )
101 exit 0
102fi
103
104# Unknown cluster status
105exit 1