aboutsummaryrefslogtreecommitdiff
path: root/net/seafile-server/files/seahub.init
blob: da3a1d1a7df78ceab2298bca344ac840f21f26b7 (plain)
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
#!/bin/bash /etc/rc.common

START=99
APP=seahub
EXTRA_HELP="	clearsessions	Clears expired sessions from database"
EXTRA_COMMANDS="clearsessions"

SEAHUB_FASTCGI=0
SEAHUB_PORT=8000
SEAHUB_METHOD=threaded
SEAHUB_WORKERS=3

[ -f /etc/config/seafile ] && \
	. /etc/config/seafile

INSTALLPATH=/usr/share/seafile/seafile-server
TOPDIR=$(dirname "${INSTALLPATH}")
default_ccnet_conf_dir=${TOPDIR}/ccnet
central_config_dir=${TOPDIR}/conf

manage_py=${INSTALLPATH}/seahub/manage.py
gunicorn_conf=${INSTALLPATH}/runtime/seahub.conf
pidfile=/var/run/seafile/seahub.pid
errorlog=${INSTALLPATH}/runtime/error.log
accesslog=${INSTALLPATH}/runtime/access.log
gunicorn_exe=/usr/bin/gunicorn

function check_python_executable() {
	if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
		return 0
	fi

	if which python2.7 2>/dev/null 1>&2; then
		PYTHON=python2.7
	elif which python27 2>/dev/null 1>&2; then
		PYTHON=python27
	else
		echo
		echo "Can't find a python executable of version 2.7 or above in PATH"
		echo "Install python 2.7+ before continue."
		echo "Or if you installed it in a non-standard PATH, set the PYTHON enviroment varirable to it"
		echo
		exit 1
	fi
}

function validate_ccnet_conf_dir() {
	if [[ ! -d ${default_ccnet_conf_dir} ]]; then
		echo "Error: there is no ccnet config directory."
		echo "Have you run '/etc/init.d/seafile setup'?"
		echo ""
		exit 1
	fi
}

function read_seafile_data_dir() {
	seafile_ini=${default_ccnet_conf_dir}/seafile.ini
	if [[ ! -f ${seafile_ini} ]]; then
		echo "Error: ${seafile_ini} not found."
		exit 1
	fi
	seafile_data_dir=$(cat "${seafile_ini}")
	if [[ ! -d ${seafile_data_dir} ]]; then
		echo "Your seafile server data directory \"${seafile_data_dir}\" is invalid or doesn't exits."
		echo "Please check it first, or create this directory yourself."
		echo ""
		exit 1
	fi
}

function validate_seahub_running() {
	if pid=$(pgrep -f "${manage_py}" 2>/dev/null); then
		return 1
	elif pid=$(pgrep -f "seahub.wsgi:application" 2>/dev/null); then
		return 1
	fi
}

function validate_port() {
	if ! [[ ${SEAHUB_PORT} =~ ^[1-9][0-9]{1,4}$ ]] ; then
		printf "\033[033m${SEAHUB_PORT}\033[m is not a valid port number\n"
		exit 1
	fi
}

function warning_if_seafile_not_running() {
	if ! pgrep -f "seafile-controller -F ${central_config_dir}" 2>/dev/null 1>&2; then
		echo
		echo "Error: seafile-controller not running. Have you run \"/etc/init.d/seafile start\"?"
		echo
		exit 1
	fi
}

function prepare_seahub_log_dir() {
	logdir="${TOPDIR}/logs"
	if ! [[ -d "${logsdir}" ]]; then
		if ! mkdir -p "${logdir}"; then
		    echo "Error: failed to create log dir \"${logdir}\""
		    exit 1
		fi
	fi
	export SEAHUB_LOG_DIR="${logdir}"
}

function before_start() {
	prepare_env
	warning_if_seafile_not_running
	if ! validate_seahub_running; then
		echo "Seahub is already running."
		exit 1
	fi
	prepare_seahub_log_dir
	validate_port
}

