Mohammadreza Pasandideh | b642ee5 | 2018-06-19 15:19:53 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Krzysztof Opasiak | fe651c1 | 2021-06-23 10:59:03 +0200 | [diff] [blame] | 2 | |
Guillaume Lambert | d9017ef | 2021-03-08 21:38:31 +0100 | [diff] [blame] | 3 | {{/* |
Mohammadreza Pasandideh | b642ee5 | 2018-06-19 15:19:53 -0400 | [diff] [blame] | 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 Latusek | e0e8ca7 | 2020-10-21 13:36:29 +0200 | [diff] [blame] | 17 | */}} |
Mohammadreza Pasandideh | b642ee5 | 2018-06-19 15:19:53 -0400 | [diff] [blame] | 18 | |
| 19 | debugLog(){ |
Guillaume Lambert | 5f4af05 | 2021-03-09 21:52:32 +0100 | [diff] [blame] | 20 | if [ "$enableDebugLogging" = true ]; then |
Mohammadreza Pasandideh | b642ee5 | 2018-06-19 15:19:53 -0400 | [diff] [blame] | 21 | if [ $# -eq 0 ]; then |
| 22 | echo "" >> $LOGFILE |
| 23 | else |
| 24 | echo $( date ) $@ >> $LOGFILE |
| 25 | fi |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | failover(){ |
| 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 | |
| 71 | LOGFILE="/app/geo.log" |
| 72 | enableDebugLogging=true |
| 73 | message_router=message-router:3904 |
| 74 | topic={{.Values.config.messageRouterTopic}} |
| 75 | SITE_NAME="sdnc01" |
| 76 | if [ "$SDNC_IS_PRIMARY_CLUSTER" = "false" ];then |
| 77 | SITE_NAME="sdnc02" |
| 78 | fi |
| 79 | |
| 80 | debugLog |
| 81 | debugLog "Executing ensureSdncActive" |
| 82 | |
| 83 | # query SDN-C cluster status |
| 84 | debugLog "Started executing sdnc.cluster" |
| 85 | clusterStatus=$( /app/bin/sdnc.cluster ) |
| 86 | debugLog "Completed executing sdnc.cluster. Cluster status is: $clusterStatus" |
| 87 | |
| 88 | if [ "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 | |
| 100 | elif [ "standby" = "$clusterStatus" ]; then |
| 101 | # Run failover in background process and allow PROM to continue |
| 102 | ( failover & ) |
| 103 | exit 0 |
| 104 | fi |
| 105 | |
| 106 | # Unknown cluster status |
| 107 | exit 1 |