diff --git a/beat/web/utils/management/commands/change_databases_root_folder.py b/beat/web/utils/management/commands/change_databases_root_folder.py new file mode 100644 index 0000000000000000000000000000000000000000..0fbc6faef2910cc82662fa44a7c3aaa6708266bb --- /dev/null +++ b/beat/web/utils/management/commands/change_databases_root_folder.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python +# vim: set fileencoding=utf-8 : + +############################################################################### +# # +# Copyright (c) 2018 Idiap Research Institute, http://www.idiap.ch/ # +# Contact: beat.support@idiap.ch # +# # +# This file is part of the beat.web module of the BEAT platform. # +# # +# Commercial License Usage # +# Licensees holding valid commercial BEAT licenses may use this file in # +# accordance with the terms contained in a written agreement between you # +# and Idiap. For further information contact tto@idiap.ch # +# # +# Alternatively, this file may be used under the terms of the GNU Affero # +# Public License version 3 as published by the Free Software and appearing # +# in the file LICENSE.AGPL included in the packaging of this file. # +# The BEAT platform is distributed in the hope that it will be useful, but # +# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # +# or FITNESS FOR A PARTICULAR PURPOSE. # +# # +# You should have received a copy of the GNU Affero Public License along # +# with the BEAT platform. If not, see http://www.gnu.org/licenses/. # +# # +############################################################################### + + +""" +Examples: + + To change the root path of installed databases: + + $ manage.py change_databases_root_folder -v1 db_root_paths.json + + Note: The format is:: + + { + "atnt/1": "/remote/databases/atnt", + "banca/2": "file:///remote/databases/banca" + "foo/3": "nfs://myhost.db/databases/foo" + } + + It's also possible to do a dry-run to just determine what would be installed + using the option ``--dry-run``:: + + $ manage.py update_installed_databases -v1 --dry-run +""" + +import os +import sys +import logging + +from django.core.management.base import BaseCommand +from django.conf import settings + +from beat.web.databases.models import Database + +from .install import load_database_folders + + +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + + help = 'Change the root path of the databases listed in the given conf file' + + + def add_arguments(self, parser): + + from argparse import RawDescriptionHelpFormatter + parser.epilog = __doc__ + parser.formatter_class = RawDescriptionHelpFormatter + + + parser.add_argument('database_root_file', type=str, + help='The JSON file containing ' \ + 'the root directories of the databases installed ' \ + 'on the platform.') + + parser.add_argument('--dry-run', '-d', action='store_true', + dest='dry_run', default=False, help='Set this flag to ' \ + 'simulate a run.') + + + def handle(self, *ignored, **arguments): + # Setup this command's logging level + global logger + arguments['verbosity'] = int(arguments['verbosity']) + if arguments['verbosity'] >= 1: + if arguments['verbosity'] == 1: + logger.setLevel(logging.INFO) + elif arguments['verbosity'] >= 2: + logger.setLevel(logging.DEBUG) + + dry_run = arguments['dry_run'] + + # Reads database root file, if provided + db_root_file = arguments['database_root_file'] + db_root = load_database_folders(db_root_file) + + for db, path in db_root.items(): + name, version = db.split('/') + try: + database = Database.objects.get(name=name, version=int(version)) + except Database.DoesNotExist: + logger.error("Failed to find %s", db) + else: + if dry_run: + logger.info("Would change %s for %s" %(database.declaration['root_folder'], + path)) + else: + logger.info("Changing %s path for %s" %(db, path)) + declaration = database.declaration + declaration['root_folder'] = path + database.declaration = declaration + database.save()