server/serverctl.py
branchstable
changeset 6867 f691757792f9
parent 6863 2e52db5cdbde
child 6882 b5e34836f84e
child 6939 8fa55cf2a8cb
equal deleted inserted replaced
6866:51d7868264b1 6867:f691757792f9
    23 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    23 # possible (for cubicweb-ctl reactivity, necessary for instance for usable bash
    24 # completion). So import locally in command helpers.
    24 # completion). So import locally in command helpers.
    25 import sys
    25 import sys
    26 import os
    26 import os
    27 
    27 
       
    28 from logilab.common import nullobject
    28 from logilab.common.configuration import Configuration
    29 from logilab.common.configuration import Configuration
    29 from logilab.common.shellutils import ASK
    30 from logilab.common.shellutils import ASK
    30 
    31 
    31 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
    32 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
    32 from cubicweb.toolsutils import Command, CommandHandler, underline_title
    33 from cubicweb.toolsutils import Command, CommandHandler, underline_title
   186             CWCTL.run(['db-create', self.config.appid, '--verbose=%s' % verbosity])
   187             CWCTL.run(['db-create', self.config.appid, '--verbose=%s' % verbosity])
   187         else:
   188         else:
   188             print ('-> nevermind, you can do it later with '
   189             print ('-> nevermind, you can do it later with '
   189                    '"cubicweb-ctl db-create %s".' % self.config.appid)
   190                    '"cubicweb-ctl db-create %s".' % self.config.appid)
   190 
   191 
       
   192 ERROR = nullobject()
       
   193 
       
   194 def confirm_on_error_or_die(msg, func, *args, **kwargs):
       
   195     try:
       
   196         return func(*args, **kwargs)
       
   197     except Exception, ex:
       
   198         print 'ERROR', ex
       
   199         if not ASK.confirm('An error occurred while %s. Continue anyway?' % msg):
       
   200             raise ExecutionError(str(ex))
       
   201     return ERROR
   191 
   202 
   192 class RepositoryDeleteHandler(CommandHandler):
   203 class RepositoryDeleteHandler(CommandHandler):
   193     cmdname = 'delete'
   204     cmdname = 'delete'
   194     cfgname = 'repository'
   205     cfgname = 'repository'
   195 
   206 
   199         source = self.config.sources()['system']
   210         source = self.config.sources()['system']
   200         dbname = source['db-name']
   211         dbname = source['db-name']
   201         helper = get_db_helper(source['db-driver'])
   212         helper = get_db_helper(source['db-driver'])
   202         if ASK.confirm('Delete database %s ?' % dbname):
   213         if ASK.confirm('Delete database %s ?' % dbname):
   203             if source['db-driver'] == 'sqlite':
   214             if source['db-driver'] == 'sqlite':
   204                 os.unlink(source['db-name'])
   215                 if confirm_on_error_or_die(
       
   216                     'deleting database file %s' % dbname,
       
   217                     os.unlink, source['db-name']) is not ERROR:
       
   218                     print '-> database %s dropped.' % dbname
   205                 return
   219                 return
   206             user = source['db-user'] or None
   220             user = source['db-user'] or None
   207             cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user)
   221             cnx = confirm_on_error_or_die('connecting to database %s' % dbname,
       
   222                                           _db_sys_cnx, source, 'DROP DATABASE', user=user)
       
   223             if cnx is ERROR:
       
   224                 return
   208             cursor = cnx.cursor()
   225             cursor = cnx.cursor()
   209             try:
   226             try:
   210                 cursor.execute('DROP DATABASE "%s"' % dbname)
   227                 if confirm_on_error_or_die(
   211                 print '-> database %s dropped.' % dbname
   228                     'dropping database %s' % dbname,
       
   229                     cursor.execute, 'DROP DATABASE "%s"' % dbname) is not ERROR:
       
   230                     print '-> database %s dropped.' % dbname
   212                 # XXX should check we are not connected as user
   231                 # XXX should check we are not connected as user
   213                 if user and helper.users_support and \
   232                 if user and helper.users_support and \
   214                        ASK.confirm('Delete user %s ?' % user, default_is_yes=False):
   233                        ASK.confirm('Delete user %s ?' % user, default_is_yes=False):
   215                     cursor.execute('DROP USER %s' % user)
   234                     if confirm_on_error_or_die(
   216                     print '-> user %s dropped.' % user
   235                         'dropping user %s' % user,
       
   236                         cursor.execute, 'DROP USER %s' % user) is not ERROR:
       
   237                         print '-> user %s dropped.' % user
   217                 cnx.commit()
   238                 cnx.commit()
   218             except:
   239             except:
   219                 cnx.rollback()
   240                 cnx.rollback()
   220                 raise
   241                 raise
   221 
   242