server/serverctl.py
changeset 2458 4d114865098f
parent 2446 440cb4ea7e5c
parent 2395 e3093fc12a00
child 2461 1f38b65cbd20
--- a/server/serverctl.py	Thu Jul 23 15:29:07 2009 +0200
+++ b/server/serverctl.py	Thu Jul 23 15:33:03 2009 +0200
@@ -13,13 +13,12 @@
 from logilab.common.configuration import Configuration
 from logilab.common.clcommands import register_commands, cmd_run, pop_arg
 
-from cubicweb import AuthenticationError, ExecutionError, ConfigurationError
+from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, underline_title
 from cubicweb.toolsutils import Command, CommandHandler, confirm
 from cubicweb.server import SOURCE_TYPES
 from cubicweb.server.utils import ask_source_config
 from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration
 
-
 # utility functions ###########################################################
 
 def source_cnx(source, dbname=None, special_privs=False, verbose=True):
@@ -32,7 +31,7 @@
     if dbname is None:
         dbname = source['db-name']
     driver = source['db-driver']
-    print '**** connecting to %s database %s@%s' % (driver, dbname, dbhost),
+    print '-> connecting to %s database %s@%s' % (driver, dbname, dbhost or 'localhost'),
     if not verbose or (not special_privs and source.get('db-user')):
         user = source['db-user']
         print 'as', user
@@ -48,7 +47,7 @@
             print special_privs
             print
         default_user = source.get('db-user', os.environ.get('USER', ''))
-        user = raw_input('user (%r by default): ' % default_user)
+        user = raw_input('Connect as user ? [%r]: ' % default_user)
         user = user or default_user
         if user == source.get('db-user') and source.get('db-password'):
             password = source['db-password']
@@ -108,7 +107,7 @@
         try:
             return in_memory_cnx(config, login, pwd)
         except AuthenticationError:
-            print 'wrong user/password'
+            print '-> Error: wrong user/password.'
             # reset cubes else we'll have an assertion error on next retry
             config._cubes = None
         login, pwd = manager_userpasswd()
@@ -124,14 +123,11 @@
         asking information necessary to build required configuration files
         """
         config = self.config
-        print 'application\'s repository configuration'
-        print '-' * 72
+        print underline_title('Configuring the repository')
         config.input_config('email', inputlevel)
         if config.pyro_enabled():
             config.input_config('pyro-server', inputlevel)
-        print
-        print 'repository sources configuration'
-        print '-' * 72
+        print '\n'+underline_title('Configuring the sources')
         sourcesfile = config.sources_file()
         sconfig = Configuration(options=SOURCE_TYPES['native'].options)
         sconfig.adapter = 'native'
@@ -142,19 +138,20 @@
             # source to use the cube, so add it.
             if cube in SOURCE_TYPES:
                 sourcescfg[cube] = ask_source_config(cube, inputlevel)
-        while raw_input('enter another source [y/N]: ').strip().lower() == 'y':
+        print
+        while confirm('Enter another source ?', default_is_yes=False):
             available = sorted(stype for stype in SOURCE_TYPES
                                if not stype in cubes)
             while True:
                 sourcetype = raw_input('source type (%s): ' % ', '.join(available))
                 if sourcetype in available:
                     break
-                print 'unknown source type, use one of the available type'
+                print '-> unknown source type, use one of the available types.'
             while True:
                 sourceuri = raw_input('source uri: ').strip()
                 if sourceuri != 'admin' and sourceuri not in sourcescfg:
                     break
-                print 'uri already used, choose another one'
+                print '-> uri already used, choose another one.'
             sourcescfg[sourceuri] = ask_source_config(sourcetype)
             sourcemodule = SOURCE_TYPES[sourcetype].module
             if not sourcemodule.startswith('cubicweb.'):
@@ -172,11 +169,12 @@
         config.write_bootstrap_cubes_file(cubes)
 
     def postcreate(self):
-        if confirm('do you want to create repository\'s system database?'):
+        if confirm('Do you want to run db-create to create the "system database" ?'):
             verbosity = (self.config.mode == 'installed') and 'y' or 'n'
             cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity)
         else:
-            print 'nevermind, you can do it later using the db-create command'
+            print ('-> nevermind, you can do it later with '
+                   '"cubicweb-ctl db-create %s".' % self.config.appid)
 
 
 class RepositoryDeleteHandler(CommandHandler):
@@ -189,18 +187,18 @@
         source = self.config.sources()['system']
         dbname = source['db-name']
         helper = get_adv_func_helper(source['db-driver'])
-        if confirm('delete database %s ?' % dbname):
+        if confirm('Delete database %s ?' % dbname):
             user = source['db-user'] or None
             cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user)
             cursor = cnx.cursor()
             try:
                 cursor.execute('DROP DATABASE %s' % dbname)
-                print 'database %s dropped' % dbname
+                print '-> database %s dropped.' % dbname
                 # XXX should check we are not connected as user
                 if user and helper.users_support and \
-                       confirm('delete user %s ?' % user, default_is_yes=False):
+                       confirm('Delete user %s ?' % user, default_is_yes=False):
                     cursor.execute('DROP USER %s' % user)
-                    print 'user %s dropped' % user
+                    print '-> user %s dropped.' % user
                 cnx.commit()
             except:
                 cnx.rollback()
@@ -271,6 +269,7 @@
         driver = source['db-driver']
         helper = get_adv_func_helper(driver)
         if create_db:
+            print '\n'+underline_title('Creating the "system database"')
             # connect on the dbms system base to create our base
             dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose)
             cursor = dbcnx.cursor()
@@ -278,12 +277,12 @@
                 if helper.users_support:
                     user = source['db-user']
                     if not helper.user_exists(cursor, user) and \
-                           confirm('create db user %s ?' % user, default_is_yes=False):
+                           confirm('Create db user %s ?' % user, default_is_yes=False):
                         helper.create_user(source['db-user'], source['db-password'])
-                        print 'user %s created' % user
+                        print '-> user %s created.' % user
                 dbname = source['db-name']
                 if dbname in helper.list_databases(cursor):
-                    if confirm('DB %s already exists -- do you want to drop it ?' % dbname):
+                    if confirm('Database %s already exists -- do you want to drop it ?' % dbname):
                         cursor.execute('DROP DATABASE %s' % dbname)
                     else:
                         return
@@ -294,7 +293,7 @@
                     helper.create_database(cursor, dbname,
                                            encoding=source['db-encoding'])
                 dbcnx.commit()
-                print 'database %s created' % source['db-name']
+                print '-> database %s created.' % source['db-name']
             except:
                 dbcnx.rollback()
                 raise
@@ -309,12 +308,13 @@
                 helper.create_language(cursor, extlang)
         cursor.close()
         cnx.commit()
-        print 'database for application %s created and necessary extensions installed' % appid
+        print '-> database for application %s created and necessary extensions installed.' % appid
         print
-        if confirm('do you want to initialize the system database?'):
+        if confirm('Do you want to run db-init to initialize the "system database" ?'):
             cmd_run('db-init', config.appid)
         else:
-            print 'nevermind, you can do it later using the db-init command'
+            print ('-> nevermind, you can do it later with '
+                   '"cubicweb-ctl db-init %s".' % self.config.appid)
 
 
 class InitApplicationCommand(Command):
@@ -339,6 +339,7 @@
         )
 
     def run(self, args):
+        print '\n'+underline_title('Initializing the "system database"')
         from cubicweb.server import init_repository
         appid = pop_arg(args, msg="No application specified !")
         config = ServerConfiguration.config_for(appid)
@@ -381,10 +382,10 @@
             cnx.rollback()
             import traceback
             traceback.print_exc()
-            print 'An error occured:', ex
+            print '-> an error occured:', ex
         else:
             cnx.commit()
-            print 'grants given to %s on application %s' % (appid, user)
+            print '-> rights granted to %s on application %s.' % (appid, user)
 
 class ResetAdminPasswordCommand(Command):
     """Reset the administrator password.
@@ -405,7 +406,7 @@
         try:
             adminlogin = sourcescfg['admin']['login']
         except KeyError:
-            print 'could not get cubicweb administrator login'
+            print '-> Error: could not get cubicweb administrator login.'
             sys.exit(1)
         cnx = source_cnx(sourcescfg['system'])
         cursor = cnx.cursor()
@@ -425,10 +426,10 @@
             cnx.rollback()
             import traceback
             traceback.print_exc()
-            print 'An error occured:', ex
+            print '-> an error occured:', ex
         else:
             cnx.commit()
-            print 'password reset, sources file regenerated'
+            print '-> password reset, sources file regenerated.'
 
 
 class StartRepositoryCommand(Command):
@@ -490,7 +491,7 @@
     rmcmd = 'ssh -t %s "rm -f /tmp/%s.dump"' % (host, appid)
     print rmcmd
     if os.system(rmcmd) and not confirm(
-        'an error occured while deleting remote dump. Continue anyway?'):
+        'An error occured while deleting remote dump. Continue anyway?'):
         raise ExecutionError('Error while deleting remote dump')
 
 def _local_dump(appid, output):
@@ -541,12 +542,12 @@
         try:
             softversion = config.cube_version(cube)
         except ConfigurationError:
-            print "no cube version information for %s, is the cube installed?" % cube
+            print "-> Error: no cube version information for %s, please check that the cube is installed." % cube
             continue
         try:
             applversion = vcconf[cube]
         except KeyError:
-            print "no cube version information for %s in version configuration" % cube
+            print "-> Error: no cube version information for %s in version configuration." % cube
             continue
         if softversion == applversion:
             continue
@@ -657,7 +658,7 @@
             _local_dump(srcappid, output)
         _local_restore(destappid, output, not self.config.no_drop)
         if self.config.keep_dump:
-            print 'you can get the dump file at', output
+            print '-> you can get the dump file at', output
         else:
             os.remove(output)