You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.6 KiB

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