130 # set_isolation_level() is psycopg specific |
130 # set_isolation_level() is psycopg specific |
131 set_isolation_level(0) |
131 set_isolation_level(0) |
132 return cnx |
132 return cnx |
133 |
133 |
134 def repo_cnx(config): |
134 def repo_cnx(config): |
135 """return a in-memory repository and a db api connection it""" |
135 """return a in-memory repository and a repoapi connection to it""" |
136 from cubicweb.dbapi import in_memory_repo_cnx |
136 from cubicweb import repoapi |
137 from cubicweb.server.utils import manager_userpasswd |
137 from cubicweb.server.utils import manager_userpasswd |
138 try: |
138 try: |
139 login = config.default_admin_config['login'] |
139 login = config.default_admin_config['login'] |
140 pwd = config.default_admin_config['password'] |
140 pwd = config.default_admin_config['password'] |
141 except KeyError: |
141 except KeyError: |
142 login, pwd = manager_userpasswd() |
142 login, pwd = manager_userpasswd() |
143 while True: |
143 while True: |
144 try: |
144 try: |
145 return in_memory_repo_cnx(config, login, password=pwd) |
145 repo = repoapi.get_repository(config=config) |
|
146 cnx = repoapi.connect(repo, login, password=pwd) |
|
147 return repo, cnx |
146 except AuthenticationError: |
148 except AuthenticationError: |
147 print '-> Error: wrong user/password.' |
149 print '-> Error: wrong user/password.' |
148 # reset cubes else we'll have an assertion error on next retry |
150 # reset cubes else we'll have an assertion error on next retry |
149 config._cubes = None |
151 config._cubes = None |
150 login, pwd = manager_userpasswd() |
152 login, pwd = manager_userpasswd() |
480 def run(self, args): |
482 def run(self, args): |
481 appid = args[0] |
483 appid = args[0] |
482 config = ServerConfiguration.config_for(appid) |
484 config = ServerConfiguration.config_for(appid) |
483 config.quick_start = True |
485 config.quick_start = True |
484 repo, cnx = repo_cnx(config) |
486 repo, cnx = repo_cnx(config) |
485 req = cnx.request() |
487 with cnx: |
486 used = set(n for n, in req.execute('Any SN WHERE S is CWSource, S name SN')) |
488 used = set(n for n, in cnx.execute('Any SN WHERE S is CWSource, S name SN')) |
487 cubes = repo.get_cubes() |
489 cubes = repo.get_cubes() |
488 while True: |
490 while True: |
489 type = raw_input('source type (%s): ' |
491 type = raw_input('source type (%s): ' |
490 % ', '.join(sorted(SOURCE_TYPES))) |
492 % ', '.join(sorted(SOURCE_TYPES))) |
491 if type not in SOURCE_TYPES: |
493 if type not in SOURCE_TYPES: |
492 print '-> unknown source type, use one of the available types.' |
494 print '-> unknown source type, use one of the available types.' |
493 continue |
|
494 sourcemodule = SOURCE_TYPES[type].module |
|
495 if not sourcemodule.startswith('cubicweb.'): |
|
496 # module names look like cubes.mycube.themodule |
|
497 sourcecube = SOURCE_TYPES[type].module.split('.', 2)[1] |
|
498 # if the source adapter is coming from an external component, |
|
499 # ensure it's specified in used cubes |
|
500 if not sourcecube in cubes: |
|
501 print ('-> this source type require the %s cube which is ' |
|
502 'not used by the instance.') |
|
503 continue |
495 continue |
504 break |
496 sourcemodule = SOURCE_TYPES[type].module |
505 while True: |
497 if not sourcemodule.startswith('cubicweb.'): |
506 sourceuri = raw_input('source identifier (a unique name used to ' |
498 # module names look like cubes.mycube.themodule |
507 'tell sources apart): ').strip() |
499 sourcecube = SOURCE_TYPES[type].module.split('.', 2)[1] |
508 if not sourceuri: |
500 # if the source adapter is coming from an external component, |
509 print '-> mandatory.' |
501 # ensure it's specified in used cubes |
510 else: |
502 if not sourcecube in cubes: |
511 sourceuri = unicode(sourceuri, sys.stdin.encoding) |
503 print ('-> this source type require the %s cube which is ' |
512 if sourceuri in used: |
504 'not used by the instance.') |
513 print '-> uri already used, choose another one.' |
505 continue |
|
506 break |
|
507 while True: |
|
508 sourceuri = raw_input('source identifier (a unique name used to ' |
|
509 'tell sources apart): ').strip() |
|
510 if not sourceuri: |
|
511 print '-> mandatory.' |
514 else: |
512 else: |
515 break |
513 sourceuri = unicode(sourceuri, sys.stdin.encoding) |
516 # XXX configurable inputlevel |
514 if sourceuri in used: |
517 sconfig = ask_source_config(config, type, inputlevel=self.config.config_level) |
515 print '-> uri already used, choose another one.' |
518 cfgstr = unicode(generate_source_config(sconfig), sys.stdin.encoding) |
516 else: |
519 req.create_entity('CWSource', name=sourceuri, |
517 break |
520 type=unicode(type), config=cfgstr) |
518 # XXX configurable inputlevel |
521 cnx.commit() |
519 sconfig = ask_source_config(config, type, inputlevel=self.config.config_level) |
|
520 cfgstr = unicode(generate_source_config(sconfig), sys.stdin.encoding) |
|
521 cnx.create_entity('CWSource', name=sourceuri, |
|
522 type=unicode(type), config=cfgstr) |
|
523 cnx.commit() |
522 |
524 |
523 |
525 |
524 class GrantUserOnInstanceCommand(Command): |
526 class GrantUserOnInstanceCommand(Command): |
525 """Grant a database user on a repository system database. |
527 """Grant a database user on a repository system database. |
526 |
528 |
976 from cubicweb.server.checkintegrity import check |
978 from cubicweb.server.checkintegrity import check |
977 appid = args[0] |
979 appid = args[0] |
978 config = ServerConfiguration.config_for(appid) |
980 config = ServerConfiguration.config_for(appid) |
979 config.repairing = self.config.force |
981 config.repairing = self.config.force |
980 repo, cnx = repo_cnx(config) |
982 repo, cnx = repo_cnx(config) |
981 check(repo, cnx, |
983 with cnx: |
982 self.config.checks, self.config.reindex, self.config.autofix) |
984 check(repo, cnx, |
|
985 self.config.checks, self.config.reindex, self.config.autofix) |
983 |
986 |
984 |
987 |
985 class RebuildFTICommand(Command): |
988 class RebuildFTICommand(Command): |
986 """Rebuild the full-text index of the system database of an instance. |
989 """Rebuild the full-text index of the system database of an instance. |
987 |
990 |
999 from cubicweb.server.checkintegrity import reindex_entities |
1002 from cubicweb.server.checkintegrity import reindex_entities |
1000 appid = args.pop(0) |
1003 appid = args.pop(0) |
1001 etypes = args or None |
1004 etypes = args or None |
1002 config = ServerConfiguration.config_for(appid) |
1005 config = ServerConfiguration.config_for(appid) |
1003 repo, cnx = repo_cnx(config) |
1006 repo, cnx = repo_cnx(config) |
1004 session = repo._get_session(cnx.sessionid, setcnxset=True) |
1007 with cnx: |
1005 reindex_entities(repo.schema, session, etypes=etypes) |
1008 reindex_entities(repo.schema, cnx._cnx, etypes=etypes) |
1006 cnx.commit() |
1009 cnx.commit() |
1007 |
1010 |
1008 |
1011 |
1009 class SynchronizeSourceCommand(Command): |
1012 class SynchronizeSourceCommand(Command): |
1010 """Force a source synchronization. |
1013 """Force a source synchronization. |
1011 |
1014 |
1072 from yams.diff import schema_diff |
1075 from yams.diff import schema_diff |
1073 appid = args.pop(0) |
1076 appid = args.pop(0) |
1074 diff_tool = args.pop(0) |
1077 diff_tool = args.pop(0) |
1075 config = ServerConfiguration.config_for(appid) |
1078 config = ServerConfiguration.config_for(appid) |
1076 repo, cnx = repo_cnx(config) |
1079 repo, cnx = repo_cnx(config) |
1077 session = repo._get_session(cnx.sessionid, setcnxset=True) |
1080 cnx.close() |
1078 fsschema = config.load_schema(expand_cubes=True) |
1081 fsschema = config.load_schema(expand_cubes=True) |
1079 schema_diff(fsschema, repo.schema, permissionshandler, diff_tool, ignore=('eid',)) |
1082 schema_diff(fsschema, repo.schema, permissionshandler, diff_tool, ignore=('eid',)) |
1080 |
1083 |
1081 |
1084 |
1082 for cmdclass in (CreateInstanceDBCommand, InitInstanceCommand, |
1085 for cmdclass in (CreateInstanceDBCommand, InitInstanceCommand, |