cwctl.py
changeset 3720 5376aaadd16b
parent 3674 387d51af966d
parent 3715 e3ccadb126d7
child 4021 280c910c8710
equal deleted inserted replaced
3678:29f74716fd70 3720:5376aaadd16b
   683             StartInstanceCommand().start_instance(appid)
   683             StartInstanceCommand().start_instance(appid)
   684         print
   684         print
   685 
   685 
   686 
   686 
   687 class ShellCommand(Command):
   687 class ShellCommand(Command):
   688     """Run an interactive migration shell. This is a python shell with
   688     """Run an interactive migration shell on an instance. This is a python shell
   689     enhanced migration commands predefined in the namespace. An additional
   689     with enhanced migration commands predefined in the namespace. An additional
   690     argument may be given corresponding to a file containing commands to
   690     argument may be given corresponding to a file containing commands to execute
   691     execute in batch mode.
   691     in batch mode.
       
   692 
       
   693     By default it will connect to a local instance using an in memory
       
   694     connection, unless -P option is specified, in which case you will be
       
   695     connected through pyro. In the later case, you won't have access to
       
   696     repository internals (session, etc...) so most migration commands won't be
       
   697     available.
   692 
   698 
   693     <instance>
   699     <instance>
   694       the identifier of the instance to connect.
   700       the identifier of the instance to connect.
   695     """
   701     """
   696     name = 'shell'
   702     name = 'shell'
   697     arguments = '<instance> [batch command file]'
   703     arguments = '<instance> [batch command file]'
   698     options = (
   704     options = (
   699         ('system-only',
   705         ('system-only',
   700          {'short': 'S', 'action' : 'store_true',
   706          {'short': 'S', 'action' : 'store_true',
   701           'default': False,
       
   702           'help': 'only connect to the system source when the instance is '
   707           'help': 'only connect to the system source when the instance is '
   703           'using multiple sources. You can\'t use this option and the '
   708           'using multiple sources. You can\'t use this option and the '
   704           '--ext-sources option at the same time.'}),
   709           '--ext-sources option at the same time.',
       
   710           'group': 'local'
       
   711          }),
   705 
   712 
   706         ('ext-sources',
   713         ('ext-sources',
   707          {'short': 'E', 'type' : 'csv', 'metavar': '<sources>',
   714          {'short': 'E', 'type' : 'csv', 'metavar': '<sources>',
   708           'default': None,
       
   709           'help': "For multisources instances, specify to which sources the \
   715           'help': "For multisources instances, specify to which sources the \
   710 repository should connect to for upgrading. When unspecified or 'all' given, \
   716 repository should connect to for upgrading. When unspecified or 'all' given, \
   711 will connect to all defined sources. If 'migration' is given, appropriate \
   717 will connect to all defined sources. If 'migration' is given, appropriate \
   712 sources for migration will be automatically selected.",
   718 sources for migration will be automatically selected.",
       
   719           'group': 'local'
   713           }),
   720           }),
   714 
   721 
   715         ('force',
   722         ('force',
   716          {'short': 'f', 'action' : 'store_true',
   723          {'short': 'f', 'action' : 'store_true',
   717           'default' : False,
   724           'help': 'don\'t check instance is up to date.',
   718           'help': 'don\'t check instance is up to date.'}
   725           'group': 'local'
   719          ),
   726           }),
       
   727 
       
   728         ('pyro',
       
   729          {'short': 'P', 'action' : 'store_true',
       
   730           'help': 'connect to a running instance through Pyro.',
       
   731           'group': 'remote',
       
   732           }),
       
   733         ('pyro-ns-host',
       
   734          {'short': 'H', 'type' : 'string', 'metavar': '<host[:port]>',
       
   735           'help': 'Pyro name server host. If not set, will be detected by '
       
   736           'using a broadcast query.',
       
   737           'group': 'remote'
       
   738           }),
   720         )
   739         )
   721 
   740 
   722     def run(self, args):
   741     def run(self, args):
   723         appid = pop_arg(args, 99, msg="No instance specified !")
   742         appid = pop_arg(args, 99, msg="No instance specified !")
   724         config = cwcfg.config_for(appid)
   743         if self.config.pyro:
   725         if self.config.ext_sources:
   744             from cubicweb import AuthenticationError
   726             assert not self.config.system_only
   745             from cubicweb.dbapi import connect
   727             sources = self.config.ext_sources
   746             from cubicweb.server.utils import manager_userpasswd
   728         elif self.config.system_only:
   747             from cubicweb.server.migractions import ServerMigrationHelper
   729             sources = ('system',)
   748             while True:
       
   749                 try:
       
   750                     login, pwd = manager_userpasswd(msg=None)
       
   751                     cnx = connect(appid, login=login, password=pwd,
       
   752                                   host=self.config.pyro_ns_host, mulcnx=False)
       
   753                 except AuthenticationError, ex:
       
   754                     print ex
       
   755                 except (KeyboardInterrupt, EOFError):
       
   756                     print
       
   757                     sys.exit(0)
       
   758                 else:
       
   759                     break
       
   760             cnx.load_appobjects()
       
   761             repo = cnx._repo
       
   762             mih = ServerMigrationHelper(None, repo=repo, cnx=cnx,
       
   763                                          # hack so it don't try to load fs schema
       
   764                                         schema=1)
   730         else:
   765         else:
   731             sources = ('all',)
   766             config = cwcfg.config_for(appid)
   732         config.set_sources_mode(sources)
   767             if self.config.ext_sources:
   733         config.repairing = self.config.force
   768                 assert not self.config.system_only
   734         mih = config.migration_handler()
   769                 sources = self.config.ext_sources
   735         if args:
   770             elif self.config.system_only:
   736             for arg in args:
   771                 sources = ('system',)
   737                 mih.process_script(arg)
   772             else:
   738         else:
   773                 sources = ('all',)
   739             mih.interactive_shell()
   774             config.set_sources_mode(sources)
   740         mih.shutdown()
   775             config.repairing = self.config.force
       
   776             mih = config.migration_handler()
       
   777         try:
       
   778             if args:
       
   779                 for arg in args:
       
   780                     mih.cmd_process_script(arg)
       
   781             else:
       
   782                 mih.interactive_shell()
       
   783         finally:
       
   784             if not self.config.pyro:
       
   785                 mih.shutdown()
       
   786             else:
       
   787                 cnx.close()
   741 
   788 
   742 
   789 
   743 class RecompileInstanceCatalogsCommand(InstanceCommand):
   790 class RecompileInstanceCatalogsCommand(InstanceCommand):
   744     """Recompile i18n catalogs for instances.
   791     """Recompile i18n catalogs for instances.
   745 
   792