author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 30 Mar 2011 11:17:21 +0200 | |
branch | stable |
changeset 7140 | ba51dac1115d |
parent 7121 | c2badb6de3fe |
child 7155 | 4bab50b02927 |
permissions | -rw-r--r-- |
7121 | 1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5410
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5967 | 18 |
"""cubicweb-ctl commands and command handlers specific to the repository""" |
0 | 19 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
20 |
__docformat__ = 'restructuredtext en' |
0 | 21 |
|
4718
3dc3ad02d091
avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4670
diff
changeset
|
22 |
# *ctl module should limit the number of import to be imported as quickly as |
3dc3ad02d091
avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4670
diff
changeset
|
23 |
# possible (for cubicweb-ctl reactivity, necessary for instance for usable bash |
3dc3ad02d091
avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4670
diff
changeset
|
24 |
# completion). So import locally in command helpers. |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
25 |
import sys |
0 | 26 |
import os |
27 |
||
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
28 |
from logilab.common import nullobject |
2107
6c4a4c514ac2
add source to sources configuration when adding a cube defining a source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2106
diff
changeset
|
29 |
from logilab.common.configuration import Configuration |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
30 |
from logilab.common.shellutils import ASK, generate_password |
0 | 31 |
|
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
32 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
33 |
from cubicweb.toolsutils import Command, CommandHandler, underline_title |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
34 |
from cubicweb.cwctl import CWCTL, check_options_consistency |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
35 |
from cubicweb.server import SOURCE_TYPES |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
36 |
from cubicweb.server.serverconfig import ( |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
37 |
USER_OPTIONS, ServerConfiguration, SourceConfiguration, |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
38 |
ask_source_config, generate_source_config) |
0 | 39 |
|
40 |
# utility functions ########################################################### |
|
41 |
||
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
42 |
def source_cnx(source, dbname=None, special_privs=False, interactive=True): |
0 | 43 |
"""open and return a connection to the system database defined in the |
44 |
given server.serverconfig |
|
45 |
""" |
|
46 |
from getpass import getpass |
|
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
47 |
from logilab.database import get_connection, get_db_helper |
4218
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
48 |
dbhost = source.get('db-host') |
0 | 49 |
if dbname is None: |
50 |
dbname = source['db-name'] |
|
51 |
driver = source['db-driver'] |
|
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
52 |
dbhelper = get_db_helper(driver) |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
53 |
if interactive: |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
54 |
print '-> connecting to %s database' % driver, |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
55 |
if dbhost: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
56 |
print '%s@%s' % (dbname, dbhost), |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
57 |
else: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
58 |
print dbname, |
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
59 |
if dbhelper.users_support: |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
60 |
if not interactive or (not special_privs and source.get('db-user')): |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
61 |
user = source.get('db-user', os.environ.get('USER', '')) |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
62 |
if interactive: |
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
63 |
print 'as', user |
6860
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
64 |
password = source.get('db-password') |
0 | 65 |
else: |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
66 |
print |
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
67 |
if special_privs: |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
68 |
print 'WARNING' |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
69 |
print ('the user will need the following special access rights ' |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
70 |
'on the database:') |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
71 |
print special_privs |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
72 |
print |
6072
9d83b214066a
[db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6060
diff
changeset
|
73 |
default_user = source.get('db-user', os.environ.get('USER', '')) |
9d83b214066a
[db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6060
diff
changeset
|
74 |
user = raw_input('Connect as user ? [%r]: ' % default_user) |
6860
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
75 |
user = user.strip() or default_user |
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
76 |
if user == source.get('db-user'): |
6939
8fa55cf2a8cb
[c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6867
diff
changeset
|
77 |
password = source.get('db-password') |
6072
9d83b214066a
[db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6060
diff
changeset
|
78 |
else: |
9d83b214066a
[db-create] fix bug introduced by 6060:353bec342e2a (we ask for user while in non verbose mode)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6060
diff
changeset
|
79 |
password = getpass('password: ') |
6060
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
80 |
else: |
353bec342e2a
[db create] don't ask user/password if the underlying db doesn't support it (eg sqlite)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5990
diff
changeset
|
81 |
user = password = None |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
82 |
extra_args = source.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
83 |
extra = extra_args and {'extra_args': extra_args} or {} |
4837
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
84 |
cnx = get_connection(driver, dbhost, dbname, user, password=password, |
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
85 |
port=source.get('db-port'), |
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
86 |
**extra) |
4845 | 87 |
try: |
88 |
cnx.logged_user = user |
|
89 |
except AttributeError: |
|
90 |
# C object, __slots__ |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
91 |
from logilab.database import _SimpleConnectionWrapper |
4845 | 92 |
cnx = _SimpleConnectionWrapper(cnx) |
93 |
cnx.logged_user = user |
|
4837
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
94 |
return cnx |
0 | 95 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
96 |
def system_source_cnx(source, dbms_system_base=False, |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
97 |
special_privs='CREATE/DROP DATABASE', interactive=True): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
98 |
"""shortcut to get a connextion to the instance system database |
0 | 99 |
defined in the given config. If <dbms_system_base> is True, |
100 |
connect to the dbms system database instead (for task such as |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
101 |
create/drop the instance database) |
0 | 102 |
""" |
103 |
if dbms_system_base: |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
104 |
from logilab.database import get_db_helper |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
105 |
system_db = get_db_helper(source['db-driver']).system_database() |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
106 |
return source_cnx(source, system_db, special_privs=special_privs, |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
107 |
interactive=interactive) |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
108 |
return source_cnx(source, special_privs=special_privs, |
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
109 |
interactive=interactive) |
0 | 110 |
|
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
111 |
def _db_sys_cnx(source, special_privs, interactive=True): |
6860
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
112 |
"""return a connection on the RDMS system table (to create/drop a user or a |
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
113 |
database) |
0 | 114 |
""" |
2417
18a14c23413c
should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2107
diff
changeset
|
115 |
import logilab.common as lgp |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
116 |
from logilab.database import get_db_helper |
2417
18a14c23413c
should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2107
diff
changeset
|
117 |
lgp.USE_MX_DATETIME = False |
0 | 118 |
driver = source['db-driver'] |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
119 |
helper = get_db_helper(driver) |
0 | 120 |
# connect on the dbms system base to create our base |
6860
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
121 |
cnx = system_source_cnx(source, True, special_privs=special_privs, |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
122 |
interactive=interactive) |
0 | 123 |
# disable autocommit (isolation_level(1)) because DROP and |
124 |
# CREATE DATABASE can't be executed in a transaction |
|
125 |
try: |
|
126 |
cnx.set_isolation_level(0) |
|
127 |
except AttributeError: |
|
128 |
# set_isolation_level() is psycopg specific |
|
129 |
pass |
|
130 |
return cnx |
|
1469 | 131 |
|
0 | 132 |
def repo_cnx(config): |
133 |
"""return a in-memory repository and a db api connection it""" |
|
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7036
diff
changeset
|
134 |
from cubicweb.dbapi import in_memory_repo_cnx |
0 | 135 |
from cubicweb.server.utils import manager_userpasswd |
136 |
try: |
|
137 |
login = config.sources()['admin']['login'] |
|
138 |
pwd = config.sources()['admin']['password'] |
|
139 |
except KeyError: |
|
140 |
login, pwd = manager_userpasswd() |
|
141 |
while True: |
|
142 |
try: |
|
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7036
diff
changeset
|
143 |
return in_memory_repo_cnx(config, login, password=pwd) |
0 | 144 |
except AuthenticationError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
145 |
print '-> Error: wrong user/password.' |
1682
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
146 |
# reset cubes else we'll have an assertion error on next retry |
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
147 |
config._cubes = None |
0 | 148 |
login, pwd = manager_userpasswd() |
1469 | 149 |
|
3904
dc7d315165fc
default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3893
diff
changeset
|
150 |
|
0 | 151 |
# repository specific command handlers ######################################## |
152 |
||
153 |
class RepositoryCreateHandler(CommandHandler): |
|
154 |
cmdname = 'create' |
|
155 |
cfgname = 'repository' |
|
156 |
||
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
157 |
def bootstrap(self, cubes, automatic=False, inputlevel=0): |
5967 | 158 |
"""create an instance by copying files from the given cube and by asking |
159 |
information necessary to build required configuration files |
|
0 | 160 |
""" |
161 |
config = self.config |
|
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
162 |
if not automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
163 |
print underline_title('Configuring the repository') |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
164 |
config.input_config('email', inputlevel) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
165 |
# ask for pyro configuration if pyro is activated and we're not |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
166 |
# using a all-in-one config, in which case this is done by the web |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
167 |
# side command handler |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
168 |
if config.pyro_enabled() and config.name != 'all-in-one': |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
169 |
config.input_config('pyro', inputlevel) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
170 |
print '\n'+underline_title('Configuring the sources') |
0 | 171 |
sourcesfile = config.sources_file() |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
172 |
# hack to make Method('default_instance_id') usable in db option defs |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
173 |
# (in native.py) |
6299
51a9494b5efb
[sqlite] quick hacks to fix #1251873 and #1251874
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6184
diff
changeset
|
174 |
sconfig = SourceConfiguration(config, |
3904
dc7d315165fc
default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3893
diff
changeset
|
175 |
options=SOURCE_TYPES['native'].options) |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
176 |
if not automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
177 |
sconfig.input_config(inputlevel=inputlevel) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
178 |
print |
0 | 179 |
sourcescfg = {'system': sconfig} |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
180 |
if automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
181 |
# XXX modify a copy |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
182 |
password = generate_password() |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
183 |
print 'Administration account is admin / %s' % password |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
184 |
USER_OPTIONS[1][1]['default'] = password |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
185 |
sconfig = Configuration(options=USER_OPTIONS) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
186 |
else: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
187 |
sconfig = Configuration(options=USER_OPTIONS) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
188 |
sconfig.input_config(inputlevel=inputlevel) |
0 | 189 |
sourcescfg['admin'] = sconfig |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
190 |
config.write_sources_file(sourcescfg) |
0 | 191 |
# remember selected cubes for later initialization of the database |
192 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 193 |
|
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
194 |
def postcreate(self, automatic=False, inputlevel=0): |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
195 |
if automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
196 |
CWCTL.run(['db-create', '--automatic', self.config.appid]) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
197 |
elif ASK.confirm('Run db-create to create the system database ?'): |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
198 |
CWCTL.run(['db-create', '--config-level', str(inputlevel), |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
199 |
self.config.appid]) |
0 | 200 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
201 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
202 |
'"cubicweb-ctl db-create %s".' % self.config.appid) |
1469 | 203 |
|
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
204 |
ERROR = nullobject() |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
205 |
|
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
206 |
def confirm_on_error_or_die(msg, func, *args, **kwargs): |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
207 |
try: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
208 |
return func(*args, **kwargs) |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
209 |
except Exception, ex: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
210 |
print 'ERROR', ex |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
211 |
if not ASK.confirm('An error occurred while %s. Continue anyway?' % msg): |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
212 |
raise ExecutionError(str(ex)) |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
213 |
return ERROR |
0 | 214 |
|
215 |
class RepositoryDeleteHandler(CommandHandler): |
|
216 |
cmdname = 'delete' |
|
217 |
cfgname = 'repository' |
|
218 |
||
219 |
def cleanup(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
220 |
"""remove instance's configuration and database""" |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
221 |
from logilab.database import get_db_helper |
0 | 222 |
source = self.config.sources()['system'] |
223 |
dbname = source['db-name'] |
|
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
224 |
helper = get_db_helper(source['db-driver']) |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
225 |
if ASK.confirm('Delete database %s ?' % dbname): |
6299
51a9494b5efb
[sqlite] quick hacks to fix #1251873 and #1251874
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6184
diff
changeset
|
226 |
if source['db-driver'] == 'sqlite': |
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
227 |
if confirm_on_error_or_die( |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
228 |
'deleting database file %s' % dbname, |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
229 |
os.unlink, source['db-name']) is not ERROR: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
230 |
print '-> database %s dropped.' % dbname |
6299
51a9494b5efb
[sqlite] quick hacks to fix #1251873 and #1251874
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6184
diff
changeset
|
231 |
return |
0 | 232 |
user = source['db-user'] or None |
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
233 |
cnx = confirm_on_error_or_die('connecting to database %s' % dbname, |
6939
8fa55cf2a8cb
[c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6867
diff
changeset
|
234 |
_db_sys_cnx, source, 'DROP DATABASE') |
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
235 |
if cnx is ERROR: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
236 |
return |
0 | 237 |
cursor = cnx.cursor() |
238 |
try: |
|
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
239 |
if confirm_on_error_or_die( |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
240 |
'dropping database %s' % dbname, |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
241 |
cursor.execute, 'DROP DATABASE "%s"' % dbname) is not ERROR: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
242 |
print '-> database %s dropped.' % dbname |
0 | 243 |
# XXX should check we are not connected as user |
244 |
if user and helper.users_support and \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
245 |
ASK.confirm('Delete user %s ?' % user, default_is_yes=False): |
6867
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
246 |
if confirm_on_error_or_die( |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
247 |
'dropping user %s' % user, |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
248 |
cursor.execute, 'DROP USER %s' % user) is not ERROR: |
f691757792f9
[c-c delete] closes #1433117 and other flavours where we don't want to crash on error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6863
diff
changeset
|
249 |
print '-> user %s dropped.' % user |
0 | 250 |
cnx.commit() |
251 |
except: |
|
252 |
cnx.rollback() |
|
253 |
raise |
|
254 |
||
1469 | 255 |
|
0 | 256 |
class RepositoryStartHandler(CommandHandler): |
257 |
cmdname = 'start' |
|
258 |
cfgname = 'repository' |
|
259 |
||
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
260 |
def start_server(self, config): |
0 | 261 |
command = ['cubicweb-ctl start-repository '] |
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
262 |
if config.debugmode: |
0 | 263 |
command.append('--debug') |
5990
6f3c77c79a88
[c-c start repo] config log's threshold is upper case while option accepts lower case. This break c-c start of repository instances.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5967
diff
changeset
|
264 |
command.append('--loglevel %s' % config['log-threshold'].lower()) |
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
265 |
command.append(config.appid) |
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2969
diff
changeset
|
266 |
os.system(' '.join(command)) |
6778
7dd4835d5198
properly tell we've started an instance (close #1391262)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6700
diff
changeset
|
267 |
return 1 |
1469 | 268 |
|
0 | 269 |
|
270 |
class RepositoryStopHandler(CommandHandler): |
|
271 |
cmdname = 'stop' |
|
272 |
cfgname = 'repository' |
|
273 |
||
274 |
def poststop(self): |
|
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
275 |
"""if pyro is enabled, ensure the repository is correctly unregistered |
0 | 276 |
""" |
277 |
if self.config.pyro_enabled(): |
|
278 |
from cubicweb.server.repository import pyro_unregister |
|
279 |
pyro_unregister(self.config) |
|
1469 | 280 |
|
0 | 281 |
|
282 |
# repository specific commands ################################################ |
|
3954
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
283 |
|
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
284 |
def createdb(helper, source, dbcnx, cursor, **kwargs): |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
285 |
if dbcnx.logged_user != source['db-user']: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
286 |
helper.create_database(cursor, source['db-name'], source['db-user'], |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
287 |
source['db-encoding'], **kwargs) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
288 |
else: |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
289 |
helper.create_database(cursor, source['db-name'], |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
290 |
dbencoding=source['db-encoding'], **kwargs) |
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
291 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
292 |
class CreateInstanceDBCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
293 |
"""Create the system database of an instance (run after 'create'). |
1469 | 294 |
|
0 | 295 |
You will be prompted for a login / password to use to connect to |
296 |
the system database. The given user should have almost all rights |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
297 |
on the database (ie a super user on the DBMS allowed to create |
0 | 298 |
database, users, languages...). |
299 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
300 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
301 |
the identifier of the instance to initialize. |
0 | 302 |
""" |
303 |
name = 'db-create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
304 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
305 |
min_args = max_args = 1 |
0 | 306 |
options = ( |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
307 |
('automatic', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
308 |
{'short': 'a', 'action' : 'store_true', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
309 |
'default': False, |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
310 |
'help': 'automatic mode: never ask and use default answer to every ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
311 |
'question. this may require that your login match a database super ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
312 |
'user (allowed to create database & all).', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
313 |
}), |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
314 |
('config-level', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
315 |
{'short': 'l', 'type' : 'int', 'metavar': '<level>', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
316 |
'default': 0, |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
317 |
'help': 'configuration level (0..2): 0 will ask for essential ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
318 |
'configuration parameters only while 2 will ask for all parameters', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
319 |
}), |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
320 |
('create-db', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
321 |
{'short': 'c', 'type': 'yn', 'metavar': '<y or n>', |
0 | 322 |
'default': True, |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
323 |
'help': 'create the database (yes by default)' |
7112
bb27cc300040
[db connection] fix db connections method: verbose should be named interactive, it's not a question of verbosity but wether question should be asked or not
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7061
diff
changeset
|
324 |
}), |
0 | 325 |
) |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
326 |
|
0 | 327 |
def run(self, args): |
328 |
"""run the command with its specific arguments""" |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
329 |
from logilab.database import get_db_helper |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
330 |
check_options_consistency(self.config) |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
331 |
automatic = self.get('automatic') |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
332 |
appid = args.pop() |
0 | 333 |
config = ServerConfiguration.config_for(appid) |
334 |
source = config.sources()['system'] |
|
4197
5446c74189b6
quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4180
diff
changeset
|
335 |
dbname = source['db-name'] |
0 | 336 |
driver = source['db-driver'] |
4831
c5aec27c1bf7
[repo] use logilab.db instead of lgc.adbh/lgc.db/lgc.sqlgen/indexer, test new date extranction functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
337 |
helper = get_db_helper(driver) |
4197
5446c74189b6
quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4180
diff
changeset
|
338 |
if driver == 'sqlite': |
5585
e1cbf6b304ea
[sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5553
diff
changeset
|
339 |
if os.path.exists(dbname) and ( |
e1cbf6b304ea
[sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5553
diff
changeset
|
340 |
automatic or |
e1cbf6b304ea
[sqlite] fix crash on db-create with sqlite abd unexistent database due to missing paren in logical expression (closes #909604)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5553
diff
changeset
|
341 |
ASK.confirm('Database %s already exists. Drop it?' % dbname)): |
4197
5446c74189b6
quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4180
diff
changeset
|
342 |
os.unlink(dbname) |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
343 |
elif self.config.create_db: |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
344 |
print '\n'+underline_title('Creating the system database') |
0 | 345 |
# connect on the dbms system base to create our base |
6939
8fa55cf2a8cb
[c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6867
diff
changeset
|
346 |
dbcnx = _db_sys_cnx(source, 'CREATE/DROP DATABASE and / or USER', |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
347 |
interactive=not automatic) |
0 | 348 |
cursor = dbcnx.cursor() |
349 |
try: |
|
350 |
if helper.users_support: |
|
351 |
user = source['db-user'] |
|
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
352 |
if not helper.user_exists(cursor, user) and (automatic or \ |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
353 |
ASK.confirm('Create db user %s ?' % user, default_is_yes=False)): |
6939
8fa55cf2a8cb
[c-c] fix crash on delete instance + potential others when user don't specify db user's password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6867
diff
changeset
|
354 |
helper.create_user(source['db-user'], source.get('db-password')) |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
355 |
print '-> user %s created.' % user |
0 | 356 |
if dbname in helper.list_databases(cursor): |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
357 |
if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
0 | 358 |
cursor.execute('DROP DATABASE %s' % dbname) |
359 |
else: |
|
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
360 |
print ('you may want to run "cubicweb-ctl db-init ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
361 |
'--drop %s" manually to continue.' % config.appid) |
0 | 362 |
return |
5754
51179e0bb250
[test] support for test on postgres database using the same mecanism as sqlite: one template database generated when necessary + actual test database created from the template
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5696
diff
changeset
|
363 |
createdb(helper, source, dbcnx, cursor) |
0 | 364 |
dbcnx.commit() |
4197
5446c74189b6
quick and dirty fix to avoid crash when running db-create command with an sqlite source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4180
diff
changeset
|
365 |
print '-> database %s created.' % dbname |
0 | 366 |
except: |
367 |
dbcnx.rollback() |
|
368 |
raise |
|
6860
a0dbf6135c72
[db-create] we should always ask db user that should be used when special privileges are required
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6779
diff
changeset
|
369 |
cnx = system_source_cnx(source, special_privs='CREATE LANGUAGE', |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
370 |
interactive=not automatic) |
0 | 371 |
cursor = cnx.cursor() |
4886
18a1735c283c
[db creation] fix some bugs introduced by the migration to logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4848
diff
changeset
|
372 |
helper.init_fti_extensions(cursor) |
1469 | 373 |
# postgres specific stuff |
0 | 374 |
if driver == 'postgres': |
375 |
# install plpythonu/plpgsql language if not installed by the cube |
|
3303 | 376 |
langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2969
diff
changeset
|
377 |
for extlang in langs: |
0 | 378 |
helper.create_language(cursor, extlang) |
379 |
cursor.close() |
|
380 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
381 |
print '-> database for instance %s created and necessary extensions installed.' % appid |
0 | 382 |
print |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
383 |
if automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
384 |
CWCTL.run(['db-init', '--automatic', '--config-level', '0', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
385 |
config.appid]) |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
386 |
elif ASK.confirm('Run db-init to initialize the system database ?'): |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
387 |
CWCTL.run(['db-init', '--config-level', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
388 |
str(self.config.config_level), config.appid]) |
0 | 389 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
390 |
print ('-> nevermind, you can do it later with ' |
3893 | 391 |
'"cubicweb-ctl db-init %s".' % config.appid) |
0 | 392 |
|
1469 | 393 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
394 |
class InitInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
395 |
"""Initialize the system database of an instance (run after 'db-create'). |
1469 | 396 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
397 |
Notice this will be done using user specified in the sources files, so this |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
398 |
user should have the create tables grant permissions on the database. |
0 | 399 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
400 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
401 |
the identifier of the instance to initialize. |
0 | 402 |
""" |
403 |
name = 'db-init' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
404 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
405 |
min_args = max_args = 1 |
0 | 406 |
options = ( |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
407 |
('automatic', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
408 |
{'short': 'a', 'action' : 'store_true', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
409 |
'default': False, |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
410 |
'help': 'automatic mode: never ask and use default answer to every ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
411 |
'question.', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
412 |
}), |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
413 |
('config-level', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
414 |
{'short': 'l', 'type': 'int', 'default': 1, |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
415 |
'help': 'level threshold for questions asked when configuring ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
416 |
'another source' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
417 |
}), |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
418 |
('drop', |
0 | 419 |
{'short': 'd', 'action': 'store_true', |
420 |
'default': False, |
|
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
421 |
'help': 'insert drop statements to remove previously existant ' |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
422 |
'tables, indexes... (no by default)' |
7030
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
423 |
}), |
0 | 424 |
) |
425 |
||
426 |
def run(self, args): |
|
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
427 |
check_options_consistency(self.config) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
428 |
print '\n'+underline_title('Initializing the system database') |
0 | 429 |
from cubicweb.server import init_repository |
5586
aabe082fee02
[c-c] use logilab.database, not lgc.db
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5585
diff
changeset
|
430 |
from logilab.database import get_connection |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
431 |
appid = args[0] |
0 | 432 |
config = ServerConfiguration.config_for(appid) |
3954
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
433 |
try: |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
434 |
system = config.sources()['system'] |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
435 |
extra_args=system.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
436 |
extra = extra_args and {'extra_args': extra_args} or {} |
3954
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
437 |
get_connection( |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
438 |
system['db-driver'], database=system['db-name'], |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
439 |
host=system.get('db-host'), port=system.get('db-port'), |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5033
diff
changeset
|
440 |
user=system.get('db-user'), password=system.get('db-password'), |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
441 |
**extra) |
3954
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
442 |
except Exception, ex: |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
443 |
raise ConfigurationError( |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
444 |
'You seem to have provided wrong connection information in '\ |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
445 |
'the %s file. Resolve this first (error: %s).' |
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
446 |
% (config.sources_file(), str(ex).strip())) |
0 | 447 |
init_repository(config, drop=self.config.drop) |
7140
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
448 |
if not self.config.automatic: |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
449 |
while ASK.confirm('Enter another source ?', default_is_yes=False): |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
450 |
CWCTL.run(['add-source', '--config-level', |
ba51dac1115d
[c-c create] unification of c-c create and its subcommands handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7121
diff
changeset
|
451 |
str(self.config.config_level), config.appid]) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
452 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
453 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
454 |
class AddSourceCommand(Command): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
455 |
"""Add a data source to an instance. |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
456 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
457 |
<instance> |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
458 |
the identifier of the instance to initialize. |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
459 |
""" |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
460 |
name = 'add-source' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
461 |
arguments = '<instance>' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
462 |
min_args = max_args = 1 |
7030
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
463 |
options = ( |
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
464 |
('config-level', |
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
465 |
{'short': 'l', 'type': 'int', 'default': 1, |
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
466 |
'help': 'level threshold for questions asked when configuring another source' |
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
467 |
}), |
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
468 |
) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
469 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
470 |
def run(self, args): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
471 |
appid = args[0] |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
472 |
config = ServerConfiguration.config_for(appid) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
473 |
config.quick_start = True |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
474 |
repo, cnx = repo_cnx(config) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
475 |
req = cnx.request() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
476 |
used = set(n for n, in req.execute('Any SN WHERE S is CWSource, S name SN')) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
477 |
cubes = repo.get_cubes() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
478 |
while True: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
479 |
type = raw_input('source type (%s): ' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
480 |
% ', '.join(sorted(SOURCE_TYPES))) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
481 |
if type not in SOURCE_TYPES: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
482 |
print '-> unknown source type, use one of the available types.' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
483 |
continue |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
484 |
sourcemodule = SOURCE_TYPES[type].module |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
485 |
if not sourcemodule.startswith('cubicweb.'): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
486 |
# module names look like cubes.mycube.themodule |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
487 |
sourcecube = SOURCE_TYPES[type].module.split('.', 2)[1] |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
488 |
# if the source adapter is coming from an external component, |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
489 |
# ensure it's specified in used cubes |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
490 |
if not sourcecube in cubes: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
491 |
print ('-> this source type require the %s cube which is ' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
492 |
'not used by the instance.') |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
493 |
continue |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
494 |
break |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
495 |
while True: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
496 |
sourceuri = raw_input('source identifier (a unique name used to ' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
497 |
'tell sources apart): ').strip() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
498 |
if not sourceuri: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
499 |
print '-> mandatory.' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
500 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
501 |
sourceuri = unicode(sourceuri, sys.stdin.encoding) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
502 |
if sourceuri in used: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
503 |
print '-> uri already used, choose another one.' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
504 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
505 |
break |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
506 |
# XXX configurable inputlevel |
7030
d48c6b0158b9
allow configuring the level of the configuration settings for cw-ctl add-source and db-init (which may call add-source)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6939
diff
changeset
|
507 |
sconfig = ask_source_config(config, type, inputlevel=self.config.config_level) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
508 |
cfgstr = unicode(generate_source_config(sconfig), sys.stdin.encoding) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
509 |
req.create_entity('CWSource', name=sourceuri, |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
510 |
type=unicode(type), config=cfgstr) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
511 |
cnx.commit() |
0 | 512 |
|
513 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
514 |
class GrantUserOnInstanceCommand(Command): |
0 | 515 |
"""Grant a database user on a repository system database. |
1469 | 516 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
517 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
518 |
the identifier of the instance |
0 | 519 |
<user> |
520 |
the database's user requiring grant access |
|
521 |
""" |
|
522 |
name = 'db-grant-user' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
523 |
arguments = '<instance> <user>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
524 |
min_args = max_args = 2 |
0 | 525 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
526 |
('set-owner', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
527 |
{'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 528 |
'default' : False, |
529 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
530 |
), |
|
531 |
) |
|
532 |
def run(self, args): |
|
533 |
"""run the command with its specific arguments""" |
|
534 |
from cubicweb.server.sqlutils import sqlexec, sqlgrants |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
535 |
appid, user = args |
0 | 536 |
config = ServerConfiguration.config_for(appid) |
537 |
source = config.sources()['system'] |
|
538 |
set_owner = self.config.set_owner |
|
539 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
540 |
cursor = cnx.cursor() |
|
541 |
schema = config.load_schema() |
|
542 |
try: |
|
543 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
544 |
set_owner=set_owner), cursor) |
|
545 |
except Exception, ex: |
|
546 |
cnx.rollback() |
|
547 |
import traceback |
|
548 |
traceback.print_exc() |
|
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6072
diff
changeset
|
549 |
print '-> an error occurred:', ex |
0 | 550 |
else: |
551 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
552 |
print '-> rights granted to %s on instance %s.' % (appid, user) |
0 | 553 |
|
3954
56a8b14d68aa
properly fail if user has entered wrong connection information for the system database, closes #549136
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3904
diff
changeset
|
554 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
555 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
556 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
557 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
558 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
559 |
the identifier of the instance |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
560 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
561 |
name = 'reset-admin-pwd' |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
562 |
arguments = '<instance>' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
563 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
564 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
565 |
"""run the command with its specific arguments""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
566 |
from cubicweb.server.utils import crypt_password, manager_userpasswd |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
567 |
appid = args[0] |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
568 |
config = ServerConfiguration.config_for(appid) |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
569 |
sourcescfg = config.read_sources_file() |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
570 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
571 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
572 |
except KeyError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
573 |
print '-> Error: could not get cubicweb administrator login.' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
574 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
575 |
cnx = source_cnx(sourcescfg['system']) |
6379
3f67f7ea5632
[R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6351
diff
changeset
|
576 |
driver = sourcescfg['system']['db-driver'] |
3f67f7ea5632
[R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6351
diff
changeset
|
577 |
from logilab.database import get_db_helper |
3f67f7ea5632
[R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6351
diff
changeset
|
578 |
dbhelper = get_db_helper(driver) |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
579 |
cursor = cnx.cursor() |
4670
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
580 |
# check admin exists |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
581 |
cursor.execute("SELECT * FROM cw_CWUser WHERE cw_login=%(l)s", |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
582 |
{'l': adminlogin}) |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
583 |
if not cursor.fetchall(): |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
584 |
print ("-> error: admin user %r specified in sources doesn't exist " |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
585 |
"in the database" % adminlogin) |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
586 |
print " fix your sources file before running this command" |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
587 |
cnx.close() |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
588 |
sys.exit(1) |
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
589 |
# ask for a new password |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
590 |
_, passwd = manager_userpasswd(adminlogin, confirm=True, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
591 |
passwdmsg='new password for %s' % adminlogin) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
592 |
try: |
4670
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
593 |
cursor.execute("UPDATE cw_CWUser SET cw_upassword=%(p)s WHERE cw_login=%(l)s", |
6379
3f67f7ea5632
[R] use dbhelper.binary_value to process passwords and other Bytes fields
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6351
diff
changeset
|
594 |
{'p': dbhelper.binary_value(crypt_password(passwd)), 'l': adminlogin}) |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
595 |
sconfig = Configuration(options=USER_OPTIONS) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
596 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
597 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
598 |
sourcescfg['admin'] = sconfig |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
599 |
config.write_sources_file(sourcescfg) |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
600 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
601 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
602 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
603 |
traceback.print_exc() |
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6072
diff
changeset
|
604 |
print '-> an error occurred:', ex |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
605 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
606 |
cnx.commit() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
607 |
print '-> password reset, sources file regenerated.' |
4670
48ba6f820fae
fix #615379 by checking admin user exists instead of making think everything is fine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
608 |
cnx.close() |
0 | 609 |
|
1469 | 610 |
|
0 | 611 |
class StartRepositoryCommand(Command): |
6700 | 612 |
"""Start a CubicWeb RQL server for a given instance. |
1469 | 613 |
|
0 | 614 |
The server will be accessible through pyro |
615 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
616 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
617 |
the identifier of the instance to initialize. |
0 | 618 |
""" |
619 |
name = 'start-repository' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
620 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
621 |
min_args = max_args = 1 |
0 | 622 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
623 |
('debug', |
0 | 624 |
{'short': 'D', 'action' : 'store_true', |
625 |
'help': 'start server in debug mode.'}), |
|
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
626 |
('loglevel', |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
627 |
{'short': 'l', 'type' : 'choice', 'metavar': '<log level>', |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
628 |
'default': None, 'choices': ('debug', 'info', 'warning', 'error'), |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
629 |
'help': 'debug if -D is set, error otherwise', |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
630 |
}), |
0 | 631 |
) |
632 |
||
633 |
def run(self, args): |
|
5553 | 634 |
from logilab.common.daemon import daemonize |
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
635 |
from cubicweb.cwctl import init_cmdline_log_threshold |
0 | 636 |
from cubicweb.server.server import RepositoryServer |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
637 |
appid = args[0] |
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
638 |
debug = self['debug'] |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
639 |
if sys.platform == 'win32' and not debug: |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
640 |
from logging import getLogger |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
641 |
logger = getLogger('cubicweb.ctl') |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
642 |
logger.info('Forcing debug mode on win32 platform') |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
643 |
debug = True |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
644 |
config = ServerConfiguration.config_for(appid, debugmode=debug) |
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
645 |
init_cmdline_log_threshold(config, self['loglevel']) |
0 | 646 |
# create the server |
5814
51cc4b61f9ae
[repo] fix 'c-c start --loglevel LEVEL' for repository only configuration. Closes #1058269.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5754
diff
changeset
|
647 |
server = RepositoryServer(config) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
648 |
# ensure the directory where the pid-file should be set exists (for |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
649 |
# instance /var/run/cubicweb may be deleted on computer restart) |
5692
59ceed788bdb
[repo server] fix startup bug, lgc.daemon.daemonize return 1 for the parent process, not -1
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5586
diff
changeset
|
650 |
pidfile = config['pid-file'] |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
651 |
piddir = os.path.dirname(pidfile) |
5692
59ceed788bdb
[repo server] fix startup bug, lgc.daemon.daemonize return 1 for the parent process, not -1
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5586
diff
changeset
|
652 |
# go ! (don't daemonize in debug mode) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
653 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
654 |
os.makedirs(piddir) |
6779
accf5978a440
[daemon] make process umask configurable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6778
diff
changeset
|
655 |
if not debug and daemonize(pidfile, umask=config['umask']): |
0 | 656 |
return |
657 |
uid = config['uid'] |
|
658 |
if uid is not None: |
|
659 |
try: |
|
660 |
uid = int(uid) |
|
661 |
except ValueError: |
|
662 |
from pwd import getpwnam |
|
663 |
uid = getpwnam(uid).pw_uid |
|
664 |
os.setuid(uid) |
|
665 |
server.install_sig_handlers() |
|
666 |
server.connect(config['host'], 0) |
|
667 |
server.run() |
|
668 |
||
669 |
||
670 |
def _remote_dump(host, appid, output, sudo=False): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
671 |
# XXX generate unique/portable file name |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
672 |
from datetime import date |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
673 |
filename = '%s-%s.tgz' % (appid, date.today().strftime('%Y-%m-%d')) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
674 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid) |
0 | 675 |
if sudo: |
676 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
677 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
678 |
print dmpcmd |
|
679 |
if os.system(dmpcmd): |
|
680 |
raise ExecutionError('Error while dumping the database') |
|
681 |
if output is None: |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
682 |
output = filename |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
683 |
cmd = 'scp %s:/tmp/%s %s' % (host, filename, output) |
0 | 684 |
print cmd |
685 |
if os.system(cmd): |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
686 |
raise ExecutionError('Error while retrieving the dump at /tmp/%s' % filename) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
687 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename) |
0 | 688 |
print rmcmd |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
689 |
if os.system(rmcmd) and not ASK.confirm( |
6109
47d9c0e0f7b7
integrate Celso's work on translation file: proper/complete spanish translation, fixed some typos in french translation, occured -> occurred fix in various places
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6072
diff
changeset
|
690 |
'An error occurred while deleting remote dump at /tmp/%s. ' |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
691 |
'Continue anyway?' % filename): |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
692 |
raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename) |
0 | 693 |
|
694 |
def _local_dump(appid, output): |
|
695 |
config = ServerConfiguration.config_for(appid) |
|
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5033
diff
changeset
|
696 |
config.quick_start = True |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5033
diff
changeset
|
697 |
mih = config.migration_handler(connect=False, verbosity=1) |
0 | 698 |
mih.backup_database(output, askconfirm=False) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
699 |
mih.shutdown() |
0 | 700 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
701 |
def _local_restore(appid, backupfile, drop, systemonly=True): |
0 | 702 |
config = ServerConfiguration.config_for(appid) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
703 |
config.verbosity = 1 # else we won't be asked for confirmation on problems |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5033
diff
changeset
|
704 |
config.quick_start = True |
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5033
diff
changeset
|
705 |
mih = config.migration_handler(connect=False, verbosity=1) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
706 |
mih.restore_database(backupfile, drop, systemonly, askconfirm=False) |
0 | 707 |
repo = mih.repo_connect() |
708 |
# version of the database |
|
709 |
dbversions = repo.get_versions() |
|
710 |
mih.shutdown() |
|
711 |
if not dbversions: |
|
712 |
print "bad or missing version information in the database, don't upgrade file system" |
|
713 |
return |
|
714 |
# version of installed software |
|
715 |
eversion = dbversions['cubicweb'] |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
716 |
status = instance_status(config, eversion, dbversions) |
0 | 717 |
# * database version > installed software |
718 |
if status == 'needsoftupgrade': |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
719 |
print "** The database of %s is more recent than the installed software!" % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
720 |
print "** Upgrade your software, then migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
721 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 722 |
return |
723 |
# * database version < installed software, an upgrade will be necessary |
|
724 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
725 |
elif status == 'needapplupgrade': |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
726 |
print "** The database of %s is older than the installed software." % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
727 |
print "** Migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
728 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 729 |
return |
730 |
# * database version = installed software, database version = instance fs version |
|
731 |
# ok! |
|
732 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
733 |
def instance_status(config, cubicwebapplversion, vcconf): |
0 | 734 |
cubicwebversion = config.cubicweb_version() |
735 |
if cubicwebapplversion > cubicwebversion: |
|
736 |
return 'needsoftupgrade' |
|
737 |
if cubicwebapplversion < cubicwebversion: |
|
738 |
return 'needapplupgrade' |
|
739 |
for cube in config.cubes(): |
|
740 |
try: |
|
741 |
softversion = config.cube_version(cube) |
|
742 |
except ConfigurationError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
743 |
print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube |
0 | 744 |
continue |
745 |
try: |
|
746 |
applversion = vcconf[cube] |
|
747 |
except KeyError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
748 |
print '-> Error: no cube version information for %s in version configuration.' % cube |
1469 | 749 |
continue |
0 | 750 |
if softversion == applversion: |
751 |
continue |
|
752 |
if softversion > applversion: |
|
753 |
return 'needsoftupgrade' |
|
754 |
elif softversion < applversion: |
|
755 |
return 'needapplupgrade' |
|
756 |
return None |
|
1469 | 757 |
|
0 | 758 |
|
759 |
class DBDumpCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
760 |
"""Backup the system database of an instance. |
1469 | 761 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
762 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
763 |
the identifier of the instance to backup |
0 | 764 |
format [[user@]host:]appname |
765 |
""" |
|
766 |
name = 'db-dump' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
767 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
768 |
min_args = max_args = 1 |
0 | 769 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
770 |
('output', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
771 |
{'short': 'o', 'type' : 'string', 'metavar' : '<file>', |
0 | 772 |
'default' : None, |
773 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
774 |
), |
|
775 |
('sudo', |
|
776 |
{'short': 's', 'action' : 'store_true', |
|
777 |
'default' : False, |
|
778 |
'help': 'Use sudo on the remote host.'} |
|
779 |
), |
|
780 |
) |
|
781 |
||
782 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
783 |
appid = args[0] |
0 | 784 |
if ':' in appid: |
785 |
host, appid = appid.split(':') |
|
786 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
787 |
else: |
|
788 |
_local_dump(appid, self.config.output) |
|
789 |
||
790 |
||
791 |
class DBRestoreCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
792 |
"""Restore the system database of an instance. |
1469 | 793 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
794 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
795 |
the identifier of the instance to restore |
0 | 796 |
""" |
797 |
name = 'db-restore' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
798 |
arguments = '<instance> <backupfile>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
799 |
min_args = max_args = 2 |
0 | 800 |
|
801 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
802 |
('no-drop', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
803 |
{'short': 'n', 'action' : 'store_true', 'default' : False, |
0 | 804 |
'help': 'for some reason the database doesn\'t exist and so ' |
805 |
'should not be dropped.'} |
|
806 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
807 |
('restore-all', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
808 |
{'short': 'r', 'action' : 'store_true', 'default' : False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
809 |
'help': 'restore everything, eg not only the system source database ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
810 |
'but also data for all sources supporting backup/restore and custom ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
811 |
'instance data. In that case, <backupfile> is expected to be the ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
812 |
'timestamp of the backup to restore, not a file'} |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
813 |
), |
0 | 814 |
) |
815 |
||
816 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
817 |
appid, backupfile = args |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
818 |
_local_restore(appid, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
819 |
drop=not self.config.no_drop, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
820 |
systemonly=not self.config.restore_all) |
0 | 821 |
|
822 |
||
823 |
class DBCopyCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
824 |
"""Copy the system database of an instance (backup and restore). |
1469 | 825 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
826 |
<src-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
827 |
the identifier of the instance to backup |
0 | 828 |
format [[user@]host:]appname |
829 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
830 |
<dest-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
831 |
the identifier of the instance to restore |
0 | 832 |
""" |
833 |
name = 'db-copy' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
834 |
arguments = '<src-instance> <dest-instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
835 |
min_args = max_args = 2 |
0 | 836 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
837 |
('no-drop', |
1469 | 838 |
{'short': 'n', 'action' : 'store_true', |
0 | 839 |
'default' : False, |
840 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
841 |
'should not be dropped.'} |
|
842 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
843 |
('keep-dump', |
0 | 844 |
{'short': 'k', 'action' : 'store_true', |
845 |
'default' : False, |
|
846 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
847 |
), |
|
848 |
('sudo', |
|
849 |
{'short': 's', 'action' : 'store_true', |
|
850 |
'default' : False, |
|
851 |
'help': 'Use sudo on the remote host.'} |
|
852 |
), |
|
853 |
) |
|
854 |
||
855 |
def run(self, args): |
|
856 |
import tempfile |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
857 |
srcappid, destappid = args |
3148 | 858 |
fd, output = tempfile.mkstemp() |
859 |
os.close(fd) |
|
0 | 860 |
if ':' in srcappid: |
861 |
host, srcappid = srcappid.split(':') |
|
862 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
863 |
else: |
|
864 |
_local_dump(srcappid, output) |
|
865 |
_local_restore(destappid, output, not self.config.no_drop) |
|
866 |
if self.config.keep_dump: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
867 |
print '-> you can get the dump file at', output |
0 | 868 |
else: |
869 |
os.remove(output) |
|
870 |
||
1469 | 871 |
|
0 | 872 |
class CheckRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
873 |
"""Check integrity of the system database of an instance. |
1469 | 874 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
875 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
876 |
the identifier of the instance to check |
0 | 877 |
""" |
878 |
name = 'db-check' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
879 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
880 |
min_args = max_args = 1 |
0 | 881 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
882 |
('checks', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
883 |
{'short': 'c', 'type' : 'csv', 'metavar' : '<check list>', |
7036
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7030
diff
changeset
|
884 |
'default' : ('entities', 'relations', |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7030
diff
changeset
|
885 |
'mandatory_relations', 'mandatory_attributes', |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7030
diff
changeset
|
886 |
'metadata', 'schema', 'text_index'), |
0 | 887 |
'help': 'Comma separated list of check to run. By default run all \ |
7036
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7030
diff
changeset
|
888 |
checks, i.e. entities, relations, mandatory_relations, mandatory_attributes, \ |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7030
diff
changeset
|
889 |
metadata, text_index and schema.'} |
0 | 890 |
), |
1469 | 891 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
892 |
('autofix', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
893 |
{'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 894 |
'default' : False, |
895 |
'help': 'Automatically correct integrity problems if this option \ |
|
896 |
is set to "y" or "yes", else only display them'} |
|
897 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
898 |
('reindex', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
899 |
{'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 900 |
'default' : False, |
901 |
'help': 're-indexes the database for full text search if this \ |
|
902 |
option is set to "y" or "yes" (may be long for large database).'} |
|
903 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
904 |
('force', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
905 |
{'short': 'f', 'action' : 'store_true', |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
906 |
'default' : False, |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
907 |
'help': 'don\'t check instance is up to date.'} |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
908 |
), |
1469 | 909 |
|
0 | 910 |
) |
911 |
||
912 |
def run(self, args): |
|
6184
da580218a5b3
[c-c db-check] add missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6138
diff
changeset
|
913 |
from cubicweb.server.checkintegrity import check |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
914 |
appid = args[0] |
0 | 915 |
config = ServerConfiguration.config_for(appid) |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
916 |
config.repairing = self.config.force |
0 | 917 |
repo, cnx = repo_cnx(config) |
918 |
check(repo, cnx, |
|
919 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
920 |
||
921 |
||
922 |
class RebuildFTICommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
923 |
"""Rebuild the full-text index of the system database of an instance. |
1469 | 924 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
925 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
926 |
the identifier of the instance to rebuild |
0 | 927 |
""" |
928 |
name = 'db-rebuild-fti' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
929 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
930 |
min_args = max_args = 1 |
0 | 931 |
|
932 |
def run(self, args): |
|
933 |
from cubicweb.server.checkintegrity import reindex_entities |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
934 |
appid = args[0] |
0 | 935 |
config = ServerConfiguration.config_for(appid) |
936 |
repo, cnx = repo_cnx(config) |
|
937 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
938 |
reindex_entities(repo.schema, session) |
|
939 |
cnx.commit() |
|
940 |
||
1469 | 941 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
942 |
class SynchronizeInstanceSchemaCommand(Command): |
0 | 943 |
"""Synchronize persistent schema with cube schema. |
1469 | 944 |
|
0 | 945 |
Will synchronize common stuff between the cube schema and the |
946 |
actual persistent schema, but will not add/remove any entity or relation. |
|
947 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
948 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
949 |
the identifier of the instance to synchronize. |
0 | 950 |
""" |
951 |
name = 'schema-sync' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
952 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
953 |
min_args = max_args = 1 |
0 | 954 |
|
955 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
956 |
appid = args[0] |
0 | 957 |
config = ServerConfiguration.config_for(appid) |
958 |
mih = config.migration_handler() |
|
959 |
mih.cmd_synchronize_schema() |
|
960 |
||
961 |
||
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
962 |
for cmdclass in (CreateInstanceDBCommand, InitInstanceCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
963 |
GrantUserOnInstanceCommand, ResetAdminPasswordCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
964 |
StartRepositoryCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
965 |
DBDumpCommand, DBRestoreCommand, DBCopyCommand, |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
966 |
AddSourceCommand, CheckRepositoryCommand, RebuildFTICommand, |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
967 |
SynchronizeInstanceSchemaCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
968 |
): |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
969 |
CWCTL.register(cmdclass) |