From 7d1b216bfa4c2af13e1d1d220ede1f129e2550b6 Mon Sep 17 00:00:00 2001 From: Howard Johnson Date: Oct 26 2016 22:10:39 +0000 Subject: Add cli args to ipsilon-upgrade-database to make it nicer to use Add an argparser to it taking similar arguments to -server-install, and rework find_config a bit. Also, change the error messages that tell the admin to run ipsilon- upgrade-database to include the correct command line arguments for the instance. Merges: #153 Signed-off-by: Howard Johnson Reviewed-by: Patrick Uiterwijk --- diff --git a/ipsilon/__init__.py b/ipsilon/__init__.py index 267e32b..1792847 100644 --- a/ipsilon/__init__.py +++ b/ipsilon/__init__.py @@ -1,17 +1,20 @@ # Copyright (C) 2015 Ipsilon project Contributors, for license see COPYING -import sys import os -def find_config(): +def find_config(instance, path): cfgfile = None - if (len(sys.argv) > 1): - cfgfile = sys.argv[-1] - elif os.path.isfile('ipsilon.conf'): + + if path is not None: + cfgfile = path + elif instance is None: cfgfile = 'ipsilon.conf' - elif os.path.isfile('/etc/ipsilon/ipsilon.conf'): - cfgfile = '/etc/ipsilon/ipsilon.conf' + elif instance != '': + cfgfile = '/etc/ipsilon/%s/ipsilon.conf' % instance else: + cfgfile = '/etc/ipsilon/ipsilon.conf' + + if not os.path.isfile(cfgfile): raise IOError("Configuration file not found") return cfgfile diff --git a/ipsilon/install/ipsilon-upgrade-database b/ipsilon/install/ipsilon-upgrade-database index 1b70146..90fad56 100755 --- a/ipsilon/install/ipsilon-upgrade-database +++ b/ipsilon/install/ipsilon-upgrade-database @@ -5,6 +5,7 @@ __requires__ = ['sqlalchemy >= 0.8'] import pkg_resources # pylint: disable=unused-import +from argparse import ArgumentParser import sys import logging logging.basicConfig(level=logging.INFO) @@ -16,8 +17,20 @@ logger = logging.getLogger(__name__) if __name__ == '__main__': + parser = ArgumentParser(description='Upgrade the ipsilon database') + group = parser.add_mutually_exclusive_group() + group.add_argument('cfgfile', metavar='CONFIGFILE', nargs='?', + help='Path to config file') + group.add_argument('--instance', default='idp', help='IdP instance name') + group.add_argument('--root-instance', default=False, action='store_true', + help='Web root mounted instance') + args = parser.parse_args() + + if args.root_instance: + args.instance = 'root' + try: - dbupgrade.execute_upgrade(find_config()) + dbupgrade.execute_upgrade(find_config(args.instance, args.cfgfile)) except Exception as ex: logger.error('Error upgrading database', exc_info=True) sys.exit(1) diff --git a/ipsilon/ipsilon b/ipsilon/ipsilon index 50e497d..ec0b27f 100755 --- a/ipsilon/ipsilon +++ b/ipsilon/ipsilon @@ -35,7 +35,7 @@ def nuke_session_locks(): except Exception: # pylint: disable=broad-except pass -cfgfile = find_config() +cfgfile = find_config(None, None) cherrypy.lib.sessions.SqlSession = ipsilon.util.sessions.SqlSession cherrypy.config.update(cfgfile) diff --git a/ipsilon/util/data.py b/ipsilon/util/data.py index 690e1f3..92c0b1c 100644 --- a/ipsilon/util/data.py +++ b/ipsilon/util/data.py @@ -427,14 +427,21 @@ class Store(Log): return current_version = self._get_schema_version() + + base = cherrypy.config.get('base.mount', '/') + if base == '/': + updbargs = '--root-instance' + else: + updbargs = '--instance %s' % base[1:] + if current_version is None: self.error('Database initialization required! ' + - 'Please run ipsilon-upgrade-database') + 'Please run ipsilon-upgrade-database ' + updbargs) raise DatabaseError('Database initialization required for %s' % self.__class__.__name__) if current_version != self._code_schema_version(): self.error('Database upgrade required! ' + - 'Please run ipsilon-upgrade-database') + 'Please run ipsilon-upgrade-database ' + updbargs) raise DatabaseError('Database upgrade required for %s' % self.__class__.__name__)