[cwctl] add configure command to cw-ctl (closes #2709702) cubicweb-centos-version-3.17.1-1 cubicweb-debian-version-3.17.1-1 cubicweb-version-3.17.1
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Fri, 24 May 2013 13:00:28 +0200
changeset 9002 f98d1c46ed9f
parent 9001 328c24289253
child 9003 dfd818290ffb
child 9185 499f9ddc4470
[cwctl] add configure command to cw-ctl (closes #2709702) usage: cw-ctl configure <appid> --param key=value,key2=value2 --param key3=value3
cwctl.py
server/serverctl.py
--- a/cwctl.py	Fri May 24 14:02:45 2013 +0200
+++ b/cwctl.py	Fri May 24 13:00:28 2013 +0200
@@ -38,10 +38,9 @@
     def getpgid():
         """win32 getpgid implementation"""
 
-
-
 from logilab.common.clcommands import CommandLine
 from logilab.common.shellutils import ASK
+from logilab.common.configuration import merge_options
 
 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
 from cubicweb.utils import support_args
@@ -846,7 +845,6 @@
         for key in sorted(vcconf):
             print key+': %s.%s.%s' % vcconf[key]
 
-
 class ShellCommand(Command):
     """Run an interactive migration shell on an instance. This is a python shell
     with enhanced migration commands predefined in the namespace. An additional
@@ -1013,6 +1011,33 @@
         for cube in cwcfg.available_cubes():
             print cube
 
+class ConfigureInstanceCommand(InstanceCommand):
+    """Configure instance.
+
+    <instance>...
+      identifier of the instance to configure.
+    """
+    name = 'configure'
+    actionverb = 'configured'
+
+    options = merge_options(InstanceCommand.options +
+                            (('param',
+                              {'short': 'p', 'type' : 'named', 'metavar' : 'key1:value1,key2:value2',
+                               'default': None,
+                               'help': 'set <key> to <value> in configuration file.',
+                               }),
+                             ))
+
+    def configure_instance(self, appid):
+        if self.config.param is not None:
+            appcfg = cwcfg.config_for(appid)
+            for key, value in self.config.param.iteritems():
+                try:
+                    appcfg.global_set_option(key, value)
+                except KeyError:
+                    raise ConfigurationError('unknown configuration key "%s" for mode %s' % (key, appcfg.name))
+            appcfg.save()
+
 for cmdcls in (ListCommand,
                CreateInstanceCommand, DeleteInstanceCommand,
                StartInstanceCommand, StopInstanceCommand, RestartInstanceCommand,
@@ -1022,10 +1047,10 @@
                ShellCommand,
                RecompileInstanceCatalogsCommand,
                ListInstancesCommand, ListCubesCommand,
+               ConfigureInstanceCommand,
                ):
     CWCTL.register(cmdcls)
 
-
 def run(args):
     """command line tool"""
     import os
--- a/server/serverctl.py	Fri May 24 14:02:45 2013 +0200
+++ b/server/serverctl.py	Fri May 24 13:00:28 2013 +0200
@@ -28,12 +28,12 @@
 import subprocess
 
 from logilab.common import nullobject
-from logilab.common.configuration import Configuration
+from logilab.common.configuration import Configuration, merge_options
 from logilab.common.shellutils import ASK, generate_password
 
 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
 from cubicweb.toolsutils import Command, CommandHandler, underline_title
-from cubicweb.cwctl import CWCTL, check_options_consistency
+from cubicweb.cwctl import CWCTL, check_options_consistency, ConfigureInstanceCommand
 from cubicweb.server import SOURCE_TYPES
 from cubicweb.server.repository import Repository
 from cubicweb.server.serverconfig import (
@@ -1096,3 +1096,34 @@
                  SynchronizeInstanceSchemaCommand, SynchronizeSourceCommand, SchemaDiffCommand,
                  ):
     CWCTL.register(cmdclass)
+
+# extend configure command to set options in sources config file ###############
+
+db_options = (
+    ('db',
+     {'short': 'd', 'type' : 'named', 'metavar' : 'key1:value1,key2:value2',
+      'default': None,
+      'help': 'set <key> to <value> in "source" configuration file.',
+      }),
+    )
+
+ConfigureInstanceCommand.options = merge_options(
+        ConfigureInstanceCommand.options + db_options)
+
+configure_instance = ConfigureInstanceCommand.configure_instance
+def configure_instance2(self, appid):
+    configure_instance(self, appid)
+    if self.config.db is not None:
+        appcfg = ServerConfiguration.config_for(appid)
+        srccfg = appcfg.read_sources_file()
+        for key, value in self.config.db.iteritems():
+            try:
+                srccfg['system'][key] = value
+            except KeyError:
+                raise ConfigurationError('unknown configuration key "%s" for source' % key)
+        admcfg = Configuration(options=USER_OPTIONS)
+        admcfg['login'] = srccfg['admin']['login']
+        admcfg['password'] = srccfg['admin']['password']
+        srccfg['admin'] = admcfg
+        appcfg.write_sources_file(srccfg)
+ConfigureInstanceCommand.configure_instance = configure_instance2