server/serverctl.py
branchstable
changeset 2395 e3093fc12a00
parent 2394 92bba46b853f
child 2458 4d114865098f
equal deleted inserted replaced
2394:92bba46b853f 2395:e3093fc12a00
    11 import os
    11 import os
    12 
    12 
    13 from logilab.common.configuration import Configuration
    13 from logilab.common.configuration import Configuration
    14 from logilab.common.clcommands import register_commands, cmd_run, pop_arg
    14 from logilab.common.clcommands import register_commands, cmd_run, pop_arg
    15 
    15 
    16 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
    16 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, underline_title
    17 from cubicweb.toolsutils import Command, CommandHandler, confirm
    17 from cubicweb.toolsutils import Command, CommandHandler, confirm
    18 from cubicweb.server import SOURCE_TYPES
    18 from cubicweb.server import SOURCE_TYPES
    19 from cubicweb.server.utils import ask_source_config
    19 from cubicweb.server.utils import ask_source_config
    20 from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration
    20 from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration
    21 
    21 
    45             print 'WARNING'
    45             print 'WARNING'
    46             print 'the user will need the following special access rights on the database:'
    46             print 'the user will need the following special access rights on the database:'
    47             print special_privs
    47             print special_privs
    48             print
    48             print
    49         default_user = source.get('db-user', os.environ.get('USER', ''))
    49         default_user = source.get('db-user', os.environ.get('USER', ''))
    50         user = raw_input('user (%r by default): ' % default_user)
    50         user = raw_input('Connect as user ? [%r]: ' % default_user)
    51         user = user or default_user
    51         user = user or default_user
    52         if user == source.get('db-user') and source.get('db-password'):
    52         if user == source.get('db-user') and source.get('db-password'):
    53             password = source['db-password']
    53             password = source['db-password']
    54         else:
    54         else:
    55             password = getpass('password: ')
    55             password = getpass('password: ')
   119     def bootstrap(self, cubes, inputlevel=0):
   119     def bootstrap(self, cubes, inputlevel=0):
   120         """create an application by copying files from the given cube and by
   120         """create an application by copying files from the given cube and by
   121         asking information necessary to build required configuration files
   121         asking information necessary to build required configuration files
   122         """
   122         """
   123         config = self.config
   123         config = self.config
   124         print 'application\'s repository configuration'
   124         print underline_title('Configuring the repository')
   125         print '-' * 72
       
   126         config.input_config('email', inputlevel)
   125         config.input_config('email', inputlevel)
   127         if config.pyro_enabled():
   126         if config.pyro_enabled():
   128             config.input_config('pyro-server', inputlevel)
   127             config.input_config('pyro-server', inputlevel)
   129         print
   128         print '\n'+underline_title('Configuring the sources')
   130         print 'repository sources configuration'
       
   131         print '-' * 72
       
   132         sourcesfile = config.sources_file()
   129         sourcesfile = config.sources_file()
   133         sconfig = Configuration(options=SOURCE_TYPES['native'].options)
   130         sconfig = Configuration(options=SOURCE_TYPES['native'].options)
   134         sconfig.adapter = 'native'
   131         sconfig.adapter = 'native'
   135         sconfig.input_config(inputlevel=inputlevel)
   132         sconfig.input_config(inputlevel=inputlevel)
   136         sourcescfg = {'system': sconfig}
   133         sourcescfg = {'system': sconfig}
   137         for cube in cubes:
   134         for cube in cubes:
   138             # if a source is named as the cube containing it, we need the
   135             # if a source is named as the cube containing it, we need the
   139             # source to use the cube, so add it.
   136             # source to use the cube, so add it.
   140             if cube in SOURCE_TYPES:
   137             if cube in SOURCE_TYPES:
   141                 sourcescfg[cube] = ask_source_config(cube, inputlevel)
   138                 sourcescfg[cube] = ask_source_config(cube, inputlevel)
   142         while raw_input('enter another source [y/N]: ').strip().lower() == 'y':
   139         print
       
   140         while confirm('Enter another source ?', default_is_yes=False):
   143             available = sorted(stype for stype in SOURCE_TYPES
   141             available = sorted(stype for stype in SOURCE_TYPES
   144                                if not stype in cubes)
   142                                if not stype in cubes)
   145             while True:
   143             while True:
   146                 sourcetype = raw_input('source type (%s): ' % ', '.join(available))
   144                 sourcetype = raw_input('source type (%s): ' % ', '.join(available))
   147                 if sourcetype in available:
   145                 if sourcetype in available:
   167         config.write_sources_file(sourcescfg)
   165         config.write_sources_file(sourcescfg)
   168         # remember selected cubes for later initialization of the database
   166         # remember selected cubes for later initialization of the database
   169         config.write_bootstrap_cubes_file(cubes)
   167         config.write_bootstrap_cubes_file(cubes)
   170 
   168 
   171     def postcreate(self):
   169     def postcreate(self):
   172         if confirm('Do you want to run db-create to create repository\'s system database?'):
   170         if confirm('Do you want to run db-create to create the "system database" ?'):
   173             verbosity = (self.config.mode == 'installed') and 'y' or 'n'
   171             verbosity = (self.config.mode == 'installed') and 'y' or 'n'
   174             cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity)
   172             cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity)
   175         else:
   173         else:
   176             print '-> nevermind, you can do it later using the db-create command.'
   174             print ('-> nevermind, you can do it later with '
       
   175                    '"cubicweb-ctl db-create %s".' % self.config.appid)
   177 
   176 
   178 
   177 
   179 class RepositoryDeleteHandler(CommandHandler):
   178 class RepositoryDeleteHandler(CommandHandler):
   180     cmdname = 'delete'
   179     cmdname = 'delete'
   181     cfgname = 'repository'
   180     cfgname = 'repository'
   266         create_db = self.config.create_db
   265         create_db = self.config.create_db
   267         source = config.sources()['system']
   266         source = config.sources()['system']
   268         driver = source['db-driver']
   267         driver = source['db-driver']
   269         helper = get_adv_func_helper(driver)
   268         helper = get_adv_func_helper(driver)
   270         if create_db:
   269         if create_db:
       
   270             print '\n'+underline_title('Creating the "system database"')
   271             # connect on the dbms system base to create our base
   271             # connect on the dbms system base to create our base
   272             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   272             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   273             cursor = dbcnx.cursor()
   273             cursor = dbcnx.cursor()
   274             try:
   274             try:
   275                 if helper.users_support:
   275                 if helper.users_support:
   306                 helper.create_language(cursor, extlang)
   306                 helper.create_language(cursor, extlang)
   307         cursor.close()
   307         cursor.close()
   308         cnx.commit()
   308         cnx.commit()
   309         print '-> database for application %s created and necessary extensions installed.' % appid
   309         print '-> database for application %s created and necessary extensions installed.' % appid
   310         print
   310         print
   311         if confirm('Do you want to run db-init to initialize the system database?'):
   311         if confirm('Do you want to run db-init to initialize the "system database" ?'):
   312             cmd_run('db-init', config.appid)
   312             cmd_run('db-init', config.appid)
   313         else:
   313         else:
   314             print '-> nevermind, you can do it later using the db-init command.'
   314             print ('-> nevermind, you can do it later with '
       
   315                    '"cubicweb-ctl db-init %s".' % self.config.appid)
   315 
   316 
   316 
   317 
   317 class InitApplicationCommand(Command):
   318 class InitApplicationCommand(Command):
   318     """Initialize the system database of an application (run after 'db-create').
   319     """Initialize the system database of an application (run after 'db-create').
   319 
   320 
   334           'help': 'insert drop statements to remove previously existant \
   335           'help': 'insert drop statements to remove previously existant \
   335 tables, indexes... (no by default)'}),
   336 tables, indexes... (no by default)'}),
   336         )
   337         )
   337 
   338 
   338     def run(self, args):
   339     def run(self, args):
       
   340         print '\n'+underline_title('Initializing the "system database"')
   339         from cubicweb.server import init_repository
   341         from cubicweb.server import init_repository
   340         appid = pop_arg(args, msg="No application specified !")
   342         appid = pop_arg(args, msg="No application specified !")
   341         config = ServerConfiguration.config_for(appid)
   343         config = ServerConfiguration.config_for(appid)
   342         init_repository(config, drop=self.config.drop)
   344         init_repository(config, drop=self.config.drop)
   343 
   345