#!/bin/sh SEAFILE_DIR_LATEST="${SEAFILE_DIR}/seafile-server-latest" version_lte() { # https://stackoverflow.com/a/4024263 [ "$1" = "`printf "$1\n$2" | sort -V | head -n1`" ] } version_lt() { # https://stackoverflow.com/a/4024263 [ "$1" = "$2" ] && return 1 || version_lte "$1" "$2" } path_version() { version="`readlink -m "$1"`" version="${version%/}" echo "${version##*-}" } download_list() { curl -sL https://download.seadrive.org/ | grep -o 'seafile-server_[^_]*_[^_]*\.tar\.gz' | sort -rV } download_select() { case "$1" in http*) echo "$1" ;; seafile-server_*.tar.gz) echo "https://download.seadrive.org/$1" ;; 6.*|7.*|8.*|9.*) echo "https://download.seadrive.org/seafile-server_$1_x86-64.tar.gz" ;; latest) selection="`download_list | head -1`" echo "https://download.seadrive.org/${selection}" ;; *) options=`download_list | awk '{print $1, "on"}'` selection="`dialog --stdout --no-items --ok-label Fetch --radiolist "Choose version:" 0 0 0 ${options}`" if [ ! "$selection" = "" ]; then echo "https://download.seadrive.org/${selection}" fi ;; esac } build_download() { url="`download_select "$@"`" if [ ! "$url" = "" ]; then echo 1>&2 "Fetching <${url}>..." curl -sL "${url}" > "${SEAFILE_DIR}/pkg.tar.gz" name="`tar -tvf "${SEAFILE_DIR}/pkg.tar.gz" | head -1 | awk '{print $6}'`" echo 1>&2 "Unpacking \`${name%/}'..." tar -xzf "${SEAFILE_DIR}/pkg.tar.gz" -C "${SEAFILE_DIR}" rm "${SEAFILE_DIR}/pkg.tar.gz" echo "${name%/}" fi } build_setup() { if [ -e "$1" ]; then version_new="`path_version "$1"`" echo 1>&2 "\033[32m Info: \033[0m Requested: ${version_new}" version_old= if [ -L "${SEAFILE_DIR_LATEST}" ]; then version_old="`path_version "${SEAFILE_DIR_LATEST}"`" echo 1>&2 "\033[32m Info: \033[0m Installed: ${version_old}" control_server stop fi if [ ! -e "${SEAFILE_DIR}/seafile-data" ]; then ( cd "${SEAFILE_DIR}/seafile-server-${version_new}" echo 1>&2 "==> ./setup-seafile.sh" server_name=${SEAFILE_SERVER_NAME} ip_or_domain=${SEAFILE_SERVER_HOST} ./setup-seafile.sh auto sed -i 's/127\.0\.0\.1/0.0.0.0/' "${SEAFILE_DIR}/conf/gunicorn.conf.py" ) fi if [ ! "${version_old}" = "" ] && [ ! "${version_old}" = "${version_new}" ]; then if version_lt "${version_old}" "${version_new}"; then echo 1>&2 "\033[32m Info: \033[0m Upgrade needed ${version_old} => ${version_new}." oldver1=`echo "$version_old" | cut -d. -f1` oldver2=`echo "$version_old" | cut -d. -f2` nxtver1=${oldver1} nxtver2=`expr ${oldver2} + 1` newver1=`echo "$version_new" | cut -d. -f1` newver2=`echo "$version_new" | cut -d. -f2` while [ $nxtver1 -le $newver1 ]; do script="${SEAFILE_DIR}/seafile-server-${version_new}/upgrade/upgrade_${oldver1}.${oldver2}_${nxtver1}.${nxtver2}.sh" if [ -f $script ]; then echo 1>&2 "==> ${script}" "${script}" if [ "$nxtver1" = "6" -a "$nxtver2" = "3" ]; then ( cd "${SEAFILE_DIR}/seafile-server-${version_new}/seahub" ./manage.py migrate_file_comment ) fi oldver2=${nxtver2} nxtver2=`expr ${nxtver2} + 1` else oldver1=${nxtver1} nxtver1=`expr ${nxtver1} + 1` nxtver2=0 fi done "${SEAFILE_DIR}/seafile-server-${version_new}/upgrade/minor-upgrade.sh" sed -i 's/127\.0\.0\.1/0.0.0.0/' "${SEAFILE_DIR}/conf/gunicorn.conf.py" else echo 1>&2 "\033[33m Warning: \033[0m Manual intervention required to downgrade ${version_old} => ${version_new}." fi fi else echo 1>&2 "\033[31m Error: \033[0m \`$1' not available." fi } control_server() { "${SEAFILE_DIR_LATEST}/seafile.sh" "$@" || return 1 "${SEAFILE_DIR_LATEST}/seahub.sh" "$@" || return 1 } collect_logs() { for fn in "${SEAFILE_DIR}/logs"/*; do if [ -s "${fn}" ]; then echo "==> ${fn} <==" >> "${SEAFILE_DIR}/logs/.history" cat "${fn}" >> "${SEAFILE_DIR}/logs/.history" echo >> "${SEAFILE_DIR}/logs/.history" truncate -s0 "${fn}" fi done } tail_logs() { tail -f "${SEAFILE_DIR}/logs"/* } wait_forever() { while :; do if [ -e "${SEAFILE_DIR}/.stop" ]; then echo 1>&2 "\033[32m Info: \033[0m Received shutdown signal." rm "${SEAFILE_DIR}/.stop" exit 0 else sleep 4 fi done } do_init() { if [ -f "${SEAFILE_DIR}/seafile-data/version" ]; then data_version=`cat "${SEAFILE_DIR}/seafile-data/version"` if [ ! "${SEAFILE_VERSION}" = "${data_version}" ]; then echo 1>&2 "\033[31m Error: \033[0m Seafile server version mismatch." echo 1>&2 "Server: ${SEAFILE_VERSION}" echo 1>&2 "Data: ${data_version}" if version_lt "${data_version}" "${SEAFILE_VERSION}"; then echo 1>&2 "\033[32m Info: \033[0m Consider running upgrade scripts per " fi echo 1>&2 "\033[32m Info: \033[0m Remove \`${SEAFILE_DIR}/seafile-data/version' after reconciling versions." return 1 fi fi if [ -e "${SEAFILE_DIR_LATEST}" ]; then collect_logs if control_server start; then echo -n "${SEAFILE_VERSION}" > "${SEAFILE_DIR}/seafile-data/version" sleep 2 else echo 1>&2 "\033[31m Error: \033[0m Seafile server not started." fi tail_logs & else echo 1>&2 "\033[31m Error: \033[0m Seafile server not installed. Run \`$0 setup' to install." fi wait_forever } CMD="$1" [ "$#" -gt 0 ] && shift case "${CMD}" in setup) version="`build_download "$@"`" build_setup "${SEAFILE_DIR}/${version}" echo 1>&2 "\033[32m Info: \033[0m Run \`$0 start' to start seafile and seahub." ;; createsuperuser) "${SEAFILE_DIR_LATEST}/seahub/manage.py" createsuperuser ;; start) control_server start "$@" ;; restart) control_server restart "$@" ;; stop) control_server stop ;; shutdown) touch "${SEAFILE_DIR}/.stop" echo 1>&2 "\033[32m Info: \033[0m Sent shutdown signal." ;; init) do_init ;; *) do_init ;; esac