init scripts for gmg
[fai-configs.git] / files / etc / init.d / mediagoblin-paster / DEFAULT
1 #!/bin/sh
2 # /etc/init.d/mediagoblin-paster
3 #
4 ## LICENSE: CC0 <http://creativecommons.org/publicdomain/zero/1.0/>
5 # To the extent possible under law, Joar Wandborg <http://wandborg.se> has
6 # waived all copyright and related or neighboring rights to
7 # mediagoblin-paster. This work is published from Sweden.
8 #
9 ## CREDIT
10 # Credit goes to jpope <http://jpope.org/> and 
11 # chimo <http://chimo.chromic.org/>. From which' Arch init scripts this is
12 # based upon.
13 #
14 ### BEGIN INIT INFO
15 # Provides:          mediagoblin-paster
16 # Required-Start:    $network $named $local_fs
17 # Required-Stop:     $remote_fs $syslog $network $named $local_fs
18 # Should-Start:      postgresql $syslog
19 # Default-Start:     2 3 4 5
20 # Default-Stop:      0 1 6
21 # Short-Description: MediaGoblin paster FCGI server init script
22 # Description:       This script will initiate the GNU MediaGoblin paster
23 #                    fcgi server.
24 ### END INIT INFO
25
26 ################################################################################
27 # CHANGE THIS
28 # to suit your environment
29 ################################################################################
30 MG_ROOT=GMG_PATH_TOKEN/mediagoblin
31 MG_USER=mediagoblin
32 ################################################################################
33 # NOW STOP
34 # You probably won't have to change anything else.
35 ################################################################################
36
37 set -e
38
39 DAEMON_NAME=mediagoblin-paster
40
41 MG_BIN=$MG_ROOT/bin
42 MG_PASTER_BIN=$MG_BIN/paster
43 MG_PASTE_INI=$MG_ROOT/paste_local.ini
44 MG_FCGI_HOST=127.0.0.1
45 MG_FCGI_PORT=26543
46 MG_PASTER_PID_FILE=/var/run/mediagoblin/$DAEMON_NAME.pid
47 MG_PASTER_LOG_FILE=/var/log/mediagoblin/$DAEMON_NAME.log
48
49 set_up_directories() {
50     install -o $MG_USER -g users -d -m 755 /var/log/mediagoblin
51     install -o $MG_USER -g users -d -m 755 /var/run/mediagoblin
52 }
53
54 set_up_directories
55
56 # Include LSB helper functions
57 . /lib/lsb/init-functions
58
59 getPID () {
60     # Discard any errors from cat
61     cat $MG_PASTER_PID_FILE 2>/dev/null
62 }
63
64 case "$1" in 
65     start)
66         # Start the MediaGoblin paster process
67         log_daemon_msg "Starting GNU MediaGoblin paster fcgi server" "$DAEMON_NAME"
68         if [ ! -f $MG_PASTE_INI ]; then
69             MG_PASTE_INI=$MG_ROOT/paste.ini
70             fi
71         if [ -z "$(getPID)" ]; then
72             su -s /bin/sh -c "CELERY_ALWAYS_EAGER=False $MG_PASTER_BIN serve \
73                 $MG_PASTE_INI \
74                 --server-name=fcgi \
75                 fcgi_host=$MG_FCGI_HOST fcgi_port=$MG_FCGI_PORT \
76                 --pid-file=$MG_PASTER_PID_FILE \
77                 --log-file=$MG_PASTER_LOG_FILE \
78                 --daemon" - $MG_USER 2>&1 > /dev/null
79
80             PASTER_RESULT=$?
81
82             # Sleep for a while until we're kind of certain that paster has
83             # had it's time to initialize
84             TRIES=0
85             while ! [ "X$PASTER_RESULT" != "X" ]; do
86                 log_action_msg "Tried $TRIES time(s)"
87                 sleep 0.1
88                 TRIES=$((TRIES+1))
89             done
90
91             log_end_msg $PASTER_RESULT
92         else
93             # Failed because the PID file indicates it's running
94             log_action_msg "PID file $MG_PASTER_BIN already exists"
95             log_end_msg 1
96         fi
97         ;;
98     stop)
99         log_daemon_msg "Stopping GNU MediaGoblin paster fcgi server" "$DAEMON_NAME"
100         if [ -z "$(getPID)" ]; then
101             # Failed because the PID file indicates it's not running
102             RET=1
103         else
104             kill $(getPID)
105
106             if [ $? -gt 0 ]; then
107                 RET=1
108             else
109                 RET=0
110             fi
111         fi
112         log_end_msg $RET
113         ;;
114     restart)
115         $0 stop
116         $0 start
117         ;;
118     status)
119         if ! [ -z "$(getPID)" ]; then
120             echo "$DAEMON_NAME start/running, process $(getPID)"
121         else
122             echo "$DAEMON_NAME stopped."
123         fi
124         ;;
125     *)
126         echo "Usage: $0 {restart|start|stop|status}"
127         exit 1
128         ;;
129 esac
130
131 exit 0