[ctl] allow to specific instance name for instance commands in $CW_INSTANCE

Closes #17253994

authorLaurent Peuch <cortex@worlddomination.be>
changeset22ece66dcd47
branchdefault
phasepublic
hiddenno
parent revision#38d24fb2ae48 [cubicweb/utils] add QueryCache.__contains__() to improve dict-like behavior. Closes #255426
child revision#d409444aa2ab [html] Add tags ol, ul, li, i, q, #7df6c6048bc8 [debug] add a new channel events mechanism for debugging
files modified by this revision
cubicweb/cwctl.py
doc/changes/3.27.rst
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1573041170 -3600
# Wed Nov 06 12:52:50 2019 +0100
# Node ID 22ece66dcd4721898ebc17abf8d6b8007a7c3caf
# Parent 38d24fb2ae4882603a13355b95ca203cb6b4e9d1
[ctl] allow to specific instance name for instance commands in $CW_INSTANCE

Closes #17253994

diff --git a/cubicweb/cwctl.py b/cubicweb/cwctl.py
@@ -19,10 +19,11 @@
1  provide a pluggable commands system.
2  """
3  # *ctl module should limit the number of import to be imported as quickly as
4  # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
5  # completion). So import locally in command helpers.
6 +import os
7  import sys
8  import traceback
9  from warnings import filterwarnings
10  from os import listdir
11  from os.path import exists, join, isdir
@@ -105,11 +106,12 @@
12  class InstanceCommand(Command):
13      """base class for command taking one instance id as arguments"""
14      arguments = '<instance>'
15 
16      # enforce having one instance
17 -    min_args = max_args = 1
18 +    min_args = 0
19 +    max_args = 1
20 
21      options = (
22          ("force",
23           {'short': 'f', 'action': 'store_true',
24            'default': False,
@@ -140,11 +142,18 @@
25 
26      def run(self, args):
27          """run the <command>_method on each argument (a list of instance
28          identifiers)
29          """
30 -        appid = args[0]
31 +        if not args:
32 +            if "CW_INSTANCE" in os.environ:
33 +                appid = os.environ["CW_INSTANCE"]
34 +            else:
35 +                raise BadCommandUsage("Error: instance id is missing")
36 +        else:
37 +            appid = args[0]
38 +
39          cmdmeth = getattr(self, '%s_instance' % self.name)
40 
41          traceback_ = None
42 
43          # debugmode=True is to force to have a StreamHandler used instead of
diff --git a/doc/changes/3.27.rst b/doc/changes/3.27.rst
@@ -47,10 +47,13 @@
44  * on DBG_SQL and/or DBG_RQL, if pygments is installed, syntax highlight sql/rql
45    debug output
46 
47  * add a new '-t/--toolbar' option the pyramid command to activate the pyramid debugtoolbar
48 
49 +* allow to specify the instance id for any instance command using the
50 +  CW_INSTANCE global variable instead of or giving it as a cli argument
51 +
52  Backwards incompatible changes
53  ------------------------------
54 
55  * Standardization on the way to launch a cubicweb instance, from now on the
56    only way to do that will be the used the ``pyramid`` command. Therefore: