author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 15 Jun 2009 12:42:16 +0200 | |
branch | stable |
changeset 2106 | 2295f2aba61d |
parent 2105 | 92ea410806fe |
child 2107 | 6c4a4c514ac2 |
permissions | -rw-r--r-- |
0 | 1 |
"""cubicweb-ctl commands and command handlers specific to the server.serverconfig |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
10 |
import sys |
0 | 11 |
import os |
12 |
||
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
|
13 |
from logilab.common.configuration import REQUIRED, Configuration |
1132 | 14 |
from logilab.common.clcommands import register_commands, cmd_run, pop_arg |
0 | 15 |
|
16 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError |
|
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
|
17 |
from cubicweb.toolsutils import Command, CommandHandler, confirm |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
18 |
from cubicweb.server import 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
|
19 |
from cubicweb.server.utils import ask_source_config |
0 | 20 |
from cubicweb.server.serverconfig import ServerConfiguration |
21 |
||
22 |
||
23 |
# utility functions ########################################################### |
|
24 |
||
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
25 |
def source_cnx(source, dbname=None, special_privs=False, verbose=True): |
0 | 26 |
"""open and return a connection to the system database defined in the |
27 |
given server.serverconfig |
|
28 |
""" |
|
29 |
from getpass import getpass |
|
30 |
from logilab.common.db import get_connection |
|
31 |
dbhost = source['db-host'] |
|
32 |
if dbname is None: |
|
33 |
dbname = source['db-name'] |
|
34 |
driver = source['db-driver'] |
|
35 |
print '**** connecting to %s database %s@%s' % (driver, dbname, dbhost), |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
36 |
if not verbose or (not special_privs and source.get('db-user')): |
0 | 37 |
user = source['db-user'] |
38 |
print 'as', user |
|
39 |
if source.get('db-password'): |
|
40 |
password = source['db-password'] |
|
41 |
else: |
|
42 |
password = getpass('password: ') |
|
43 |
else: |
|
44 |
print |
|
45 |
if special_privs: |
|
46 |
print 'WARNING' |
|
47 |
print 'the user will need the following special access rights on the database:' |
|
48 |
print special_privs |
|
49 |
print |
|
50 |
default_user = source.get('db-user', os.environ.get('USER', '')) |
|
51 |
user = raw_input('user (%r by default): ' % default_user) |
|
52 |
user = user or default_user |
|
53 |
if user == source.get('db-user') and source.get('db-password'): |
|
54 |
password = source['db-password'] |
|
55 |
else: |
|
56 |
password = getpass('password: ') |
|
57 |
return get_connection(driver, dbhost, dbname, user, password=password, |
|
58 |
port=source.get('db-port')) |
|
59 |
||
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
60 |
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
|
61 |
special_privs='CREATE/DROP DATABASE', verbose=True): |
0 | 62 |
"""shortcut to get a connextion to the application system database |
63 |
defined in the given config. If <dbms_system_base> is True, |
|
64 |
connect to the dbms system database instead (for task such as |
|
65 |
create/drop the application database) |
|
66 |
""" |
|
67 |
if dbms_system_base: |
|
68 |
from logilab.common.adbh import get_adv_func_helper |
|
69 |
system_db = get_adv_func_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
|
70 |
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
|
71 |
return source_cnx(source, special_privs=special_privs, verbose=verbose) |
0 | 72 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
73 |
def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
0 | 74 |
"""return a connection on the RDMS system table (to create/drop a user |
75 |
or a database |
|
76 |
""" |
|
77 |
from logilab.common.adbh import get_adv_func_helper |
|
78 |
special_privs = '' |
|
79 |
driver = source['db-driver'] |
|
80 |
helper = get_adv_func_helper(driver) |
|
81 |
if user is not None and helper.users_support: |
|
82 |
special_privs += '%s USER' % what |
|
83 |
if db is not None: |
|
84 |
special_privs += ' %s DATABASE' % what |
|
85 |
# 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
|
86 |
cnx = system_source_cnx(source, True, special_privs=special_privs, verbose=verbose) |
0 | 87 |
# disable autocommit (isolation_level(1)) because DROP and |
88 |
# CREATE DATABASE can't be executed in a transaction |
|
89 |
try: |
|
90 |
cnx.set_isolation_level(0) |
|
91 |
except AttributeError: |
|
92 |
# set_isolation_level() is psycopg specific |
|
93 |
pass |
|
94 |
return cnx |
|
1469 | 95 |
|
0 | 96 |
def repo_cnx(config): |
97 |
"""return a in-memory repository and a db api connection it""" |
|
98 |
from cubicweb.dbapi import in_memory_cnx |
|
99 |
from cubicweb.server.utils import manager_userpasswd |
|
100 |
try: |
|
101 |
login = config.sources()['admin']['login'] |
|
102 |
pwd = config.sources()['admin']['password'] |
|
103 |
except KeyError: |
|
104 |
login, pwd = manager_userpasswd() |
|
105 |
while True: |
|
106 |
try: |
|
107 |
return in_memory_cnx(config, login, pwd) |
|
108 |
except AuthenticationError: |
|
109 |
print 'wrong user/password' |
|
1682
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
110 |
# 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
|
111 |
config._cubes = None |
0 | 112 |
login, pwd = manager_userpasswd() |
1469 | 113 |
|
0 | 114 |
# repository specific command handlers ######################################## |
115 |
||
116 |
class RepositoryCreateHandler(CommandHandler): |
|
117 |
cmdname = 'create' |
|
118 |
cfgname = 'repository' |
|
119 |
||
120 |
def bootstrap(self, cubes, inputlevel=0): |
|
121 |
"""create an application by copying files from the given cube and by |
|
122 |
asking information necessary to build required configuration files |
|
123 |
""" |
|
124 |
config = self.config |
|
125 |
print 'application\'s repository configuration' |
|
126 |
print '-' * 72 |
|
127 |
config.input_config('email', inputlevel) |
|
128 |
if config.pyro_enabled(): |
|
129 |
config.input_config('pyro-server', inputlevel) |
|
130 |
print |
|
131 |
print 'repository sources configuration' |
|
132 |
print '-' * 72 |
|
133 |
sourcesfile = config.sources_file() |
|
134 |
sconfig = Configuration(options=SOURCE_TYPES['native'].options) |
|
135 |
sconfig.adapter = 'native' |
|
136 |
sconfig.input_config(inputlevel=inputlevel) |
|
137 |
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
|
138 |
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
|
139 |
# 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
|
140 |
# 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
|
141 |
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
|
142 |
sourcescfg[cube] = ask_source_config(cube, inputlevel) |
0 | 143 |
while raw_input('enter another source [y/N]: ').strip().lower() == 'y': |
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
|
144 |
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
|
145 |
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
|
146 |
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
|
147 |
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
|
148 |
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
|
149 |
break |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
150 |
print 'unknown source type, use one of the available type' |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
151 |
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
|
152 |
sourceuri = raw_input('source uri: ').strip() |
2106
2295f2aba61d
check not using "admin" as uri (reserved)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
153 |
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
|
154 |
break |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
155 |
print 'uri already used, choose another one' |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
156 |
sourcescfg[sourceuri] = ask_source_config(sourcetype) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
157 |
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
|
158 |
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
|
159 |
# 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
|
160 |
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
|
161 |
# 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
|
162 |
# 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
|
163 |
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
|
164 |
cubes.append(sourcecube) |
0 | 165 |
sconfig = Configuration(options=USER_OPTIONS) |
166 |
sconfig.input_config(inputlevel=inputlevel) |
|
167 |
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
|
168 |
config.write_sources_file(sourcescfg) |
0 | 169 |
# remember selected cubes for later initialization of the database |
170 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 171 |
|
0 | 172 |
def postcreate(self): |
173 |
if confirm('do you want to create repository\'s system database?'): |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
174 |
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
|
175 |
cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity) |
0 | 176 |
else: |
177 |
print 'nevermind, you can do it later using the db-create command' |
|
1469 | 178 |
|
0 | 179 |
USER_OPTIONS = ( |
180 |
('login', {'type' : 'string', |
|
181 |
'default': REQUIRED, |
|
182 |
'help': "cubicweb manager account's login " |
|
183 |
'(this user will be created)', |
|
184 |
'inputlevel': 0, |
|
185 |
}), |
|
186 |
('password', {'type' : 'password', |
|
187 |
'help': "cubicweb manager account's password", |
|
188 |
'inputlevel': 0, |
|
189 |
}), |
|
190 |
) |
|
191 |
||
192 |
||
193 |
class RepositoryDeleteHandler(CommandHandler): |
|
194 |
cmdname = 'delete' |
|
195 |
cfgname = 'repository' |
|
196 |
||
197 |
def cleanup(self): |
|
198 |
"""remove application's configuration and database""" |
|
199 |
from logilab.common.adbh import get_adv_func_helper |
|
200 |
source = self.config.sources()['system'] |
|
201 |
dbname = source['db-name'] |
|
202 |
helper = get_adv_func_helper(source['db-driver']) |
|
203 |
if confirm('delete database %s ?' % dbname): |
|
204 |
user = source['db-user'] or None |
|
205 |
cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
|
206 |
cursor = cnx.cursor() |
|
207 |
try: |
|
208 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
209 |
print 'database %s dropped' % dbname |
|
210 |
# XXX should check we are not connected as user |
|
211 |
if user and helper.users_support and \ |
|
212 |
confirm('delete user %s ?' % user, default_is_yes=False): |
|
213 |
cursor.execute('DROP USER %s' % user) |
|
214 |
print 'user %s dropped' % user |
|
215 |
cnx.commit() |
|
216 |
except: |
|
217 |
cnx.rollback() |
|
218 |
raise |
|
219 |
||
1469 | 220 |
|
0 | 221 |
class RepositoryStartHandler(CommandHandler): |
222 |
cmdname = 'start' |
|
223 |
cfgname = 'repository' |
|
224 |
||
225 |
def start_command(self, ctlconf, debug): |
|
226 |
command = ['cubicweb-ctl start-repository '] |
|
227 |
if debug: |
|
228 |
command.append('--debug') |
|
229 |
command.append(self.config.appid) |
|
230 |
return ' '.join(command) |
|
1469 | 231 |
|
0 | 232 |
|
233 |
class RepositoryStopHandler(CommandHandler): |
|
234 |
cmdname = 'stop' |
|
235 |
cfgname = 'repository' |
|
236 |
||
237 |
def poststop(self): |
|
238 |
"""if pyro is enabled, ensure the repository is correctly |
|
239 |
unregistered |
|
240 |
""" |
|
241 |
if self.config.pyro_enabled(): |
|
242 |
from cubicweb.server.repository import pyro_unregister |
|
243 |
pyro_unregister(self.config) |
|
1469 | 244 |
|
0 | 245 |
|
246 |
# repository specific commands ################################################ |
|
247 |
class CreateApplicationDBCommand(Command): |
|
248 |
"""Create the system database of an application (run after 'create'). |
|
1469 | 249 |
|
0 | 250 |
You will be prompted for a login / password to use to connect to |
251 |
the system database. The given user should have almost all rights |
|
252 |
on the database (ie a super user on the dbms allowed to create |
|
253 |
database, users, languages...). |
|
254 |
||
255 |
<application> |
|
256 |
the identifier of the application to initialize. |
|
257 |
""" |
|
258 |
name = 'db-create' |
|
259 |
arguments = '<application>' |
|
1469 | 260 |
|
0 | 261 |
options = ( |
262 |
("create-db", |
|
263 |
{'short': 'c', 'type': "yn", 'metavar': '<y or n>', |
|
264 |
'default': True, |
|
265 |
'help': 'create the database (yes by default)'}), |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
266 |
("verbose", |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
267 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
268 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
269 |
'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
|
270 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
271 |
), |
0 | 272 |
) |
273 |
def run(self, args): |
|
274 |
"""run the command with its specific arguments""" |
|
275 |
from logilab.common.adbh import get_adv_func_helper |
|
276 |
from indexer import get_indexer |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
277 |
verbose = self.get('verbose') |
0 | 278 |
appid = pop_arg(args, msg="No application specified !") |
279 |
config = ServerConfiguration.config_for(appid) |
|
280 |
create_db = self.config.create_db |
|
281 |
source = config.sources()['system'] |
|
282 |
driver = source['db-driver'] |
|
283 |
helper = get_adv_func_helper(driver) |
|
284 |
if create_db: |
|
285 |
# 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
|
286 |
dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
0 | 287 |
cursor = dbcnx.cursor() |
288 |
try: |
|
289 |
if helper.users_support: |
|
290 |
user = source['db-user'] |
|
291 |
if not helper.user_exists(cursor, user) and \ |
|
292 |
confirm('create db user %s ?' % user, default_is_yes=False): |
|
293 |
helper.create_user(source['db-user'], source['db-password']) |
|
294 |
print 'user %s created' % user |
|
295 |
dbname = source['db-name'] |
|
296 |
if dbname in helper.list_databases(cursor): |
|
297 |
if confirm('DB %s already exists -- do you want to drop it ?' % dbname): |
|
298 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
299 |
else: |
|
300 |
return |
|
301 |
if dbcnx.logged_user != source['db-user']: |
|
302 |
helper.create_database(cursor, dbname, source['db-user'], |
|
303 |
source['db-encoding']) |
|
304 |
else: |
|
305 |
helper.create_database(cursor, dbname, |
|
306 |
encoding=source['db-encoding']) |
|
307 |
dbcnx.commit() |
|
308 |
print 'database %s created' % source['db-name'] |
|
309 |
except: |
|
310 |
dbcnx.rollback() |
|
311 |
raise |
|
1469 | 312 |
cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
0 | 313 |
cursor = cnx.cursor() |
314 |
indexer = get_indexer(driver) |
|
315 |
indexer.init_extensions(cursor) |
|
1469 | 316 |
# postgres specific stuff |
0 | 317 |
if driver == 'postgres': |
318 |
# install plpythonu/plpgsql language if not installed by the cube |
|
319 |
for extlang in ('plpythonu', 'plpgsql'): |
|
320 |
helper.create_language(cursor, extlang) |
|
321 |
cursor.close() |
|
322 |
cnx.commit() |
|
323 |
print 'database for application %s created and necessary extensions installed' % appid |
|
324 |
print |
|
325 |
if confirm('do you want to initialize the system database?'): |
|
326 |
cmd_run('db-init', config.appid) |
|
327 |
else: |
|
328 |
print 'nevermind, you can do it later using the db-init command' |
|
329 |
||
1469 | 330 |
|
0 | 331 |
class InitApplicationCommand(Command): |
332 |
"""Initialize the system database of an application (run after 'db-create'). |
|
1469 | 333 |
|
0 | 334 |
You will be prompted for a login / password to use to connect to |
335 |
the system database. The given user should have the create tables, |
|
336 |
and grant permissions. |
|
337 |
||
338 |
<application> |
|
339 |
the identifier of the application to initialize. |
|
340 |
""" |
|
341 |
name = 'db-init' |
|
342 |
arguments = '<application>' |
|
1469 | 343 |
|
0 | 344 |
options = ( |
345 |
("drop", |
|
346 |
{'short': 'd', 'action': 'store_true', |
|
347 |
'default': False, |
|
348 |
'help': 'insert drop statements to remove previously existant \ |
|
349 |
tables, indexes... (no by default)'}), |
|
350 |
) |
|
351 |
||
352 |
def run(self, args): |
|
353 |
from cubicweb.server import init_repository |
|
354 |
appid = pop_arg(args, msg="No application specified !") |
|
355 |
config = ServerConfiguration.config_for(appid) |
|
356 |
init_repository(config, drop=self.config.drop) |
|
357 |
||
358 |
||
359 |
class GrantUserOnApplicationCommand(Command): |
|
360 |
"""Grant a database user on a repository system database. |
|
1469 | 361 |
|
0 | 362 |
<application> |
363 |
the identifier of the application |
|
364 |
<user> |
|
365 |
the database's user requiring grant access |
|
366 |
""" |
|
367 |
name = 'db-grant-user' |
|
368 |
arguments = '<application> <user>' |
|
369 |
||
370 |
options = ( |
|
371 |
("set-owner", |
|
1469 | 372 |
{'short': 'o', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 373 |
'default' : False, |
374 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
375 |
), |
|
376 |
) |
|
377 |
def run(self, args): |
|
378 |
"""run the command with its specific arguments""" |
|
379 |
from cubicweb.server.sqlutils import sqlexec, sqlgrants |
|
380 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
381 |
user = pop_arg(args, msg="No user specified !") |
|
382 |
config = ServerConfiguration.config_for(appid) |
|
383 |
source = config.sources()['system'] |
|
384 |
set_owner = self.config.set_owner |
|
385 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
386 |
cursor = cnx.cursor() |
|
387 |
schema = config.load_schema() |
|
388 |
try: |
|
389 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
390 |
set_owner=set_owner), cursor) |
|
391 |
except Exception, ex: |
|
392 |
cnx.rollback() |
|
393 |
import traceback |
|
394 |
traceback.print_exc() |
|
395 |
print 'An error occured:', ex |
|
396 |
else: |
|
397 |
cnx.commit() |
|
398 |
print 'grants given to %s on application %s' % (appid, user) |
|
399 |
||
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
400 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
401 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
402 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
403 |
<application> |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
404 |
the identifier of the application |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
405 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
406 |
name = 'reset-admin-pwd' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
407 |
arguments = '<application>' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
408 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
409 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
410 |
"""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
|
411 |
from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
412 |
from cubicweb.server.utils import crypt_password, manager_userpasswd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
413 |
appid = pop_arg(args, 1, msg="No application specified !") |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
414 |
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
|
415 |
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
|
416 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
417 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
418 |
except KeyError: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
419 |
print 'could not get cubicweb administrator login' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
420 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
421 |
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
|
422 |
cursor = cnx.cursor() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
423 |
_, 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
|
424 |
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
|
425 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
426 |
sqlexec("UPDATE %(sp)sCWUser SET %(sp)supassword='%(p)s' WHERE %(sp)slogin='%(l)s'" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
427 |
% {'sp': SQL_PREFIX, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
428 |
'p': crypt_password(passwd), 'l': adminlogin}, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
429 |
cursor, withpb=False) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
430 |
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
|
431 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
432 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
433 |
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
|
434 |
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
|
435 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
436 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
437 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
438 |
traceback.print_exc() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
439 |
print 'An error occured:', ex |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
440 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
441 |
cnx.commit() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
442 |
print 'password reset, sources file regenerated' |
0 | 443 |
|
1469 | 444 |
|
0 | 445 |
class StartRepositoryCommand(Command): |
446 |
"""Start an CubicWeb RQL server for a given application. |
|
1469 | 447 |
|
0 | 448 |
The server will be accessible through pyro |
449 |
||
450 |
<application> |
|
451 |
the identifier of the application to initialize. |
|
452 |
""" |
|
453 |
name = 'start-repository' |
|
454 |
arguments = '<application>' |
|
1469 | 455 |
|
0 | 456 |
options = ( |
457 |
("debug", |
|
458 |
{'short': 'D', 'action' : 'store_true', |
|
459 |
'help': 'start server in debug mode.'}), |
|
460 |
) |
|
461 |
||
462 |
def run(self, args): |
|
463 |
from cubicweb.server.server import RepositoryServer |
|
464 |
appid = pop_arg(args, msg="No application specified !") |
|
465 |
config = ServerConfiguration.config_for(appid) |
|
466 |
debug = self.config.debug |
|
467 |
# create the server |
|
468 |
server = RepositoryServer(config, debug) |
|
469 |
# go ! (don't daemonize in debug mode) |
|
470 |
if not debug and server.daemonize(config['pid-file']) == -1: |
|
471 |
return |
|
472 |
uid = config['uid'] |
|
473 |
if uid is not None: |
|
474 |
try: |
|
475 |
uid = int(uid) |
|
476 |
except ValueError: |
|
477 |
from pwd import getpwnam |
|
478 |
uid = getpwnam(uid).pw_uid |
|
479 |
os.setuid(uid) |
|
480 |
server.install_sig_handlers() |
|
481 |
server.connect(config['host'], 0) |
|
482 |
server.run() |
|
483 |
||
484 |
||
485 |
def _remote_dump(host, appid, output, sudo=False): |
|
486 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s.dump %s' % (appid, appid) |
|
487 |
if sudo: |
|
488 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
489 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
490 |
print dmpcmd |
|
491 |
if os.system(dmpcmd): |
|
492 |
raise ExecutionError('Error while dumping the database') |
|
493 |
if output is None: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
494 |
from datetime import date |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
495 |
date = date.today().strftime('%Y-%m-%d') |
0 | 496 |
output = '%s-%s.dump' % (appid, date) |
497 |
cmd = 'scp %s:/tmp/%s.dump %s' % (host, appid, output) |
|
498 |
print cmd |
|
499 |
if os.system(cmd): |
|
500 |
raise ExecutionError('Error while retrieving the dump') |
|
501 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s.dump"' % (host, appid) |
|
502 |
print rmcmd |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
503 |
if os.system(rmcmd) and not confirm( |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
504 |
'an error occured while deleting remote dump. Continue anyway?'): |
0 | 505 |
raise ExecutionError('Error while deleting remote dump') |
506 |
||
507 |
def _local_dump(appid, output): |
|
508 |
config = ServerConfiguration.config_for(appid) |
|
509 |
# schema=1 to avoid unnecessary schema loading |
|
510 |
mih = config.migration_handler(connect=False, schema=1) |
|
511 |
mih.backup_database(output, askconfirm=False) |
|
512 |
||
513 |
def _local_restore(appid, backupfile, drop): |
|
514 |
config = ServerConfiguration.config_for(appid) |
|
515 |
# schema=1 to avoid unnecessary schema loading |
|
516 |
mih = config.migration_handler(connect=False, schema=1) |
|
517 |
mih.restore_database(backupfile, drop) |
|
518 |
repo = mih.repo_connect() |
|
519 |
# version of the database |
|
520 |
dbversions = repo.get_versions() |
|
521 |
mih.shutdown() |
|
522 |
if not dbversions: |
|
523 |
print "bad or missing version information in the database, don't upgrade file system" |
|
524 |
return |
|
525 |
# version of installed software |
|
526 |
eversion = dbversions['cubicweb'] |
|
527 |
status = application_status(config, eversion, dbversions) |
|
528 |
# * database version > installed software |
|
529 |
if status == 'needsoftupgrade': |
|
530 |
print "database is using some earlier version than installed software!" |
|
531 |
print "please upgrade your software and then upgrade the instance" |
|
532 |
print "using command 'cubicweb-ctl upgrade %s'" % config.appid |
|
533 |
return |
|
534 |
# * database version < installed software, an upgrade will be necessary |
|
535 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
536 |
if status == 'needapplupgrade': |
|
537 |
print "database is using some older version than installed software." |
|
538 |
print "You'll have to upgrade the instance using command" |
|
539 |
print "'cubicweb-ctl upgrade %s'" % config.appid |
|
540 |
return |
|
541 |
# * database version = installed software, database version = instance fs version |
|
542 |
# ok! |
|
543 |
||
544 |
||
545 |
def application_status(config, cubicwebapplversion, vcconf): |
|
546 |
cubicwebversion = config.cubicweb_version() |
|
547 |
if cubicwebapplversion > cubicwebversion: |
|
548 |
return 'needsoftupgrade' |
|
549 |
if cubicwebapplversion < cubicwebversion: |
|
550 |
return 'needapplupgrade' |
|
551 |
for cube in config.cubes(): |
|
552 |
try: |
|
553 |
softversion = config.cube_version(cube) |
|
554 |
except ConfigurationError: |
|
555 |
print "no cube version information for %s, is the cube installed?" % cube |
|
556 |
continue |
|
557 |
try: |
|
558 |
applversion = vcconf[cube] |
|
559 |
except KeyError: |
|
560 |
print "no cube version information for %s in version configuration" % cube |
|
1469 | 561 |
continue |
0 | 562 |
if softversion == applversion: |
563 |
continue |
|
564 |
if softversion > applversion: |
|
565 |
return 'needsoftupgrade' |
|
566 |
elif softversion < applversion: |
|
567 |
return 'needapplupgrade' |
|
568 |
return None |
|
1469 | 569 |
|
0 | 570 |
|
571 |
class DBDumpCommand(Command): |
|
572 |
"""Backup the system database of an application. |
|
1469 | 573 |
|
0 | 574 |
<application> |
575 |
the identifier of the application to backup |
|
576 |
format [[user@]host:]appname |
|
577 |
""" |
|
578 |
name = 'db-dump' |
|
579 |
arguments = '<application>' |
|
580 |
||
581 |
options = ( |
|
582 |
("output", |
|
1469 | 583 |
{'short': 'o', 'type' : "string", 'metavar' : '<file>', |
0 | 584 |
'default' : None, |
585 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
586 |
), |
|
587 |
('sudo', |
|
588 |
{'short': 's', 'action' : 'store_true', |
|
589 |
'default' : False, |
|
590 |
'help': 'Use sudo on the remote host.'} |
|
591 |
), |
|
592 |
) |
|
593 |
||
594 |
def run(self, args): |
|
595 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
596 |
if ':' in appid: |
|
597 |
host, appid = appid.split(':') |
|
598 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
599 |
else: |
|
600 |
_local_dump(appid, self.config.output) |
|
601 |
||
602 |
||
603 |
class DBRestoreCommand(Command): |
|
604 |
"""Restore the system database of an application. |
|
1469 | 605 |
|
0 | 606 |
<application> |
607 |
the identifier of the application to restore |
|
608 |
""" |
|
609 |
name = 'db-restore' |
|
610 |
arguments = '<application> <backupfile>' |
|
611 |
||
612 |
options = ( |
|
613 |
("no-drop", |
|
1469 | 614 |
{'short': 'n', 'action' : 'store_true', |
0 | 615 |
'default' : False, |
616 |
'help': 'for some reason the database doesn\'t exist and so ' |
|
617 |
'should not be dropped.'} |
|
618 |
), |
|
619 |
) |
|
620 |
||
621 |
def run(self, args): |
|
622 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
623 |
backupfile = pop_arg(args, msg="No backup file specified !") |
|
624 |
_local_restore(appid, backupfile, not self.config.no_drop) |
|
625 |
||
626 |
||
627 |
class DBCopyCommand(Command): |
|
628 |
"""Copy the system database of an application (backup and restore). |
|
1469 | 629 |
|
0 | 630 |
<src-application> |
631 |
the identifier of the application to backup |
|
632 |
format [[user@]host:]appname |
|
633 |
||
634 |
<dest-application> |
|
635 |
the identifier of the application to restore |
|
636 |
""" |
|
637 |
name = 'db-copy' |
|
638 |
arguments = '<src-application> <dest-application>' |
|
639 |
||
640 |
options = ( |
|
641 |
("no-drop", |
|
1469 | 642 |
{'short': 'n', 'action' : 'store_true', |
0 | 643 |
'default' : False, |
644 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
645 |
'should not be dropped.'} |
|
646 |
), |
|
647 |
("keep-dump", |
|
648 |
{'short': 'k', 'action' : 'store_true', |
|
649 |
'default' : False, |
|
650 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
651 |
), |
|
652 |
('sudo', |
|
653 |
{'short': 's', 'action' : 'store_true', |
|
654 |
'default' : False, |
|
655 |
'help': 'Use sudo on the remote host.'} |
|
656 |
), |
|
657 |
) |
|
658 |
||
659 |
def run(self, args): |
|
660 |
import tempfile |
|
661 |
srcappid = pop_arg(args, 1, msg="No source application specified !") |
|
662 |
destappid = pop_arg(args, msg="No destination application specified !") |
|
663 |
output = tempfile.mktemp() |
|
664 |
if ':' in srcappid: |
|
665 |
host, srcappid = srcappid.split(':') |
|
666 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
667 |
else: |
|
668 |
_local_dump(srcappid, output) |
|
669 |
_local_restore(destappid, output, not self.config.no_drop) |
|
670 |
if self.config.keep_dump: |
|
671 |
print 'you can get the dump file at', output |
|
672 |
else: |
|
673 |
os.remove(output) |
|
674 |
||
1469 | 675 |
|
0 | 676 |
class CheckRepositoryCommand(Command): |
677 |
"""Check integrity of the system database of an application. |
|
1469 | 678 |
|
0 | 679 |
<application> |
680 |
the identifier of the application to check |
|
681 |
""" |
|
682 |
name = 'db-check' |
|
683 |
arguments = '<application>' |
|
684 |
||
685 |
options = ( |
|
686 |
("checks", |
|
1469 | 687 |
{'short': 'c', 'type' : "csv", 'metavar' : '<check list>', |
0 | 688 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
689 |
'help': 'Comma separated list of check to run. By default run all \ |
|
690 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
691 |
), |
|
1469 | 692 |
|
0 | 693 |
("autofix", |
1469 | 694 |
{'short': 'a', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 695 |
'default' : False, |
696 |
'help': 'Automatically correct integrity problems if this option \ |
|
697 |
is set to "y" or "yes", else only display them'} |
|
698 |
), |
|
699 |
("reindex", |
|
1469 | 700 |
{'short': 'r', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 701 |
'default' : False, |
702 |
'help': 're-indexes the database for full text search if this \ |
|
703 |
option is set to "y" or "yes" (may be long for large database).'} |
|
704 |
), |
|
1469 | 705 |
|
0 | 706 |
) |
707 |
||
708 |
def run(self, args): |
|
709 |
from cubicweb.server.checkintegrity import check |
|
710 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
711 |
config = ServerConfiguration.config_for(appid) |
|
712 |
repo, cnx = repo_cnx(config) |
|
713 |
check(repo, cnx, |
|
714 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
715 |
||
716 |
||
717 |
class RebuildFTICommand(Command): |
|
718 |
"""Rebuild the full-text index of the system database of an application. |
|
1469 | 719 |
|
0 | 720 |
<application> |
721 |
the identifier of the application to rebuild |
|
722 |
""" |
|
723 |
name = 'db-rebuild-fti' |
|
724 |
arguments = '<application>' |
|
725 |
||
726 |
options = () |
|
727 |
||
728 |
def run(self, args): |
|
729 |
from cubicweb.server.checkintegrity import reindex_entities |
|
730 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
731 |
config = ServerConfiguration.config_for(appid) |
|
732 |
repo, cnx = repo_cnx(config) |
|
733 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
734 |
reindex_entities(repo.schema, session) |
|
735 |
cnx.commit() |
|
736 |
||
1469 | 737 |
|
0 | 738 |
class SynchronizeApplicationSchemaCommand(Command): |
739 |
"""Synchronize persistent schema with cube schema. |
|
1469 | 740 |
|
0 | 741 |
Will synchronize common stuff between the cube schema and the |
742 |
actual persistent schema, but will not add/remove any entity or relation. |
|
743 |
||
744 |
<application> |
|
745 |
the identifier of the application to synchronize. |
|
746 |
""" |
|
747 |
name = 'schema-sync' |
|
748 |
arguments = '<application>' |
|
749 |
||
750 |
def run(self, args): |
|
751 |
appid = pop_arg(args, msg="No application specified !") |
|
752 |
config = ServerConfiguration.config_for(appid) |
|
753 |
mih = config.migration_handler() |
|
754 |
mih.cmd_synchronize_schema() |
|
755 |
||
756 |
||
1469 | 757 |
register_commands( (CreateApplicationDBCommand, |
0 | 758 |
InitApplicationCommand, |
759 |
GrantUserOnApplicationCommand, |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
760 |
ResetAdminPasswordCommand, |
0 | 761 |
StartRepositoryCommand, |
762 |
DBDumpCommand, |
|
763 |
DBRestoreCommand, |
|
764 |
DBCopyCommand, |
|
765 |
CheckRepositoryCommand, |
|
766 |
RebuildFTICommand, |
|
767 |
SynchronizeApplicationSchemaCommand, |
|
768 |
) ) |