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