1#!/usr/local/bin/bash
2
3SCRIPT=$(readlink -f "$0") # haiwen/seafile-server-1.3.0/upgrade/upgrade_xx_xx.sh
4UPGRADE_DIR=$(dirname "$SCRIPT") # haiwen/seafile-server-1.3.0/upgrade/
5INSTALLPATH=$(dirname "$UPGRADE_DIR") # haiwen/seafile-server-1.3.0/
6TOPDIR=$(dirname "${INSTALLPATH}") # haiwen/
7default_ccnet_conf_dir=${TOPDIR}/ccnet
8default_seafile_data_dir=${TOPDIR}/seafile-data
9default_seahub_db=${TOPDIR}/seahub.db
10
11export CCNET_CONF_DIR=${default_ccnet_conf_dir}
12export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.6/site-packages:${INSTALLPATH}/seafile/lib64/python2.6/site-packages:${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seahub/thirdpart:$PYTHONPATH
13export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seafile/lib64/python2.7/site-packages:$PYTHONPATH
14
15prev_version=1.2.0
16current_version=1.3.0
17
18echo
19echo "-------------------------------------------------------------"
20echo "This script would upgrade your seafile server from ${prev_version} to ${current_version}"
21echo "Press [ENTER] to contiune"
22echo "-------------------------------------------------------------"
23echo
24read dummy
25
26function check_python_executable() {
27    if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
28        return 0
29    fi
30
31    if which python2.7 2>/dev/null 1>&2; then
32        PYTHON=python2.7
33    elif which python27 2>/dev/null 1>&2; then
34        PYTHON=python27
35    elif which python2.6 2>/dev/null 1>&2; then
36        PYTHON=python2.6
37    elif which python26 2>/dev/null 1>&2; then
38        PYTHON=python26
39    else
40        echo
41        echo "Can't find a python executable of version 2.6 or above in PATH"
42        echo "Install python 2.6+ before continue."
43        echo "Or if you installed it in a non-standard PATH, set the PYTHON enviroment varirable to it"
44        echo
45        exit 1
46    fi
47}
48
49function read_seafile_data_dir () {
50    seafile_ini=${default_ccnet_conf_dir}/seafile.ini
51    if [[ ! -f ${seafile_ini} ]]; then
52        echo "${seafile_ini} not found. Now quit"
53        exit 1
54    fi
55    seafile_data_dir=$(cat "${seafile_ini}")
56    if [[ ! -d ${seafile_data_dir} ]]; then
57        echo "Your seafile server data directory \"${seafile_data_dir}\" is invalid or doesn't exits."
58        echo "Please check it first, or create this directory yourself."
59        echo ""
60        exit 1;
61    fi
62}
63
64check_python_executable
65read_seafile_data_dir
66
67export SEAFILE_CONF_DIR=$seafile_data_dir
68
69# test whether seafile server has been stopped.
70if pgrep seaf-server 2>/dev/null 1>&2 ; then
71    echo
72    echo "seafile server is still running !"
73    echo "stop it using scripts before upgrade."
74    echo
75    exit 1
76elif pgrep -f "manage.py run_gunicorn" 2>/dev/null 1>&2 ; then
77    echo
78    echo "seahub server is still running !"
79    echo "stop it before upgrade."
80    echo
81    exit 1
82fi
83
84# run django syncdb command
85echo "------------------------------"
86echo "updating seahub database ... "
87echo
88manage_py=${INSTALLPATH}/seahub/manage.py
89pushd "${INSTALLPATH}/seahub" 2>/dev/null 1>&2
90if ! $PYTHON manage.py syncdb 2>/dev/null 1>&2; then
91    echo "failed"
92    exit -1
93fi
94popd 2>/dev/null 1>&2
95
96echo "DONE"
97echo "------------------------------"
98echo
99
100echo "------------------------------"
101echo "migrating avatars ..."
102echo
103media_dir=${INSTALLPATH}/seahub/media
104orig_avatar_dir=${INSTALLPATH}/seahub/media/avatars
105dest_avatar_dir=${TOPDIR}/seahub-data/avatars
106
107# move "media/avatars" directory outside
108if [[ ! -d ${dest_avatar_dir} ]]; then
109    mkdir -p "${TOPDIR}/seahub-data"
110    mv "${orig_avatar_dir}" "${dest_avatar_dir}" 2>/dev/null 1>&2
111    ln -s ../../../seahub-data/avatars ${media_dir}
112
113elif [[ ! -L ${orig_avatar_dir} ]]; then
114    mv ${orig_avatar_dir}/* "${dest_avatar_dir}" 2>/dev/null 1>&2
115    rm -rf "${orig_avatar_dir}"
116    ln -s ../../../seahub-data/avatars ${media_dir}
117fi
118
119echo "DONE"
120echo "------------------------------"
121echo