]> info9.net Git - wiki.git/blob - tmarble/posts/Setting_Up_MediaGoblin/etc/init.d/mediagoblin-paster
Copy over old blog
[wiki.git] / tmarble / posts / Setting_Up_MediaGoblin / etc / init.d / mediagoblin-paster
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=/srv/mediagoblin
31 ################################################################################
32 # NOW STOP
33 # You probably won't have to change anything else.
34 ################################################################################
35
36 set -e
37
38 DAEMON_NAME=mediagoblin-paster
39
40 MG_BIN=$MG_ROOT/bin
41 MG_PASTER_BIN=$MG_BIN/paster
42 MG_PASTE_INI=$MG_ROOT/paste_local.ini
43 MG_USER=www-data
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 [ -z "$(getPID)" ]; then
69             su -s /bin/sh -c "CELERY_ALWAYS_EAGER=False $MG_PASTER_BIN serve \
70                 $MG_PASTE_INI \
71                 --server-name=fcgi \
72                 fcgi_host=$MG_FCGI_HOST fcgi_port=$MG_FCGI_PORT \
73                 --pid-file=$MG_PASTER_PID_FILE \
74                 --log-file=$MG_PASTER_LOG_FILE \
75                 --daemon" - $MG_USER 2>&1 > /dev/null
76
77             PASTER_RESULT=$?
78
79             # Sleep for a while until we're kind of certain that paster has
80             # had it's time to initialize
81             TRIES=0
82             while ! [ "X$PASTER_RESULT" != "X" ]; do
83                 log_action_msg "Tried $TRIES time(s)"
84                 sleep 0.1
85                 TRIES=$((TRIES+1))
86             done
87
88             log_end_msg $PASTER_RESULT
89         else
90             # Failed because the PID file indicates it's running
91             log_action_msg "PID file $MG_PASTER_BIN already exists"
92             log_end_msg 1
93         fi
94         ;;
95     stop)
96         log_daemon_msg "Stopping GNU MediaGoblin paster fcgi server" "$DAEMON_NAME"
97         if [ -z "$(getPID)" ]; then
98             # Failed because the PID file indicates it's not running
99             RET=1
100         else
101             kill $(getPID)
102
103             if [ $? -gt 0 ]; then
104                 RET=1
105             else
106                 RET=0
107             fi
108         fi
109         log_end_msg $RET
110         ;;
111     restart)
112         $0 stop
113         sleep 3
114         $0 start
115         ;;
116     status)
117         if ! [ -z "$(getPID)" ]; then
118             echo "$DAEMON_NAME start/running, process $(getPID)"
119         else
120             echo "$DAEMON_NAME stopped."
121         fi
122         ;;
123     *)
124         echo "Usage: $0 {restart|start|stop|status}"
125         exit 1
126         ;;
127 esac
128
129 exit 0