author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 28 Jun 2011 16:33:53 +0200 | |
changeset 7569 | 02c338197322 |
parent 7479 | b0603fc4ed6d |
parent 7564 | 1d64c8d33156 |
child 7577 | 9892937d9041 |
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 |
7457
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
27 |
import logging |
0 | 28 |
|
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
|
29 |
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
|
30 |
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
|
31 |
from logilab.common.shellutils import ASK, generate_password |
0 | 32 |
|
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
33 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
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
|
38 |
USER_OPTIONS, ServerConfiguration, SourceConfiguration, |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
39 |
ask_source_config, generate_source_config) |
0 | 40 |
|
41 |
# utility functions ########################################################### |
|
42 |
||
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
|
43 |
def source_cnx(source, dbname=None, special_privs=False, interactive=True): |
0 | 44 |
"""open and return a connection to the system database defined in the |
45 |
given server.serverconfig |
|
46 |
""" |
|
47 |
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
|
48 |
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
|
49 |
dbhost = source.get('db-host') |
0 | 50 |
if dbname is None: |
51 |
dbname = source['db-name'] |
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
password = source.get('db-password') |
0 | 66 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
'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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
user = password = None |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
83 |
extra_args = source.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
port=source.get('db-port'), |
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
87 |
**extra) |
4845 | 88 |
try: |
89 |
cnx.logged_user = user |
|
90 |
except AttributeError: |
|
91 |
# C object, __slots__ |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
92 |
from logilab.database import _SimpleConnectionWrapper |
4845 | 93 |
cnx = _SimpleConnectionWrapper(cnx) |
94 |
cnx.logged_user = user |
|
4837
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
95 |
return cnx |
0 | 96 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
"""shortcut to get a connextion to the instance system database |
0 | 100 |
defined in the given config. If <dbms_system_base> is True, |
101 |
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
|
102 |
create/drop the instance database) |
0 | 103 |
""" |
104 |
if dbms_system_base: |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
interactive=interactive) |
0 | 111 |
|
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
|
112 |
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
|
113 |
"""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
|
114 |
database) |
0 | 115 |
""" |
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
|
116 |
import logilab.common as lgp |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
117 |
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
|
118 |
lgp.USE_MX_DATETIME = False |
0 | 119 |
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
|
120 |
helper = get_db_helper(driver) |
0 | 121 |
# 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
|
122 |
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
|
123 |
interactive=interactive) |
0 | 124 |
# disable autocommit (isolation_level(1)) because DROP and |
125 |
# CREATE DATABASE can't be executed in a transaction |
|
126 |
try: |
|
127 |
cnx.set_isolation_level(0) |
|
128 |
except AttributeError: |
|
129 |
# set_isolation_level() is psycopg specific |
|
130 |
pass |
|
131 |
return cnx |
|
1469 | 132 |
|
0 | 133 |
def repo_cnx(config): |
134 |
"""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
|
135 |
from cubicweb.dbapi import in_memory_repo_cnx |
0 | 136 |
from cubicweb.server.utils import manager_userpasswd |
137 |
try: |
|
138 |
login = config.sources()['admin']['login'] |
|
139 |
pwd = config.sources()['admin']['password'] |
|
140 |
except KeyError: |
|
141 |
login, pwd = manager_userpasswd() |
|
142 |
while True: |
|
143 |
try: |
|
7056
51f88f13d6f3
[dbapi] split in_memory_cnx functions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7036
diff
changeset
|
144 |
return in_memory_repo_cnx(config, login, password=pwd) |
0 | 145 |
except AuthenticationError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
146 |
print '-> Error: wrong user/password.' |
1682
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
147 |
# 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
|
148 |
config._cubes = None |
0 | 149 |
login, pwd = manager_userpasswd() |
1469 | 150 |
|
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
|
151 |
|
0 | 152 |
# repository specific command handlers ######################################## |
153 |
||
154 |
class RepositoryCreateHandler(CommandHandler): |
|
155 |
cmdname = 'create' |
|
156 |
cfgname = 'repository' |
|
157 |
||
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
|
158 |
def bootstrap(self, cubes, automatic=False, inputlevel=0): |
5967 | 159 |
"""create an instance by copying files from the given cube and by asking |
160 |
information necessary to build required configuration files |
|
0 | 161 |
""" |
162 |
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
|
163 |
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
|
164 |
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
|
165 |
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
|
166 |
# 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
|
167 |
# 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
|
168 |
# 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
|
169 |
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
|
170 |
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
|
171 |
print '\n'+underline_title('Configuring the sources') |
0 | 172 |
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
|
173 |
# 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
|
174 |
# (in native.py) |
6299
51a9494b5efb
[sqlite] quick hacks to fix #1251873 and #1251874
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6184
diff
changeset
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
print |
0 | 180 |
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
|
181 |
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
|
182 |
# 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
|
183 |
password = generate_password() |
7155
4bab50b02927
[cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7140
diff
changeset
|
184 |
print '-> set administrator account to admin / %s' % password |
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
|
185 |
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
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
sconfig.input_config(inputlevel=inputlevel) |
0 | 190 |
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
|
191 |
config.write_sources_file(sourcescfg) |
0 | 192 |
# remember selected cubes for later initialization of the database |
193 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 194 |
|
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
self.config.appid]) |
0 | 201 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
202 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
203 |
'"cubicweb-ctl db-create %s".' % self.config.appid) |
1469 | 204 |
|
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
|
205 |
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
|
206 |
|
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 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
return ERROR |
0 | 215 |
|
216 |
class RepositoryDeleteHandler(CommandHandler): |
|
217 |
cmdname = 'delete' |
|
218 |
cfgname = 'repository' |
|
219 |
||
220 |
def cleanup(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
221 |
"""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
|
222 |
from logilab.database import get_db_helper |
0 | 223 |
source = self.config.sources()['system'] |
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
'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
|
230 |
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
|
231 |
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
|
232 |
return |
0 | 233 |
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
|
234 |
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
|
235 |
_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
|
236 |
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
|
237 |
return |
0 | 238 |
cursor = cnx.cursor() |
239 |
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
|
240 |
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
|
241 |
'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
|
242 |
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
|
243 |
print '-> database %s dropped.' % dbname |
0 | 244 |
# XXX should check we are not connected as user |
245 |
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
|
246 |
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
|
247 |
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
|
248 |
'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
|
249 |
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
|
250 |
print '-> user %s dropped.' % user |
0 | 251 |
cnx.commit() |
252 |
except: |
|
253 |
cnx.rollback() |
|
254 |
raise |
|
255 |
||
1469 | 256 |
|
0 | 257 |
class RepositoryStartHandler(CommandHandler): |
258 |
cmdname = 'start' |
|
259 |
cfgname = 'repository' |
|
260 |
||
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
|
261 |
def start_server(self, config): |
0 | 262 |
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
|
263 |
if config.debugmode: |
0 | 264 |
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
|
265 |
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
|
266 |
command.append(config.appid) |
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2969
diff
changeset
|
267 |
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
|
268 |
return 1 |
1469 | 269 |
|
0 | 270 |
|
271 |
class RepositoryStopHandler(CommandHandler): |
|
272 |
cmdname = 'stop' |
|
273 |
cfgname = 'repository' |
|
274 |
||
275 |
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
|
276 |
"""if pyro is enabled, ensure the repository is correctly unregistered |
0 | 277 |
""" |
278 |
if self.config.pyro_enabled(): |
|
279 |
from cubicweb.server.repository import pyro_unregister |
|
280 |
pyro_unregister(self.config) |
|
1469 | 281 |
|
0 | 282 |
|
283 |
# 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
|
284 |
|
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
|
285 |
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
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
293 |
class CreateInstanceDBCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
294 |
"""Create the system database of an instance (run after 'create'). |
1469 | 295 |
|
0 | 296 |
You will be prompted for a login / password to use to connect to |
297 |
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
|
298 |
on the database (ie a super user on the DBMS allowed to create |
0 | 299 |
database, users, languages...). |
300 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
301 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
302 |
the identifier of the instance to initialize. |
0 | 303 |
""" |
304 |
name = 'db-create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
305 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
306 |
min_args = max_args = 1 |
0 | 307 |
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
|
308 |
('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
|
309 |
{'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
|
310 |
'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
|
311 |
'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
|
312 |
'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
|
313 |
'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
|
314 |
}), |
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 |
('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
|
316 |
{'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
|
317 |
'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
|
318 |
'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
|
319 |
'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
|
320 |
}), |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
321 |
('create-db', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
322 |
{'short': 'c', 'type': 'yn', 'metavar': '<y or n>', |
0 | 323 |
'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
|
324 |
'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
|
325 |
}), |
0 | 326 |
) |
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
|
327 |
|
0 | 328 |
def run(self, args): |
329 |
"""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
|
330 |
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
|
331 |
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
|
332 |
automatic = self.get('automatic') |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
333 |
appid = args.pop() |
0 | 334 |
config = ServerConfiguration.config_for(appid) |
335 |
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
|
336 |
dbname = source['db-name'] |
0 | 337 |
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
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
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
|
342 |
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
|
343 |
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
|
344 |
elif self.config.create_db: |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
345 |
print '\n'+underline_title('Creating the system database') |
0 | 346 |
# 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
|
347 |
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
|
348 |
interactive=not automatic) |
0 | 349 |
cursor = dbcnx.cursor() |
350 |
try: |
|
351 |
if helper.users_support: |
|
352 |
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
|
353 |
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
|
354 |
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
|
355 |
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
|
356 |
print '-> user %s created.' % user |
0 | 357 |
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
|
358 |
if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
7564
1d64c8d33156
[server] "overrule" case insensitivity of database name (closes: #611294)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
359 |
cursor.execute('DROP DATABASE "%s"' % dbname) |
0 | 360 |
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
|
361 |
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
|
362 |
'--drop %s" manually to continue.' % config.appid) |
0 | 363 |
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
|
364 |
createdb(helper, source, dbcnx, cursor) |
0 | 365 |
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
|
366 |
print '-> database %s created.' % dbname |
0 | 367 |
except: |
368 |
dbcnx.rollback() |
|
369 |
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
|
370 |
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
|
371 |
interactive=not automatic) |
0 | 372 |
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
|
373 |
helper.init_fti_extensions(cursor) |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
374 |
cnx.commit() |
1469 | 375 |
# postgres specific stuff |
0 | 376 |
if driver == 'postgres': |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
377 |
# install plpythonu/plpgsql languages |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
378 |
langs = ('plpythonu', 'plpgsql') |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2969
diff
changeset
|
379 |
for extlang in langs: |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
380 |
if automatic or ASK.confirm('Create language %s ?' % extlang): |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
381 |
try: |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
382 |
helper.create_language(cursor, extlang) |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
383 |
except Exception, exc: |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
384 |
print '-> ERROR:', exc |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
385 |
print '-> could not create language %s, some stored procedures might be unusable' % extlang |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
386 |
cnx.rollback() |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
387 |
else: |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7457
diff
changeset
|
388 |
cnx.commit() |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
389 |
print '-> database for instance %s created and necessary extensions installed.' % appid |
0 | 390 |
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
|
391 |
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
|
392 |
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
|
393 |
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
|
394 |
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
|
395 |
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
|
396 |
str(self.config.config_level), config.appid]) |
0 | 397 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
398 |
print ('-> nevermind, you can do it later with ' |
3893 | 399 |
'"cubicweb-ctl db-init %s".' % config.appid) |
0 | 400 |
|
1469 | 401 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
402 |
class InitInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
403 |
"""Initialize the system database of an instance (run after 'db-create'). |
1469 | 404 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
405 |
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
|
406 |
user should have the create tables grant permissions on the database. |
0 | 407 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
408 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
409 |
the identifier of the instance to initialize. |
0 | 410 |
""" |
411 |
name = 'db-init' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
412 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
413 |
min_args = max_args = 1 |
0 | 414 |
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
|
415 |
('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
|
416 |
{'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
|
417 |
'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
|
418 |
'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
|
419 |
'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
|
420 |
}), |
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 |
('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
|
422 |
{'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
|
423 |
'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
|
424 |
'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
|
425 |
}), |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
426 |
('drop', |
0 | 427 |
{'short': 'd', 'action': 'store_true', |
428 |
'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
|
429 |
'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
|
430 |
'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
|
431 |
}), |
0 | 432 |
) |
433 |
||
434 |
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
|
435 |
check_options_consistency(self.config) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
436 |
print '\n'+underline_title('Initializing the system database') |
0 | 437 |
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
|
438 |
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
|
439 |
appid = args[0] |
0 | 440 |
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
|
441 |
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
|
442 |
system = config.sources()['system'] |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
443 |
extra_args=system.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
444 |
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
|
445 |
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
|
446 |
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
|
447 |
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
|
448 |
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
|
449 |
**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
|
450 |
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
|
451 |
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
|
452 |
'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
|
453 |
'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
|
454 |
% (config.sources_file(), str(ex).strip())) |
0 | 455 |
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
|
456 |
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
|
457 |
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
|
458 |
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
|
459 |
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
|
460 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
461 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
462 |
class AddSourceCommand(Command): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
463 |
"""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
|
464 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
465 |
<instance> |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
466 |
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
|
467 |
""" |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
468 |
name = 'add-source' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
469 |
arguments = '<instance>' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
470 |
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
|
471 |
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
|
472 |
('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
|
473 |
{'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
|
474 |
'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
|
475 |
}), |
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
|
476 |
) |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
477 |
|
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
478 |
def run(self, args): |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
479 |
appid = args[0] |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
480 |
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
|
481 |
config.quick_start = True |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
482 |
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
|
483 |
req = cnx.request() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
484 |
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
|
485 |
cubes = repo.get_cubes() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
486 |
while True: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
487 |
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
|
488 |
% ', '.join(sorted(SOURCE_TYPES))) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
489 |
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
|
490 |
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
|
491 |
continue |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
492 |
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
|
493 |
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
|
494 |
# 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
|
495 |
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
|
496 |
# 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
|
497 |
# 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
|
498 |
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
|
499 |
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
|
500 |
'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
|
501 |
continue |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
502 |
break |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
503 |
while True: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
504 |
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
|
505 |
'tell sources apart): ').strip() |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
506 |
if not sourceuri: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
507 |
print '-> mandatory.' |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
508 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
509 |
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
|
510 |
if sourceuri in used: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
511 |
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
|
512 |
else: |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
513 |
break |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
514 |
# 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
|
515 |
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
|
516 |
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
|
517 |
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
|
518 |
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
|
519 |
cnx.commit() |
0 | 520 |
|
521 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
522 |
class GrantUserOnInstanceCommand(Command): |
0 | 523 |
"""Grant a database user on a repository system database. |
1469 | 524 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
526 |
the identifier of the instance |
0 | 527 |
<user> |
528 |
the database's user requiring grant access |
|
529 |
""" |
|
530 |
name = 'db-grant-user' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
531 |
arguments = '<instance> <user>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
532 |
min_args = max_args = 2 |
0 | 533 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
534 |
('set-owner', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
535 |
{'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 536 |
'default' : False, |
537 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
538 |
), |
|
539 |
) |
|
540 |
def run(self, args): |
|
541 |
"""run the command with its specific arguments""" |
|
542 |
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
|
543 |
appid, user = args |
0 | 544 |
config = ServerConfiguration.config_for(appid) |
545 |
source = config.sources()['system'] |
|
546 |
set_owner = self.config.set_owner |
|
547 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
548 |
cursor = cnx.cursor() |
|
549 |
schema = config.load_schema() |
|
550 |
try: |
|
551 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
552 |
set_owner=set_owner), cursor) |
|
553 |
except Exception, ex: |
|
554 |
cnx.rollback() |
|
555 |
import traceback |
|
556 |
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
|
557 |
print '-> an error occurred:', ex |
0 | 558 |
else: |
559 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
560 |
print '-> rights granted to %s on instance %s.' % (appid, user) |
0 | 561 |
|
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
|
562 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
563 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
564 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
565 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
566 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
567 |
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
|
568 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
569 |
name = 'reset-admin-pwd' |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
570 |
arguments = '<instance>' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
571 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
572 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
573 |
"""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
|
574 |
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
|
575 |
appid = args[0] |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
576 |
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
|
577 |
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
|
578 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
579 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
580 |
except KeyError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
581 |
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
|
582 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
583 |
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
|
584 |
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
|
585 |
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
|
586 |
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
|
587 |
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
|
588 |
# 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
|
589 |
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
|
590 |
{'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
|
591 |
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
|
592 |
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
|
593 |
"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
|
594 |
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
|
595 |
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
|
596 |
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
|
597 |
# 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
|
598 |
_, 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
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
{'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
|
603 |
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
|
604 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
605 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
606 |
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
|
607 |
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
|
608 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
609 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
610 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
611 |
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
|
612 |
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
|
613 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
614 |
cnx.commit() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
615 |
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
|
616 |
cnx.close() |
0 | 617 |
|
1469 | 618 |
|
0 | 619 |
class StartRepositoryCommand(Command): |
6700 | 620 |
"""Start a CubicWeb RQL server for a given instance. |
1469 | 621 |
|
0 | 622 |
The server will be accessible through pyro |
623 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
624 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
625 |
the identifier of the instance to initialize. |
0 | 626 |
""" |
627 |
name = 'start-repository' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
628 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
629 |
min_args = max_args = 1 |
0 | 630 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
631 |
('debug', |
0 | 632 |
{'short': 'D', 'action' : 'store_true', |
633 |
'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
|
634 |
('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
|
635 |
{'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
|
636 |
'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
|
637 |
'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
|
638 |
}), |
0 | 639 |
) |
640 |
||
641 |
def run(self, args): |
|
5553 | 642 |
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
|
643 |
from cubicweb.cwctl import init_cmdline_log_threshold |
0 | 644 |
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
|
645 |
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
|
646 |
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
|
647 |
if sys.platform == 'win32' and not debug: |
7457
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
648 |
logger = logging.getLogger('cubicweb.ctl') |
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
|
649 |
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
|
650 |
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
|
651 |
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
|
652 |
init_cmdline_log_threshold(config, self['loglevel']) |
0 | 653 |
# 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
|
654 |
server = RepositoryServer(config) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
655 |
# 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
|
656 |
# 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
|
657 |
pidfile = config['pid-file'] |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
658 |
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
|
659 |
# go ! (don't daemonize in debug mode) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
660 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
661 |
os.makedirs(piddir) |
6779
accf5978a440
[daemon] make process umask configurable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6778
diff
changeset
|
662 |
if not debug and daemonize(pidfile, umask=config['umask']): |
0 | 663 |
return |
664 |
uid = config['uid'] |
|
665 |
if uid is not None: |
|
666 |
try: |
|
667 |
uid = int(uid) |
|
668 |
except ValueError: |
|
669 |
from pwd import getpwnam |
|
670 |
uid = getpwnam(uid).pw_uid |
|
671 |
os.setuid(uid) |
|
672 |
server.install_sig_handlers() |
|
673 |
server.connect(config['host'], 0) |
|
674 |
server.run() |
|
675 |
||
676 |
||
677 |
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
|
678 |
# 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
|
679 |
from datetime import date |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
680 |
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
|
681 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid) |
0 | 682 |
if sudo: |
683 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
684 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
685 |
print dmpcmd |
|
686 |
if os.system(dmpcmd): |
|
687 |
raise ExecutionError('Error while dumping the database') |
|
688 |
if output is None: |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
689 |
output = filename |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
690 |
cmd = 'scp %s:/tmp/%s %s' % (host, filename, output) |
0 | 691 |
print cmd |
692 |
if os.system(cmd): |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
693 |
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
|
694 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename) |
0 | 695 |
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
|
696 |
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
|
697 |
'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
|
698 |
'Continue anyway?' % filename): |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
699 |
raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename) |
0 | 700 |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
701 |
|
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
702 |
def _local_dump(appid, output, format='native'): |
0 | 703 |
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
|
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) |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
706 |
mih.backup_database(output, askconfirm=False, format=format) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
707 |
mih.shutdown() |
0 | 708 |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
709 |
def _local_restore(appid, backupfile, drop, systemonly=True, format='native'): |
0 | 710 |
config = ServerConfiguration.config_for(appid) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
711 |
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
|
712 |
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
|
713 |
mih = config.migration_handler(connect=False, verbosity=1) |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
714 |
mih.restore_database(backupfile, drop, systemonly, askconfirm=False, format=format) |
0 | 715 |
repo = mih.repo_connect() |
716 |
# version of the database |
|
717 |
dbversions = repo.get_versions() |
|
718 |
mih.shutdown() |
|
719 |
if not dbversions: |
|
720 |
print "bad or missing version information in the database, don't upgrade file system" |
|
721 |
return |
|
722 |
# version of installed software |
|
723 |
eversion = dbversions['cubicweb'] |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
724 |
status = instance_status(config, eversion, dbversions) |
0 | 725 |
# * database version > installed software |
726 |
if status == 'needsoftupgrade': |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
727 |
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
|
728 |
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
|
729 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 730 |
return |
731 |
# * database version < installed software, an upgrade will be necessary |
|
732 |
# 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
|
733 |
elif status == 'needapplupgrade': |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
734 |
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
|
735 |
print "** Migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
736 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 737 |
return |
738 |
# * database version = installed software, database version = instance fs version |
|
739 |
# ok! |
|
740 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
741 |
def instance_status(config, cubicwebapplversion, vcconf): |
0 | 742 |
cubicwebversion = config.cubicweb_version() |
743 |
if cubicwebapplversion > cubicwebversion: |
|
744 |
return 'needsoftupgrade' |
|
745 |
if cubicwebapplversion < cubicwebversion: |
|
746 |
return 'needapplupgrade' |
|
747 |
for cube in config.cubes(): |
|
748 |
try: |
|
749 |
softversion = config.cube_version(cube) |
|
750 |
except ConfigurationError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
751 |
print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube |
0 | 752 |
continue |
753 |
try: |
|
754 |
applversion = vcconf[cube] |
|
755 |
except KeyError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
756 |
print '-> Error: no cube version information for %s in version configuration.' % cube |
1469 | 757 |
continue |
0 | 758 |
if softversion == applversion: |
759 |
continue |
|
760 |
if softversion > applversion: |
|
761 |
return 'needsoftupgrade' |
|
762 |
elif softversion < applversion: |
|
763 |
return 'needapplupgrade' |
|
764 |
return None |
|
1469 | 765 |
|
0 | 766 |
|
767 |
class DBDumpCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
768 |
"""Backup the system database of an instance. |
1469 | 769 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
770 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
771 |
the identifier of the instance to backup |
0 | 772 |
format [[user@]host:]appname |
773 |
""" |
|
774 |
name = 'db-dump' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
775 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
776 |
min_args = max_args = 1 |
0 | 777 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
778 |
('output', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
779 |
{'short': 'o', 'type' : 'string', 'metavar' : '<file>', |
0 | 780 |
'default' : None, |
781 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
782 |
), |
|
783 |
('sudo', |
|
784 |
{'short': 's', 'action' : 'store_true', |
|
785 |
'default' : False, |
|
786 |
'help': 'Use sudo on the remote host.'} |
|
787 |
), |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
788 |
('format', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
789 |
{'short': 'f', 'default': 'native', 'type': 'choice', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
790 |
'choices': ('native', 'portable'), |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
791 |
'help': '"native" format uses db backend utilities to dump the database. ' |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
792 |
'"portable" format uses a database independent format'} |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
793 |
), |
0 | 794 |
) |
795 |
||
796 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
797 |
appid = args[0] |
0 | 798 |
if ':' in appid: |
799 |
host, appid = appid.split(':') |
|
800 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
801 |
else: |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
802 |
_local_dump(appid, self.config.output, format=self.config.format) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
803 |
|
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
804 |
|
0 | 805 |
|
806 |
||
807 |
class DBRestoreCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
808 |
"""Restore the system database of an instance. |
1469 | 809 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
810 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
811 |
the identifier of the instance to restore |
0 | 812 |
""" |
813 |
name = 'db-restore' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
814 |
arguments = '<instance> <backupfile>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
815 |
min_args = max_args = 2 |
0 | 816 |
|
817 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
818 |
('no-drop', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
819 |
{'short': 'n', 'action' : 'store_true', 'default' : False, |
0 | 820 |
'help': 'for some reason the database doesn\'t exist and so ' |
821 |
'should not be dropped.'} |
|
822 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
823 |
('restore-all', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
824 |
{'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
|
825 |
'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
|
826 |
'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
|
827 |
'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
|
828 |
'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
|
829 |
), |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
830 |
('format', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
831 |
{'short': 'f', 'default': 'native', 'type': 'choice', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
832 |
'choices': ('native', 'portable'), |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
833 |
'help': 'the format used when dumping the database'}), |
0 | 834 |
) |
835 |
||
836 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
837 |
appid, backupfile = args |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
838 |
if self.config.format == 'portable': |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
839 |
# we need to ensure a DB exist before restoring from portable format |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
840 |
if not self.config.no_drop: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
841 |
try: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
842 |
CWCTL.run(['db-create', '--automatic', appid]) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
843 |
except SystemExit, exc: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
844 |
# continue if the command exited with status 0 (success) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
845 |
if exc.code: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
846 |
raise |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
847 |
_local_restore(appid, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
848 |
drop=not self.config.no_drop, |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
849 |
systemonly=not self.config.restore_all, |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
850 |
format=self.config.format) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
851 |
if self.config.format == 'portable': |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
852 |
try: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
853 |
CWCTL.run(['db-rebuild-fti', appid]) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
854 |
except SystemExit, exc: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
855 |
if exc.code: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
856 |
raise |
0 | 857 |
|
858 |
||
859 |
class DBCopyCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
860 |
"""Copy the system database of an instance (backup and restore). |
1469 | 861 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
862 |
<src-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
863 |
the identifier of the instance to backup |
0 | 864 |
format [[user@]host:]appname |
865 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
866 |
<dest-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
867 |
the identifier of the instance to restore |
0 | 868 |
""" |
869 |
name = 'db-copy' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
870 |
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
|
871 |
min_args = max_args = 2 |
0 | 872 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
873 |
('no-drop', |
1469 | 874 |
{'short': 'n', 'action' : 'store_true', |
0 | 875 |
'default' : False, |
876 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
877 |
'should not be dropped.'} |
|
878 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
879 |
('keep-dump', |
0 | 880 |
{'short': 'k', 'action' : 'store_true', |
881 |
'default' : False, |
|
882 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
883 |
), |
|
884 |
('sudo', |
|
885 |
{'short': 's', 'action' : 'store_true', |
|
886 |
'default' : False, |
|
887 |
'help': 'Use sudo on the remote host.'} |
|
888 |
), |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
889 |
('format', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
890 |
{'short': 'f', 'default': 'native', 'type': 'choice', |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
891 |
'choices': ('native', 'portable'), |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
892 |
'help': '"native" format uses db backend utilities to dump the database. ' |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
893 |
'"portable" format uses a database independent format'} |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
894 |
), |
0 | 895 |
) |
896 |
||
897 |
def run(self, args): |
|
898 |
import tempfile |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
899 |
srcappid, destappid = args |
3148 | 900 |
fd, output = tempfile.mkstemp() |
901 |
os.close(fd) |
|
0 | 902 |
if ':' in srcappid: |
903 |
host, srcappid = srcappid.split(':') |
|
904 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
905 |
else: |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
906 |
_local_dump(srcappid, output, format=self.config.format) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
907 |
_local_restore(destappid, output, not self.config.no_drop, |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7155
diff
changeset
|
908 |
self.config.format) |
0 | 909 |
if self.config.keep_dump: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
910 |
print '-> you can get the dump file at', output |
0 | 911 |
else: |
912 |
os.remove(output) |
|
913 |
||
1469 | 914 |
|
0 | 915 |
class CheckRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
916 |
"""Check integrity of the system database of an instance. |
1469 | 917 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
918 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
919 |
the identifier of the instance to check |
0 | 920 |
""" |
921 |
name = 'db-check' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
922 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
923 |
min_args = max_args = 1 |
0 | 924 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
925 |
('checks', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
926 |
{'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
|
927 |
'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
|
928 |
'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
|
929 |
'metadata', 'schema', 'text_index'), |
0 | 930 |
'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
|
931 |
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
|
932 |
metadata, text_index and schema.'} |
0 | 933 |
), |
1469 | 934 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
935 |
('autofix', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
936 |
{'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 937 |
'default' : False, |
938 |
'help': 'Automatically correct integrity problems if this option \ |
|
939 |
is set to "y" or "yes", else only display them'} |
|
940 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
941 |
('reindex', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
942 |
{'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 943 |
'default' : False, |
944 |
'help': 're-indexes the database for full text search if this \ |
|
945 |
option is set to "y" or "yes" (may be long for large database).'} |
|
946 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
947 |
('force', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
948 |
{'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
|
949 |
'default' : False, |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
950 |
'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
|
951 |
), |
1469 | 952 |
|
0 | 953 |
) |
954 |
||
955 |
def run(self, args): |
|
6184
da580218a5b3
[c-c db-check] add missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6138
diff
changeset
|
956 |
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
|
957 |
appid = args[0] |
0 | 958 |
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
|
959 |
config.repairing = self.config.force |
0 | 960 |
repo, cnx = repo_cnx(config) |
961 |
check(repo, cnx, |
|
962 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
963 |
||
964 |
||
965 |
class RebuildFTICommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
966 |
"""Rebuild the full-text index of the system database of an instance. |
1469 | 967 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
968 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
969 |
the identifier of the instance to rebuild |
0 | 970 |
""" |
971 |
name = 'db-rebuild-fti' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
972 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
973 |
min_args = max_args = 1 |
0 | 974 |
|
975 |
def run(self, args): |
|
976 |
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
|
977 |
appid = args[0] |
0 | 978 |
config = ServerConfiguration.config_for(appid) |
979 |
repo, cnx = repo_cnx(config) |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
980 |
session = repo._get_session(cnx.sessionid, setcnxset=True) |
0 | 981 |
reindex_entities(repo.schema, session) |
982 |
cnx.commit() |
|
983 |
||
1469 | 984 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
985 |
class SynchronizeInstanceSchemaCommand(Command): |
0 | 986 |
"""Synchronize persistent schema with cube schema. |
1469 | 987 |
|
0 | 988 |
Will synchronize common stuff between the cube schema and the |
989 |
actual persistent schema, but will not add/remove any entity or relation. |
|
990 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
991 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
992 |
the identifier of the instance to synchronize. |
0 | 993 |
""" |
994 |
name = 'schema-sync' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
995 |
arguments = '<instance>' |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
996 |
min_args = max_args = 1 |
0 | 997 |
|
998 |
def run(self, args): |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
999 |
appid = args[0] |
0 | 1000 |
config = ServerConfiguration.config_for(appid) |
1001 |
mih = config.migration_handler() |
|
1002 |
mih.cmd_synchronize_schema() |
|
1003 |
||
1004 |
||
7457
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1005 |
class SynchronizeSourceCommand(Command): |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1006 |
"""Force a source synchronization. |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1007 |
|
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1008 |
<instance> |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1009 |
the identifier of the instance |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1010 |
<source> |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1011 |
the name of the source to synchronize. |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1012 |
""" |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1013 |
name = 'source-sync' |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1014 |
arguments = '<instance> <source>' |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1015 |
min_args = max_args = 2 |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1016 |
|
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1017 |
def run(self, args): |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1018 |
config = ServerConfiguration.config_for(args[0]) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1019 |
config.global_set_option('log-file', None) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1020 |
config.log_format = '%(levelname)s %(name)s: %(message)s' |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1021 |
logger = logging.getLogger('cubicweb.sources') |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1022 |
logger.setLevel(logging.INFO) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1023 |
# only retrieve cnx to trigger authentication, close it right away |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1024 |
repo, cnx = repo_cnx(config) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1025 |
cnx.close() |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1026 |
try: |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1027 |
source = repo.sources_by_uri[args[1]] |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1028 |
except KeyError: |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1029 |
raise ExecutionError('no source named %r' % args[1]) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1030 |
session = repo.internal_session() |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1031 |
stats = source.pull_data(session, force=True, raise_on_error=True) |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1032 |
for key, val in stats.iteritems(): |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1033 |
if val: |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1034 |
print key, ':', val |
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1035 |
|
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1036 |
|
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1037 |
for cmdclass in (CreateInstanceDBCommand, InitInstanceCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1038 |
GrantUserOnInstanceCommand, ResetAdminPasswordCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1039 |
StartRepositoryCommand, |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1040 |
DBDumpCommand, DBRestoreCommand, DBCopyCommand, |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6379
diff
changeset
|
1041 |
AddSourceCommand, CheckRepositoryCommand, RebuildFTICommand, |
7457
f7c97a3cd121
[datafeed] provide a source-sync command to launch source synchronization through the command line. Closes #1725692
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7398
diff
changeset
|
1042 |
SynchronizeInstanceSchemaCommand, SynchronizeSourceCommand |
6138
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1043 |
): |
65f5e488f983
update to lgc.clcommands 0.51 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
1044 |
CWCTL.register(cmdclass) |