author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 09 Jun 2010 10:06:46 +0200 | |
changeset 5696 | 98d390c28edb |
parent 5590 | a56eb02f9ce7 |
parent 5692 | 59ceed788bdb |
child 5754 | 51179e0bb250 |
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/>. |
0 | 18 |
"""cubicweb-ctl commands and command handlers specific to the server.serverconfig |
19 |
||
20 |
""" |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
21 |
__docformat__ = 'restructuredtext en' |
0 | 22 |
|
4718
3dc3ad02d091
avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4670
diff
changeset
|
23 |
# *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
|
24 |
# 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
|
25 |
# 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
|
26 |
import sys |
0 | 27 |
import os |
28 |
||
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 |
1132 | 30 |
from logilab.common.clcommands import register_commands, cmd_run, pop_arg |
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
|
31 |
from logilab.common.shellutils import ASK |
0 | 32 |
|
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
33 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
34 |
from cubicweb.toolsutils import Command, CommandHandler, underline_title |
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 |
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
|
36 |
from cubicweb.server.serverconfig import (USER_OPTIONS, ServerConfiguration, |
dc7d315165fc
default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3893
diff
changeset
|
37 |
SourceConfiguration) |
0 | 38 |
|
39 |
# utility functions ########################################################### |
|
40 |
||
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
41 |
def source_cnx(source, dbname=None, special_privs=False, verbose=True): |
0 | 42 |
"""open and return a connection to the system database defined in the |
43 |
given server.serverconfig |
|
44 |
""" |
|
45 |
from getpass import getpass |
|
5586
aabe082fee02
[c-c] use logilab.database, not lgc.db
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5585
diff
changeset
|
46 |
from logilab.database import get_connection |
4218
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
47 |
dbhost = source.get('db-host') |
0 | 48 |
if dbname is None: |
49 |
dbname = source['db-name'] |
|
50 |
driver = source['db-driver'] |
|
4218
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
51 |
print '-> connecting to %s database' % driver, |
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
52 |
if dbhost: |
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
53 |
print '%s@%s' % (dbname, dbhost), |
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
54 |
else: |
deb2dcffb301
[cwctl] reset-admin-pwd was failing when db-host was not set
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4212
diff
changeset
|
55 |
print dbname, |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
56 |
if not verbose or (not special_privs and source.get('db-user')): |
0 | 57 |
user = source['db-user'] |
58 |
print 'as', user |
|
59 |
if source.get('db-password'): |
|
60 |
password = source['db-password'] |
|
61 |
else: |
|
62 |
password = getpass('password: ') |
|
63 |
else: |
|
64 |
print |
|
65 |
if special_privs: |
|
66 |
print 'WARNING' |
|
67 |
print 'the user will need the following special access rights on the database:' |
|
68 |
print special_privs |
|
69 |
print |
|
70 |
default_user = source.get('db-user', os.environ.get('USER', '')) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
71 |
user = raw_input('Connect as user ? [%r]: ' % default_user) |
0 | 72 |
user = user or default_user |
73 |
if user == source.get('db-user') and source.get('db-password'): |
|
74 |
password = source['db-password'] |
|
75 |
else: |
|
76 |
password = getpass('password: ') |
|
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
77 |
extra_args = source.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
port=source.get('db-port'), |
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
81 |
**extra) |
4845 | 82 |
try: |
83 |
cnx.logged_user = user |
|
84 |
except AttributeError: |
|
85 |
# C object, __slots__ |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
86 |
from logilab.database import _SimpleConnectionWrapper |
4845 | 87 |
cnx = _SimpleConnectionWrapper(cnx) |
88 |
cnx.logged_user = user |
|
4837
54969eec48eb
misc fixes to ensure logilab.db compatibility
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
89 |
return cnx |
0 | 90 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
91 |
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
|
92 |
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
|
93 |
"""shortcut to get a connextion to the instance system database |
0 | 94 |
defined in the given config. If <dbms_system_base> is True, |
95 |
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
|
96 |
create/drop the instance database) |
0 | 97 |
""" |
98 |
if dbms_system_base: |
|
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
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
|
102 |
return source_cnx(source, special_privs=special_privs, verbose=verbose) |
0 | 103 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
104 |
def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
0 | 105 |
"""return a connection on the RDMS system table (to create/drop a user |
106 |
or a database |
|
107 |
""" |
|
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
|
108 |
import logilab.common as lgp |
4848
41f84eea63c9
rename logilab.db into logilab.database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4845
diff
changeset
|
109 |
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
|
110 |
lgp.USE_MX_DATETIME = False |
0 | 111 |
special_privs = '' |
112 |
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
|
113 |
helper = get_db_helper(driver) |
0 | 114 |
if user is not None and helper.users_support: |
115 |
special_privs += '%s USER' % what |
|
116 |
if db is not None: |
|
117 |
special_privs += ' %s DATABASE' % what |
|
118 |
# connect on the dbms system base to create our base |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
119 |
cnx = system_source_cnx(source, True, special_privs=special_privs, verbose=verbose) |
0 | 120 |
# disable autocommit (isolation_level(1)) because DROP and |
121 |
# CREATE DATABASE can't be executed in a transaction |
|
122 |
try: |
|
123 |
cnx.set_isolation_level(0) |
|
124 |
except AttributeError: |
|
125 |
# set_isolation_level() is psycopg specific |
|
126 |
pass |
|
127 |
return cnx |
|
1469 | 128 |
|
0 | 129 |
def repo_cnx(config): |
130 |
"""return a in-memory repository and a db api connection it""" |
|
131 |
from cubicweb.dbapi import in_memory_cnx |
|
132 |
from cubicweb.server.utils import manager_userpasswd |
|
133 |
try: |
|
134 |
login = config.sources()['admin']['login'] |
|
135 |
pwd = config.sources()['admin']['password'] |
|
136 |
except KeyError: |
|
137 |
login, pwd = manager_userpasswd() |
|
138 |
while True: |
|
139 |
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
|
140 |
return in_memory_cnx(config, login, password=pwd) |
0 | 141 |
except AuthenticationError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
142 |
print '-> Error: wrong user/password.' |
1682
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
143 |
# 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
|
144 |
config._cubes = None |
0 | 145 |
login, pwd = manager_userpasswd() |
1469 | 146 |
|
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
|
147 |
|
0 | 148 |
# repository specific command handlers ######################################## |
149 |
||
150 |
class RepositoryCreateHandler(CommandHandler): |
|
151 |
cmdname = 'create' |
|
152 |
cfgname = 'repository' |
|
153 |
||
154 |
def bootstrap(self, cubes, inputlevel=0): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
155 |
"""create an instance by copying files from the given cube and by |
0 | 156 |
asking information necessary to build required configuration files |
157 |
""" |
|
4718
3dc3ad02d091
avoid module import in c-c plugins module, and remind it
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4670
diff
changeset
|
158 |
from cubicweb.server.utils import ask_source_config |
0 | 159 |
config = self.config |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
160 |
print underline_title('Configuring the repository') |
0 | 161 |
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
|
162 |
# 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
|
163 |
# 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
|
164 |
# 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
|
165 |
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
|
166 |
config.input_config('pyro', inputlevel) |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
167 |
print '\n'+underline_title('Configuring the sources') |
0 | 168 |
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
|
169 |
# 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
|
170 |
# defs (in native.py) |
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
|
171 |
sconfig = SourceConfiguration(config.appid, |
dc7d315165fc
default_instance id should also be available when silently rebuilding soures file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3893
diff
changeset
|
172 |
options=SOURCE_TYPES['native'].options) |
0 | 173 |
sconfig.adapter = 'native' |
174 |
sconfig.input_config(inputlevel=inputlevel) |
|
175 |
sourcescfg = {'system': 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
|
176 |
for cube in cubes: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
177 |
# if a source is named as the cube containing it, we need the |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
178 |
# source to use the cube, so add it. |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
179 |
if cube in SOURCE_TYPES: |
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 |
sourcescfg[cube] = ask_source_config(cube, inputlevel) |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
181 |
print |
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
|
182 |
while ASK.confirm('Enter another source ?', default_is_yes=False): |
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
|
183 |
available = sorted(stype for stype in SOURCE_TYPES |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
184 |
if not stype in cubes) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
185 |
while True: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
186 |
sourcetype = raw_input('source type (%s): ' % ', '.join(available)) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
187 |
if sourcetype in available: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
188 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
189 |
print '-> unknown source type, use one of the available types.' |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
190 |
while True: |
5410
3cccd5e9dd7d
cubicweb-ctl create improvements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
191 |
sourceuri = raw_input('source identifier (a unique name used to tell sources apart): ').strip() |
2106
2295f2aba61d
check not using "admin" as uri (reserved)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
192 |
if sourceuri != 'admin' and sourceuri not in sourcescfg: |
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
|
193 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
194 |
print '-> uri already used, choose another one.' |
5410
3cccd5e9dd7d
cubicweb-ctl create improvements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5043
diff
changeset
|
195 |
sourcescfg[sourceuri] = ask_source_config(sourcetype, inputlevel) |
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
|
196 |
sourcemodule = SOURCE_TYPES[sourcetype].module |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
197 |
if not sourcemodule.startswith('cubicweb.'): |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
198 |
# module names look like cubes.mycube.themodule |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
199 |
sourcecube = SOURCE_TYPES[sourcetype].module.split('.', 2)[1] |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
200 |
# if the source adapter is coming from an external component, |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
201 |
# ensure it's specified in used cubes |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
202 |
if not sourcecube in cubes: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
203 |
cubes.append(sourcecube) |
0 | 204 |
sconfig = Configuration(options=USER_OPTIONS) |
205 |
sconfig.input_config(inputlevel=inputlevel) |
|
206 |
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
|
207 |
config.write_sources_file(sourcescfg) |
0 | 208 |
# remember selected cubes for later initialization of the database |
209 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 210 |
|
0 | 211 |
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
|
212 |
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
|
213 |
verbosity = (self.config.mode == 'installed') and 'y' or 'n' |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
214 |
cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity) |
0 | 215 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
216 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
217 |
'"cubicweb-ctl db-create %s".' % self.config.appid) |
1469 | 218 |
|
0 | 219 |
|
220 |
class RepositoryDeleteHandler(CommandHandler): |
|
221 |
cmdname = 'delete' |
|
222 |
cfgname = 'repository' |
|
223 |
||
224 |
def cleanup(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
225 |
"""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
|
226 |
from logilab.database import get_db_helper |
0 | 227 |
source = self.config.sources()['system'] |
228 |
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
|
229 |
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
|
230 |
if ASK.confirm('Delete database %s ?' % dbname): |
0 | 231 |
user = source['db-user'] or None |
232 |
cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
|
233 |
cursor = cnx.cursor() |
|
234 |
try: |
|
235 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
236 |
print '-> database %s dropped.' % dbname |
0 | 237 |
# XXX should check we are not connected as user |
238 |
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
|
239 |
ASK.confirm('Delete user %s ?' % user, default_is_yes=False): |
0 | 240 |
cursor.execute('DROP USER %s' % user) |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
241 |
print '-> user %s dropped.' % user |
0 | 242 |
cnx.commit() |
243 |
except: |
|
244 |
cnx.rollback() |
|
245 |
raise |
|
246 |
||
1469 | 247 |
|
0 | 248 |
class RepositoryStartHandler(CommandHandler): |
249 |
cmdname = 'start' |
|
250 |
cfgname = 'repository' |
|
251 |
||
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
252 |
def start_server(self, ctlconf): |
0 | 253 |
command = ['cubicweb-ctl start-repository '] |
5442
3ed8afbbdf70
[webconfig] refactor/cleanup debug mode management on startup: simply use config.debugmode instead of debug argument everywhere...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
254 |
if ctlconf.debugmode: |
0 | 255 |
command.append('--debug') |
256 |
command.append(self.config.appid) |
|
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2969
diff
changeset
|
257 |
os.system(' '.join(command)) |
1469 | 258 |
|
0 | 259 |
|
260 |
class RepositoryStopHandler(CommandHandler): |
|
261 |
cmdname = 'stop' |
|
262 |
cfgname = 'repository' |
|
263 |
||
264 |
def poststop(self): |
|
265 |
"""if pyro is enabled, ensure the repository is correctly |
|
266 |
unregistered |
|
267 |
""" |
|
268 |
if self.config.pyro_enabled(): |
|
269 |
from cubicweb.server.repository import pyro_unregister |
|
270 |
pyro_unregister(self.config) |
|
1469 | 271 |
|
0 | 272 |
|
273 |
# 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
|
274 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
275 |
class CreateInstanceDBCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
276 |
"""Create the system database of an instance (run after 'create'). |
1469 | 277 |
|
0 | 278 |
You will be prompted for a login / password to use to connect to |
279 |
the system database. The given user should have almost all rights |
|
280 |
on the database (ie a super user on the dbms allowed to create |
|
281 |
database, users, languages...). |
|
282 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
283 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
284 |
the identifier of the instance to initialize. |
0 | 285 |
""" |
286 |
name = 'db-create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
287 |
arguments = '<instance>' |
1469 | 288 |
|
0 | 289 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
290 |
('create-db', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
291 |
{'short': 'c', 'type': 'yn', 'metavar': '<y or n>', |
0 | 292 |
'default': True, |
293 |
'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
|
294 |
('verbose', |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
295 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
296 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
297 |
'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
|
298 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
299 |
), |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
300 |
('automatic', |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
301 |
{'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
|
302 |
'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
|
303 |
'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
|
304 |
} |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
305 |
), |
0 | 306 |
) |
307 |
def run(self, args): |
|
308 |
"""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
|
309 |
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
|
310 |
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
|
311 |
automatic = self.get('automatic') |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
312 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 313 |
config = ServerConfiguration.config_for(appid) |
314 |
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
|
315 |
dbname = source['db-name'] |
0 | 316 |
driver = source['db-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
|
317 |
create_db = self.config.create_db |
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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
os.unlink(dbname) |
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
|
324 |
elif create_db: |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
325 |
print '\n'+underline_title('Creating the system database') |
0 | 326 |
# connect on the dbms system base to create our base |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
327 |
dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
0 | 328 |
cursor = dbcnx.cursor() |
329 |
try: |
|
330 |
if helper.users_support: |
|
331 |
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
|
332 |
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
|
333 |
ASK.confirm('Create db user %s ?' % user, default_is_yes=False)): |
0 | 334 |
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
|
335 |
print '-> user %s created.' % user |
0 | 336 |
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
|
337 |
if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
0 | 338 |
cursor.execute('DROP DATABASE %s' % dbname) |
339 |
else: |
|
340 |
return |
|
341 |
if dbcnx.logged_user != source['db-user']: |
|
342 |
helper.create_database(cursor, dbname, source['db-user'], |
|
343 |
source['db-encoding']) |
|
344 |
else: |
|
345 |
helper.create_database(cursor, dbname, |
|
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
|
346 |
dbencoding=source['db-encoding']) |
0 | 347 |
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
|
348 |
print '-> database %s created.' % dbname |
0 | 349 |
except: |
350 |
dbcnx.rollback() |
|
351 |
raise |
|
1469 | 352 |
cnx = system_source_cnx(source, special_privs='LANGUAGE C', 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 ?'): |
0 | 366 |
cmd_run('db-init', config.appid) |
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 |
|
0 | 375 |
You will be prompted for a login / password to use to connect to |
376 |
the system database. The given user should have the create tables, |
|
377 |
and grant permissions. |
|
378 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
379 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
380 |
the identifier of the instance to initialize. |
0 | 381 |
""" |
382 |
name = 'db-init' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
383 |
arguments = '<instance>' |
1469 | 384 |
|
0 | 385 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
386 |
('drop', |
0 | 387 |
{'short': 'd', 'action': 'store_true', |
388 |
'default': False, |
|
389 |
'help': 'insert drop statements to remove previously existant \ |
|
390 |
tables, indexes... (no by default)'}), |
|
391 |
) |
|
392 |
||
393 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
394 |
print '\n'+underline_title('Initializing the system database') |
0 | 395 |
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
|
396 |
from logilab.database import get_connection |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
397 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 398 |
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
|
399 |
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
|
400 |
system = config.sources()['system'] |
4180
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
401 |
extra_args=system.get('db-extra-arguments') |
9fb0d06926cc
more extra_args cleanup
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4177
diff
changeset
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
**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
|
408 |
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
|
409 |
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
|
410 |
'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
|
411 |
'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
|
412 |
% (config.sources_file(), str(ex).strip())) |
0 | 413 |
init_repository(config, drop=self.config.drop) |
414 |
||
415 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
416 |
class GrantUserOnInstanceCommand(Command): |
0 | 417 |
"""Grant a database user on a repository system database. |
1469 | 418 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
419 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
420 |
the identifier of the instance |
0 | 421 |
<user> |
422 |
the database's user requiring grant access |
|
423 |
""" |
|
424 |
name = 'db-grant-user' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
425 |
arguments = '<instance> <user>' |
0 | 426 |
|
427 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
428 |
('set-owner', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
429 |
{'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 430 |
'default' : False, |
431 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
432 |
), |
|
433 |
) |
|
434 |
def run(self, args): |
|
435 |
"""run the command with its specific arguments""" |
|
436 |
from cubicweb.server.sqlutils import sqlexec, sqlgrants |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
437 |
appid = pop_arg(args, 1, msg='No instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
438 |
user = pop_arg(args, msg='No user specified !') |
0 | 439 |
config = ServerConfiguration.config_for(appid) |
440 |
source = config.sources()['system'] |
|
441 |
set_owner = self.config.set_owner |
|
442 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
443 |
cursor = cnx.cursor() |
|
444 |
schema = config.load_schema() |
|
445 |
try: |
|
446 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
447 |
set_owner=set_owner), cursor) |
|
448 |
except Exception, ex: |
|
449 |
cnx.rollback() |
|
450 |
import traceback |
|
451 |
traceback.print_exc() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
452 |
print '-> an error occured:', ex |
0 | 453 |
else: |
454 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
455 |
print '-> rights granted to %s on instance %s.' % (appid, user) |
0 | 456 |
|
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
|
457 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
458 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
459 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
460 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
461 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
462 |
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
|
463 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
464 |
name = 'reset-admin-pwd' |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
465 |
arguments = '<instance>' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
466 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
467 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
468 |
"""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
|
469 |
from cubicweb.server.utils import crypt_password, manager_userpasswd |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
470 |
appid = pop_arg(args, 1, msg='No instance specified !') |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
471 |
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
|
472 |
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
|
473 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
474 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
475 |
except KeyError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
476 |
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
|
477 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
478 |
cnx = source_cnx(sourcescfg['system']) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
479 |
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
|
480 |
# 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
|
481 |
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
|
482 |
{'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
|
483 |
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
|
484 |
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
|
485 |
"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
|
486 |
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
|
487 |
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
|
488 |
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
|
489 |
# 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
|
490 |
_, 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
|
491 |
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
|
492 |
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
|
493 |
cursor.execute("UPDATE cw_CWUser SET cw_upassword=%(p)s 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
|
494 |
{'p': 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
|
495 |
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
|
496 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
497 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
498 |
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
|
499 |
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
|
500 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
501 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
502 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
503 |
traceback.print_exc() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
504 |
print '-> an error occured:', ex |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
505 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
506 |
cnx.commit() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
507 |
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
|
508 |
cnx.close() |
0 | 509 |
|
1469 | 510 |
|
0 | 511 |
class StartRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
512 |
"""Start an CubicWeb RQL server for a given instance. |
1469 | 513 |
|
0 | 514 |
The server will be accessible through pyro |
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 to initialize. |
0 | 518 |
""" |
519 |
name = 'start-repository' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
520 |
arguments = '<instance>' |
1469 | 521 |
|
0 | 522 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
523 |
('debug', |
0 | 524 |
{'short': 'D', 'action' : 'store_true', |
525 |
'help': 'start server in debug mode.'}), |
|
526 |
) |
|
527 |
||
528 |
def run(self, args): |
|
5553 | 529 |
from logilab.common.daemon import daemonize |
0 | 530 |
from cubicweb.server.server import RepositoryServer |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
531 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 532 |
config = ServerConfiguration.config_for(appid) |
3786
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
533 |
if sys.platform == 'win32': |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
534 |
if not self.config.debug: |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
535 |
from logging import getLogger |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
536 |
logger = getLogger('cubicweb.ctl') |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
537 |
logger.info('Forcing debug mode on win32 platform') |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
538 |
self.config.debug = True |
0 | 539 |
debug = self.config.debug |
540 |
# create the server |
|
541 |
server = RepositoryServer(config, debug) |
|
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
542 |
# 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
|
543 |
# 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
|
544 |
pidfile = config['pid-file'] |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
545 |
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
|
546 |
# go ! (don't daemonize in debug mode) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
547 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
548 |
os.makedirs(piddir) |
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
|
549 |
if not debug and daemonize(pidfile): |
0 | 550 |
return |
551 |
uid = config['uid'] |
|
552 |
if uid is not None: |
|
553 |
try: |
|
554 |
uid = int(uid) |
|
555 |
except ValueError: |
|
556 |
from pwd import getpwnam |
|
557 |
uid = getpwnam(uid).pw_uid |
|
558 |
os.setuid(uid) |
|
559 |
server.install_sig_handlers() |
|
560 |
server.connect(config['host'], 0) |
|
561 |
server.run() |
|
562 |
||
563 |
||
564 |
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
|
565 |
# 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
|
566 |
from datetime import date |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
567 |
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
|
568 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid) |
0 | 569 |
if sudo: |
570 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
571 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
572 |
print dmpcmd |
|
573 |
if os.system(dmpcmd): |
|
574 |
raise ExecutionError('Error while dumping the database') |
|
575 |
if output is None: |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
576 |
output = filename |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
577 |
cmd = 'scp %s:/tmp/%s %s' % (host, filename, output) |
0 | 578 |
print cmd |
579 |
if os.system(cmd): |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
580 |
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
|
581 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename) |
0 | 582 |
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
|
583 |
if os.system(rmcmd) and not ASK.confirm( |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
584 |
'An error occured while deleting remote dump at /tmp/%s. ' |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
585 |
'Continue anyway?' % filename): |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
586 |
raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename) |
0 | 587 |
|
588 |
def _local_dump(appid, output): |
|
589 |
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
|
590 |
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
|
591 |
mih = config.migration_handler(connect=False, verbosity=1) |
0 | 592 |
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
|
593 |
mih.shutdown() |
0 | 594 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
595 |
def _local_restore(appid, backupfile, drop, systemonly=True): |
0 | 596 |
config = ServerConfiguration.config_for(appid) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
mih.restore_database(backupfile, drop, systemonly, askconfirm=False) |
0 | 601 |
repo = mih.repo_connect() |
602 |
# version of the database |
|
603 |
dbversions = repo.get_versions() |
|
604 |
mih.shutdown() |
|
605 |
if not dbversions: |
|
606 |
print "bad or missing version information in the database, don't upgrade file system" |
|
607 |
return |
|
608 |
# version of installed software |
|
609 |
eversion = dbversions['cubicweb'] |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
610 |
status = instance_status(config, eversion, dbversions) |
0 | 611 |
# * database version > installed software |
612 |
if status == 'needsoftupgrade': |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
613 |
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
|
614 |
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
|
615 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 616 |
return |
617 |
# * database version < installed software, an upgrade will be necessary |
|
618 |
# 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
|
619 |
elif status == 'needapplupgrade': |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
620 |
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
|
621 |
print "** Migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
622 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 623 |
return |
624 |
# * database version = installed software, database version = instance fs version |
|
625 |
# ok! |
|
626 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
627 |
def instance_status(config, cubicwebapplversion, vcconf): |
0 | 628 |
cubicwebversion = config.cubicweb_version() |
629 |
if cubicwebapplversion > cubicwebversion: |
|
630 |
return 'needsoftupgrade' |
|
631 |
if cubicwebapplversion < cubicwebversion: |
|
632 |
return 'needapplupgrade' |
|
633 |
for cube in config.cubes(): |
|
634 |
try: |
|
635 |
softversion = config.cube_version(cube) |
|
636 |
except ConfigurationError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
637 |
print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube |
0 | 638 |
continue |
639 |
try: |
|
640 |
applversion = vcconf[cube] |
|
641 |
except KeyError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
642 |
print '-> Error: no cube version information for %s in version configuration.' % cube |
1469 | 643 |
continue |
0 | 644 |
if softversion == applversion: |
645 |
continue |
|
646 |
if softversion > applversion: |
|
647 |
return 'needsoftupgrade' |
|
648 |
elif softversion < applversion: |
|
649 |
return 'needapplupgrade' |
|
650 |
return None |
|
1469 | 651 |
|
0 | 652 |
|
653 |
class DBDumpCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
654 |
"""Backup the system database of an instance. |
1469 | 655 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
656 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
657 |
the identifier of the instance to backup |
0 | 658 |
format [[user@]host:]appname |
659 |
""" |
|
660 |
name = 'db-dump' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
661 |
arguments = '<instance>' |
0 | 662 |
|
663 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
664 |
('output', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
665 |
{'short': 'o', 'type' : 'string', 'metavar' : '<file>', |
0 | 666 |
'default' : None, |
667 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
668 |
), |
|
669 |
('sudo', |
|
670 |
{'short': 's', 'action' : 'store_true', |
|
671 |
'default' : False, |
|
672 |
'help': 'Use sudo on the remote host.'} |
|
673 |
), |
|
674 |
) |
|
675 |
||
676 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
677 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 678 |
if ':' in appid: |
679 |
host, appid = appid.split(':') |
|
680 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
681 |
else: |
|
682 |
_local_dump(appid, self.config.output) |
|
683 |
||
684 |
||
685 |
class DBRestoreCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
686 |
"""Restore the system database of an instance. |
1469 | 687 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
688 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
689 |
the identifier of the instance to restore |
0 | 690 |
""" |
691 |
name = 'db-restore' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
692 |
arguments = '<instance> <backupfile>' |
0 | 693 |
|
694 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
695 |
('no-drop', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
696 |
{'short': 'n', 'action' : 'store_true', 'default' : False, |
0 | 697 |
'help': 'for some reason the database doesn\'t exist and so ' |
698 |
'should not be dropped.'} |
|
699 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
700 |
('restore-all', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
701 |
{'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
|
702 |
'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
|
703 |
'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
|
704 |
'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
|
705 |
'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
|
706 |
), |
0 | 707 |
) |
708 |
||
709 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
710 |
appid = pop_arg(args, 1, msg='No instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
711 |
backupfile = pop_arg(args, msg='No backup file or timestamp specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
712 |
_local_restore(appid, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
713 |
drop=not self.config.no_drop, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
714 |
systemonly=not self.config.restore_all) |
0 | 715 |
|
716 |
||
717 |
class DBCopyCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
718 |
"""Copy the system database of an instance (backup and restore). |
1469 | 719 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
720 |
<src-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 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
724 |
<dest-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
725 |
the identifier of the instance to restore |
0 | 726 |
""" |
727 |
name = 'db-copy' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
728 |
arguments = '<src-instance> <dest-instance>' |
0 | 729 |
|
730 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
731 |
('no-drop', |
1469 | 732 |
{'short': 'n', 'action' : 'store_true', |
0 | 733 |
'default' : False, |
734 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
735 |
'should not be dropped.'} |
|
736 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
737 |
('keep-dump', |
0 | 738 |
{'short': 'k', 'action' : 'store_true', |
739 |
'default' : False, |
|
740 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
741 |
), |
|
742 |
('sudo', |
|
743 |
{'short': 's', 'action' : 'store_true', |
|
744 |
'default' : False, |
|
745 |
'help': 'Use sudo on the remote host.'} |
|
746 |
), |
|
747 |
) |
|
748 |
||
749 |
def run(self, args): |
|
750 |
import tempfile |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
751 |
srcappid = pop_arg(args, 1, msg='No source instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
752 |
destappid = pop_arg(args, msg='No destination instance specified !') |
3148 | 753 |
fd, output = tempfile.mkstemp() |
754 |
os.close(fd) |
|
0 | 755 |
if ':' in srcappid: |
756 |
host, srcappid = srcappid.split(':') |
|
757 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
758 |
else: |
|
759 |
_local_dump(srcappid, output) |
|
760 |
_local_restore(destappid, output, not self.config.no_drop) |
|
761 |
if self.config.keep_dump: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
762 |
print '-> you can get the dump file at', output |
0 | 763 |
else: |
764 |
os.remove(output) |
|
765 |
||
1469 | 766 |
|
0 | 767 |
class CheckRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
768 |
"""Check integrity of the system database of an instance. |
1469 | 769 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
770 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
771 |
the identifier of the instance to check |
0 | 772 |
""" |
773 |
name = 'db-check' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
774 |
arguments = '<instance>' |
0 | 775 |
|
776 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
777 |
('checks', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
778 |
{'short': 'c', 'type' : 'csv', 'metavar' : '<check list>', |
0 | 779 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
780 |
'help': 'Comma separated list of check to run. By default run all \ |
|
781 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
782 |
), |
|
1469 | 783 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
784 |
('autofix', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
785 |
{'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 786 |
'default' : False, |
787 |
'help': 'Automatically correct integrity problems if this option \ |
|
788 |
is set to "y" or "yes", else only display them'} |
|
789 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
790 |
('reindex', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
791 |
{'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 792 |
'default' : False, |
793 |
'help': 're-indexes the database for full text search if this \ |
|
794 |
option is set to "y" or "yes" (may be long for large database).'} |
|
795 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
796 |
('force', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
797 |
{'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
|
798 |
'default' : False, |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
799 |
'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
|
800 |
), |
1469 | 801 |
|
0 | 802 |
) |
803 |
||
804 |
def run(self, args): |
|
805 |
from cubicweb.server.checkintegrity import check |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
806 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 807 |
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
|
808 |
config.repairing = self.config.force |
0 | 809 |
repo, cnx = repo_cnx(config) |
810 |
check(repo, cnx, |
|
811 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
812 |
||
813 |
||
814 |
class RebuildFTICommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
815 |
"""Rebuild the full-text index of the system database of an instance. |
1469 | 816 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
817 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
818 |
the identifier of the instance to rebuild |
0 | 819 |
""" |
820 |
name = 'db-rebuild-fti' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
821 |
arguments = '<instance>' |
0 | 822 |
|
823 |
options = () |
|
824 |
||
825 |
def run(self, args): |
|
826 |
from cubicweb.server.checkintegrity import reindex_entities |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
827 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 828 |
config = ServerConfiguration.config_for(appid) |
829 |
repo, cnx = repo_cnx(config) |
|
830 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
831 |
reindex_entities(repo.schema, session) |
|
832 |
cnx.commit() |
|
833 |
||
1469 | 834 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
835 |
class SynchronizeInstanceSchemaCommand(Command): |
0 | 836 |
"""Synchronize persistent schema with cube schema. |
1469 | 837 |
|
0 | 838 |
Will synchronize common stuff between the cube schema and the |
839 |
actual persistent schema, but will not add/remove any entity or relation. |
|
840 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
841 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
842 |
the identifier of the instance to synchronize. |
0 | 843 |
""" |
844 |
name = 'schema-sync' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
845 |
arguments = '<instance>' |
0 | 846 |
|
847 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
848 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 849 |
config = ServerConfiguration.config_for(appid) |
850 |
mih = config.migration_handler() |
|
851 |
mih.cmd_synchronize_schema() |
|
852 |
||
853 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
854 |
register_commands( (CreateInstanceDBCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
855 |
InitInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
856 |
GrantUserOnInstanceCommand, |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
857 |
ResetAdminPasswordCommand, |
0 | 858 |
StartRepositoryCommand, |
859 |
DBDumpCommand, |
|
860 |
DBRestoreCommand, |
|
861 |
DBCopyCommand, |
|
862 |
CheckRepositoryCommand, |
|
863 |
RebuildFTICommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
864 |
SynchronizeInstanceSchemaCommand, |
0 | 865 |
) ) |