function start_seahub() {
	before_start
	echo "Starting seahub at port ${SEAHUB_PORT} ..."
	check_init_admin
	$PYTHON $gunicorn_exe seahub.wsgi:application -c "${gunicorn_conf}" -b "0.0.0.0:${SEAHUB_PORT}" --preload

	# Ensure seahub is started successfully
	retry=1
	while ! validate_seahub_running && [[ ! -f "${pidfile}"  ]] && [[ $retry -lt 120 ]]; do sleep 1; ((retry++)); done
	if ! validate_seahub_running && [[ -f "${pidfile}" ]]; then
		echo
		echo "Seahub is started"
		echo
	else
		printf "\033[33mError: Seahub failed to start.\033[m\n"
		exit 1
	fi
}

function start_seahub_fastcgi() {
	before_start

	# Returns 127.0.0.1 if SEAFILE_FASTCGI_HOST is unset or hasn't got any value,
	# otherwise returns value of SEAFILE_FASTCGI_HOST environment variable
	address=`(test -z "$SEAFILE_FASTCGI_HOST" && echo "127.0.0.1") || echo $SEAFILE_FASTCGI_HOST`

	echo "Starting seahub (fastcgi) at ${address}:${SEAHUB_PORT} ..."
	check_init_admin
	$PYTHON "${manage_py}" runfcgi host=${address} port=${SEAHUB_PORT} pidfile=${pidfile} \
		outlog=${accesslog} errlog=${errorlog} maxchildren=${SEAHUB_WORKERS} method=${SEAHUB_METHOD}

	# Ensure seahub is started successfully
	retry=1
	while ! validate_seahub_running && [[ ! -f "${pidfile}"  ]] && [[ $retry -lt 120 ]]; do sleep 1; ((retry++)); done
	if ! validate_seahub_running && [[ -f "${pidfile}" ]]; then
		echo
		echo "Seahub is started"
		echo
	else
		printf "\033[33mError: Seahub failed to start.\033[m\n"
		exit 1
	fi
}

function prepare_env() {
	check_python_executable
	validate_ccnet_conf_dir
	read_seafile_data_dir

	export CCNET_CONF_DIR=${default_ccnet_conf_dir}
	export SEAFILE_CONF_DIR=${seafile_data_dir}
	export SEAFILE_CENTRAL_CONF_DIR=${central_config_dir}
	export PYTHONPATH="${INSTALLPATH}/seahub:${INSTALLPATH}/seahub/thirdpart:${PYTHONPATH}"
}

function clear_sessions() {
	prepare_env

	echo "Start clear expired session records ..."
	$PYTHON "${manage_py}" clearsessions

	echo
	echo "Done"
	echo
}

function stop_seahub() {
	if [[ -f ${pidfile} ]]; then
		pid=$(cat "${pidfile}")
		echo "Stopping seahub ..."
		kill ${pid}
		rm -f ${pidfile}
		retry=1
		while ! validate_seahub_running && [ $retry -lt 60 ]; do sleep 1; ((retry++)); done
		if ! validate_seahub_running; then
			echo "Error: seahub cannot be stopped. Please try stopping it manually by running \"kill $(echo "$pid" | tr '\n' ' ')\"."
			echo "To force killing the processes, use \"kill -9 $(echo "$pid" | tr '\n' ' ')\"."
		fi
	else
		echo "Seahub is not running"
	fi
}

function check_init_admin() {
	check_init_admin_script=${INSTALLPATH}/check_init_admin.py
	if ! $PYTHON $check_init_admin_script; then
		exit 1
	fi
}

function start() {
	if [ "$SEAHUB_FASTCGI" == "1" ]; then
		start_seahub_fastcgi
	else
		start_seahub
	fi
}

function stop() {
	stop_seahub
}

function restart() {
	stop
	start
}

function clearsessions() {
	clear_sessions
}