server/serverctl.py
changeset 5754 51179e0bb250
parent 5696 98d390c28edb
child 5814 51cc4b61f9ae
equal deleted inserted replaced
5753:cd20ddaef124 5754:51179e0bb250
    46     from logilab.database import get_connection
    46     from logilab.database import get_connection
    47     dbhost = source.get('db-host')
    47     dbhost = source.get('db-host')
    48     if dbname is None:
    48     if dbname is None:
    49         dbname = source['db-name']
    49         dbname = source['db-name']
    50     driver = source['db-driver']
    50     driver = source['db-driver']
    51     print '-> connecting to %s database' % driver,
    51     if verbose:
    52     if dbhost:
    52         print '-> connecting to %s database' % driver,
    53         print '%s@%s' % (dbname, dbhost),
    53         if dbhost:
    54     else:
    54             print '%s@%s' % (dbname, dbhost),
    55         print dbname,
    55         else:
       
    56             print dbname,
    56     if not verbose or (not special_privs and source.get('db-user')):
    57     if not verbose or (not special_privs and source.get('db-user')):
    57         user = source['db-user']
    58         user = source['db-user']
    58         print 'as', user
    59         if verbose:
       
    60             print 'as', user
    59         if source.get('db-password'):
    61         if source.get('db-password'):
    60             password = source['db-password']
    62             password = source['db-password']
    61         else:
    63         else:
    62             password = getpass('password: ')
    64             password = getpass('password: ')
    63     else:
    65     else:
   270             pyro_unregister(self.config)
   272             pyro_unregister(self.config)
   271 
   273 
   272 
   274 
   273 # repository specific commands ################################################
   275 # repository specific commands ################################################
   274 
   276 
       
   277 def createdb(helper, source, dbcnx, cursor, **kwargs):
       
   278     if dbcnx.logged_user != source['db-user']:
       
   279         helper.create_database(cursor, source['db-name'], source['db-user'],
       
   280                                source['db-encoding'], **kwargs)
       
   281     else:
       
   282         helper.create_database(cursor, source['db-name'],
       
   283                                dbencoding=source['db-encoding'], **kwargs)
       
   284 
   275 class CreateInstanceDBCommand(Command):
   285 class CreateInstanceDBCommand(Command):
   276     """Create the system database of an instance (run after 'create').
   286     """Create the system database of an instance (run after 'create').
   277 
   287 
   278     You will be prompted for a login / password to use to connect to
   288     You will be prompted for a login / password to use to connect to
   279     the system database.  The given user should have almost all rights
   289     the system database.  The given user should have almost all rights
   312         appid = pop_arg(args, msg='No instance specified !')
   322         appid = pop_arg(args, msg='No instance specified !')
   313         config = ServerConfiguration.config_for(appid)
   323         config = ServerConfiguration.config_for(appid)
   314         source = config.sources()['system']
   324         source = config.sources()['system']
   315         dbname = source['db-name']
   325         dbname = source['db-name']
   316         driver = source['db-driver']
   326         driver = source['db-driver']
   317         create_db = self.config.create_db
       
   318         helper = get_db_helper(driver)
   327         helper = get_db_helper(driver)
   319         if driver == 'sqlite':
   328         if driver == 'sqlite':
   320             if os.path.exists(dbname) and (
   329             if os.path.exists(dbname) and (
   321                 automatic or
   330                 automatic or
   322                 ASK.confirm('Database %s already exists. Drop it?' % dbname)):
   331                 ASK.confirm('Database %s already exists. Drop it?' % dbname)):
   323                 os.unlink(dbname)
   332                 os.unlink(dbname)
   324         elif create_db:
   333         elif self.config.create_db:
   325             print '\n'+underline_title('Creating the system database')
   334             print '\n'+underline_title('Creating the system database')
   326             # connect on the dbms system base to create our base
   335             # connect on the dbms system base to create our base
   327             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   336             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   328             cursor = dbcnx.cursor()
   337             cursor = dbcnx.cursor()
   329             try:
   338             try:
   336                 if dbname in helper.list_databases(cursor):
   345                 if dbname in helper.list_databases(cursor):
   337                     if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
   346                     if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname):
   338                         cursor.execute('DROP DATABASE %s' % dbname)
   347                         cursor.execute('DROP DATABASE %s' % dbname)
   339                     else:
   348                     else:
   340                         return
   349                         return
   341                 if dbcnx.logged_user != source['db-user']:
   350                 createdb(helper, source, dbcnx, cursor)
   342                     helper.create_database(cursor, dbname, source['db-user'],
       
   343                                            source['db-encoding'])
       
   344                 else:
       
   345                     helper.create_database(cursor, dbname,
       
   346                                            dbencoding=source['db-encoding'])
       
   347                 dbcnx.commit()
   351                 dbcnx.commit()
   348                 print '-> database %s created.' % dbname
   352                 print '-> database %s created.' % dbname
   349             except:
   353             except:
   350                 dbcnx.rollback()
   354                 dbcnx.rollback()
   351                 raise
   355                 raise