-
Notifications
You must be signed in to change notification settings - Fork 2
/
callback.sh
41 lines (37 loc) · 959 Bytes
/
callback.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
export PGPASSWORD=postgres
readonly cb_name=$1
readonly role=$2
readonly scope=$3
# sleep 3 seconds in case that postgres is not ready
sleep 3
function usage() {
echo "Usage: $0 <on_start|on_role_change> <role> <scope>";
exit 1;
}
echo "this is patroni callback $cb_name $role $scope"
create_table="
CREATE TABLE IF NOT EXISTS current_master(
id serial PRIMARY KEY,
hostname text
);
"
insert_record="
INSERT INTO current_master (hostname) VALUES ('$HOSTNAME');
"
case $cb_name in
on_start)
if [[ $role == 'master' ]]; then
psql -h localhost -U postgres -d postgres -c "${create_table}";
psql -h localhost -U postgres -d postgres -c "${insert_record}";
fi
;;
on_role_change)
if [[ $role == 'master' ]]; then
psql -h localhost -U postgres -d postgres -c "${insert_record}";
fi
;;
*)
usage
;;
esac