dev-python/pyfeyn: cleaning old
[gentoo.git] / dev-db / drizzle / files / drizzle.init.d
1 #!/sbin/runscript
2 # Copyright 1999-2011 Pavel Stratil, senbonzakura.eu
3 # Some functions were taken from debian init script. Licensed under GPL-2
4 # Distributed under the terms of the GNU General Public License v2
5 # $Id$
6
7 #########################
8 ### Construct vars ######
9 #########################
10
11
12 SUFFIX=".${SVCNAME#*.}"
13 [ "${SUFFIX}" == ".drizzled" ] && SUFFIX=''
14
15 BASE_CNF="/etc/drizzle/drizzled"
16 BASE_PID="/var/run/drizzle/drizzled"
17 BASE_LOG="/var/log/drizzle/drizzled"
18 BASE_DIR="/var/lib/drizzle/drizzled"
19
20 PIDFILE="${BASE_PID}${SUFFIX}.pid"
21 CNFFILE="${BASE_CNF}${SUFFIX}.cnf"
22 LOGFILE="${BASE_LOG}${SUFFIX}.log"
23 DATADIR="${BASE_DIR}${SUFFIX}"
24 DRIZZLE="/usr/bin/drizzle"
25 DRIZZLE_USER="drizzle"
26 DRIZZLE_DAEMON="/usr/sbin/drizzled"
27 DRIZZLE_EXTRA=""
28
29 #########################
30 ### Helper functions ####
31 #########################
32
33
34 #
35 # drizzle_status() checks if there is a server running and if it is accessible.
36 # "check_alive" insists on a pingable server, "check_dead" also fails
37 # if there is a lost drizzled in the process list
38 # Usage: boolean drizzle_status [check_alive|check_dead] [warn|nowarn]
39 #
40 drizzle_status() {
41         ping_output=`$DRIZZLE --ping 2>&1`; ping_alive=$(( ! $? ))
42         ps_alive=0
43         if [ -f "$PIDFILE" ] && ps `cat $PIDFILE` >/dev/null 2>&1; then ps_alive=1; fi
44
45         if [ "$1" = "check_alive"  -a  $ping_alive = 1 ] ||
46            [ "$1" = "check_dead"   -a  $ping_alive = 0  -a  $ps_alive = 0 ]; then
47            return 0 # EXIT_SUCCESS
48         else
49         if [ "$2" = "warn" ]; then
50                 echo -e "$ps_alive processes alive and '$DRIZZLE --ping' resulted in\n$ping_output\n"
51         fi
52         return 1 # EXIT_FAILURE
53         fi
54 }
55
56 #########################
57 ### Main ################
58 #########################
59
60 checkconfig() {
61         # TODO: --print-defaults no longer a valid option. Needs to be rewritten.
62         #CNFDATADIR=`drizzle_get_param datadir`
63                 #if [ -z "${CNFDATADIR}" ] ; then
64         #   ewarn "Datadir not set in ${CNFFILE}."
65         #   ewarn "Trying to use ${DATADIR}"
66         #else
67            DATADIR="${CNFDATADIR}"
68         #fi
69
70         if [[ ! -d "${DATADIR}" ]] ; then
71                 eerror "Drizzle datadir is empty or invalid."
72                 eerror "Please check your configuration ${CNFFILE} and DRIZZLE_EXTRA"
73                 return 1
74         fi
75
76         if [ ! -f "${CNFFILE}" ]; then
77                 eerror "The configuration file $CNFFILE was not found!"
78         fi
79 }
80
81 depend() {
82         use localmount
83         use gearmand
84         use memcached
85
86         # TODO use drizzle_get_param() to decide if gearmand and memcached
87         #      are needed. Then the useflag based sed-ing of this script
88         #      can be removed from the ebuild.
89 }
90
91
92 stop() {
93         ebegin "Stopping ${SVCNAME}"
94         start-stop-daemon --pidfile ${PIDFILE} --stop \
95                 --exec ${DRIZZLE_DAEMON}
96         eend $?
97         drizzle_status check_dead warn
98 }
99
100 start() {
101         #checkconfig
102         ebegin "Starting ${SVCNAME}"
103         # Test if ${BASE_PID}, ${BASE_LOG} and ${LOG_FILE} exist, create if not.
104         [ ! -e ${BASE_PID} ] && mkdir -p ${BASE_PID} && chown ${DRIZZLE_USER}:nogroup ${BASE_PID}
105         [ ! -e ${BASE_LOG} ] && mkdir -p ${BASE_LOG} && chown ${DRIZZLE_USER}:nogroup ${BASE_LOG}
106         [ ! -e ${LOGFILE} ]  && touch ${LOGFILE} && chown ${DRIZZLE_USER}:nogroup ${LOGFILE}
107         start-stop-daemon --background --pidfile ${PIDFILE} --stderr ${LOGFILE} \
108                 --user ${DRIZZLE_USER} --start --exec ${DRIZZLE_DAEMON} -- \
109                 --datadir=${DATADIR} --pid-file=${PIDFILE} --user=${DRIZZLE_USER} \
110                 ${DRIZZLE_EXTRA}
111         eend $?
112
113                 # TODO in order to have replication always working we should add the
114                 #      --server-id=# option. AFAIK only integers are allowed, though
115                 #      ${HOSTNAME}${SVCNAME}${SUFFIX} whould be much easier to handle.
116
117         for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
118                 sleep 1
119                 if drizzle_status check_alive nowarn ; then break ; fi
120         done
121         if drizzle_status check_alive warn ; then
122                 einfo "${SVCNAME} is alive!"
123         else
124                 eerror "${SVCNAME} died!"
125         fi
126 }
127
128 status() {
129         if drizzle_status check_alive nowarn; then
130                 einfo "status: started"
131         else
132                 einfo "status: stopped"
133         fi
134 }