cwctl.py
branchstable
changeset 6138 65f5e488f983
parent 5814 51cc4b61f9ae
child 6144 40af8e328e64
equal deleted inserted replaced
6132:440df442d705 6138:65f5e488f983
    35     def getpgid():
    35     def getpgid():
    36         """win32 getpgid implementation"""
    36         """win32 getpgid implementation"""
    37 
    37 
    38 from os.path import exists, join, isfile, isdir, dirname, abspath
    38 from os.path import exists, join, isfile, isdir, dirname, abspath
    39 
    39 
    40 from logilab.common.clcommands import register_commands, pop_arg
    40 from logilab.common.clcommands import CommandLine
    41 from logilab.common.shellutils import ASK
    41 from logilab.common.shellutils import ASK
    42 
    42 
    43 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
    43 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage
    44 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS
    44 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS
    45 from cubicweb.toolsutils import Command, main_run, rm, create_dir, underline_title
    45 from cubicweb.toolsutils import Command, rm, create_dir, underline_title
       
    46 from cubicweb.__pkginfo__ import version
       
    47 
       
    48 CWCTL = CommandLine('cubicweb-ctl', 'The CubicWeb swiss-knife.', version=version)
    46 
    49 
    47 def wait_process_end(pid, maxtry=10, waittime=1):
    50 def wait_process_end(pid, maxtry=10, waittime=1):
    48     """wait for a process to actually die"""
    51     """wait for a process to actually die"""
    49     import signal
    52     import signal
    50     from time import sleep
    53     from time import sleep
   299     <instance>
   302     <instance>
   300       an identifier for the instance to create
   303       an identifier for the instance to create
   301     """
   304     """
   302     name = 'create'
   305     name = 'create'
   303     arguments = '<cube> <instance>'
   306     arguments = '<cube> <instance>'
       
   307     min_args = max_args = 2
   304     options = (
   308     options = (
   305         ("config-level",
   309         ("config-level",
   306          {'short': 'l', 'type' : 'int', 'metavar': '<level>',
   310          {'short': 'l', 'type' : 'int', 'metavar': '<level>',
   307           'default': 0,
   311           'default': 0,
   308           'help': 'configuration level (0..2): 0 will ask for essential \
   312           'help': 'configuration level (0..2): 0 will ask for essential \
   323 
   327 
   324     def run(self, args):
   328     def run(self, args):
   325         """run the command with its specific arguments"""
   329         """run the command with its specific arguments"""
   326         from logilab.common.textutils import splitstrip
   330         from logilab.common.textutils import splitstrip
   327         configname = self.config.config
   331         configname = self.config.config
   328         cubes = splitstrip(pop_arg(args, 1))
   332         appid, cubes = args
   329         appid = pop_arg(args)
   333         cubes = splitstrip(cubes)
   330         # get the configuration and helper
   334         # get the configuration and helper
   331         config = cwcfg.config_for(appid, configname)
   335         config = cwcfg.config_for(appid, configname)
   332         config.set_language = False
   336         config.set_language = False
   333         cubes = config.expand_cubes(cubes)
   337         cubes = config.expand_cubes(cubes)
   334         config.init_cubes(cubes)
   338         config.init_cubes(cubes)
   413     """Delete an instance. Will remove instance's files and
   417     """Delete an instance. Will remove instance's files and
   414     unregister it.
   418     unregister it.
   415     """
   419     """
   416     name = 'delete'
   420     name = 'delete'
   417     arguments = '<instance>'
   421     arguments = '<instance>'
   418 
   422     min_args = max_args = 1
   419     options = ()
   423     options = ()
   420 
   424 
   421     def run(self, args):
   425     def run(self, args):
   422         """run the command with its specific arguments"""
   426         """run the command with its specific arguments"""
   423         appid = pop_arg(args, msg="No instance specified !")
   427         appid = args[0]
   424         configs = [cwcfg.config_for(appid, configname)
   428         configs = [cwcfg.config_for(appid, configname)
   425                    for configname in cwcfg.possible_configurations(appid)]
   429                    for configname in cwcfg.possible_configurations(appid)]
   426         if not configs:
   430         if not configs:
   427             raise ExecutionError('unable to guess configuration for %s' % appid)
   431             raise ExecutionError('unable to guess configuration for %s' % appid)
   428         for config in configs:
   432         for config in configs:
   794     <instance>
   798     <instance>
   795       the identifier of the instance to connect.
   799       the identifier of the instance to connect.
   796     """
   800     """
   797     name = 'shell'
   801     name = 'shell'
   798     arguments = '<instance> [batch command file(s)] [-- <script arguments>]'
   802     arguments = '<instance> [batch command file(s)] [-- <script arguments>]'
       
   803     min_args = 1
   799     options = (
   804     options = (
   800         ('system-only',
   805         ('system-only',
   801          {'short': 'S', 'action' : 'store_true',
   806          {'short': 'S', 'action' : 'store_true',
   802           'help': 'only connect to the system source when the instance is '
   807           'help': 'only connect to the system source when the instance is '
   803           'using multiple sources. You can\'t use this option and the '
   808           'using multiple sources. You can\'t use this option and the '
   832           'group': 'remote'
   837           'group': 'remote'
   833           }),
   838           }),
   834         )
   839         )
   835 
   840 
   836     def run(self, args):
   841     def run(self, args):
   837         appid = pop_arg(args, None, msg="No instance specified !")
   842         appid = args.pop(0)
   838         if self.config.pyro:
   843         if self.config.pyro:
   839             from cubicweb import AuthenticationError
   844             from cubicweb import AuthenticationError
   840             from cubicweb.dbapi import connect
   845             from cubicweb.dbapi import connect
   841             from cubicweb.server.utils import manager_userpasswd
   846             from cubicweb.server.utils import manager_userpasswd
   842             from cubicweb.server.migractions import ServerMigrationHelper
   847             from cubicweb.server.migractions import ServerMigrationHelper
   928     def run(self, args):
   933     def run(self, args):
   929         """run the command with its specific arguments"""
   934         """run the command with its specific arguments"""
   930         for cube in cwcfg.available_cubes():
   935         for cube in cwcfg.available_cubes():
   931             print cube
   936             print cube
   932 
   937 
   933 register_commands((ListCommand,
   938 for cmdcls in (ListCommand,
   934                    CreateInstanceCommand,
   939                CreateInstanceCommand, DeleteInstanceCommand,
   935                    DeleteInstanceCommand,
   940                StartInstanceCommand, StopInstanceCommand, RestartInstanceCommand,
   936                    StartInstanceCommand,
   941                ReloadConfigurationCommand, StatusCommand,
   937                    StopInstanceCommand,
   942                UpgradeInstanceCommand,
   938                    RestartInstanceCommand,
   943                ShellCommand,
   939                    ReloadConfigurationCommand,
   944                RecompileInstanceCatalogsCommand,
   940                    StatusCommand,
   945                ListInstancesCommand, ListCubesCommand,
   941                    UpgradeInstanceCommand,
   946                ):
   942                    ShellCommand,
   947     CWCTL.register(cmdcls)
   943                    RecompileInstanceCatalogsCommand,
       
   944                    ListInstancesCommand, ListCubesCommand,
       
   945                    ))
       
   946 
   948 
   947 
   949 
   948 def run(args):
   950 def run(args):
   949     """command line tool"""
   951     """command line tool"""
   950     cwcfg.load_cwctl_plugins()
   952     cwcfg.load_cwctl_plugins()
   951     main_run(args, """%%prog %s [options] %s
   953     try:
   952 
   954         CWCTL.run(args)
   953 The CubicWeb swiss-knife.
   955     except ConfigurationError, err:
   954 
   956         print 'ERROR: ', err
   955 %s"""
   957         sys.exit(1)
   956 )
   958     except ExecutionError, err:
       
   959         print err
       
   960         sys.exit(2)
   957 
   961 
   958 if __name__ == '__main__':
   962 if __name__ == '__main__':
   959     run(sys.argv[1:])
   963     run(sys.argv[1:])