# HG changeset patch # User Laurent Peuch # Date 1558449372 -7200 # Node ID 84a8a891551276341bf76187e138e5464814073f # Parent d464495452aa536594766f987914e81ef0d1a864 [cubicweb-ctl] move to accepting only once instance name per command The rationals behind this decision are: - while in the past managing all instances sytem wide made a lot of sens, pratices have evolved today and we've moved to managing one instance by one - this makes things easier to debug since commands since using them on several instances were making this harder (errors hidden in the middle) - also solve the problem of the return code to have, before it was always 0 which prevented to do things like: ipython --pdb $(which cubicweb-ctl) $command $instance or shell scripts that used it - this simplify the code and is always good to take diff -r d464495452aa -r 84a8a8915512 cubicweb/cwctl.py --- a/cubicweb/cwctl.py Tue Jul 09 11:13:08 2019 +0200 +++ b/cubicweb/cwctl.py Tue May 21 16:36:12 2019 +0200 @@ -23,7 +23,7 @@ # completion). So import locally in command helpers. import sys from warnings import filterwarnings -from os import listdir, system +from os import listdir from os.path import exists, join, isdir try: @@ -98,10 +98,12 @@ class InstanceCommand(Command): - """base class for command taking 0 to n instance id as arguments - (0 meaning all registered instances) - """ - arguments = '[...]' + """base class for command taking one instance id as arguments""" + arguments = '' + + # enforce having one instance + min_args = max_args = 1 + options = ( ("force", {'short': 'f', 'action': 'store_true', @@ -116,33 +118,9 @@ """run the _method on each argument (a list of instance identifiers) """ - if not args: - args = list_instances(cwcfg.instances_dir()) - try: - askconfirm = not self.config.force - except AttributeError: - # no force option - askconfirm = False - else: - askconfirm = False - self.run_args(args, askconfirm) + appid = args[0] + cmdmeth = getattr(self, '%s_instance' % self.name) - def run_args(self, args, askconfirm): - status = 0 - for appid in args: - if askconfirm: - print('*' * 72) - if not ASK.confirm('%s instance %r ?' % (self.name, appid)): - continue - try: - status = max(status, self.run_arg(appid)) - except (KeyboardInterrupt, SystemExit): - sys.stderr.write('%s aborted\n' % self.name) - return 2 # specific error code - sys.exit(status) - - def run_arg(self, appid): - cmdmeth = getattr(self, '%s_instance' % self.name) try: status = cmdmeth(appid) or 0 except (ExecutionError, ConfigurationError) as ex: @@ -155,30 +133,12 @@ sys.stderr.write('instance %s not %s: %s\n' % ( appid, self.actionverb, ex)) status = 8 - return status - - -class InstanceCommandFork(InstanceCommand): - """Same as `InstanceCommand`, but command is forked in a new environment - for each argument - """ - def run_args(self, args, askconfirm): - if len(args) > 1: - forkcmd = ' '.join(w for w in sys.argv if w not in args) - else: - forkcmd = None - for appid in args: - if askconfirm: - print('*' * 72) - if not ASK.confirm('%s instance %r ?' % (self.name, appid)): - continue - if forkcmd: - status = system('%s %s' % (forkcmd, appid)) - if status: - print('%s exited with status %s' % (forkcmd, status)) - else: - self.run_arg(appid) + except (KeyboardInterrupt, SystemExit): + sys.stderr.write('%s aborted\n' % self.name) + status = 2 # specific error code + + sys.exit(status) # base commands ############################################################### @@ -465,16 +425,15 @@ config.init_log(config['log-threshold'], force=True) -class UpgradeInstanceCommand(InstanceCommandFork): +class UpgradeInstanceCommand(InstanceCommand): """Upgrade an instance after cubicweb and/or component(s) upgrade. For repository update, you will be prompted for a login / password to use to connect to the system database. For some upgrades, the given user should have create or alter table permissions. - ... - identifiers of the instances to upgrade. If no instance is - given, upgrade them all. + + identifier of the instance to upgrade. """ name = 'upgrade' actionverb = 'upgraded' @@ -638,6 +597,7 @@ name = 'shell' arguments = ' [batch command file(s)] [--