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_conf_dir=${TOPDIR}/conf
9seafile_server_symlink=${TOPDIR}/seafile-server-latest
10seahub_data_dir=${TOPDIR}/seahub-data
11seahub_settings_py=${TOPDIR}/seahub_settings.py
12
13manage_py=${INSTALLPATH}/seahub/manage.py
14
15export CCNET_CONF_DIR=${default_ccnet_conf_dir}
16export SEAFILE_CENTRAL_CONF_DIR=${default_conf_dir}
17export 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
18export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seafile/lib64/python2.7/site-packages:$PYTHONPATH
19export SEAFILE_LD_LIBRARY_PATH=${INSTALLPATH}/seafile/lib/:${INSTALLPATH}/seafile/lib64:${LD_LIBRARY_PATH}
20
21prev_version=6.0
22current_version=6.1
23
24echo
25echo "-------------------------------------------------------------"
26echo "This script would upgrade your seafile server from ${prev_version} to ${current_version}"
27echo "Press [ENTER] to contiune"
28echo "-------------------------------------------------------------"
29echo
30read dummy
31
32function check_python_executable() {
33    if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
34        return 0
35    fi
36
37    if which python2.7 2>/dev/null 1>&2; then
38        PYTHON=python2.7
39    elif which python27 2>/dev/null 1>&2; then
40        PYTHON=python27
41    else
42        echo
43        echo "Can't find a python executable of version 2.7 or above in PATH"
44        echo "Install python 2.7+ before continue."
45        echo "Or if you installed it in a non-standard PATH, set the PYTHON enviroment varirable to it"
46        echo
47        exit 1
48    fi
49}
50
51function read_seafile_data_dir () {
52    seafile_ini=${default_ccnet_conf_dir}/seafile.ini
53    if [[ ! -f ${seafile_ini} ]]; then
54        echo "${seafile_ini} not found. Now quit"
55        exit 1
56    fi
57    seafile_data_dir=$(cat "${seafile_ini}")
58    if [[ ! -d ${seafile_data_dir} ]]; then
59        echo "Your seafile server data directory \"${seafile_data_dir}\" is invalid or doesn't exits."
60        echo "Please check it first, or create this directory yourself."
61        echo ""
62        exit 1;
63    fi
64
65    export SEAFILE_CONF_DIR=$seafile_data_dir
66}
67
68function ensure_server_not_running() {
69    # test whether seafile server has been stopped.
70    if 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
76    elif pgrep -f "${manage_py} run_gunicorn" 2>/dev/null 1>&2 \
77         || pgrep -f "seahub.wsgi:application" 2>/dev/null 1>&2; then
78        echo
79        echo "seahub server is still running !"
80        echo "stop it before upgrade."
81        echo
82        exit 1
83    elif pgrep -f "${manage_py} runfcgi" 2>/dev/null 1>&2 ; then
84        echo
85        echo "seahub server is still running !"
86        echo "stop it before upgrade."
87        echo
88        exit 1
89    fi
90}
91
92function migrate_avatars() {
93    echo
94    echo "migrating avatars ..."
95    echo
96    media_dir=${INSTALLPATH}/seahub/media
97    orig_avatar_dir=${INSTALLPATH}/seahub/media/avatars
98    dest_avatar_dir=${TOPDIR}/seahub-data/avatars
99
100    # move "media/avatars" directory outside
101    if [[ ! -d ${dest_avatar_dir} ]]; then
102        mkdir -p "${TOPDIR}/seahub-data"
103        mv "${orig_avatar_dir}" "${dest_avatar_dir}" 2>/dev/null 1>&2
104        ln -s ../../../seahub-data/avatars "${media_dir}"
105
106    elif [[ ! -L ${orig_avatar_dir} ]]; then
107        mv "${orig_avatar_dir}"/* "${dest_avatar_dir}" 2>/dev/null 1>&2
108        rm -rf "${orig_avatar_dir}"
109        ln -s ../../../seahub-data/avatars "${media_dir}"
110    fi
111    echo "Done"
112}
113
114function update_database() {
115    echo
116    echo "Updating seafile/seahub database ..."
117    echo
118
119    db_update_helper=${UPGRADE_DIR}/db_update_helper.py
120    if ! $PYTHON "${db_update_helper}" 6.1.0; then
121        echo
122        echo "Failed to upgrade your database"
123        echo
124        exit 1
125    fi
126    echo "Done"
127}
128
129function upgrade_seafile_server_latest_symlink() {
130    # update the symlink seafile-server to the new server version
131    if [[ -L "${seafile_server_symlink}" || ! -e "${seafile_server_symlink}" ]]; then
132        echo
133        printf "updating \033[33m${seafile_server_symlink}\033[m symbolic link to \033[33m${INSTALLPATH}\033[m ...\n\n"
134        echo
135        if ! rm -f "${seafile_server_symlink}"; then
136            echo "Failed to remove ${seafile_server_symlink}"
137            echo
138            exit 1;
139        fi
140
141        if ! ln -s "$(basename ${INSTALLPATH})" "${seafile_server_symlink}"; then
142            echo "Failed to update ${seafile_server_symlink} symbolic link."
143            echo
144            exit 1;
145        fi
146    fi
147}
148
149function make_media_custom_symlink() {
150    media_symlink=${INSTALLPATH}/seahub/media/custom
151    if [[ -L "${media_symlink}" ]]; then
152        return
153
154    elif [[ ! -e "${media_symlink}" ]]; then
155        ln -s ../../../seahub-data/custom "${media_symlink}"
156        return
157
158
159    elif [[ -d "${media_symlink}" ]]; then
160        cp -rf "${media_symlink}" "${seahub_data_dir}/"
161        rm -rf "${media_symlink}"
162        ln -s ../../../seahub-data/custom "${media_symlink}"
163    fi
164
165}
166
167function move_old_customdir_outside() {
168    # find the path of the latest seafile server folder
169    if [[ -L ${seafile_server_symlink} ]]; then
170        latest_server=$(readlink -f "${seafile_server_symlink}")
171    else
172        return
173    fi
174
175    old_customdir=${latest_server}/seahub/media/custom
176
177    # old customdir is already a symlink, do nothing
178    if [[ -L "${old_customdir}" ]]; then
179        return
180    fi
181
182    # old customdir does not exist, do nothing
183    if [[ ! -e "${old_customdir}" ]]; then
184        return
185    fi
186
187    # media/custom exist and is not a symlink
188    cp -rf "${old_customdir}" "${seahub_data_dir}/"
189}
190
191#################
192# The main execution flow of the script
193################
194
195check_python_executable;
196read_seafile_data_dir;
197ensure_server_not_running;
198
199update_database;
200migrate_avatars;
201
202move_old_customdir_outside;
203make_media_custom_symlink;
204upgrade_seafile_server_latest_symlink;
205
206echo
207echo "-----------------------------------------------------------------"
208echo "Upgraded your seafile server successfully."
209echo "-----------------------------------------------------------------"
210echo
211