First Commit
[librecmc/package-feed.git] / net / seafile-server / files / seahub.init
1 #!/bin/bash /etc/rc.common
2
3 START=99
4 APP=seahub
5 EXTRA_HELP="    clearsessions   Clears expired sessions from database"
6 EXTRA_COMMANDS="clearsessions"
7
8 SEAHUB_FASTCGI=0
9 SEAHUB_PORT=8000
10 SEAHUB_METHOD=threaded
11 SEAHUB_WORKERS=3
12
13 [ -f /etc/config/seafile ] && \
14         . /etc/config/seafile
15
16 INSTALLPATH=/usr/share/seafile/seafile-server
17 TOPDIR=$(dirname "${INSTALLPATH}")
18 default_ccnet_conf_dir=${TOPDIR}/ccnet
19 central_config_dir=${TOPDIR}/conf
20
21 manage_py=${INSTALLPATH}/seahub/manage.py
22 gunicorn_conf=${INSTALLPATH}/runtime/seahub.conf
23 pidfile=/var/run/seafile/seahub.pid
24 errorlog=${INSTALLPATH}/runtime/error.log
25 accesslog=${INSTALLPATH}/runtime/access.log
26 gunicorn_exe=/usr/bin/gunicorn
27
28 function check_python_executable() {
29         if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
30                 return 0
31         fi
32
33         if which python2.7 2>/dev/null 1>&2; then
34                 PYTHON=python2.7
35         elif which python27 2>/dev/null 1>&2; then
36                 PYTHON=python27
37         else
38                 echo
39                 echo "Can't find a python executable of version 2.7 or above in PATH"
40                 echo "Install python 2.7+ before continue."
41                 echo "Or if you installed it in a non-standard PATH, set the PYTHON enviroment varirable to it"
42                 echo
43                 exit 1
44         fi
45 }
46
47 function validate_ccnet_conf_dir() {
48         if [[ ! -d ${default_ccnet_conf_dir} ]]; then
49                 echo "Error: there is no ccnet config directory."
50                 echo "Have you run '/etc/init.d/seafile setup'?"
51                 echo ""
52                 exit 1
53         fi
54 }
55
56 function read_seafile_data_dir() {
57         seafile_ini=${default_ccnet_conf_dir}/seafile.ini
58         if [[ ! -f ${seafile_ini} ]]; then
59                 echo "Error: ${seafile_ini} not found."
60                 exit 1
61         fi
62         seafile_data_dir=$(cat "${seafile_ini}")
63         if [[ ! -d ${seafile_data_dir} ]]; then
64                 echo "Your seafile server data directory \"${seafile_data_dir}\" is invalid or doesn't exits."
65                 echo "Please check it first, or create this directory yourself."
66                 echo ""
67                 exit 1
68         fi
69 }
70
71 function validate_seahub_running() {
72         if pid=$(pgrep -f "${manage_py}" 2>/dev/null); then
73                 return 1
74         elif pid=$(pgrep -f "seahub.wsgi:application" 2>/dev/null); then
75                 return 1
76         fi
77 }
78
79 function validate_port() {
80         if ! [[ ${SEAHUB_PORT} =~ ^[1-9][0-9]{1,4}$ ]] ; then
81                 printf "\033[033m${SEAHUB_PORT}\033[m is not a valid port number\n"
82                 exit 1
83         fi
84 }
85
86 function warning_if_seafile_not_running() {
87         if ! pgrep -f "seafile-controller -F ${central_config_dir}" 2>/dev/null 1>&2; then
88                 echo
89                 echo "Error: seafile-controller not running. Have you run \"/etc/init.d/seafile start\"?"
90                 echo
91                 exit 1
92         fi
93 }
94
95 function prepare_seahub_log_dir() {
96         logdir="${TOPDIR}/logs"
97         if ! [[ -d "${logsdir}" ]]; then
98                 if ! mkdir -p "${logdir}"; then
99                     echo "Error: failed to create log dir \"${logdir}\""
100                     exit 1
101                 fi
102         fi
103         export SEAHUB_LOG_DIR="${logdir}"
104 }
105
106 function before_start() {
107         prepare_env
108         warning_if_seafile_not_running
109         if ! validate_seahub_running; then
110                 echo "Seahub is already running."
111                 exit 1
112         fi
113         prepare_seahub_log_dir
114         validate_port
115 }
116
117 function start_seahub() {
118         before_start
119         echo "Starting seahub at port ${SEAHUB_PORT} ..."
120         check_init_admin
121         $PYTHON $gunicorn_exe seahub.wsgi:application -c "${gunicorn_conf}" -b "0.0.0.0:${SEAHUB_PORT}" --preload
122
123         # Ensure seahub is started successfully
124         retry=1
125         while ! validate_seahub_running && [[ ! -f "${pidfile}"  ]] && [[ $retry -lt 120 ]]; do sleep 1; ((retry++)); done
126         if ! validate_seahub_running && [[ -f "${pidfile}" ]]; then
127                 echo
128                 echo "Seahub is started"
129                 echo
130         else
131                 printf "\033[33mError: Seahub failed to start.\033[m\n"
132                 exit 1
133         fi
134 }
135
136 function start_seahub_fastcgi() {
137         before_start
138
139         # Returns 127.0.0.1 if SEAFILE_FASTCGI_HOST is unset or hasn't got any value,
140         # otherwise returns value of SEAFILE_FASTCGI_HOST environment variable
141         address=`(test -z "$SEAFILE_FASTCGI_HOST" && echo "127.0.0.1") || echo $SEAFILE_FASTCGI_HOST`
142
143         echo "Starting seahub (fastcgi) at ${address}:${SEAHUB_PORT} ..."
144         check_init_admin
145         $PYTHON "${manage_py}" runfcgi host=${address} port=${SEAHUB_PORT} pidfile=${pidfile} \
146                 outlog=${accesslog} errlog=${errorlog} maxchildren=${SEAHUB_WORKERS} method=${SEAHUB_METHOD}
147
148         # Ensure seahub is started successfully
149         retry=1
150         while ! validate_seahub_running && [[ ! -f "${pidfile}"  ]] && [[ $retry -lt 120 ]]; do sleep 1; ((retry++)); done
151         if ! validate_seahub_running && [[ -f "${pidfile}" ]]; then
152                 echo
153                 echo "Seahub is started"
154                 echo
155         else
156                 printf "\033[33mError: Seahub failed to start.\033[m\n"
157                 exit 1
158         fi
159 }
160
161 function prepare_env() {
162         check_python_executable
163         validate_ccnet_conf_dir
164         read_seafile_data_dir
165
166         export CCNET_CONF_DIR=${default_ccnet_conf_dir}
167         export SEAFILE_CONF_DIR=${seafile_data_dir}
168         export SEAFILE_CENTRAL_CONF_DIR=${central_config_dir}
169         export PYTHONPATH="${INSTALLPATH}/seahub:${INSTALLPATH}/seahub/thirdpart:${PYTHONPATH}"
170 }
171
172 function clear_sessions() {
173         prepare_env
174
175         echo "Start clear expired session records ..."
176         $PYTHON "${manage_py}" clearsessions
177
178         echo
179         echo "Done"
180         echo
181 }
182
183 function stop_seahub() {
184         if [[ -f ${pidfile} ]]; then
185                 pid=$(cat "${pidfile}")
186                 echo "Stopping seahub ..."
187                 kill ${pid}
188                 rm -f ${pidfile}
189                 retry=1
190                 while ! validate_seahub_running && [ $retry -lt 60 ]; do sleep 1; ((retry++)); done
191                 if ! validate_seahub_running; then
192                         echo "Error: seahub cannot be stopped. Please try stopping it manually by running \"kill $(echo "$pid" | tr '\n' ' ')\"."
193                         echo "To force killing the processes, use \"kill -9 $(echo "$pid" | tr '\n' ' ')\"."
194                 fi
195         else
196                 echo "Seahub is not running"
197         fi
198 }
199
200 function check_init_admin() {
201         check_init_admin_script=${INSTALLPATH}/check_init_admin.py
202         if ! $PYTHON $check_init_admin_script; then
203                 exit 1
204         fi
205 }
206
207 function start() {
208         if [ "$SEAHUB_FASTCGI" == "1" ]; then
209                 start_seahub_fastcgi
210         else
211                 start_seahub
212         fi
213 }
214
215 function stop() {
216         stop_seahub
217 }
218
219 function restart() {
220         stop
221         start
222 }
223
224 function clearsessions() {
225         clear_sessions
226 }