NoPaste

Seahub Startscript

von Johnnii360

SNIPPET_TEXT:
  1. #!/bin/bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides:          seahub
  5. # Required-Start:    $local_fs $remote_fs $network
  6. # Required-Stop:     $local_fs
  7. # Default-Start:     1 2 3 4 5
  8. # Default-Stop:
  9. # Short-Description: Starts Seahub
  10. # Description:       starts Seahub
  11. ### END INIT INFO
  12.  
  13. echo ""
  14.  
  15. SCRIPT=$(readlink -f "$0")
  16. INSTALLPATH=$(dirname "${SCRIPT}")
  17. TOPDIR=$(dirname "${INSTALLPATH}")
  18. default_ccnet_conf_dir=${TOPDIR}/ccnet
  19. default_seafile_data_dir=${TOPDIR}/seafile-data
  20. central_config_dir=${TOPDIR}/conf
  21. seafile_rpc_pipe_path=${INSTALLPATH}/runtime
  22.  
  23. manage_py=${INSTALLPATH}/seahub/manage.py
  24. gunicorn_conf=${TOPDIR}/conf/gunicorn.conf.py
  25. pidfile=${TOPDIR}/pids/seahub.pid
  26. errorlog=${TOPDIR}/logs/gunicorn_error.log
  27. accesslog=${TOPDIR}/logs/gunicorn_access.log
  28. gunicorn_exe=${INSTALLPATH}/seahub/thirdpart/bin/gunicorn
  29.  
  30. script_name=$0
  31. function usage () {
  32.     echo "Usage: "
  33.     echo
  34.     echo "  $(basename ${script_name}) { start <port> | stop | restart <port> }"
  35.     echo
  36.     echo "To run seahub in fastcgi:"
  37.     echo
  38.     echo "  $(basename ${script_name}) { start-fastcgi <port> | stop | restart-fastcgi <port> }"
  39.     echo
  40.     echo "<port> is optional, and defaults to 8000"
  41.     echo ""
  42. }
  43.  
  44. # Check args
  45. if [[ $1 != "start" && $1 != "stop" && $1 != "restart" \
  46.     && $1 != "start-fastcgi" && $1 != "restart-fastcgi" && $1 != "clearsessions" && $1 != "python-env" ]]; then
  47.     usage;
  48.     exit 1;
  49. fi
  50.  
  51. function check_python_executable() {
  52.     if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
  53.         return 0
  54.     fi
  55.  
  56.     if which python3 2>/dev/null 1>&2; then
  57.         PYTHON=python3
  58.     elif !(python --version 2>&1 | grep "3\.[0-9]\.[0-9]") 2>/dev/null 1>&2; then
  59.         echo
  60.         echo "The current version of python is not 3.x.x, please use Python 3.x.x ."
  61.         echo
  62.         exit 1
  63.     else
  64.         PYTHON="python"$(python --version | cut -b 8-10)
  65.         if !which $PYTHON 2>/dev/null 1>&2; then
  66.             echo
  67.             echo "Can't find a python executable of $PYTHON in PATH"
  68.             echo "Install $PYTHON before continue."
  69.             echo "Or if you installed it in a non-standard PATH, set the PYTHON enviroment varirable to it"
  70.             echo
  71.             exit 1
  72.         fi
  73.     fi
  74. }
  75.  
  76. function validate_ccnet_conf_dir () {
  77.     if [[ ! -d ${default_ccnet_conf_dir} ]]; then
  78.         echo "Error: there is no ccnet config directory."
  79.         echo "Have you run setup-seafile.sh before this?"
  80.         echo ""
  81.         exit -1;
  82.     fi
  83. }
  84.  
  85. function validate_seafile_data_dir () {
  86.     if [[ ! -d ${default_seafile_data_dir} ]]; then
  87.         echo "Error: there is no seafile server data directory."
  88.         echo "Have you run setup-seafile.sh before this?"
  89.         echo ""
  90.         exit 1;
  91.     fi
  92. }
  93.  
  94. function validate_seahub_running () {
  95.     if pgrep -f "${manage_py}" 2>/dev/null 1>&2; then
  96.         echo "Seahub is already running."
  97.         exit 1;
  98.     elif pgrep -f "seahub.wsgi:application" 2>/dev/null 1>&2; then
  99.         echo "Seahub is already running."
  100.         exit 1;
  101.     fi
  102. }
  103.  
  104. function validate_port () {
  105.     if ! [[ ${port} =~ ^[1-9][0-9]{1,4}$ ]] ; then
  106.         printf "\033[033m${port}\033[m is not a valid port number\n\n"
  107.         usage;
  108.         exit 1
  109.     fi
  110. }
  111.  
  112. if [[ ($1 == "start" || $1 == "restart" || $1 == "start-fastcgi" || $1 == "restart-fastcgi") \
  113.     && ($# == 2 || $# == 1) ]]; then
  114.     if [[ $# == 2 ]]; then
  115.         port=$2
  116.         validate_port
  117.     else
  118.         port=8000
  119.     fi
  120. elif [[ $1 == "stop" && $# == 1 ]]; then
  121.     dummy=dummy
  122. elif [[ $1 == "clearsessions" && $# == 1 ]]; then
  123.     dummy=dummy
  124. elif [[ $1 == "python-env" ]]; then
  125.     dummy=dummy
  126. else
  127.     usage;
  128.     exit 1
  129. fi
  130.  
  131. function warning_if_seafile_not_running () {
  132.     if ! pgrep -f "seafile-controller -c ${default_ccnet_conf_dir}" 2>/dev/null 1>&2; then
  133.         echo
  134.         echo "Warning: seafile-controller not running. Have you run \"./seafile.sh start\" ?"
  135.         echo
  136.         exit 1
  137.     fi
  138. }
  139.  
  140. function prepare_seahub_log_dir() {
  141.     logdir=${TOPDIR}/logs
  142.     if ! [[ -d ${logsdir} ]]; then
  143.         if ! mkdir -p "${logdir}"; then
  144.             echo "ERROR: failed to create logs dir \"${logdir}\""
  145.             exit 1
  146.         fi
  147.     fi
  148.     export SEAHUB_LOG_DIR=${logdir}
  149. }
  150.  
  151. function before_start() {
  152.     prepare_env;
  153.     warning_if_seafile_not_running;
  154.     validate_seahub_running;
  155.     prepare_seahub_log_dir;
  156. }
  157.  
  158. function start_seahub () {
  159.     before_start;
  160.     echo "Starting seahub at port ${port} ..."
  161.     check_init_admin;
  162.     $PYTHON $gunicorn_exe seahub.wsgi:application -c "${gunicorn_conf}" --preload
  163.  
  164.     # Ensure seahub is started successfully
  165.     sleep 5
  166.     if ! pgrep -f "seahub.wsgi:application" 2>/dev/null 1>&2; then
  167.         printf "\033[33mError:Seahub failed to start.\033[m\n"
  168.         echo "Please try to run \"./seahub.sh start\" again"
  169.         exit 1;
  170.     fi
  171.     echo
  172.     echo "Seahub is started"
  173.     echo
  174. }
  175.  
  176. function start_seahub_fastcgi () {
  177.     before_start;
  178.  
  179.     # Returns 127.0.0.1 if SEAFILE_FASTCGI_HOST is unset or hasn't got any value,
  180.     # otherwise returns value of SEAFILE_FASTCGI_HOST environment variable
  181.     address=`(test -z "$SEAFILE_FASTCGI_HOST" && echo "127.0.0.1") || echo $SEAFILE_FASTCGI_HOST`
  182.  
  183.     echo "Starting seahub (fastcgi) at ${address}:${port} ..."
  184.     check_init_admin;
  185.     $PYTHON "${manage_py}" runfcgi maxchildren=8 host=$address port=$port pidfile=$pidfile \
  186.         outlog=${accesslog} errlog=${errorlog}
  187.  
  188.     # Ensure seahub is started successfully
  189.     sleep 5
  190.     if ! pgrep -f "${manage_py}" 1>/dev/null; then
  191.         printf "\033[33mError:Seahub failed to start.\033[m\n"
  192.         exit 1;
  193.     fi
  194.     echo
  195.     echo "Seahub is started"
  196.     echo
  197. }
  198.  
  199. function prepare_env() {
  200.     check_python_executable;
  201.     validate_ccnet_conf_dir;
  202.     validate_seafile_data_dir;
  203.  
  204.     if [[ -z "$LANG" ]]; then
  205.         echo "LANG is not set in ENV, set to en_US.UTF-8"
  206.         export LANG='en_US.UTF-8'
  207.     fi
  208.     if [[ -z "$LC_ALL" ]]; then
  209.         echo "LC_ALL is not set in ENV, set to en_US.UTF-8"
  210.         export LC_ALL='en_US.UTF-8'
  211.     fi
  212.  
  213.     export CCNET_CONF_DIR=${default_ccnet_conf_dir}
  214.     export SEAFILE_CONF_DIR=${default_seafile_data_dir}
  215.     export SEAFILE_CENTRAL_CONF_DIR=${central_config_dir}
  216.     export SEAFILE_RPC_PIPE_PATH=${seafile_rpc_pipe_path}
  217.     export PYTHONPATH=${INSTALLPATH}/seafile/lib/python3.6/site-packages:${INSTALLPATH}/seafile/lib64/python3.6/site-packages:${INSTALLPATH}/seahub:${INSTALLPATH}/seahub/thirdpart:$PYTHONPATH
  218.  
  219.  
  220. }
  221.  
  222. function clear_sessions () {
  223.     prepare_env;
  224.  
  225.     echo "Start clear expired session records ..."
  226.     $PYTHON "${manage_py}" clearsessions
  227.  
  228.     echo
  229.     echo "Done"
  230.     echo
  231. }
  232.  
  233. function stop_seahub () {
  234.     if [[ -f ${pidfile} ]]; then
  235.         echo "Stopping seahub ..."
  236.         pkill -9 -f "thirdpart/bin/gunicorn"
  237.         sleep 1
  238.         if pgrep -f "thirdpart/bin/gunicorn" 2>/dev/null 1>&2 ; then
  239.             echo 'Failed to stop seahub.'
  240.             exit 1
  241.         fi
  242.         rm -f ${pidfile}
  243.         return 0
  244.     else
  245.         echo "Seahub is not running"
  246.     fi
  247. }
  248.  
  249. function check_init_admin() {
  250.     check_init_admin_script=${INSTALLPATH}/check_init_admin.py
  251.     if ! $PYTHON $check_init_admin_script; then
  252.         exit 1
  253.     fi
  254. }
  255.  
  256. function run_python_env() {
  257.     local pyexec
  258.  
  259.     prepare_env;
  260.  
  261.     if which ipython 2>/dev/null; then
  262.         pyexec=ipython
  263.     else
  264.         pyexec=$PYTHON
  265.     fi
  266.  
  267.     if [[ $# -eq 0 ]]; then
  268.         $pyexec "$@"
  269.     else
  270.         "$@"
  271.     fi
  272. }
  273.  
  274. case $1 in
  275.     "start" )
  276.         start_seahub;
  277.         ;;
  278.     "start-fastcgi" )
  279.         start_seahub_fastcgi;
  280.         ;;
  281.     "stop" )
  282.         stop_seahub;
  283.         ;;
  284.     "restart" )
  285.         stop_seahub
  286.         sleep 2
  287.         start_seahub
  288.         ;;
  289.     "restart-fastcgi" )
  290.         stop_seahub
  291.         sleep 2
  292.         start_seahub_fastcgi
  293.         ;;
  294.     "python-env")
  295.         shift
  296.         run_python_env "$@"
  297.         ;;
  298.     "clearsessions" )
  299.         clear_sessions
  300.         ;;
  301. esac
  302.  
  303. echo "Done."
  304. echo ""

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN