cwctl.py
changeset 8682 20bd1cdf86ae
parent 8669 62213a34726e
child 8695 358d8bed9626
equal deleted inserted replaced
8677:a75bb09d6d19 8682:20bd1cdf86ae
    26 # completion). So import locally in command helpers.
    26 # completion). So import locally in command helpers.
    27 import sys
    27 import sys
    28 from warnings import warn
    28 from warnings import warn
    29 from os import remove, listdir, system, pathsep
    29 from os import remove, listdir, system, pathsep
    30 from os.path import exists, join, isfile, isdir, dirname, abspath
    30 from os.path import exists, join, isfile, isdir, dirname, abspath
       
    31 from urlparse import urlparse
       
    32 
    31 try:
    33 try:
    32     from os import kill, getpgid
    34     from os import kill, getpgid
    33 except ImportError:
    35 except ImportError:
    34     def kill(*args):
    36     def kill(*args):
    35         """win32 kill implementation"""
    37         """win32 kill implementation"""
   876 directly give URI as instance id instead',
   878 directly give URI as instance id instead',
   877           'group': 'remote'
   879           'group': 'remote'
   878           }),
   880           }),
   879         )
   881         )
   880 
   882 
       
   883     def _handle_inmemory(self, appid):
       
   884         """ returns migration context handler & shutdown function """
       
   885         config = cwcfg.config_for(appid)
       
   886         if self.config.ext_sources:
       
   887             assert not self.config.system_only
       
   888             sources = self.config.ext_sources
       
   889         elif self.config.system_only:
       
   890             sources = ('system',)
       
   891         else:
       
   892             sources = ('all',)
       
   893         config.set_sources_mode(sources)
       
   894         config.repairing = self.config.force
       
   895         mih = config.migration_handler()
       
   896         return mih, lambda: mih.shutdown()
       
   897 
       
   898     def _handle_networked(self, appuri):
       
   899         """ returns migration context handler & shutdown function """
       
   900         from cubicweb import AuthenticationError
       
   901         from cubicweb.dbapi import connect
       
   902         from cubicweb.server.utils import manager_userpasswd
       
   903         from cubicweb.server.migractions import ServerMigrationHelper
       
   904         while True:
       
   905             try:
       
   906                 login, pwd = manager_userpasswd(msg=None)
       
   907                 cnx = connect(appuri, login=login, password=pwd, mulcnx=False)
       
   908             except AuthenticationError, ex:
       
   909                 print ex
       
   910             except (KeyboardInterrupt, EOFError):
       
   911                 print
       
   912                 sys.exit(0)
       
   913             else:
       
   914                 break
       
   915         cnx.load_appobjects()
       
   916         repo = cnx._repo
       
   917         mih = ServerMigrationHelper(None, repo=repo, cnx=cnx, verbosity=0,
       
   918                                     # hack so it don't try to load fs schema
       
   919                                     schema=1)
       
   920         return mih, lambda: cnx.close()
       
   921 
   881     def run(self, args):
   922     def run(self, args):
   882         from urlparse import urlparse
       
   883         appuri = args.pop(0)
   923         appuri = args.pop(0)
   884         if self.config.repo_uri:
   924         if self.config.repo_uri:
   885             warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id',
   925             warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id',
   886                  DeprecationWarning)
   926                  DeprecationWarning)
   887             if urlparse(self.config.repo_uri).scheme in ('pyro', 'inmemory'):
   927             if urlparse(self.config.repo_uri).scheme in ('pyro', 'inmemory'):
   888                 appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri)
   928                 appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri)
   889         scheme = urlparse(self.config.repo_uri).scheme
   929 
   890         if scheme not in ('', 'inmemory'):
   930         from cubicweb.utils import parse_repo_uri
   891             from cubicweb import AuthenticationError
   931         protocol, hostport, appid = parse_repo_uri(appuri)
   892             from cubicweb.dbapi import connect
   932         if protocol == 'inmemory':
   893             from cubicweb.server.utils import manager_userpasswd
   933             mih, shutdown_callback = self._handle_inmemory(appid)
   894             from cubicweb.server.migractions import ServerMigrationHelper
       
   895             while True:
       
   896                 try:
       
   897                     login, pwd = manager_userpasswd(msg=None)
       
   898                     cnx = connect(appuri, login=login, password=pwd, mulcnx=False)
       
   899                 except AuthenticationError, ex:
       
   900                     print ex
       
   901                 except (KeyboardInterrupt, EOFError):
       
   902                     print
       
   903                     sys.exit(0)
       
   904                 else:
       
   905                     break
       
   906             cnx.load_appobjects()
       
   907             repo = cnx._repo
       
   908             mih = ServerMigrationHelper(None, repo=repo, cnx=cnx, verbosity=0,
       
   909                                          # hack so it don't try to load fs schema
       
   910                                         schema=1)
       
   911         else:
   934         else:
   912             config = cwcfg.config_for(appid)
   935             mih, shutdown_callback = self._handle_networked(appuri)
   913             if self.config.ext_sources:
       
   914                 assert not self.config.system_only
       
   915                 sources = self.config.ext_sources
       
   916             elif self.config.system_only:
       
   917                 sources = ('system',)
       
   918             else:
       
   919                 sources = ('all',)
       
   920             config.set_sources_mode(sources)
       
   921             config.repairing = self.config.force
       
   922             mih = config.migration_handler()
       
   923         try:
   936         try:
   924             if args:
   937             if args:
   925                 # use cmdline parser to access left/right attributes only
   938                 # use cmdline parser to access left/right attributes only
   926                 # remember that usage requires instance appid as first argument
   939                 # remember that usage requires instance appid as first argument
   927                 scripts, args = self.cmdline_parser.largs[1:], self.cmdline_parser.rargs
   940                 scripts, args = self.cmdline_parser.largs[1:], self.cmdline_parser.rargs
   929                     mih.cmd_process_script(script, scriptargs=args)
   942                     mih.cmd_process_script(script, scriptargs=args)
   930                     mih.commit()
   943                     mih.commit()
   931             else:
   944             else:
   932                 mih.interactive_shell()
   945                 mih.interactive_shell()
   933         finally:
   946         finally:
   934             if scheme in ('', 'inmemory'): # shutdown in-memory repo
   947             shutdown_callback()
   935                 mih.shutdown()
       
   936             else:
       
   937                 cnx.close()
       
   938 
   948 
   939 
   949 
   940 class RecompileInstanceCatalogsCommand(InstanceCommand):
   950 class RecompileInstanceCatalogsCommand(InstanceCommand):
   941     """Recompile i18n catalogs for instances.
   951     """Recompile i18n catalogs for instances.
   942 
   952