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
11manage_py=${INSTALLPATH}/seahub/manage.py
12
13export CCNET_CONF_DIR=${default_ccnet_conf_dir}
14export 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
15export PYTHONPATH=${INSTALLPATH}/seafile/lib/python2.7/site-packages:${INSTALLPATH}/seafile/lib64/python2.7/site-packages:$PYTHONPATH
16
17prev_version=1.8
18current_version=2.0
19
20echo
21echo "-------------------------------------------------------------"
22echo "This script would upgrade your seafile server from ${prev_version} to ${current_version}"
23echo "Press [ENTER] to contiune"
24echo "-------------------------------------------------------------"
25echo
26read dummy
27
28function check_python_executable() {
29    if [[ "$PYTHON" != "" && -x $PYTHON ]]; then
30        return 0
31    fi
32
33    if which python2.7 2>/dev/null 1>&2; then
34        PYTHON=python2.7
35    elif which python27 2>/dev/null 1>&2; then
36        PYTHON=python27
37    elif which python2.6 2>/dev/null 1>&2; then
38        PYTHON=python2.6
39    elif which python26 2>/dev/null 1>&2; then
40        PYTHON=python26
41    else
42        echo
43        echo "Can't find a python executable of version 2.6 or above in PATH"
44        echo "Install python 2.6+ 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
66check_python_executable
67read_seafile_data_dir
68
69export SEAFILE_CONF_DIR=$seafile_data_dir
70
71# test whether seafile server has been stopped.
72if pgrep seaf-server 2>/dev/null 1>&2 ; then
73    echo
74    echo "seafile server is still running !"
75    echo "stop it using scripts before upgrade."
76    echo
77    exit 1
78elif pgrep -f "${manage_py} run_gunicorn" 2>/dev/null 1>&2 ; then
79    echo
80    echo "seahub server is still running !"
81    echo "stop it before upgrade."
82    echo
83    exit 1
84elif pgrep -f "${manage_py} run_fcgi" 2>/dev/null 1>&2 ; then
85    echo
86    echo "seahub server is still running !"
87    echo "stop it before upgrade."
88    echo
89    exit 1
90fi
91
92echo
93echo "------------------------------"
94echo "migrating avatars ..."
95echo
96media_dir=${INSTALLPATH}/seahub/media
97orig_avatar_dir=${INSTALLPATH}/seahub/media/avatars
98dest_avatar_dir=${TOPDIR}/seahub-data/avatars
99
100# move "media/avatars" directory outside
101if [[ ! -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
106elif [[ ! -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}
110fi
111
112echo "DONE"
113echo "------------------------------"
114echo
115
116echo
117echo "------------------------------"
118echo "Updating seafile/seahub database ..."
119echo
120
121seahub_db=${TOPDIR}/seahub.db
122seahub_sql=${UPGRADE_DIR}/sql/2.0.0/sqlite3/seahub.sql
123if ! sqlite3 "${seahub_db}" < "${seahub_sql}"; then
124    echo "Failed to update seahub database"
125    exit 1
126fi
127
128add_collate_script=${UPGRADE_DIR}/add_collate.sh
129echo "fix seafile database case issues..."
130if ! ${add_collate_script} ${default_ccnet_conf_dir} ${seafile_data_dir} ${seahub_db}; then
131    echo "Failed."
132    exit 1
133fi
134
135echo "DONE"
136echo "------------------------------"
137echo
138