1#!@BASH_SHELL@
2#
3# Copyright (C) 1997-2003 Sistina Software, Inc.  All rights reserved.
4# Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19#
20
21export LC_ALL=C
22export LANG=C
23export PATH=/bin:/sbin:/usr/bin:/usr/sbin
24
25. $(dirname $0)/ocf-shellfuncs
26. $(dirname $0)/utils/config-utils.sh
27. $(dirname $0)/utils/messages.sh
28. $(dirname $0)/utils/ra-skelet.sh
29
30declare PSQL_POSTMASTER="/usr/bin/postmaster"
31declare PSQL_CTL="/usr/bin/pg_ctl"
32declare PSQL_pid_file="`generate_name_for_pid_file`"
33declare PSQL_conf_dir="`generate_name_for_conf_dir`"
34declare PSQL_gen_config_file="$PSQL_conf_dir/postgresql.conf"
35declare PSQL_kill_timeout="5"
36declare PSQL_stop_timeout="15"
37if [ -z "$OCF_RESKEY_startup_wait" ]; then
38	OCF_RESKEY_startup_wait=10
39fi
40
41verify_all()
42{
43	clog_service_verify $CLOG_INIT
44
45	if [ -z "$OCF_RESKEY_name" ]; then
46		clog_service_verify $CLOG_FAILED "Invalid Name Of Service"
47		return $OCF_ERR_ARGS
48	fi
49
50	if [ -z "$OCF_RESKEY_service_name" ]; then
51		clog_service_verify $CLOG_FAILED_NOT_CHILD
52		return $OCF_ERR_ARGS
53	fi
54
55	if [ -z "$OCF_RESKEY_config_file" ]; then
56		clog_check_file_exist $CLOG_FAILED_INVALID "$OCF_RESKEY_config_file"
57		clog_service_verify $CLOG_FAILED
58		return $OCF_ERR_ARGS
59	fi
60
61	if [ ! -r "$OCF_RESKEY_config_file" ]; then
62		clog_check_file_exist $CLOG_FAILED_NOT_READABLE $OCF_RESKEY_config_file
63		clog_service_verify $CLOG_FAILED
64		return $OCF_ERR_ARGS
65	fi
66
67	if [ -z "$OCF_RESKEY_postmaster_user" ]; then
68		clog_servicer_verify $CLOG_FAILED "Invalid User"
69		return $OCF_ERR_ARGS
70	fi
71
72	clog_service_verify $CLOG_SUCCEED
73
74	return 0
75}
76
77generate_config_file()
78{
79	declare original_file="$1"
80	declare generated_file="$2"
81	declare ip_addressess="$3"
82
83	declare ip_comma="";
84
85	if [ -f "$generated_file" ]; then
86		sha1_verify "$generated_file"
87		if [ $? -ne 0 ]; then
88			clog_check_sha1 $CLOG_FAILED
89			return 0
90		fi
91	fi
92
93	clog_generate_config $CLOG_INIT "$original_file" "$generated_file"
94
95	declare x=1
96	for i in $ip_addressess; do
97		i=`echo $i | sed -e 's/\/.*$//'`
98		if [ $x -eq 1 ]; then
99			x=0
100			ip_comma=$i
101		else
102			ip_comma=$ip_comma,$i
103		fi
104	done
105
106	generate_configTemplate "$generated_file" "$1"
107	echo "external_pid_file = '$PSQL_pid_file'" >> "$generated_file"
108	echo "listen_addresses = '$ip_comma'" >> "$generated_file"
109
110	echo >> "$generated_file"
111	sed 's/^[[:space:]]*external_pid_file/### external_pid_file/i;s/^[[:space:]]*listen_addresses/### listen_addresses/i' < "$original_file" >> "$generated_file"
112
113        sha1_addToFile "$generated_file"
114	clog_generate_config $CLOG_SUCCEED "$original_file" "$generated_file"
115
116	return 0;
117}
118
119start()
120{
121	declare pguser_group
122	declare count=0
123	clog_service_start $CLOG_INIT
124
125	create_pid_directory
126	create_conf_directory "$PSQL_conf_dir"
127	check_pid_file "$PSQL_pid_file"
128
129	if [ $? -ne 0 ]; then
130		clog_check_pid $CLOG_FAILED "$PSQL_pid_file"
131		clog_service_start $CLOG_FAILED
132		return $OCF_ERR_GENERIC
133	fi
134
135	#
136	# Create an empty PID file for the postgres user and
137	# change it to be owned by the postgres user so that
138	# postmaster doesn't complain.
139	#
140	pguser_group=`groups $OCF_RESKEY_postmaster_user | cut -f3 -d ' '`
141	touch $PSQL_pid_file
142	chown $OCF_RESKEY_postmaster_user.$pguser_group $PSQL_pid_file
143
144	clog_looking_for $CLOG_INIT "IP Addresses"
145
146        get_service_ip_keys "$OCF_RESKEY_service_name"
147        ip_addresses=`build_ip_list`
148
149	if [ -z "$ip_addresses" ]; then
150		clog_looking_for $CLOG_FAILED_NOT_FOUND "IP Addresses"
151		return $OCF_ERR_GENERIC
152	fi
153
154	clog_looking_for $CLOG_SUCCEED "IP Addresses"
155
156	generate_config_file "$OCF_RESKEY_config_file" "$PSQL_gen_config_file" "$ip_addresses"
157
158	su - "$OCF_RESKEY_postmaster_user" -c "$PSQL_POSTMASTER -c config_file=\"$PSQL_gen_config_file\" \
159		$OCF_RESKEY_postmaster_options" &> /dev/null &
160
161	# We need to sleep briefly to allow pg_ctl to detect that we've started.
162	# We need to fetch "-D /path/to/pgsql/data" from $OCF_RESKEY_postmaster_options
163	until [ "$count" -gt "$OCF_RESKEY_startup_wait" ] ||
164		[ `su - "$OCF_RESKEY_postmaster_user" -c \
165			"$PSQL_CTL status $OCF_RESKEY_postmaster_options" &> /dev/null; echo $?` = '0' ]
166	do
167		sleep 1
168		let count=$count+1
169	done
170
171	if [ "$count" -gt "$OCF_RESKEY_startup_wait" ]; then
172		clog_service_start $CLOG_FAILED
173		return $OCF_ERR_GENERIC
174	fi
175
176	clog_service_start $CLOG_SUCCEED
177	return 0;
178}
179
180stop()
181{
182	clog_service_stop $CLOG_INIT
183
184	## Send -INT to close connections and stop.  -QUIT is used if -INT signal does not stop process.
185	stop_generic_sigkill "$PSQL_pid_file" "$PSQL_stop_timeout" "$PSQL_kill_timeout" "-INT"
186	if [ $? -ne 0 ]; then
187		clog_service_stop $CLOG_FAILED
188		return $OCF_ERR_GENERIC
189	fi
190
191	clog_service_stop $CLOG_SUCCEED
192	return 0;
193}
194
195status()
196{
197	clog_service_status $CLOG_INIT
198
199	status_check_pid "$PSQL_pid_file"
200	if [ $? -ne 0 ]; then
201		clog_service_status $CLOG_FAILED "$PSQL_pid_file"
202		return $OCF_ERR_GENERIC
203	fi
204
205	clog_service_status $CLOG_SUCCEED
206	return 0
207}
208
209case $1 in
210	meta-data)
211		cat `echo $0 | sed 's/^\(.*\)\.sh$/\1.metadata/'`
212		exit 0
213		;;
214	validate-all)
215		verify_all
216		exit $?
217		;;
218	start)
219		verify_all && start
220		exit $?
221		;;
222	stop)
223		verify_all && stop
224		exit $?
225		;;
226	status|monitor)
227		verify_all
228		status
229		exit $?
230		;;
231	restart)
232		verify_all
233		stop
234		start
235		exit $?
236		;;
237	*)
238		echo "Usage: $0 {start|stop|status|monitor|restart|meta-data|validate-all}"
239		exit $OCF_ERR_UNIMPLEMENTED
240		;;
241esac
242