author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 31 Jul 2009 14:25:30 +0200 | |
changeset 2589 | 92f2bc945261 |
parent 2476 | 1294a6bdf3bf |
parent 2588 | 3a590ff82e99 |
child 2593 | 16d9419a4a79 |
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.' |
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
|
95 |
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
|
96 |
rset = session.execute('INSERT CWGroup X: X name %(name)s', |
0 | 97 |
{'name': unicode(group)}) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
98 |
rset = session.execute('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s', |
0 | 99 |
{'login': login, 'pwd': pwd}) |
100 |
session.execute('SET U in_group G WHERE G name "managers"') |
|
101 |
session.commit() |
|
102 |
# reloging using the admin user |
|
103 |
config._cubes = None # avoid assertion error |
|
104 |
repo, cnx = in_memory_cnx(config, login, pwd) |
|
105 |
assert len(repo.sources) == 1, repo.sources |
|
106 |
handler = config.migration_handler(schema, interactive=False, |
|
107 |
cnx=cnx, repo=repo) |
|
108 |
initialize_schema(config, schema, handler) |
|
109 |
# insert versions |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
110 |
handler.cmd_add_entity('CWProperty', pkey=u'system.version.cubicweb', |
0 | 111 |
value=unicode(config.cubicweb_version())) |
112 |
for cube in config.cubes(): |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
113 |
handler.cmd_add_entity('CWProperty', |
0 | 114 |
pkey=u'system.version.%s' % cube.lower(), |
115 |
value=unicode(config.cube_version(cube))) |
|
2588
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
116 |
# some entities have been added before schema entities, fix the 'is' and |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
117 |
# 'is_instance_of' relations |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
118 |
for rtype in ('is', 'is_instance_of'): |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
119 |
handler.sqlexec( |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
120 |
'INSERT INTO %s_relation ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
121 |
'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
122 |
'WHERE X.type=ET.cw_name AND NOT EXISTS(' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
123 |
' SELECT 1 from is_relation ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
124 |
' WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype) |
0 | 125 |
# yoo ! |
126 |
cnx.commit() |
|
127 |
config.enabled_sources = None |
|
128 |
for uri, source_config in config.sources().items(): |
|
129 |
if uri in ('admin', 'system'): |
|
130 |
# not an actual source or init_creating already called |
|
131 |
continue |
|
132 |
source = repo.get_source(uri, source_config) |
|
133 |
source.init_creating() |
|
134 |
cnx.commit() |
|
135 |
cnx.close() |
|
136 |
session.close() |
|
137 |
# restore initial configuration |
|
138 |
config.creating = False |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
139 |
config.read_instance_schema = read_instance_schema |
0 | 140 |
config.bootstrap_schema = bootstrap_schema |
141 |
config.consider_user_state = True |
|
142 |
config.set_language = True |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
143 |
print '-> database for instance %s initialized.' % config.appid |
0 | 144 |
|
145 |
||
146 |
def initialize_schema(config, schema, mhandler, event='create'): |
|
147 |
from cubicweb.server.schemaserial import serialize_schema |
|
148 |
paths = [p for p in config.cubes_path() + [config.apphome] |
|
149 |
if exists(join(p, 'migration'))] |
|
150 |
# execute cubicweb's pre<event> script |
|
151 |
mhandler.exec_event_script('pre%s' % event) |
|
152 |
# execute cubes pre<event> script if any |
|
153 |
for path in reversed(paths): |
|
154 |
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
|
155 |
# enter instance'schema into the database |
0 | 156 |
serialize_schema(mhandler.rqlcursor, schema) |
157 |
# execute cubicweb's post<event> script |
|
158 |
mhandler.exec_event_script('post%s' % event) |
|
159 |
# execute cubes'post<event> script if any |
|
160 |
for path in reversed(paths): |
|
161 |
mhandler.exec_event_script('post%s' % event, path) |
|
162 |
||
163 |
def set_debug(debugmode): |
|
164 |
global DEBUG |
|
165 |
DEBUG = debugmode |
|
166 |
||
167 |
def debugged(func): |
|
168 |
"""decorator to activate debug mode""" |
|
169 |
def wrapped(*args, **kwargs): |
|
170 |
global DEBUG |
|
171 |
DEBUG = True |
|
172 |
try: |
|
173 |
return func(*args, **kwargs) |
|
174 |
finally: |
|
175 |
DEBUG = False |
|
176 |
return wrapped |
|
177 |
||
178 |
# sqlite'stored procedures have to be registered at connexion opening time |
|
179 |
SQL_CONNECT_HOOKS = {} |
|
180 |
||
181 |
# add to this set relations which should have their add security checking done |
|
182 |
# *BEFORE* adding the actual relation (done after by default) |
|
183 |
BEFORE_ADD_RELATIONS = set(('owned_by',)) |
|
184 |
||
185 |
# add to this set relations which should have their add security checking done |
|
186 |
# *at COMMIT TIME* (done after by default) |
|
187 |
ON_COMMIT_ADD_RELATIONS = set(()) |
|
188 |
||
189 |
# available sources registry |
|
190 |
SOURCE_TYPES = {'native': LazyObject('cubicweb.server.sources.native', 'NativeSQLSource'), |
|
191 |
# XXX private sources installed by an external cube |
|
192 |
'pyrorql': LazyObject('cubicweb.server.sources.pyrorql', 'PyroRQLSource'), |
|
193 |
'ldapuser': LazyObject('cubicweb.server.sources.ldapuser', 'LDAPUserSource'), |
|
194 |
} |