server/serverctl.py
changeset 3998 94cc7cad3d2d
parent 3890 d7a270f50f54
parent 3954 56a8b14d68aa
child 4204 60256056bda6
equal deleted inserted replaced
3895:92ead039d3d0 3998:94cc7cad3d2d
    16 
    16 
    17 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
    17 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
    18 from cubicweb.toolsutils import Command, CommandHandler, underline_title
    18 from cubicweb.toolsutils import Command, CommandHandler, underline_title
    19 from cubicweb.server import SOURCE_TYPES
    19 from cubicweb.server import SOURCE_TYPES
    20 from cubicweb.server.utils import ask_source_config
    20 from cubicweb.server.utils import ask_source_config
    21 from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration
    21 from cubicweb.server.serverconfig import (USER_OPTIONS, ServerConfiguration,
       
    22                                           SourceConfiguration)
    22 
    23 
    23 # utility functions ###########################################################
    24 # utility functions ###########################################################
    24 
    25 
    25 def source_cnx(source, dbname=None, special_privs=False, verbose=True):
    26 def source_cnx(source, dbname=None, special_privs=False, verbose=True):
    26     """open and return a connection to the system database defined in the
    27     """open and return a connection to the system database defined in the
   111             print '-> Error: wrong user/password.'
   112             print '-> Error: wrong user/password.'
   112             # reset cubes else we'll have an assertion error on next retry
   113             # reset cubes else we'll have an assertion error on next retry
   113             config._cubes = None
   114             config._cubes = None
   114         login, pwd = manager_userpasswd()
   115         login, pwd = manager_userpasswd()
   115 
   116 
       
   117 
   116 # repository specific command handlers ########################################
   118 # repository specific command handlers ########################################
   117 
   119 
   118 class RepositoryCreateHandler(CommandHandler):
   120 class RepositoryCreateHandler(CommandHandler):
   119     cmdname = 'create'
   121     cmdname = 'create'
   120     cfgname = 'repository'
   122     cfgname = 'repository'
   133             config.input_config('pyro', inputlevel)
   135             config.input_config('pyro', inputlevel)
   134         print '\n'+underline_title('Configuring the sources')
   136         print '\n'+underline_title('Configuring the sources')
   135         sourcesfile = config.sources_file()
   137         sourcesfile = config.sources_file()
   136         # XXX hack to make Method('default_instance_id') usable in db option
   138         # XXX hack to make Method('default_instance_id') usable in db option
   137         # defs (in native.py)
   139         # defs (in native.py)
   138         Configuration.default_instance_id = staticmethod(lambda: config.appid)
   140         sconfig = SourceConfiguration(config.appid,
   139         sconfig = Configuration(options=SOURCE_TYPES['native'].options)
   141                                       options=SOURCE_TYPES['native'].options)
   140         sconfig.adapter = 'native'
   142         sconfig.adapter = 'native'
   141         sconfig.input_config(inputlevel=inputlevel)
   143         sconfig.input_config(inputlevel=inputlevel)
   142         sourcescfg = {'system': sconfig}
   144         sourcescfg = {'system': sconfig}
   143         for cube in cubes:
   145         for cube in cubes:
   144             # if a source is named as the cube containing it, we need the
   146             # if a source is named as the cube containing it, we need the
   236             from cubicweb.server.repository import pyro_unregister
   238             from cubicweb.server.repository import pyro_unregister
   237             pyro_unregister(self.config)
   239             pyro_unregister(self.config)
   238 
   240 
   239 
   241 
   240 # repository specific commands ################################################
   242 # repository specific commands ################################################
       
   243 
   241 class CreateInstanceDBCommand(Command):
   244 class CreateInstanceDBCommand(Command):
   242     """Create the system database of an instance (run after 'create').
   245     """Create the system database of an instance (run after 'create').
   243 
   246 
   244     You will be prompted for a login / password to use to connect to
   247     You will be prompted for a login / password to use to connect to
   245     the system database.  The given user should have almost all rights
   248     the system database.  The given user should have almost all rights
   327         print
   330         print
   328         if automatic or ASK.confirm('Run db-init to initialize the system database ?'):
   331         if automatic or ASK.confirm('Run db-init to initialize the system database ?'):
   329             cmd_run('db-init', config.appid)
   332             cmd_run('db-init', config.appid)
   330         else:
   333         else:
   331             print ('-> nevermind, you can do it later with '
   334             print ('-> nevermind, you can do it later with '
   332                    '"cubicweb-ctl db-init %s".' % self.config.appid)
   335                    '"cubicweb-ctl db-init %s".' % config.appid)
   333 
   336 
   334 
   337 
   335 class InitInstanceCommand(Command):
   338 class InitInstanceCommand(Command):
   336     """Initialize the system database of an instance (run after 'db-create').
   339     """Initialize the system database of an instance (run after 'db-create').
   337 
   340 
   354         )
   357         )
   355 
   358 
   356     def run(self, args):
   359     def run(self, args):
   357         print '\n'+underline_title('Initializing the system database')
   360         print '\n'+underline_title('Initializing the system database')
   358         from cubicweb.server import init_repository
   361         from cubicweb.server import init_repository
       
   362         from logilab.common.db import get_connection
   359         appid = pop_arg(args, msg='No instance specified !')
   363         appid = pop_arg(args, msg='No instance specified !')
   360         config = ServerConfiguration.config_for(appid)
   364         config = ServerConfiguration.config_for(appid)
       
   365         try:
       
   366             system = config.sources()['system']
       
   367             get_connection(
       
   368                 system['db-driver'], database=system['db-name'],
       
   369                 host=system.get('db-host'), port=system.get('db-port'),
       
   370                 user=system.get('db-user'), password=system.get('db-password'))
       
   371         except Exception, ex:
       
   372             raise ConfigurationError(
       
   373                 'You seem to have provided wrong connection information in '\
       
   374                 'the %s file. Resolve this first (error: %s).'
       
   375                 % (config.sources_file(), str(ex).strip()))
   361         init_repository(config, drop=self.config.drop)
   376         init_repository(config, drop=self.config.drop)
   362 
   377 
   363 
   378 
   364 class GrantUserOnInstanceCommand(Command):
   379 class GrantUserOnInstanceCommand(Command):
   365     """Grant a database user on a repository system database.
   380     """Grant a database user on a repository system database.
   399             traceback.print_exc()
   414             traceback.print_exc()
   400             print '-> an error occured:', ex
   415             print '-> an error occured:', ex
   401         else:
   416         else:
   402             cnx.commit()
   417             cnx.commit()
   403             print '-> rights granted to %s on instance %s.' % (appid, user)
   418             print '-> rights granted to %s on instance %s.' % (appid, user)
       
   419 
   404 
   420 
   405 class ResetAdminPasswordCommand(Command):
   421 class ResetAdminPasswordCommand(Command):
   406     """Reset the administrator password.
   422     """Reset the administrator password.
   407 
   423 
   408     <instance>
   424     <instance>