44 from logilab.common.shellutils import ASK |
44 from logilab.common.shellutils import ASK |
45 from logilab.common.configuration import merge_options |
45 from logilab.common.configuration import merge_options |
46 from logilab.common.deprecation import deprecated |
46 from logilab.common.deprecation import deprecated |
47 |
47 |
48 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage |
48 from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage |
49 from cubicweb.utils import support_args |
49 from cubicweb.utils import admincnx |
50 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS |
50 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CWDEV, CONFIGURATIONS |
51 from cubicweb.toolsutils import Command, rm, create_dir, underline_title |
51 from cubicweb.toolsutils import Command, rm, create_dir, underline_title |
52 from cubicweb.__pkginfo__ import version |
52 from cubicweb.__pkginfo__ import version |
53 |
53 |
54 # don't check duplicated commands, it occurs when reloading site_cubicweb |
54 # don't check duplicated commands, it occurs when reloading site_cubicweb |
866 directly give URI as instance id instead', |
866 directly give URI as instance id instead', |
867 'group': 'remote' |
867 'group': 'remote' |
868 }), |
868 }), |
869 ) |
869 ) |
870 |
870 |
871 def _handle_inmemory(self, appid): |
871 def _get_mih(self, appid): |
872 """ returns migration context handler & shutdown function """ |
872 """ returns migration context handler & shutdown function """ |
873 config = cwcfg.config_for(appid) |
873 config = cwcfg.config_for(appid) |
874 if self.config.ext_sources: |
874 if self.config.ext_sources: |
875 assert not self.config.system_only |
875 assert not self.config.system_only |
876 sources = self.config.ext_sources |
876 sources = self.config.ext_sources |
878 sources = ('system',) |
878 sources = ('system',) |
879 else: |
879 else: |
880 sources = ('all',) |
880 sources = ('all',) |
881 config.set_sources_mode(sources) |
881 config.set_sources_mode(sources) |
882 config.repairing = self.config.force |
882 config.repairing = self.config.force |
|
883 cnx = admincnx(appid) |
883 mih = config.migration_handler() |
884 mih = config.migration_handler() |
884 return mih, lambda: mih.shutdown() |
885 return mih, lambda: mih.shutdown() |
885 |
|
886 def _handle_networked(self, appuri): |
|
887 """ returns migration context handler & shutdown function """ |
|
888 from cubicweb import AuthenticationError |
|
889 from cubicweb.repoapi import connect, get_repository |
|
890 from cubicweb.server.utils import manager_userpasswd |
|
891 from cubicweb.server.migractions import ServerMigrationHelper |
|
892 while True: |
|
893 try: |
|
894 login, pwd = manager_userpasswd(msg=None) |
|
895 repo = get_repository(appuri) |
|
896 cnx = connect(repo, login=login, password=pwd, mulcnx=False) |
|
897 except AuthenticationError as ex: |
|
898 print(ex) |
|
899 except (KeyboardInterrupt, EOFError): |
|
900 print() |
|
901 sys.exit(0) |
|
902 else: |
|
903 break |
|
904 cnx.load_appobjects() |
|
905 repo = cnx._repo |
|
906 mih = ServerMigrationHelper(None, repo=repo, cnx=cnx, verbosity=0, |
|
907 # hack so it don't try to load fs schema |
|
908 schema=1) |
|
909 return mih, lambda: cnx.close() |
|
910 |
886 |
911 def run(self, args): |
887 def run(self, args): |
912 appuri = args.pop(0) |
888 appuri = args.pop(0) |
913 if self.config.repo_uri: |
889 if self.config.repo_uri: |
914 warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id', |
890 warn('[3.16] --repo-uri option is deprecated, directly give the URI as instance id', |
915 DeprecationWarning) |
891 DeprecationWarning) |
916 if urlparse(self.config.repo_uri).scheme == 'inmemory': |
892 if urlparse(self.config.repo_uri).scheme == 'inmemory': |
917 appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri) |
893 appuri = '%s/%s' % (self.config.repo_uri.rstrip('/'), appuri) |
918 |
894 |
919 from cubicweb.utils import parse_repo_uri |
895 mih, shutdown_callback = self._get_mih(appuri) |
920 protocol, hostport, appid = parse_repo_uri(appuri) |
|
921 if protocol == 'inmemory': |
|
922 mih, shutdown_callback = self._handle_inmemory(appid) |
|
923 else: |
|
924 mih, shutdown_callback = self._handle_networked(appuri) |
|
925 try: |
896 try: |
926 with mih.cnx: |
897 with mih.cnx: |
927 with mih.cnx.security_enabled(False, False): |
898 with mih.cnx.security_enabled(False, False): |
928 if args: |
899 if args: |
929 # use cmdline parser to access left/right attributes only |
900 # use cmdline parser to access left/right attributes only |