[cwctl] allow to overload config parameters as cmdline options when starting an instance (closes #5557656)

eg.

cubicweb-ctl start -p port:8082 myapp

to overload the port parameter defined in the all-in-one.conf file.

authorDavid Douard <david.douard@logilab.fr>
changeset029985fe53da
branchdefault
phasedraft
hiddenyes
parent revision#e7ee508a8b2f [js] Using appropriate http method when calling a remote function to respect REST principles.
child revision<not specified>
files modified by this revision
cwconfig.py
cwctl.py
devtools/__init__.py
devtools/devctl.py
web/webconfig.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1435263169 -7200
# Thu Jun 25 22:12:49 2015 +0200
# Node ID 029985fe53da4fd9eb34688653294b94b482efbb
# Parent e7ee508a8b2f4b074bd9b1a3b5eae8e6c86ba6a4
[cwctl] allow to overload config parameters as cmdline options when starting an instance (closes #5557656)

eg.

cubicweb-ctl start -p port:8082 myapp

to overload the port parameter defined in the all-in-one.conf file.

diff --git a/cwconfig.py b/cwconfig.py
@@ -833,11 +833,11 @@
1      # wouldn't be possible otherwise
2      repairing = False
3 
4      # set by upgrade command
5      verbosity = 0
6 -
7 +    cmdline_options = None
8      options = CubicWebNoAppConfiguration.options + (
9          ('log-file',
10           {'type' : 'string',
11            'default': Method('default_log_file'),
12            'help': 'file where output logs should be written',
@@ -1027,11 +1027,11 @@
13          super(CubicWebConfiguration, self).init_cubes(cubes)
14          # reload config file in cases options are defined in cubes __init__
15          # or site_cubicweb files
16          self.load_file_configuration(self.main_config_file())
17          # configuration initialization hook
18 -        self.load_configuration()
19 +        self.load_configuration(**(self.cmdline_options or {}))
20 
21      def add_cubes(self, cubes):
22          """add given cubes to the list of used cubes"""
23          if not isinstance(cubes, list):
24              cubes = list(cubes)
@@ -1094,13 +1094,13 @@
25              version = self.cube_version(pkg)
26              infos.append('%s-%s' % (pkg, version))
27          infos.append('cubicweb-%s' % str(self.cubicweb_version()))
28          return md5(';'.join(infos)).hexdigest()
29 
30 -    def load_configuration(self):
31 +    def load_configuration(self, **kw):
32          """load instance's configuration files"""
33 -        super(CubicWebConfiguration, self).load_configuration()
34 +        super(CubicWebConfiguration, self).load_configuration(**kw)
35          if self.apphome and not self.creating:
36              # init gettext
37              self._gettext_init()
38 
39      def _load_site_cubicweb(self, sitefile):
diff --git a/cwctl.py b/cwctl.py
@@ -518,12 +518,23 @@
40          ('loglevel',
41           {'short': 'l', 'type' : 'choice', 'metavar': '<log level>',
42            'default': None, 'choices': ('debug', 'info', 'warning', 'error'),
43            'help': 'debug if -D is set, error otherwise',
44            }),
45 -        )
46 +        ('param',
47 +         {'short': 'p', 'type' : 'named', 'metavar' : 'key1:value1,key2:value2',
48 +          'default': None,
49 +          'help': 'set <key> to <value> in configuration file.',
50 +         }),
51 +       )
52 
53 +    def get_params(self, cfg):
54 +        params = {}
55 +        if self.config.param is not None:
56 +            params.update(self.config.param)
57 +        return params
58 +                
59      def start_instance(self, appid):
60          """start the instance's server"""
61          try:
62              import twisted  # noqa
63          except ImportError:
@@ -532,10 +543,12 @@
64                  "Either install it, or use one of the alternative commands:\n"
65                  "- '{ctl} wsgi {appid}'\n"
66                  "- '{ctl} pyramid {appid}' (requires the pyramid cube)\n")
67              raise ExecutionError(msg.format(ctl='cubicweb-ctl', appid=appid))
68          config = cwcfg.config_for(appid, debugmode=self['debug'])
69 +        config.cmdline_options = self.get_params(config)
70 +        # overload config values from cmdline options 
71          init_cmdline_log_threshold(config, self['loglevel'])
72          if self['profile']:
73              config.global_set_option('profile', self.config.profile)
74          helper = self.config_helper(config, cmdname='start')
75          pidf = config['pid-file']
diff --git a/devtools/__init__.py b/devtools/__init__.py
@@ -171,12 +171,12 @@
76      @property
77      def apphome(self):
78          return self._apphome
79      appdatahome = apphome
80 
81 -    def load_configuration(self):
82 -        super(TestServerConfiguration, self).load_configuration()
83 +    def load_configuration(self, **kw):
84 +        super(TestServerConfiguration, self).load_configuration(**kw)
85          # no undo support in tests
86          self.global_set_option('undo-enabled', 'n')
87 
88      def main_config_file(self):
89          """return instance's control configuration file"""
diff --git a/devtools/devctl.py b/devtools/devctl.py
@@ -71,11 +71,11 @@
90 
91      def main_config_file(self):
92          return None
93      def init_log(self):
94          pass
95 -    def load_configuration(self):
96 +    def load_configuration(self, **kw):
97          pass
98      def default_log_file(self):
99          return None
100      def default_stats_file(self):
101          return None
diff --git a/web/webconfig.py b/web/webconfig.py
@@ -373,13 +373,13 @@
102          for directory in path:
103              fpath = join(directory, rdirectory, rid)
104              if exists(fpath):
105                  yield join(fpath)
106 
107 -    def load_configuration(self):
108 +    def load_configuration(self, **kw):
109          """load instance's configuration files"""
110 -        super(WebConfiguration, self).load_configuration()
111 +        super(WebConfiguration, self).load_configuration(**kw)
112          # load external resources definition
113          self._init_base_url()
114          self._build_ui_properties()
115 
116      def _init_base_url(self):