diff --git a/seafile-server/scripts/upgrade.py b/seafile-server/scripts/upgrade.py index d19cca0..35ef1df 100644 --- a/seafile-server/scripts/upgrade.py +++ b/seafile-server/scripts/upgrade.py @@ -11,10 +11,8 @@ import glob import logging import os from os.path import abspath, basename, exists, dirname, join, isdir, islink -import shutil import sys import time -import configparser from utils import ( call, get_install_dir, get_script, get_command_output, replace_file_pattern, @@ -43,9 +41,34 @@ def collect_upgrade_scripts(from_version, to_version): from_major_ver = '.'.join(from_version.split('.')[:2]) to_major_ver = '.'.join(to_version.split('.')[:2]) + def version_str_to_float(filename): + v = filename.split('_')[1] + try: + return float(v) + except: + return str(v) + + """ String '8.0' < '9.0' is True, but '9.0' < '10.0' is False in Python3. + So we need to convert the version string to float to compare. + + fn: script filepath, eg: /opt/seafile/seafile-server-latest/upgrade/upgrade_10.0_11.0.sh + va: from_major_version in script filename, eg: 10.0 + vb: to_major_version in script filename, eg: 11.0 + """ scripts = [] - for fn in sorted(glob.glob(join(installdir, 'upgrade', 'upgrade_*_*.sh'))): + for fn in sorted(glob.glob(join(installdir, 'upgrade', 'upgrade_*_*.sh')), key=version_str_to_float): va, vb = parse_upgrade_script_version(fn) + try: + va = float(va) + vb = float(vb) + from_major_ver = float(from_major_ver) + to_major_ver = float(to_major_ver) + except: + va = str(va) + vb = str(vb) + from_major_ver = str(from_major_ver) + to_major_ver = str(to_major_ver) + if va >= from_major_ver and vb <= to_major_ver: scripts.append(fn) return scripts @@ -65,108 +88,36 @@ def is_minor_upgrade(v1, v2): get_major_version = lambda x: x.split('.')[:2] return v1 != v2 and get_major_version(v1) == get_major_version(v2) -def fix_media_symlinks(current_version): +def fix_media_symlinks(): """ If the container was recreated and it's not a minor/major upgrade, we need to fix the media/avatars and media/custom symlink. """ - media_dir = join( - installdir, - 'seafile-server-{}/seahub/media'.format(current_version) - ) + media_dir = join(installdir, 'seahub/media') avatars_dir = join(media_dir, 'avatars') + custom_dir = join(media_dir, 'custom') + dst_media_dir = ('/shared/seafile/seahub-data') + dst_avatars_dir = join(dst_media_dir, 'avatars') + dst_custom_dir = join(dst_media_dir, 'custom') if not islink(avatars_dir): - logger.info('The container was recreated, running minor-upgrade.sh to fix the media symlinks') - run_minor_upgrade(current_version) + logger.info('The container was recreated, start fix the media symlinks') + call('mv -n %s/* %s' % (avatars_dir, dst_avatars_dir)) + call('rm -rf %s' % avatars_dir) + call('ln -sf %s %s' % (dst_avatars_dir, avatars_dir)) + call('rm -rf %s' % custom_dir) + call('ln -sf %s %s' % (dst_custom_dir, custom_dir)) + logger.info('Done') def run_minor_upgrade(current_version): minor_upgrade_script = join(installdir, 'upgrade', 'minor-upgrade.sh') run_script_and_update_version_stamp(minor_upgrade_script, current_version) -def fix_custom_dir(): - real_custom_dir = '/shared/seafile/seahub-data/custom' - if not exists(real_custom_dir): - os.mkdir(real_custom_dir) - -def fix_ccent_conf(): - ccnet_conf_path = '/shared/seafile/conf/ccnet.conf' - if exists(ccnet_conf_path): - cp = configparser.ConfigParser({}) - try: - cp.read(ccnet_conf_path) - except configparser.DuplicateSectionError as e: - with open(ccnet_conf_path, 'r+') as fp: - content_list = fp.readlines() - aim = '[Client]\n' - count = content_list.count(aim) - if count > 1: - new_content_list = list() - client_port_index = -1 - for index, text in enumerate(content_list): - if text == aim and 'PORT = ' in content_list[index + 1]: - client_port_index = index + 1 - continue - if index == client_port_index: - client_port_index = -1 - continue - new_content_list.append(text) - - new_content = ''.join(new_content_list) - fp.seek(0) - fp.truncate() - fp.write(new_content) - print('\n------------------------------') - print('Fix ccnet conf success') - print('------------------------------\n') - -def fix_seafevents_conf(): - seafevents_conf_path = '/shared/seafile/conf/seafevents.conf' - seahub_conf_path = '/shared/seafile/conf/seahub_settings.py' - pro_data_dir = '/shared/seafile/pro-data/' - if exists(seafevents_conf_path): - os.makedirs(pro_data_dir, exist_ok=True) - - with open(seafevents_conf_path, 'r') as fp: - fp_lines = fp.readlines() - if 'port = 6000\n' in fp_lines: - return - - if '[INDEX FILES]\n' in fp_lines and 'external_es_server = true\n' not in fp_lines: - insert_index = fp_lines.index('[INDEX FILES]\n') + 1 - insert_lines = ['es_port = 9200\n', 'es_host = elasticsearch\n', 'external_es_server = true\n'] - for line in insert_lines: - fp_lines.insert(insert_index, line) - - if '[OFFICE CONVERTER]\n' in fp_lines and 'port = 6000\n' not in fp_lines: - insert_index = fp_lines.index('[OFFICE CONVERTER]\n') + 1 - insert_lines = ['host = 127.0.0.1\n', 'port = 6000\n'] - for line in insert_lines: - fp_lines.insert(insert_index, line) - - with open(seafevents_conf_path, 'w') as fp: - fp.writelines(fp_lines) - - with open(seahub_conf_path, 'r') as fp: - fp_lines = fp.readlines() - if "OFFICE_CONVERTOR_ROOT = 'http://127.0.0.1:6000/'\n" not in fp_lines: - fp_lines.append("OFFICE_CONVERTOR_ROOT = 'http://127.0.0.1:6000/'\n") - - with open(seahub_conf_path, 'w') as fp: - fp.writelines(fp_lines) - print('\n------------------------------') - print('Fix seafevents conf success') - print('------------------------------\n') - def check_upgrade(): - fix_custom_dir() - fix_ccent_conf() - fix_seafevents_conf() - last_version = read_version_stamp() current_version = os.environ['SEAFILE_VERSION'] if last_version == current_version: - fix_media_symlinks(current_version) + fix_media_symlinks() return elif is_minor_upgrade(last_version, current_version): run_minor_upgrade(current_version) @@ -190,4 +141,4 @@ def main(): check_upgrade() if __name__ == '__main__': - main() + main() \ No newline at end of file