/
postgresxc.init
362 lines (312 loc) · 8.67 KB
/
postgresxc.init
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
#!/bin/sh
#
# postgresxc This is the init script for starting up the Postgres-XC
# server.
#
# chkconfig: - 64 36
# description: Postgres-XC database server.
# processname: postmaster
# pidfile="/var/run/${NAME}.pid"
# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresxc)
# Version 1.1. David Wheeler <david@justatheory.com>
# Based on PostgreSQL script by Devrim Gunduz <devrim@gunduz.org>.
# PGXCVERSION is the full package version, e.g., 1.1.0
# Note: the specfile inserts the correct value during package build
PGVERSION=xxxx
# PGMAJORVERSION is major version, e.g., 1.1 (this should match PG_VERSION)
PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'`
PGSQLMAJORVERSION=xxxx # this should match PG_VERSION
PGPREVMAJORVERSION=1.0
# Source function library.
INITD=/etc/rc.d/init.d
. $INITD/functions
# Get function listing for cross-distribution logic.
TYPESET=`typeset -f|grep "declare"`
# Get network config.
. /etc/sysconfig/network
# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
NAME=${NAME:3}
fi
# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
SU=runuser
else
SU=su
fi
# Define variable for locale parameter:
LOCALEPARAMETER=$2
# Set defaults for configuration variables
PGENGINE=/usr/pgxc-$PGMAJORVERSION/bin
PGPORT=5432
PGDATA=/var/lib/pgxc/$PGMAJORVERSION/data
PGLOG=/var/lib/pgxc/$PGMAJORVERSION/pgstartup.log
# Log file for pg_upgrade
PGUPLOG=/var/lib/pgxc/$PGMAJORVERSION/pgupgrade.log
# This node's role
PGCOMPONENT=datanode
#PGCOMPONENT=coordinator
lockfile="/var/lock/subsys/${NAME}"
pidfile="/var/run/${NAME}.pid"
# Override defaults from /etc/sysconfig/pgxc if file is present
[ -f /etc/sysconfig/pgxc/${NAME} ] && . /etc/sysconfig/pgxc/${NAME}
export PGDATA
export PGPORT
[ -f "$PGENGINE/postmaster" ] || exit 1
script_result=0
start(){
[ -x "$PGENGINE/postmaster" ] || exit 5
PSQL_START=$"Starting ${NAME} service: "
# Make sure startup-time log file is valid
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
then
touch "$PGLOG" || exit 1
chown postgres:postgres "$PGLOG"
chmod go-rwx "$PGLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
# Check for the PGDATA structure
if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ]
then
# Check version of existing PGDATA
if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGSQLMAJORVERSION" ]
then
SYSDOCDIR="(Your System's documentation directory)"
if [ -d "/usr/doc/postgresxc-$PGVERSION" ]
then
SYSDOCDIR=/usr/doc
fi
if [ -d "/usr/share/doc/postgresxc-$PGVERSION" ]
then
SYSDOCDIR=/usr/share/doc
fi
if [ -d "/usr/doc/packages/postgresxc-$PGVERSION" ]
then
SYSDOCDIR=/usr/doc/packages
fi
if [ -d "/usr/share/doc/packages/postgresxc-$PGVERSION" ]
then
SYSDOCDIR=/usr/share/doc/packages
fi
echo
echo $"An old version of the database format was found."
echo $"You need to upgrade the data format before using Postgres-XC."
echo $"See $SYSDOCDIR/postgresxc-$PGVERSION/README.rpm-dist for more information."
exit 1
fi
else
# No existing PGDATA! Warn the user to initdb it.
echo
echo "$PGDATA is missing. Use \"service $NAME initdb\" to initialize the cluster first."
echo_failure
echo
exit 1
fi
echo -n "$PSQL_START"
$SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' --$PGCOMPONENT ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null
sleep 2
pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
if [ "x$pid" != x ]
then
success "$PSQL_START"
touch "$lockfile"
echo $pid > "$pidfile"
echo
else
failure "$PSQL_START"
echo
script_result=1
fi
}
stop(){
echo -n $"Stopping ${NAME} service: "
if [ -e "$lockfile" ]
then
$SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null
ret=$?
if [ $ret -eq 0 ]
then
echo_success
rm -f "$pidfile"
rm -f "$lockfile"
else
echo_failure
script_result=1
fi
else
# not running; per LSB standards this is "ok"
echo_success
fi
echo
}
restart(){
stop
start
}
initdb(){
# If the locale name is specified just after the initdb parameter, use it:
if [ -z $LOCALEPARAMETER ]
then
LOCALE=`echo $LANG`
else
LOCALE=`echo $LOCALEPARAMETER`
fi
LOCALESTRING="--locale=$LOCALE"
if [ -f "$PGDATA/PG_VERSION" ]
then
echo "Data directory is not empty!"
echo_failure
else
echo -n $"Initializing database: "
if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ]
then
mkdir -p "$PGDATA" || exit 1
chown postgres:postgres "$PGDATA"
chmod go-rwx "$PGDATA"
fi
# Clean up SELinux tagging for PGDATA
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
# Make sure the startup-time log file is OK, too
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
then
touch "$PGLOG" || exit 1
chown postgres:postgres "$PGLOG"
chmod go-rwx "$PGLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
fi
# Initialize the database
$SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident' --nodename=`hostname -s` $LOCALESTRING" >> "$PGLOG" 2>&1 < /dev/null
# Create directory for postmaster log
mkdir "$PGDATA/pg_log"
chown postgres:postgres "$PGDATA/pg_log"
chmod go-rwx "$PGDATA/pg_log"
[ -f "$PGDATA/PG_VERSION" ] && echo_success
[ ! -f "$PGDATA/PG_VERSION" ] && echo_failure
echo
fi
}
upgrade(){
# The second parameter is the new database version, i.e. $PGMAJORVERSION in this case.
# Use "postgresxc-$PGMAJORVERSION" service, if not specified.
INIT_SCRIPT="$2"
if [ x"$INIT_SCRIPT" = x ]
then
INIT_SCRIPT=postgresxc-$PGMAJORVERSION
fi
# The third parameter is the old database version, i.e. $PGPREVMAJORVERSION in this case.
# Use "postgresxc-$PGPREVMAJORVERSION" service, if not specified.
OLD_INIT_SCRIPT="$3"
if [ x"$OLD_INIT_SCRIPT" = x ]
then
OLD_INIT_SCRIPT=postgresxc-$PGPREVMAJORVERSION
fi
# Find the init script of the new version:
if [ ! -f "/etc/init.d/${INIT_SCRIPT}" ]
then
echo "Could not find init script /etc/init.d/${INIT_SCRIPT}"
fi
# Find the init script of the old version
if [ ! -f "/etc/init.d/${OLD_INIT_SCRIPT}" ]
then
echo "Could not find init script /etc/init.d/${OLD_INIT_SCRIPT}"
echo "Please install postgresxc91-server RPM first."
exit
fi
# Get port number and data directory of the old instance from the init script
OLDPGDATA=` sed -n 's/^PGDATA=//p' /etc/init.d/postgresxc-$PGPREVMAJORVERSION`
OLDPGPORT=`sed -n 's/^PGPORT=//p' /etc/init.d/postgresxc-$PGPREVMAJORVERSION`
# Get port number and data directory of the new instance from the init script
NEWPGDATA=` sed -n 's/^PGDATA=//p' /etc/init.d/postgresxc-$PGMAJORVERSION`
NEWPGPORT=`sed -n 's/^PGPORT=//p' /etc/init.d/postgresxc-$PGMAJORVERSION`
if [ ! -x "$PGENGINE/pg_upgrade" ]
then
echo
echo $"Please install the postgresxc$PGSQLMAJORVERSION-contrib RPM for pg_upgrade command."
echo
exit 5
fi
# Perform initdb on the new server
/sbin/service $NAME initdb
RETVAL=$?
if [ $RETVAL -ne 0 ]
then
echo "initdb failed!"
exit 1
fi
# Check the clusters first, without changing any data:
su -l postgres -c "$PGENGINE/pg_upgrade -b /usr/pgxc-$PGPREVMAJORVERSION/bin/ -B $PGENGINE/ -d $OLDPGDATA -D $NEWPGDATA -p $OLDPGPORT -P $NEWPGPORT -c"
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
echo "Clusters checked successfully, proceeding with upgrade from $PGPREVMAJORVERSION to $PGMAJORVERSION"
echo "Stopping old cluster"
/sbin/service $OLD_INIT_SCRIPT stop
# Set up log file for pg_upgrade
rm -f "$PGUPLOG"
touch "$PGUPLOG" || exit 1
chown postgres:postgres "$PGUPLOG"
chmod go-rwx "$PGUPLOG"
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
echo "Performing upgrade"
su -l postgres -c "$PGENGINE/pg_upgrade \
-b /usr/pgxc-$PGPREVMAJORVERSION/bin/ -B $PGENGINE/ \
-d $OLDPGDATA -D $NEWPGDATA \
-p $OLDPGPORT -P $NEWPGPORT" >> "$PGUPLOG" 2>&1 < /dev/null
else
echo "Cluster check failed. Please see the output above."
exit 1
fi
echo
exit 0
}
condrestart(){
[ -e "$lockfile" ] && restart || :
}
reload(){
$SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
promote(){
$SU -l postgres -c "$PGENGINE/pg_ctl promote -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p $pidfile $NAME
script_result=$?
;;
restart)
restart
;;
initdb)
initdb
;;
promote)
promote
;;
upgrade)
upgrade
;;
condrestart|try-restart)
condrestart
;;
reload)
reload
;;
force-reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|upgrade|condrestart|try-restart|reload|force-reload|initdb|promote}"
exit 2
esac
exit $script_result