server/serverctl.py
changeset 136 ff51a18c66a3
parent 0 b97547f5f1fa
child 1016 26387b836099
child 1251 af40e615dc89
equal deleted inserted replaced
135:1138432a56a4 136:ff51a18c66a3
    16 from cubicweb.server.serverconfig import ServerConfiguration
    16 from cubicweb.server.serverconfig import ServerConfiguration
    17 
    17 
    18 
    18 
    19 # utility functions ###########################################################
    19 # utility functions ###########################################################
    20 
    20 
    21 def source_cnx(source, dbname=None, special_privs=False):
    21 def source_cnx(source, dbname=None, special_privs=False, verbose=True):
    22     """open and return a connection to the system database defined in the
    22     """open and return a connection to the system database defined in the
    23     given server.serverconfig
    23     given server.serverconfig
    24     """
    24     """
    25     from getpass import getpass
    25     from getpass import getpass
    26     from logilab.common.db import get_connection
    26     from logilab.common.db import get_connection
    27     dbhost = source['db-host']
    27     dbhost = source['db-host']
    28     if dbname is None:
    28     if dbname is None:
    29         dbname = source['db-name']
    29         dbname = source['db-name']
    30     driver = source['db-driver']
    30     driver = source['db-driver']
    31     print '**** connecting to %s database %s@%s' % (driver, dbname, dbhost),
    31     print '**** connecting to %s database %s@%s' % (driver, dbname, dbhost),
    32     if not special_privs and source.get('db-user'):
    32     if not verbose or (not special_privs and source.get('db-user')):
    33         user = source['db-user']
    33         user = source['db-user']
    34         print 'as', user
    34         print 'as', user
    35         if source.get('db-password'):
    35         if source.get('db-password'):
    36             password = source['db-password']
    36             password = source['db-password']
    37         else:
    37         else:
    51         else:
    51         else:
    52             password = getpass('password: ')
    52             password = getpass('password: ')
    53     return get_connection(driver, dbhost, dbname, user, password=password,
    53     return get_connection(driver, dbhost, dbname, user, password=password,
    54                           port=source.get('db-port'))
    54                           port=source.get('db-port'))
    55 
    55 
    56 def system_source_cnx(source, dbms_system_base=False, special_privs=None):
    56 def system_source_cnx(source, dbms_system_base=False,
       
    57                       special_privs='CREATE/DROP DATABASE', verbose=True):
    57     """shortcut to get a connextion to the application system database
    58     """shortcut to get a connextion to the application system database
    58     defined in the given config. If <dbms_system_base> is True,
    59     defined in the given config. If <dbms_system_base> is True,
    59     connect to the dbms system database instead (for task such as
    60     connect to the dbms system database instead (for task such as
    60     create/drop the application database)
    61     create/drop the application database)
    61     """
    62     """
    62     if dbms_system_base:
    63     if dbms_system_base:
    63         from logilab.common.adbh import get_adv_func_helper
    64         from logilab.common.adbh import get_adv_func_helper
    64         system_db = get_adv_func_helper(source['db-driver']).system_database()
    65         system_db = get_adv_func_helper(source['db-driver']).system_database()
    65         special_privs = special_privs or 'CREATE/DROP DATABASE'
    66         return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose)
    66         return source_cnx(source, system_db, special_privs=special_privs)
    67     return source_cnx(source, special_privs=special_privs, verbose=verbose)
    67     return source_cnx(source, special_privs=special_privs)
    68 
    68 
    69 def _db_sys_cnx(source, what, db=None, user=None, verbose=True):
    69 def _db_sys_cnx(source, what, db=None, user=None):
       
    70     """return a connection on the RDMS system table (to create/drop a user
    70     """return a connection on the RDMS system table (to create/drop a user
    71     or a database
    71     or a database
    72     """
    72     """
    73     from logilab.common.adbh import get_adv_func_helper
    73     from logilab.common.adbh import get_adv_func_helper
    74     special_privs = ''
    74     special_privs = ''
    77     if user is not None and helper.users_support:
    77     if user is not None and helper.users_support:
    78         special_privs += '%s USER' % what
    78         special_privs += '%s USER' % what
    79     if db is not None:
    79     if db is not None:
    80         special_privs += ' %s DATABASE' % what
    80         special_privs += ' %s DATABASE' % what
    81     # connect on the dbms system base to create our base
    81     # connect on the dbms system base to create our base
    82     cnx = system_source_cnx(source, True, special_privs=special_privs)
    82     cnx = system_source_cnx(source, True, special_privs=special_privs, verbose=verbose)
    83     # disable autocommit (isolation_level(1)) because DROP and
    83     # disable autocommit (isolation_level(1)) because DROP and
    84     # CREATE DATABASE can't be executed in a transaction
    84     # CREATE DATABASE can't be executed in a transaction
    85     try:
    85     try:
    86         cnx.set_isolation_level(0)
    86         cnx.set_isolation_level(0)
    87     except AttributeError:
    87     except AttributeError:
   187         # remember selected cubes for later initialization of the database
   187         # remember selected cubes for later initialization of the database
   188         config.write_bootstrap_cubes_file(cubes)
   188         config.write_bootstrap_cubes_file(cubes)
   189         
   189         
   190     def postcreate(self):
   190     def postcreate(self):
   191         if confirm('do you want to create repository\'s system database?'):
   191         if confirm('do you want to create repository\'s system database?'):
   192             cmd_run('db-create', self.config.appid)
   192             verbosity = (self.config.mode == 'installed') and 'y' or 'n'
       
   193             cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity)
   193         else:
   194         else:
   194             print 'nevermind, you can do it later using the db-create command'
   195             print 'nevermind, you can do it later using the db-create command'
   195             
   196             
   196 USER_OPTIONS =  (
   197 USER_OPTIONS =  (
   197     ('login', {'type' : 'string',
   198     ('login', {'type' : 'string',
   278     options = (
   279     options = (
   279         ("create-db",
   280         ("create-db",
   280          {'short': 'c', 'type': "yn", 'metavar': '<y or n>',
   281          {'short': 'c', 'type': "yn", 'metavar': '<y or n>',
   281           'default': True,
   282           'default': True,
   282           'help': 'create the database (yes by default)'}),
   283           'help': 'create the database (yes by default)'}),
       
   284         ("verbose",
       
   285          {'short': 'v', 'type' : 'yn', 'metavar': '<verbose>',
       
   286           'default': 'n',
       
   287           'help': 'verbose mode: will ask all possible configuration questions',
       
   288           }
       
   289          ),
   283         )
   290         )
   284     def run(self, args):
   291     def run(self, args):
   285         """run the command with its specific arguments"""
   292         """run the command with its specific arguments"""
   286         from logilab.common.adbh import get_adv_func_helper
   293         from logilab.common.adbh import get_adv_func_helper
   287         from indexer import get_indexer
   294         from indexer import get_indexer
       
   295         verbose = self.get('verbose')
   288         appid = pop_arg(args, msg="No application specified !")
   296         appid = pop_arg(args, msg="No application specified !")
   289         config = ServerConfiguration.config_for(appid)
   297         config = ServerConfiguration.config_for(appid)
   290         create_db = self.config.create_db
   298         create_db = self.config.create_db
   291         source = config.sources()['system']
   299         source = config.sources()['system']
   292         driver = source['db-driver']
   300         driver = source['db-driver']
   293         helper = get_adv_func_helper(driver)
   301         helper = get_adv_func_helper(driver)
   294         if create_db:
   302         if create_db:
   295             # connect on the dbms system base to create our base
   303             # connect on the dbms system base to create our base
   296             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER')
   304             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
   297             cursor = dbcnx.cursor()
   305             cursor = dbcnx.cursor()
   298             try:
   306             try:
   299                 if helper.users_support:
   307                 if helper.users_support:
   300                     user = source['db-user']
   308                     user = source['db-user']
   301                     if not helper.user_exists(cursor, user) and \
   309                     if not helper.user_exists(cursor, user) and \
   317                 dbcnx.commit()
   325                 dbcnx.commit()
   318                 print 'database %s created' % source['db-name']
   326                 print 'database %s created' % source['db-name']
   319             except:
   327             except:
   320                 dbcnx.rollback()
   328                 dbcnx.rollback()
   321                 raise
   329                 raise
   322         cnx = system_source_cnx(source, special_privs='LANGUAGE C') 
   330         cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) 
   323         cursor = cnx.cursor()
   331         cursor = cnx.cursor()
   324         indexer = get_indexer(driver)
   332         indexer = get_indexer(driver)
   325         indexer.init_extensions(cursor)
   333         indexer.init_extensions(cursor)
   326         # postgres specific stuff        
   334         # postgres specific stuff        
   327         if driver == 'postgres':
   335         if driver == 'postgres':