author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 14:33:37 +0200 | |
changeset 2476 | 1294a6bdf3bf |
parent 2458 | 4d114865098f |
child 2589 | 92f2bc945261 |
permissions | -rw-r--r-- |
0 | 1 |
"""Server subcube of cubicweb : defines objects used only on the server |
2 |
(repository) side |
|
3 |
||
4 |
This module contains functions to initialize a new repository. |
|
5 |
||
6 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 8 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
9 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 10 |
""" |
11 |
__docformat__ = "restructuredtext en" |
|
12 |
||
13 |
import sys |
|
14 |
from os.path import join, exists |
|
15 |
||
16 |
from logilab.common.modutils import LazyObject |
|
17 |
||
18 |
# server debugging flag |
|
19 |
DEBUG = False |
|
20 |
||
21 |
def init_repository(config, interactive=True, drop=False, vreg=None): |
|
22 |
"""initialise a repository database by creating tables add filling them |
|
23 |
with the minimal set of entities (ie at least the schema, base groups and |
|
24 |
a initial user) |
|
25 |
""" |
|
26 |
from glob import glob |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
27 |
from yams import BASE_GROUPS |
0 | 28 |
from cubicweb.dbapi import in_memory_cnx |
29 |
from cubicweb.server.repository import Repository |
|
30 |
from cubicweb.server.utils import manager_userpasswd |
|
31 |
from cubicweb.server.sqlutils import sqlexec, sqlschema, sqldropschema |
|
32 |
# configuration to avoid db schema loading and user'state checking |
|
33 |
# on connection |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
34 |
read_instance_schema = config.read_instance_schema |
0 | 35 |
bootstrap_schema = config.bootstrap_schema |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
36 |
config.read_instance_schema = False |
0 | 37 |
config.creating = True |
38 |
config.bootstrap_schema = True |
|
39 |
config.consider_user_state = False |
|
40 |
config.set_language = False |
|
41 |
# only enable the system source at initialization time + admin which is not |
|
42 |
# an actual source but contains initial manager account information |
|
43 |
config.enabled_sources = ('system', 'admin') |
|
44 |
repo = Repository(config, vreg=vreg) |
|
45 |
assert len(repo.sources) == 1, repo.sources |
|
46 |
schema = repo.schema |
|
47 |
sourcescfg = config.sources() |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
48 |
_title = '-> creating tables ' |
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
49 |
print _title, |
0 | 50 |
source = sourcescfg['system'] |
51 |
driver = source['db-driver'] |
|
52 |
sqlcnx = repo.system_source.get_connection() |
|
53 |
sqlcursor = sqlcnx.cursor() |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
54 |
execute = sqlcursor.execute |
0 | 55 |
if drop: |
56 |
dropsql = sqldropschema(schema, driver) |
|
57 |
try: |
|
58 |
sqlexec(dropsql, execute) |
|
59 |
except Exception, ex: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
60 |
print '-> drop failed, skipped (%s).' % ex |
0 | 61 |
sqlcnx.rollback() |
62 |
# schema entities and relations tables |
|
63 |
# can't skip entities table even if system source doesn't support them, |
|
64 |
# they are used sometimes by generated sql. Keeping them empty is much |
|
65 |
# simpler than fixing this... |
|
66 |
if sqlcnx.logged_user != source['db-user']: |
|
67 |
schemasql = sqlschema(schema, driver, user=source['db-user']) |
|
68 |
else: |
|
69 |
schemasql = sqlschema(schema, driver) |
|
70 |
#skip_entities=[str(e) for e in schema.entities() |
|
71 |
# if not repo.system_source.support_entity(str(e))]) |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
72 |
sqlexec(schemasql, execute, pbtitle=_title) |
0 | 73 |
# install additional driver specific sql files |
74 |
for fpath in glob(join(config.schemas_lib_dir(), '*.sql.%s' % driver)): |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
75 |
print '-> installing', fpath |
0 | 76 |
sqlexec(open(fpath).read(), execute, False, delimiter=';;') |
77 |
for directory in config.cubes_path(): |
|
78 |
for fpath in glob(join(directory, 'schema', '*.sql.%s' % driver)): |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
79 |
print '-> installing', fpath |
0 | 80 |
sqlexec(open(fpath).read(), execute, False, delimiter=';;') |
81 |
sqlcursor.close() |
|
82 |
sqlcnx.commit() |
|
83 |
sqlcnx.close() |
|
84 |
session = repo.internal_session() |
|
85 |
try: |
|
86 |
login = unicode(sourcescfg['admin']['login']) |
|
87 |
pwd = sourcescfg['admin']['password'] |
|
88 |
except KeyError: |
|
89 |
if interactive: |
|
90 |
msg = 'enter login and password of the initial manager account' |
|
91 |
login, pwd = manager_userpasswd(msg=msg, confirm=True) |
|
92 |
else: |
|
93 |
login, pwd = unicode(source['db-user']), source['db-password'] |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
94 |
print '-> inserting default user and default groups.' |
0 | 95 |
needisfix = [] |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
96 |
for group in BASE_GROUPS: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
97 |
rset = session.execute('INSERT CWGroup X: X name %(name)s', |
0 | 98 |
{'name': unicode(group)}) |
99 |
needisfix.append( (rset.rows[0][0], rset.description[0][0]) ) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
100 |
rset = session.execute('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s', |
0 | 101 |
{'login': login, 'pwd': pwd}) |
102 |
needisfix.append( (rset.rows[0][0], rset.description[0][0]) ) |
|
103 |
session.execute('SET U in_group G WHERE G name "managers"') |
|
104 |
session.commit() |
|
105 |
# reloging using the admin user |
|
106 |
config._cubes = None # avoid assertion error |
|
107 |
repo, cnx = in_memory_cnx(config, login, pwd) |
|
108 |
assert len(repo.sources) == 1, repo.sources |
|
109 |
handler = config.migration_handler(schema, interactive=False, |
|
110 |
cnx=cnx, repo=repo) |
|
111 |
initialize_schema(config, schema, handler) |
|
112 |
# admin user and groups have been added before schema entities, fix the 'is' |
|
113 |
# relation |
|
114 |
for eid, etype in needisfix: |
|
115 |
handler.session.unsafe_execute('SET X is E WHERE X eid %(x)s, E name %(name)s', |
|
116 |
{'x': eid, 'name': etype}, 'x') |
|
480
71376fda9b36
insert missing is_instance_of relations during db-init
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
117 |
handler.session.unsafe_execute('SET X is_instance_of E WHERE X eid %(x)s, E name %(name)s', |
71376fda9b36
insert missing is_instance_of relations during db-init
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
118 |
{'x': eid, 'name': etype}, 'x') |
0 | 119 |
# insert versions |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
120 |
handler.cmd_add_entity('CWProperty', pkey=u'system.version.cubicweb', |
0 | 121 |
value=unicode(config.cubicweb_version())) |
122 |
for cube in config.cubes(): |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
123 |
handler.cmd_add_entity('CWProperty', |
0 | 124 |
pkey=u'system.version.%s' % cube.lower(), |
125 |
value=unicode(config.cube_version(cube))) |
|
126 |
# yoo ! |
|
127 |
cnx.commit() |
|
128 |
config.enabled_sources = None |
|
129 |
for uri, source_config in config.sources().items(): |
|
130 |
if uri in ('admin', 'system'): |
|
131 |
# not an actual source or init_creating already called |
|
132 |
continue |
|
133 |
source = repo.get_source(uri, source_config) |
|
134 |
source.init_creating() |
|
135 |
cnx.commit() |
|
136 |
cnx.close() |
|
137 |
session.close() |
|
138 |
# restore initial configuration |
|
139 |
config.creating = False |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
140 |
config.read_instance_schema = read_instance_schema |
0 | 141 |
config.bootstrap_schema = bootstrap_schema |
142 |
config.consider_user_state = True |
|
143 |
config.set_language = True |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
144 |
print '-> database for instance %s initialized.' % config.appid |
0 | 145 |
|
146 |
||
147 |
def initialize_schema(config, schema, mhandler, event='create'): |
|
148 |
from cubicweb.server.schemaserial import serialize_schema |
|
149 |
paths = [p for p in config.cubes_path() + [config.apphome] |
|
150 |
if exists(join(p, 'migration'))] |
|
151 |
# execute cubicweb's pre<event> script |
|
152 |
mhandler.exec_event_script('pre%s' % event) |
|
153 |
# execute cubes pre<event> script if any |
|
154 |
for path in reversed(paths): |
|
155 |
mhandler.exec_event_script('pre%s' % event, path) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
156 |
# enter instance'schema into the database |
0 | 157 |
serialize_schema(mhandler.rqlcursor, schema) |
158 |
# execute cubicweb's post<event> script |
|
159 |
mhandler.exec_event_script('post%s' % event) |
|
160 |
# execute cubes'post<event> script if any |
|
161 |
for path in reversed(paths): |
|
162 |
mhandler.exec_event_script('post%s' % event, path) |
|
163 |
||
164 |
def set_debug(debugmode): |
|
165 |
global DEBUG |
|
166 |
DEBUG = debugmode |
|
167 |
||
168 |
def debugged(func): |
|
169 |
"""decorator to activate debug mode""" |
|
170 |
def wrapped(*args, **kwargs): |
|
171 |
global DEBUG |
|
172 |
DEBUG = True |
|
173 |
try: |
|
174 |
return func(*args, **kwargs) |
|
175 |
finally: |
|
176 |
DEBUG = False |
|
177 |
return wrapped |
|
178 |
||
179 |
# sqlite'stored procedures have to be registered at connexion opening time |
|
180 |
SQL_CONNECT_HOOKS = {} |
|
181 |
||
182 |
# add to this set relations which should have their add security checking done |
|
183 |
# *BEFORE* adding the actual relation (done after by default) |
|
184 |
BEFORE_ADD_RELATIONS = set(('owned_by',)) |
|
185 |
||
186 |
# add to this set relations which should have their add security checking done |
|
187 |
# *at COMMIT TIME* (done after by default) |
|
188 |
ON_COMMIT_ADD_RELATIONS = set(()) |
|
189 |
||
190 |
# available sources registry |
|
191 |
SOURCE_TYPES = {'native': LazyObject('cubicweb.server.sources.native', 'NativeSQLSource'), |
|
192 |
# XXX private sources installed by an external cube |
|
193 |
'pyrorql': LazyObject('cubicweb.server.sources.pyrorql', 'PyroRQLSource'), |
|
194 |
'ldapuser': LazyObject('cubicweb.server.sources.ldapuser', 'LDAPUserSource'), |
|
195 |
} |