diff --git a/nginx.conf b/nginx.conf index 46f3697..f0be735 100644 --- a/nginx.conf +++ b/nginx.conf @@ -2,76 +2,82 @@ events { worker_connections 12; } -pid /opt/seafile/pids/nginx.pid; +http{ -server { - listen 80; + upstream hub { + server 127.0.0.1:8000; + keepalive 2; + } - error_log syslog:server=unix:/dev/log; - access_log syslog:server=unix:/dev/log; + upstream files { + server 127.0.0.1:8082; + keepalive 4; + } - upstream hub { - server 127.0.0.1:8000; - keepalive 2; - } + upstream webdav { + server 127.0.0.1:8080; + keepalive 2; + } - upstream files { - server 127.0.0.1:8082; - keepalive 4; - } + disable_symlinks off; - upstream webdav { - server 127.0.0.1:8080; - keepalive 2; - } + server { + listen 80; + + error_log syslog:server=unix:/dev/log; + access_log syslog:server=unix:/dev/log; - proxy_set_header X-Forwarded-For $remote_addr; - location / { - proxy_pass http://hub; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host $server_name; - proxy_read_timeout 1200s; - # used for view/edit office file via Office Online Server - client_max_body_size 0; + proxy_set_header X-Forwarded-For $remote_addr; - } + location / { + proxy_pass http://hub; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $server_name; + proxy_read_timeout 1200s; - location /seafhttp { - rewrite ^/seafhttp(.*)$ $1 break; - proxy_http_version 1.1; - proxy_pass http://files; - client_max_body_size 0; - proxy_connect_timeout 3s; - proxy_read_timeout 3600s; - proxy_send_timeout 3600s; - send_timeout 3600s; - } + # used for view/edit office file via Office Online Server + client_max_body_size 0; - location /seafdav { - fastcgi_pass webdav; - fastcgi_keep_conn on; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_param PATH_INFO $fastcgi_script_name; + } - fastcgi_param SERVER_PROTOCOL $server_protocol; - fastcgi_param QUERY_STRING $query_string; - fastcgi_param REQUEST_METHOD $request_method; - fastcgi_param CONTENT_TYPE $content_type; - fastcgi_param CONTENT_LENGTH $content_length; - fastcgi_param SERVER_ADDR $server_addr; - fastcgi_param SERVER_PORT $server_port; - fastcgi_param SERVER_NAME $server_name; + location /seafhttp { + rewrite ^/seafhttp(.*)$ $1 break; + proxy_pass http://files; + proxy_http_version 1.1; + client_max_body_size 0; + proxy_connect_timeout 3s; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + send_timeout 3600s; + } - client_max_body_size 0; + location /seafdav { + fastcgi_pass webdav; + fastcgi_keep_conn on; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_script_name; - } + fastcgi_param SERVER_PROTOCOL $server_protocol; + fastcgi_param QUERY_STRING $query_string; + fastcgi_param REQUEST_METHOD $request_method; + fastcgi_param CONTENT_TYPE $content_type; + fastcgi_param CONTENT_LENGTH $content_length; + fastcgi_param SERVER_ADDR $server_addr; + fastcgi_param SERVER_PORT $server_port; + fastcgi_param SERVER_NAME $server_name; + + client_max_body_size 0; + + } + + # location /media { + # root /opt/seafile/seafile-server-latest/seahub; + # } + } - location /media { - root /opt/seafile/seafile-server-latest/seahub/media/ - } } diff --git a/scripts/seafile-server.sh b/scripts/seafile-server.sh index b81ad5e..bd6b3c6 100755 --- a/scripts/seafile-server.sh +++ b/scripts/seafile-server.sh @@ -1,79 +1,165 @@ -#!/bin/sh +#!/bin/bash LATEST=$SEAF/seafile-server-latest PIDS=$SEAF/pids +PIDFILES=("$PIDS" "$LATEST/runtime" "/var/run/nginx") +COMMANDS=("seaf-server $LATEST/seafile.sh start" "seahub $LATEST/seahub.sh ${SEAF_HUB_MODE:-start} 8000" "nginx /usr/sbin/nginx") -function stop() { +function stop { + #Kill when hitting CTRL+C repeatedly + trap stop_kill SIGTERM $0 stop } +function stop_kill { + $0 kill +} +function ext_url { + echo "${SEAF_URL:-http://$(hostname)}/seafhttp" +} +function patch_seahub_conf { + KEY="FILE_SERVER_ROOT" + ENTRY="$KEY = '$(ext_url)'" + CONF="$SEAF/conf/seahub_settings.py" + grep "$KEY" -i "$CONF" + if [ $? -eq 1 ]; then + echo "Patched seahub_settings.py: set $ENTRY" + echo -e "\n$ENTRY" >> "$CONF" + fi +} +function last_update { + if [ -e "$LATEST/install_date" ]; then + date -d $(cat "$LATEST/install_date") "+%s" + else + echo 0 + fi +} + +function pid { + for LOC in ${PIDFILES[@]}; do + if [ -e "$LOC/$1.pid" ]; then + cat "$LOC/$1.pid" + return 0 + fi + done + pgrep -f "/$1" +} case "$1" in run) if [ ! -e "$LATEST" ]; then (>&2 echo This appears to be the fist run, installing..) + mkdir -p $SEAF/conf + cp /etc/seafile/* $SEAF/conf/* -rf $0 run-upgrade + exit else - $0 manage-component seaf-server start $LATEST/seafile.sh start - $0 manage-component seahub start $LATEST/seahub.sh "${SEAF_HUB_MODE:-start} 8000" & - $0 manage-component nginx start nginx & + patch_seahub_conf + for CMD in "${COMMANDS[@]}"; do + INFO=($CMD) + $0 manage-component "${INFO[0]}" start "${INFO[1]}" "${INFO[@]:2}" + done wait fi - trap stop SIGINT SIGTERM - while pgrep -f "seafile-controller" 2>&1 >/dev/null; do - sleep 2; + trap stop SIGINT SIGQUIT SIGTERM + LAST_UPGRADE=$(last_update) + UPGRADE=0 + while eval "$0 status > /dev/null"; do + sleep 10; + SINCE_UPGRADE=$(((($(date "+%s") - $LAST_UPGRADE))/(60*60*24))) + if [ $LAST_UPGRADE -gt 0 ] && [ "0$SEAF_UPGRADE_INTERVAL" -gt 0 ] && [ $SINCE_UPGRADE -gt "0$SEAF_UPGRADE_INTERVAL" ]; then + echo "It's been $SINCE_UPGRADE days since the last Upgrade\nSTOPPING TO PERFORM UPGRADE NOW\nSet SEAF_UPGRADE_INTERVAL=0 disable automatic updates" + UPGRADE=1 + $0 stop + fi done + if [ $UPGRADE -eq 1 ]; then + $0 stop-upgrade-run + else + echo "Terminated" + fi ;; run-upgrade) - $0 upgrade && $0 run + $0 upgrade + RES=$? + if [ $RES -eq 127 ]; then + #No upgrade + true + fi + if [ $RES -eq 126 ] && [ ! -e "$LATEST/install_date" ]; then + date +%Y-%m-%d > "$LATEST/install_date" + fi + $0 run ;; stop) - $0 manage-component seaf-server stop & - $0 manage-component seahub stop & - $0 manage-component nginx stop & - $0 manage-component ccnet stop & - wait + PIDS="" + for CMD in seafile-controller ccnet seafdav "${COMMANDS[@]}" seafile-controller ccnet; do + $0 manage-component $(echo $CMD | cut -d' ' -f1) stop & + PIDS="$PIDS $!" + done + wait $PIDS ;; - stop-upgrade) - $0 stop && $0 upgrade + kill) + export SEAF_STOP_TRIES=0 + $0 stop + ;; + stop-upgrade-run) + $0 stop && $0 upgrade && $0 run ;; upgrade) /usr/local/sbin/upgrade main kill -SIGHUP $(pgrep -o nginx) #Reload nginx ;; + status) + RUNNING=1 + for CMD in seafile-controller ccnet "${COMMANDS[@]}"; do + SERVICE="$(echo $CMD | cut -d' ' -f1)" + echo -n "$SERVICE: " + $0 manage-component $SERVICE status + [ $? -eq 0 ] || [ $RUNNING -eq 0 ] + RUNNING=$? + done + [ $RUNNING -eq 0 ] + ;; manage-component) mkdir -p /var/run/seafile NAME="$2" - PIDFILE=/var/run/seafile/$NAME.pid + PID=$(pid $NAME) case "$3" in start) - CMD="$4"; ARGS="$5" - "$CMD $ARGS &" + CMD="$4" + ARGS="$5" + eval "$CMD $ARGS" sleep 1 - ( ! "$0 $1 $NAME status") && ((>&2 echo Failed to start $NAME); exit 1) - PID=$? - #echo $PID > /var/run/seafile/$NAME.pid + ( ! eval "$0 $1 $NAME status" ) && ((>&2 echo Failed to start $NAME); exit 1) ;; status) - ([ -e "$PIDFILE" ] && kill -0 $(cat $PIDFILE) 2> /dev/null && echo Running) || (echo Stopped && exit 1) + (kill -0 $PID 2> /dev/null && echo Running) || (echo Stopped && exit 1) ;; stop) - if "$0 $1 $NAME status"; then - kill $(cat $PIDFILE) - TRIES=30 - while "$0 $1 $NAME status"; do + QUERY="$0 $1 $NAME status > /dev/null" + if eval "$QUERY"; then + kill $PID + TRIES=${SEAF_STOP_TRIES:-30} + while eval "$QUERY"; do TRIES=$((TRIES - 1)) - if [ $TRIES -eq 0 ]; then - (>&2 echo Failed to stop $NAME) + if [ $TRIES -lt 1 ]; then + (>&2 echo Failed to stop gracefully $NAME) + kill -9 $PID exit 1 + else + kill $PID + [ $? -eq 1 ] && break fi sleep 1 done (>&2 echo $NAME has been stopped) else (>&2 echo $NAME is not running) - exit 1 fi ;; + *) + echo "You're doing wrong pal" + ;; esac ;; *) diff --git a/scripts/upgrade.py b/scripts/upgrade.py index d47a23d..a621dbc 100755 --- a/scripts/upgrade.py +++ b/scripts/upgrade.py @@ -1,6 +1,6 @@ #! /usr/bin/env python -import requests, platform, re, collections, tempfile, os, sys, shutil +import requests, platform, re, collections, tempfile, os, sys, shutil, math from subprocess import call version_re = "seafile-server-?_?([\d\.]*)" @@ -19,6 +19,16 @@ def available(page): available[m.group(2)] = m.group(0) #{version: link} return collections.OrderedDict(sorted(available.items(), reverse=True)) +def version_int(version): + parts=version.split('.') + rank=len(parts) + from math import pow + no=0 + for p in parts: + rank=rank-1 + no=no+int(p)*pow(10,rank) + return no + def list_command(page="https://www.seafile.com/en/download/"): for (version, link) in available(page).items(): print("- %s: %s" % (version, link)) @@ -30,7 +40,7 @@ def current_version_command(): def current_version(): current = os.path.realpath(os.path.join(seaf_home, "seafile-server-latest")) res = re.findall(version_re,current) - res[0] = "0" + res[0] + if len(res[0]) == 0: return '0' return '.'.join(res) def perform_upgrade_command(version,mysql=False,yes=True): @@ -39,6 +49,7 @@ def perform_upgrade_command(version,mysql=False,yes=True): target = map(lambda x: int(x), version.split(".")) print len(current) == 1 and current[0] == 0 if len(current) == 1 and current[0] == 0: #Not installed + print("Running installer, please don't change any default values regarding ports.") installer = os.path.join(seaf_home,"seafile-server-%s" % version,"setup-seafile%s.sh" % ("-mysql" if mysql else "")) call(["sh","-c",installer],stdout=sys.stdout, stdin=sys.stdin) return 0 @@ -71,7 +82,7 @@ def install_command(url): if len(current) == 1: current = current[0] else: current = "0.0.0" print("Upgrading from: %s -> %s" % (current, target_version)) - perform_upgrade_command(target_version) + perform_upgrade_command(target_version,mysql=(os.environ["SEAF_MYSQL"] or False)) if len(os.listdir(tmp)) != 0: print("Failed to unpack update") os.rmdir(tmp) @@ -79,14 +90,18 @@ def install_command(url): os.rmdir(tmp) return 0 +#TODO: figure out how to rename main to "" def main_command(version="latest",page="https://www.seafile.com/en/download/"): avail = available(page) install = None if version == "latest": - install = avail.items()[0] + (install,version) = avail.items() else: if version in dict(avail.items()): install = dict(avail.items())[version] + if version_int(current_version()) == version_int(version): + print("%s(%s) is already installed" % (version,version_int(version))) + return 1 if install: print("Installing: %s" % install[1]) return install_command(install[1])