blob: 25e867109b0c2af7ed214860ee86b3ae6781da24 [file] [log] [blame]
srikanth116e6e82014-08-19 07:22:37 -07001#!/bin/bash
2#
3# Copyright (c) 2013 Big Switch Networks, Inc.
4#
5# Licensed under the Eclipse Public License, Version 1.0 (the
6# "License"); you may not use this file except in compliance with the
7# License. You may obtain a copy of the License at
8#
9# http://www.eclipse.org/legal/epl-v10.html
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
14# implied. See the License for the specific language governing
15# permissions and limitations under the License.
16
17# Map the keepalived state name to the corresponding sdnplatform role
18NEW_STATE=$3
19
20logger "notify_role.sh: Called with '$1', '$2', '$3'"
21
22
23case "$NEW_STATE" in
24"MASTER")
25 ROLE=MASTER
26 MESSAGE="Master re-election or failover."
27 ;;
28"BACKUP")
29 MESSAGE="System start up or master re-election."
30 ROLE=SLAVE
31 ;;
32"FAULT")
33 MESSAGE="Master re-election"
34 ROLE=SLAVE
35 ;;
36*)
37 logger "notify_role.sh: Unexpected keepalived state: '$NEW_STATE'"
38 exit 1
39 ;;
40esac
41
42if [ -e "/opt/sdnplatform/health-check-error-message" ]
43then
44 MESSAGE=$(</opt/sdnplatform/health-check-error-message)
45 #echo $MESSAGE
46 # Only error code is used since encoding a string into json
47 # is error prone until a better way found to send the rest call/json
48 # There are 3 conditions that may triger failover
49 # 1) user request failover
50 # 2) ha-health-check.sh failed with non-zero
51 # 3) keepalived detects failure and initiate faili over
52 # currently keepalived does not post detailed information
53 # for this failover. More investigation needs to be done.
54 logger "last health check with error $MESSAGE"
55 rm -f /opt/sdnplatform/health-check-error-message
56fi
57
58logger "notify_role.sh: Updating role file with $ROLE"
59# Update the current_role file
60echo "sdnplatform.role=$ROLE" > /opt/sdnplatform/current_role
61
62logger "notify_role.sh: Updating Sdnplatform REST with $ROLE"
63# Call the sdnplatform REST API to notify about the new role
64curl -X POST -d "{\"role\":\"$ROLE\",\"change-description\":\"$MESSAGE\"}" -H "Content-Type: application/json" "http://localhost:8080/wm/core/role/json"
65
66exit 0