-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql
More file actions
3053 lines (2504 loc) · 97.9 KB
/
postgresql
File metadata and controls
3053 lines (2504 loc) · 97.9 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#1.0-082818
#!/bin/bash
#
#
# PostgreSQL
#
# Description: Manages a PostgreSQL database as Linux-HA resource
#
# Authors: Madan Kumar K: PostgreSQL support
#
# This resource agent script is re-write for PostgreSQL and is based on percona pacemaker replication agent for MySQL
# available at : https://github.com/Percona-Lab/pacemaker-replication-agents/tree/master/agents
#
# Support: linux-ha@lists.linux-ha.org
# License: GNU General Public License (GPL)
#
# (c) 2002-2005 International Business Machines, Inc.
# 2005-2010 Linux-HA contributors
#
# The pacemaker RA for PostgreSQL supports standalone as well as replication setup.
# In replication, it support both Asynchronous and Synchronous replicaiton for 2 or 3 nodes.
# It do includes minimal pgbouncer support.
#
# See usage() function below for more details...
#
# OCF instance parameters:
# OCF_RESKEY_binary
# OCF_RESKEY_binary_prefix
# OCF_RESKEY_client_binary
# OCF_RESKEY_config
# OCF_RESKEY_datadir
# OCF_RESKEY_user
# OCF_RESKEY_group
# OCF_RESKEY_test_user
# OCF_RESKEY_test_passwd
# OCF_RESKEY_replicaConfig
# OCF_RESKEY_additional_parameters
# OCF_RESKEY_log
# OCF_RESKEY_pid
# OCF_RESKEY_socket
# OCF_RESKEY_replication_user
# OCF_RESKEY_replication_passwd
# OCF_RESKEY_replication_port
# OCF_RESKEY_replication_options
# OCF_RESKEY_backup_lockfile
# OCF_RESKEY_sg_promote_notify_endpoint
# OCF_RESKEY_db_type
# OCF_RESKEY_default_database
# OCF_RESKEY_master_internal_dns
# OCF_RESKEY_recovery_conf_file
# OCF_RESKEY_turn_off_sync_if_no_slaves_available
# OCF_RESKEY_use_replication_slots
# OCF_RESKEY_sg_tmp_dir
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
#######################################################################
SG_POSTGRESQL_DB="pgsql"
OCF_RESKEY_db_type_default=$SG_POSTGRESQL_DB
: ${OCF_RESKEY_db_type=${OCF_RESKEY_db_type_default}}
OCF_RESKEY_sg_tmp_dir_default="/var/tmp/scalegrid"
: ${OCF_RESKEY_sg_tmp_dir=${OCF_RESKEY_sg_tmp_dir_default}}
if [ "$OCF_RESKEY_db_type" = "$SG_POSTGRESQL_DB" ]; then
OCF_RESKEY_binary_prefix_default="/usr/bin"
OCF_RESKEY_client_binary_default="${OCF_RESKEY_binary_prefix}/psql"
OCF_RESKEY_test_user_default="postgres"
OCF_RESKEY_test_passwd_default=""
OCF_RESKEY_replicaConfig_default="ASYNC"
OCF_RESKEY_additional_parameters_default=""
OCF_RESKEY_replication_port_default="5432"
OCF_RESKEY_binary_default="${OCF_RESKEY_binary_prefix}/postgres"
OCF_RESKEY_datadir_default="/var/lib/pgsql/data"
OCF_RESKEY_config_default="${OCF_RESKEY_datadir}/postgresql.conf"
OCF_RESKEY_user_default="postgres"
OCF_RESKEY_group_default="postgres"
OCF_RESKEY_log_default="/var/log/postgresql/postgresql.log"
OCF_RESKEY_pid_default="${OCF_RESKEY_datadir}/postmaster.pid"
OCF_RESKEY_socket_default="/var/run/postgresql/.s.PGSQL.${OCF_RESKEY_replication_port}"
OCF_RESKEY_backup_lockfile_default="${OCF_RESKEY_datadir}/backup_label"
OCF_RESKEY_default_database_default="postgres"
OCF_RESKEY_recovery_conf_file_default="${OCF_RESKEY_datadir}/recovery.conf"
OCF_RESKEY_turn_off_sync_if_no_slaves_available_default="false"
OCF_RESKEY_use_replication_slots_default="false"
else
## Error out
exit $OCF_ERR_INSTALLED
fi
: ${OCF_RESKEY_binary_prefix=${OCF_RESKEY_binary_prefix_default}}
: ${OCF_RESKEY_datadir=${OCF_RESKEY_datadir_default}}
: ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
: ${OCF_RESKEY_client_binary=${OCF_RESKEY_client_binary_default}}
: ${OCF_RESKEY_config=${OCF_RESKEY_config_default}}
: ${OCF_RESKEY_user=${OCF_RESKEY_user_default}}
: ${OCF_RESKEY_group=${OCF_RESKEY_group_default}}
: ${OCF_RESKEY_log=${OCF_RESKEY_log_default}}
: ${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}
: ${OCF_RESKEY_test_user=${OCF_RESKEY_test_user_default}}
: ${OCF_RESKEY_test_passwd=${OCF_RESKEY_test_passwd_default}}
: ${OCF_RESKEY_replicaConfig=${OCF_RESKEY_replicaConfig_default}}
: ${OCF_RESKEY_additional_parameters=${OCF_RESKEY_additional_parameters_default}}
: ${OCF_RESKEY_replication_user=${OCF_RESKEY_replication_user_default}}
: ${OCF_RESKEY_replication_passwd=${OCF_RESKEY_replication_passwd_default}}
: ${OCF_RESKEY_replication_port=${OCF_RESKEY_replication_port_default}}
: ${OCF_RESKEY_replication_options=${OCF_RESKEY_replication_options_default}}
: ${OCF_RESKEY_socket=${OCF_RESKEY_socket_default}}
: ${OCF_RESKEY_backup_lockfile=${OCF_RESKEY_backup_lockfile_default}}
: ${OCF_RESKEY_sg_promote_notify_endpoint}=""
: ${OCF_RESKEY_default_database=${OCF_RESKEY_default_database_default}}
: ${OCF_RESKEY_master_internal_dns}=""
: ${OCF_RESKEY_recovery_conf_file=${OCF_RESKEY_recovery_conf_file_default}}
: ${OCF_RESKEY_turn_off_sync_if_no_slaves_available=${OCF_RESKEY_turn_off_sync_if_no_slaves_available_default}}
: ${OCF_RESKEY_use_replication_slots=${OCF_RESKEY_use_replication_slots_default}}
#######################################################################
# Convenience variables PostgreSQL
POSTGRES="${OCF_RESKEY_binary_prefix}/postgres"
PG_CTL="$TIMEOUT ${OCF_RESKEY_binary_prefix}/pg_ctl"
PSQL="$TIMEOUT ${OCF_RESKEY_binary_prefix}/psql"
PG_CONTROLDATA="$TIMEOUT ${OCF_RESKEY_binary_prefix}/pg_controldata"
PG_ISREADY="$TIMEOUT ${OCF_RESKEY_binary_prefix}/pg_isready"
POSTGRESQL_LAST_ERR=0
POSTGRESQL_TOO_MANY_CONN_ERR=53300
SLAVE_REPL_STAT_ATTR_NAME="slave_repl_stat"
WAL_STATE_STREAMING="streaming"
PG_RESOURCE_NODES="resource_nodes"
PG_NO_OF_SYNC_STANDBY_NODES_CRM_ATTR_NAME="number_of_sync_standby_nodes"
PG_DISABLE_SYNC_IF_NO_STANDBY_CRM_ATTR_NAME="disable_sync_if_standby_nodes_not_available"
PGSQL_PGBOUNCER_CRM_ATTR_SET_NAME="pgsql_pgbouncer"
PGSQL_PGBOUNCER_PORT_CRM_ATTR_NAME="pgbouncer_port"
PGSQL_PGBOUNCER_SERVICE_NAME_CRM_ATTR_NAME="pgbouncer_service_name"
#######################################################################
# Convenience variables
CRM_MASTER="$TIMEOUT ${HA_SBIN_DIR}/crm_master -l reboot "
HOSTNAME=`uname -n`
CRM_ATTR="$TIMEOUT ${HA_SBIN_DIR}/crm_attribute"
INSTANCE_ATTR_NAME=`echo ${OCF_RESOURCE_INSTANCE}| awk -F : '{print $1}'`
CRM_CONFIG_ATTR="$TIMEOUT ${HA_SBIN_DIR}/crm_attribute --type crm_config -q"
CRM_NODE="$TIMEOUT ${HA_SBIN_DIR}/crm_node --quiet"
CRM_RES="$TIMEOUT ${HA_SBIN_DIR}/crm_resource"
CRM_TICKET="$TIMEOUT ${HA_SBIN_DIR}/crm_ticket"
SSH="$TIMEOUT /usr/bin/ssh "
# Below is used as witness file for async start operation. Stop operation is sync only.
ASYNC_START_WITNESS_FILE="${HA_RSCTMP}/start_${INSTANCE_ATTR_NAME}"
TIMEOUT_EXIT_STATUS=124
MONITOR_ERROR_COUNT_ATTR_NAME="monitor_error_count"
MONITOR_FAIL_RETRY_COUNT=3
CIB_BOOTSTRAP_OPTIONS_PROPERTY_SET="cib-bootstrap-options"
SYMMETRIC_CLUSTER_PROPERTY="symmetric-cluster"
CRM_ATTR_NAME_FAILOVER_PROPS="failover_props"
REPLICATION_CRM_ATTR_SET_NAME="${OCF_RESKEY_db_type}_replication"
REPLICATION_INFO_CRM_ATTR_NAME="${INSTANCE_ATTR_NAME}_REPL_INFO"
REPLICATION_STATUS_CRM_ATTR_NAME="${INSTANCE_ATTR_NAME}_REPL_STATUS"
SG_TMP_FOLDER_FOR_DB="${OCF_RESKEY_sg_tmp_dir}/${OCF_RESKEY_db_type}"
#######################################################################
# Version check functions - copied here to ensure same functions are used in AWS Linux and Cent OS
# Adapted from https://github.com/ClusterLabs/resource-agents/blob/master/heartbeat/ocf-shellfuncs.in
# License Info (GNU GPL v2) - https://github.com/ClusterLabs/resource-agents/blob/master/COPYING
sg_is_ver() {
echo $1 | grep '^[0-9][0-9.-]*[0-9]$' >/dev/null 2>&1
}
sg_ver2num() {
echo $1 | awk -F'[.-]' '
{for(i=1; i<=NF; i++) s=s*1000+$i; print s}
'
}
sg_ver_level(){
echo $1 | awk -F'[.-]' '{print NF}'
}
sg_ver_complete_level(){
local ver="$1"
local level="$2"
local i=0
while [ $i -lt $level ]; do
ver=${ver}.0
i=`expr $i + 1`
done
echo $ver
}
sg_version_cmp() {
sg_is_ver "$1" || return 3
sg_is_ver "$2" || return 3
local v1=$1
local v2=$2
local v1_level=`sg_ver_level $v1`
local v2_level=`sg_ver_level $v2`
local level_diff
if [ $v1_level -lt $v2_level ]; then
level_diff=`expr $v2_level - $v1_level`
v1=`sg_ver_complete_level $v1 $level_diff`
elif [ $v1_level -gt $v2_level ]; then
level_diff=`expr $v1_level - $v2_level`
v2=`sg_ver_complete_level $v2 $level_diff`
fi
v1=`sg_ver2num $v1`
v2=`sg_ver2num $v2`
if [ $v1 -eq $v2 ]; then
return 1
elif [ $v1 -lt $v2 ]; then
return 0
else
return 2 # -1 would look funny in shell ;-)
fi
}
#######################################################################
usage() {
cat <<UEND
usage: $0 (start|stop|validate-all|meta-data|monitor|promote|demote|notify)
$0 manages a PostgreSQL Database as an HA resource.
The 'start' operation starts the database.
The 'stop' operation stops the database.
The 'status' operation reports whether the database is running
The 'monitor' operation reports whether the database seems to be working
The 'promote' operation makes this pgsql server run as master
The 'demote' operation makes this pgsql server run as slave
The 'notify' operation performs checks when promoting/demoting a master
The 'validate-all' operation reports whether the parameters are valid
UEND
}
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="pgsql">
<version>1.0</version>
<longdesc lang="en">
Resource script for PostgreSQL.
May manage a standalone PostgreSQL database, a clone set with externally
managed replication, or a complete master/slave replication setup.
While managing replication, the default behavior is to use uname -n
values in the change master to command. Other IPs can be specified
manually by adding a node attribute \${INSTANCE_ATTR_NAME}_pgsql_master_IP
giving the IP to use for replication. For example, if the pgsql primitive
you are using is p_pgsql, the attribute to set will be
p_pgsql_pgsql_master_IP.
</longdesc>
<shortdesc lang="en">Manages a PostgreSQL database instance</shortdesc>
<parameters>
<parameter name="binary" unique="0" required="0">
<longdesc lang="en">
Location of the PostgreSQL server binary
</longdesc>
<shortdesc lang="en">PostgreSQL server binary</shortdesc>
<content type="string" default="${OCF_RESKEY_binary_default}" />
</parameter>
<parameter name="binary_prefix" unique="0" required="0">
<longdesc lang="en">
A prefix to the PostgreSQL server binary. I could be for example a LD_PRELOAD or
a call to numactl.
</longdesc>
<shortdesc lang="en">PostgreSQL server binary prefix</shortdesc>
<content type="string" default="${OCF_RESKEY_binary_prefix_default}" />
</parameter>
<parameter name="client_binary" unique="0" required="0">
<longdesc lang="en">
Location of the PostgreSQL client binary
</longdesc>
<shortdesc lang="en">PostgreSQL client binary</shortdesc>
<content type="string" default="${OCF_RESKEY_client_binary_default}" />
</parameter>
<parameter name="config" unique="0" required="0">
<longdesc lang="en">
Configuration file
</longdesc>
<shortdesc lang="en">PostgreSQL config</shortdesc>
<content type="string" default="${OCF_RESKEY_config_default}" />
</parameter>
<parameter name="datadir" unique="0" required="0">
<longdesc lang="en">
Directory containing databases
</longdesc>
<shortdesc lang="en">PostgreSQL datadir</shortdesc>
<content type="string" default="${OCF_RESKEY_datadir_default}" />
</parameter>
<parameter name="user" unique="0" required="0">
<longdesc lang="en">
User running PostgreSQL daemon
</longdesc>
<shortdesc lang="en">PostgreSQL user</shortdesc>
<content type="string" default="${OCF_RESKEY_user_default}" />
</parameter>
<parameter name="group" unique="0" required="0">
<longdesc lang="en">
Group running PostgreSQL daemon (for logfile and directory permissions)
</longdesc>
<shortdesc lang="en">PostgreSQL group</shortdesc>
<content type="string" default="${OCF_RESKEY_group_default}"/>
</parameter>
<parameter name="log" unique="0" required="0">
<longdesc lang="en">
The logfile to be used for pgsql.
</longdesc>
<shortdesc lang="en">PostgreSQL log file</shortdesc>
<content type="string" default="${OCF_RESKEY_log_default}"/>
</parameter>
<parameter name="pid" unique="0" required="0">
<longdesc lang="en">
The pidfile to be used for pgsql.
</longdesc>
<shortdesc lang="en">PostgreSQL pid file</shortdesc>
<content type="string" default="${OCF_RESKEY_pid_default}"/>
</parameter>
<parameter name="socket" unique="0" required="0">
<longdesc lang="en">
The socket to be used for pgsql.
</longdesc>
<shortdesc lang="en">PostgreSQL socket</shortdesc>
<content type="string" default="${OCF_RESKEY_socket_default}"/>
</parameter>
<parameter name="test_user" unique="0" required="0">
<longdesc lang="en">
PostgreSQL test user, must have select privilege on test_table
</longdesc>
<shortdesc lang="en">PostgreSQL test user</shortdesc>
<content type="string" default="${OCF_RESKEY_test_user_default}" />
</parameter>
<parameter name="test_passwd" unique="0" required="0">
<longdesc lang="en">
PostgreSQL test user password
</longdesc>
<shortdesc lang="en">PostgreSQL test user password</shortdesc>
<content type="string" default="${OCF_RESKEY_test_passwd_default}" />
</parameter>
<parameter name="replicaConfig" unique="0" required="0">
<longdesc lang="en">
PostgreSQL replication mode
</longdesc>
<shortdesc lang="en">PostgreSQL replication mode</shortdesc>
<content type="string" default="${OCF_RESKEY_replicaConfig_default}" />
</parameter>
<parameter name="replication_user" unique="0" required="0">
<longdesc lang="en">
PostgreSQL replication user.
</longdesc>
<shortdesc lang="en">PostgreSQL replication user</shortdesc>
<content type="string" default="${OCF_RESKEY_replication_user_default}" />
</parameter>
<parameter name="replication_passwd" unique="0" required="0">
<longdesc lang="en">
PostgreSQL replication password. Used for replication client and standby.
Mandatory if you define a master-slave resource.
</longdesc>
<shortdesc lang="en">PostgreSQL replication user password</shortdesc>
<content type="string" default="${OCF_RESKEY_replication_passwd_default}" />
</parameter>
<parameter name="replication_port" unique="0" required="0">
<longdesc lang="en">
The port on which the Master PostgreSQL instance is listening.
</longdesc>
<shortdesc lang="en">PostgreSQL replication port</shortdesc>
<content type="string" default="${OCF_RESKEY_replication_port_default}" />
</parameter>
</parameters>
<actions>
<action name="start" timeout="120" />
<action name="stop" timeout="120" />
<action name="status" timeout="60" />
<action name="monitor" depth="0" timeout="30" interval="20" />
<action name="monitor" role="Master" depth="0" timeout="30" interval="10" />
<action name="monitor" role="Slave" depth="0" timeout="30" interval="30" />
<action name="promote" timeout="120" />
<action name="demote" timeout="120" />
<action name="notify" timeout="90" />
<action name="validate-all" timeout="5" />
<action name="meta-data" timeout="5" />
</actions>
</resource-agent>
END
}
#######################################################################
# Convenience functions for MySQL & PostgreSQL
# @MySQLErrorSafe - doesn't call MySQL
# @PostgreSQLErrorSafe - doesn't touch PostgreSQL
set_master_score() {
ocf_log debug "debug: inside set_master_score"
ocf_log info "Setting master score to: $1"
$CRM_MASTER -v $1
}
# @MySQLErrorSafe - doesn't touch MySQL
# @PostgreSQLErrorSafe - doesn't touch PostgreSQL
# Determines what IP address is attached to the current host. The output of the
# crm_attribute command looks like this:
# scope=nodes name=IP value=10.2.2.161
# If the ${INSTANCE_ATTR_NAME}_${OCF_RESKEY_db_type}_MASTER_IP node attribute is not defined, fallback is to uname -n
# The ${INSTANCE_ATTR_NAME}_${OCF_RESKEY_db_type}_MASTER_IP is the IP address that will be used for the
# change master to command.
get_local_ip() {
ocf_log debug "debug: inside get_local_ip"
local IP
IP=`$CRM_ATTR -N $HOSTNAME -l forever -n ${INSTANCE_ATTR_NAME}_${OCF_RESKEY_db_type}_master_IP -q -G`
if [ ! $? -eq 0 ]; then
uname -n
else
echo $IP
fi
}
#This function allows upto MONITOR_FAIL_RETRY_COUNT retries , if a failure is hit during monitor calls.
#If retry count is already exceeded, returns an error
handle_error_from_monitor()
{
ocf_log debug "debug: inside handle_error_from_monitor"
local error_count=$1
if [ $error_count -lt $MONITOR_FAIL_RETRY_COUNT ]; then
set_crm_node_attr_val $HOSTNAME $MONITOR_ERROR_COUNT_ATTR_NAME "$((${error_count}+1))"
ocf_log debug "handle_error_from_monitor: ${OCF_RESKEY_db_type} monitor: returning success";
if [ "$OCF_RESKEY_CRM_meta_role" = "Slave" ]; then
return $OCF_SUCCESS
else
return $OCF_RUNNING_MASTER
fi
else
set_crm_node_attr_val $HOSTNAME $MONITOR_ERROR_COUNT_ATTR_NAME '0'
return $OCF_ERR_GENERIC;
fi
}
# Adds internal master dns entry to /etc/hosts
add_or_update_master_dns_entry() {
ocf_log debug "debug: inside add_or_update_master_dns_entry"
local master_host
local master_internal_dns_alias
local master_internal_dns="$OCF_RESKEY_master_internal_dns"
local dns_entry_line
local repl_info
if [ -z "$master_internal_dns" ]; then
ocf_log err "add_or_update_master_dns_entry: master internal dns is empty"
return $OCF_ERR_GENERIC
fi
master_internal_dns_alias=`echo "$master_internal_dns" | cut -d '.' -f1`
if [ "$glb_master_exists" -eq "1" ]; then
ocf_log info "add_or_update_master_dns_entry: Master exist. Getting master host from cib"
repl_info=`get_crm_config_attr_val "$REPLICATION_CRM_ATTR_SET_NAME" "$REPLICATION_INFO_CRM_ATTR_NAME"`
master_host=`echo $repl_info | cut -d'|' -f1`
else
ocf_log info "add_or_update_master_dns_entry: Master doesn't exist. Remove the dns entry"
#Delete existing entry if exists
sed -i "/${master_internal_dns}/d" /etc/hosts
return $OCF_SUCCESS;
fi
if [ -z "$master_host" ]; then
ocf_log err "add_or_update_master_dns_entry: master host is empty"
return $OCF_ERR_GENERIC
fi
ocf_log info "add_or_update_master_dns_entry: master_host = $master_host"
#Delete existing entry if exists
sed -i "/${master_internal_dns}/d" /etc/hosts
#Add new entry
dns_entry_line="$master_host $master_internal_dns $master_internal_dns_alias"
sed -i "\$ a $dns_entry_line" /etc/hosts
}
# Removes internal master dns entry in /etc/hosts
remove_master_dns_entry() {
ocf_log debug "debug: inside remove_master_dns_entry"
local master_internal_dns="$OCF_RESKEY_master_internal_dns"
if [ -z "$master_internal_dns" ]; then
ocf_log err "remove_master_dns_entry: master internal dns is empty"
return $OCF_ERR_GENERIC
fi
ocf_log err "remove_master_dns_entry: removing master internal dns entry"
sed -i "/${master_internal_dns}/d" /etc/hosts
return $OCF_SUCCESS;
}
# Get CRM attribute whose lifetime is reboot
# get_crm_node_attr_val "host" "key" ["defaultVal"]
get_crm_node_attr_val() {
ocf_log debug "inside get_crm_node_attr_val"
local node=$1
local key=$2
local defaultVal=$3
local crm_node_attr_get_cmd
local out
ocf_log debug "get_crm_node_attr_val: node=$node key=$key defaultVal=$defaultVal"
crm_node_attr_get_cmd="$CRM_ATTR -N $node -l reboot --name $key --query -q"
if [ -n "$defaultVal" ]; then
crm_node_attr_get_cmd="${crm_node_attr_get_cmd} --default=${defaultVal}"
fi
out=`$crm_node_attr_get_cmd`
ocf_log debug "get_crm_node_attr_val: value=$out"
echo $out
}
# Set CRM attribute whose lifetime is reboot
# set_crm_node_attr_val [-b] "host" "key" "value"
set_crm_node_attr_val() {
ocf_log debug "inside set_crm_node_attr_val"
local node
local key
local value
local background
local crm_node_attr_set_cmd
for var in 1 2 3 4
do
case "$1" in
"-b")
background=1
shift 1;;
*)
;;
esac
done
node=$1
key=$2
value=$3
ocf_log debug "set_crm_node_attr_val: node=$node key=$key value=$value"
crm_node_attr_set_cmd="$CRM_ATTR -N $node -l reboot --name $key -v $value"
if [ -n "$background" ]; then
$crm_node_attr_set_cmd &
else
$crm_node_attr_set_cmd
fi
}
# Get CRM config attribute value
# get_crm_config_attr_val "attr_set_name" "attr_name" ["defaultVal"]
get_crm_config_attr_val() {
ocf_log debug "inside get_crm_config_attr_val"
local attr_set_name=$1
local attr_name=$2
local defaultVal=$3
local crm_config_attr_get_cmd
local out
local rc
ocf_log debug "get_crm_config_attr_val: attribute_name=$attr_name attribute_set_name=$attr_set_name defaultVal=$defaultVal"
crm_config_attr_get_cmd="$CRM_CONFIG_ATTR -s $attr_set_name --name $attr_name --query"
if [ -n "$defaultVal" ]; then
crm_config_attr_get_cmd="${crm_config_attr_get_cmd} --default=${defaultVal}"
fi
out=`$crm_config_attr_get_cmd`
rc=$?
ocf_log debug "get_crm_config_attr_val: value=$out"
echo $out
return $rc
}
# Set CRM attribute whose lifetime is reboot
# set_crm_config_attr_val [-b] "attr_set_name" "attr_name" "attr_value"
set_crm_config_attr_val() {
ocf_log debug "inside set_crm_config_attr_val"
local attr_set_name
local attr_name
local attr_value
local background
local crm_config_attr_set_cmd
local rc
for var in 1 2 3 4
do
case "$1" in
"-b")
background=1
shift 1;;
*)
;;
esac
done
attr_set_name=$1
attr_name=$2
attr_value=$3
ocf_log debug "set_crm_config_attr_val: attribute_name=$attr_name attribute_set_name=$attr_set_name attribute_value=$attr_value"
crm_config_attr_set_cmd="$CRM_CONFIG_ATTR -s $attr_set_name --name $attr_name -v $attr_value"
if [ -n "$background" ]; then
$crm_config_attr_set_cmd &
else
$crm_config_attr_set_cmd
fi
rc=$?
return $rc
}
is_symmetric_cluster() {
ocf_log debug "inside is_symmetric_cluster"
local is_symmetric_cluster
local rc
is_symmetric_cluster=`get_crm_config_attr_val "${CIB_BOOTSTRAP_OPTIONS_PROPERTY_SET}" "${SYMMETRIC_CLUSTER_PROPERTY}"`
if ocf_is_true $is_symmetric_cluster; then
return $OCF_SUCCESS
else
return $OCF_ERR_GENERIC
fi
}
# Get All resource nodes configured under pacemaker
# This will ensure only nodes which can run resources are returned.
get_crm_resource_nodes() {
ocf_log debug "inside get_crm_resource_nodes"
crm_resource_nodes_out=`get_crm_config_attr_values_with_retries "$REPLICATION_CRM_ATTR_SET_NAME" "$PG_RESOURCE_NODES"`
rc=$?
crm_resource_nodes_out=`echo "$crm_resource_nodes_out" | xargs`
echo "$crm_resource_nodes_out"
return $rc
}
# Checks if the process with pid exists
is_process_with_pid_exists() {
ocf_log debug "inside is_process_with_pid_exists"
local pid=$1
local rc
if [ -d /proc -a -d /proc/1 ]; then
[ "u$pid" != "u" -a -d /proc/$pid ]
else
kill -s 0 $pid >/dev/null 2>&1
fi
rc=$?
if [ $rc -eq 0 ]; then
return $OCF_SUCCESS
else
return $OCF_ERR_GENERIC
fi
}
get_crm_config_attr_values_with_retries() {
local attr_set_name=$1
local attr_name=$2
local default_val=$3
local attr_value
local Iter=5
while [ "$Iter" -gt 0 ]; do
if [ -z "$default_val" ]; then
attr_value=`get_crm_config_attr_val "$attr_set_name" "$attr_name"`
else
attr_value=`get_crm_config_attr_val "$attr_set_name" "$attr_name" "$default_val"`
fi
rc=$?
if [ "$rc" -eq 0 ]; then
break;
else
sleep 1
let Iter-=1
fi
done
echo "$attr_value"
return $rc
}
get_repl_status_attribute_from_cib() {
# Get the last repl. status from the cib, it may happen after a crash and
# pacemaker may take longer to reply so we loop
local repl_status_attr
repl_status_attr=`get_crm_config_attr_values_with_retries "$REPLICATION_CRM_ATTR_SET_NAME" "$REPLICATION_STATUS_CRM_ATTR_NAME"`
rc=$?
echo "$repl_status_attr"
return $rc
}
# Notify SG regarding the promotion
sg_notify_promote_event() {
ocf_log debug "inside sg_notify_promote_event"
local rc
if [ -n "$OCF_RESKEY_sg_promote_notify_endpoint" ]; then
ocf_log debug "sg_notify_promote_event: Invoking sg promote notify endpoint ${OCF_RESKEY_sg_promote_notify_endpoint}"
curl ${OCF_RESKEY_sg_promote_notify_endpoint}
rc=$?
if [ $rc -eq 0 ]; then
return $OCF_SUCCESS
else
return $OCF_ERR_GENERIC
fi
else
ocf_log err "sg_notify_promote_event: sg promote notify endpoint not specified"
return $OCF_ERR_GENERIC
fi
}
#######################################################################
# Convenience functions for PostgreSQL
# run_as_resource_owner: Run the given command in the Resource owner environment.
# Usage: run_as_resource_owner [-Q] [-info|-warn|-err] [-O] <command>
# -Q: don't log the output of the command if it succeeds
# -info|-warn|-err: log the output of the command at given
# severity if it fails (defaults to err)
# -O: echo the output of the command
# Adapted from ocf_run.
#
run_as_resource_owner() {
ocf_log debug "inside run_as_resource_owner"
local rc
local output outputfile
local verbose=1
local returnoutput
local loglevel=err
local var
for var in 1 2 3
do
case "$1" in
"-Q")
verbose=""
shift 1;;
"-info"|"-warn"|"-err")
loglevel=`echo $1 | sed -e s/-//g`
shift 1;;
"-O")
returnoutput=1
shift 1;;
*)
;;
esac
done
outputfile=`mktemp ${HA_RSCTMP}/run_as_resource_owner.${OCF_RESOURCE_INSTANCE}.XXXXXX`
error=`/bin/su - $OCF_RESKEY_user -c "$@" 2>&1 1>$outputfile`
rc=$?
output=`cat $outputfile`
rm -f $outputfile
if [ $rc -eq 0 ]; then
if [ "$verbose" -a ! -z "$output" ]; then
ocf_log info "run_as_resource_owner: Output = $output"
fi
if [ "$returnoutput" -a ! -z "$output" ]; then
echo "$output"
fi
return $OCF_SUCCESS
else
if [ ! -z "$error" ]; then
ocf_log $loglevel "run_as_resource_owner: Error = $error"
else
ocf_log $loglevel "run_as_resource_owner: command failed: $*"
fi
# No output to parse so return the standard exit code.
return $rc
fi
}
# Returns the port to connect to PostgreSQL
# If pgbouncer is configured then it will use that port else default to repl port.
get_pgsql_port_to_connect() {
ocf_log debug "inside get_pgsql_port_to_connect"
if [ -z "$pgport" ]; then
pgport="$OCF_RESKEY_replication_port"
local pgbouncer_port=`get_pg_bouncer_port`
if [ ! -z "$pgbouncer_port" ]; then
pgport="$pgbouncer_port"
fi
fi
echo "$pgport"
}
get_pg_bouncer_port() {
ocf_log debug "inside get_pg_bouncer_port"
local pgbouncer_port=`get_crm_config_attr_values_with_retries "$PGSQL_PGBOUNCER_CRM_ATTR_SET_NAME" "$PGSQL_PGBOUNCER_PORT_CRM_ATTR_NAME" ""`
echo "$pgbouncer_port"
}
get_pg_bouncer_service_name() {
ocf_log debug "inside get_pg_bouncer_service_name"
local pgbouncer_service_name=`get_crm_config_attr_values_with_retries "$PGSQL_PGBOUNCER_CRM_ATTR_SET_NAME" "$PGSQL_PGBOUNCER_SERVICE_NAME_CRM_ATTR_NAME" ""`
echo "$pgbouncer_service_name"
}
# Best effort to stop pgbouncer
stop_pg_bouncer() {
ocf_log debug "inside stop_pg_bouncer"
local pgbouncer_port=`get_pg_bouncer_port`
if [ -n "$pgbouncer_port" ]; then
local pgbouncer_service_name=`get_pg_bouncer_service_name`
if [ -n "$pgbouncer_service_name" ]; then
service $pgbouncer_service_name stop
fi
fi
}
# Best effort to start pgbouncer
start_pg_bouncer() {
ocf_log debug "inside start_pg_bouncer"
local pgbouncer_port=`get_pg_bouncer_port`
if [ -n "$pgbouncer_port" ]; then
local pgbouncer_service_name=`get_pg_bouncer_service_name`
if [ -n "$pgbouncer_service_name" ]; then
service $pgbouncer_service_name start
fi
fi
}
# Creates PostgreSQL recovery configuration file
create_recovery_conf() {
ocf_log debug "inside create_recovery_conf"
local rc
if [ -z "$OCF_RESKEY_recovery_conf_file" ]; then
ocf_log err "create_recovery_conf: recovery file not specified"
return $OCF_ERR_GENERIC
fi
run_as_resource_owner -Q -err -O "touch $OCF_RESKEY_recovery_conf_file"
rc=$?
if [ $rc -ne 0 ]; then
ocf_exit_reason "create_recovery_conf: Unable to create recovery conf file $OCF_RESKEY_recovery_conf_file"
return $OCF_ERR_GENERIC
fi
cat > $OCF_RESKEY_recovery_conf_file <<EOF
standby_mode = 'on'
primary_conninfo = 'host=${OCF_RESKEY_master_internal_dns} port=${OCF_RESKEY_replication_port} user=${OCF_RESKEY_replication_user} password=${OCF_RESKEY_replication_passwd} application_name=${HOSTNAME}'
recovery_target_timeline = 'latest'
EOF
if ocf_is_true $OCF_RESKEY_use_replication_slots; then
local primary_slot_name=`convert_replication_slot_name_to_adhere_conventions "${HOSTNAME}"`
local primary_slot="primary_slot_name = '${primary_slot_name}'"
echo $primary_slot >> $OCF_RESKEY_recovery_conf_file
fi
# Recovery conf should be readable by PostgreSQL User/Group only
chmod 0600 $OCF_RESKEY_recovery_conf_file
chown $OCF_RESKEY_user $OCF_RESKEY_recovery_conf_file
chgrp $OCF_RESKEY_group $OCF_RESKEY_recovery_conf_file
}
# psql_run: Run the given PostgreSQL query.
# Usage: psql_run [-Q] [-info|-warn|-err] [-O] [-test|-repl] [-usePgPort] [-x] <query>
# -Q: don't log the output of the psql if it succeeds
# -info|-warn|-err: log the output of the command at given
# severity if it fails (defaults to err)
# -O: echo the output of the command
# -test|-repl: Which credentials needs to be used to connect to psql
# -x: Enable extended output of records (print in key value pair)
# -usePgPort: Use PostgreSQL port itself and not any connection pooler
# Adapted from ocf_run.
#
psql_run() {
ocf_log debug "inside psql_run"
local rc
local output outputfile
local verbose=1
local returnoutput
local loglevel=err
local var
local inputfile
local query
local cred_type
local psql_options=""
local psql_cmd
local extendedOut
local usePgPort
for var in 1 2 3 4 5
do
case "$1" in
"-Q")
verbose=""
shift 1;;
"-info"|"-warn"|"-err")
loglevel=`echo $1 | sed -e s/-//g`
shift 1;;
"-O")
returnoutput=1
shift 1;;
"-test"|"-repl")
cred_type=`echo $1 | sed -e s/-//g`
shift 1;;
"-x")
extendedOut=1
shift 1;;