server/serverctl.py
changeset 4204 60256056bda6
parent 3998 94cc7cad3d2d
parent 4197 5446c74189b6
child 4252 6c4f109c2b03
equal deleted inserted replaced
4192:8e2eaa6b3733 4204:60256056bda6
    53         user = user or default_user
    53         user = user or default_user
    54         if user == source.get('db-user') and source.get('db-password'):
    54         if user == source.get('db-user') and source.get('db-password'):
    55             password = source['db-password']
    55             password = source['db-password']
    56         else:
    56         else:
    57             password = getpass('password: ')
    57             password = getpass('password: ')
       
    58     extra_args = source.get('db-extra-arguments')
       
    59     extra = extra_args and {'extra_args': extra_args} or {}
    58     return get_connection(driver, dbhost, dbname, user, password=password,
    60     return get_connection(driver, dbhost, dbname, user, password=password,
    59                           port=source.get('db-port'))
    61                           port=source.get('db-port'),
       
    62                           **extra)
    60 
    63 
    61 def system_source_cnx(source, dbms_system_base=False,
    64 def system_source_cnx(source, dbms_system_base=False,
    62                       special_privs='CREATE/DROP DATABASE', verbose=True):
    65                       special_privs='CREATE/DROP DATABASE', verbose=True):
    63     """shortcut to get a connextion to the instance system database
    66     """shortcut to get a connextion to the instance system database
    64     defined in the given config. If <dbms_system_base> is True,
    67     defined in the given config. If <dbms_system_base> is True,
   279         from indexer import get_indexer
   282         from indexer import get_indexer
   280         verbose = self.get('verbose')
   283         verbose = self.get('verbose')
   281         automatic = self.get('automatic')
   284         automatic = self.get('automatic')
   282         appid = pop_arg(args, msg='No instance specified !')
   285         appid = pop_arg(args, msg='No instance specified !')
   283         config = ServerConfiguration.config_for(appid)
   286         config = ServerConfiguration.config_for(appid)
       
   287         source = config.sources()['system']
       
   288         dbname = source['db-name']
       
   289         driver = source['db-driver']
   284         create_db = self.config.create_db
   290         create_db = self.config.create_db
   285         source = config.sources()['system']
       
   286         driver = source['db-driver']
       
   287         helper = get_adv_func_helper(driver)
   291         helper = get_adv_func_helper(driver)
   288         if create_db:
   292         if driver == 'sqlite':
       
   293             if os.path.exists(dbname) and automatic or \
       
   294                    ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
       
   295                 os.unlink(dbname)
       
   296         elif create_db:
   289             print '\n'+underline_title('Creating the system database')
   297             print '\n'+underline_title('Creating the system database')
   290             # connect on the dbms system base to create our base
   298             # connect on the dbms system base to create our base
   291             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   299             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   292             cursor = dbcnx.cursor()
   300             cursor = dbcnx.cursor()
   293             try:
   301             try:
   295                     user = source['db-user']
   303                     user = source['db-user']
   296                     if not helper.user_exists(cursor, user) and (automatic or \
   304                     if not helper.user_exists(cursor, user) and (automatic or \
   297                            ASK.confirm('Create db user %s ?' % user, default_is_yes=False)):
   305                            ASK.confirm('Create db user %s ?' % user, default_is_yes=False)):
   298                         helper.create_user(source['db-user'], source['db-password'])
   306                         helper.create_user(source['db-user'], source['db-password'])
   299                         print '-> user %s created.' % user
   307                         print '-> user %s created.' % user
   300                 dbname = source['db-name']
       
   301                 if dbname in helper.list_databases(cursor):
   308                 if dbname in helper.list_databases(cursor):
   302                     if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
   309                     if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
   303                         cursor.execute('DROP DATABASE %s' % dbname)
   310                         cursor.execute('DROP DATABASE %s' % dbname)
   304                     else:
   311                     else:
   305                         return
   312                         return
   308                                            source['db-encoding'])
   315                                            source['db-encoding'])
   309                 else:
   316                 else:
   310                     helper.create_database(cursor, dbname,
   317                     helper.create_database(cursor, dbname,
   311                                            encoding=source['db-encoding'])
   318                                            encoding=source['db-encoding'])
   312                 dbcnx.commit()
   319                 dbcnx.commit()
   313                 print '-> database %s created.' % source['db-name']
   320                 print '-> database %s created.' % dbname
   314             except:
   321             except:
   315                 dbcnx.rollback()
   322                 dbcnx.rollback()
   316                 raise
   323                 raise
   317         cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose)
   324         cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose)
   318         cursor = cnx.cursor()
   325         cursor = cnx.cursor()
   362         from logilab.common.db import get_connection
   369         from logilab.common.db import get_connection
   363         appid = pop_arg(args, msg='No instance specified !')
   370         appid = pop_arg(args, msg='No instance specified !')
   364         config = ServerConfiguration.config_for(appid)
   371         config = ServerConfiguration.config_for(appid)
   365         try:
   372         try:
   366             system = config.sources()['system']
   373             system = config.sources()['system']
       
   374             extra_args=system.get('db-extra-arguments')
       
   375             extra = extra_args and {'extra_args': extra_args} or {}
   367             get_connection(
   376             get_connection(
   368                 system['db-driver'], database=system['db-name'],
   377                 system['db-driver'], database=system['db-name'],
   369                 host=system.get('db-host'), port=system.get('db-port'),
   378                 host=system.get('db-host'), port=system.get('db-port'),
   370                 user=system.get('db-user'), password=system.get('db-password'))
   379                 user=system.get('db-user'), password=system.get('db-password'), 
       
   380                 **extra)
   371         except Exception, ex:
   381         except Exception, ex:
   372             raise ConfigurationError(
   382             raise ConfigurationError(
   373                 'You seem to have provided wrong connection information in '\
   383                 'You seem to have provided wrong connection information in '\
   374                 'the %s file. Resolve this first (error: %s).'
   384                 'the %s file. Resolve this first (error: %s).'
   375                 % (config.sources_file(), str(ex).strip()))
   385                 % (config.sources_file(), str(ex).strip()))