author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 06 Jul 2009 17:36:45 +0200 | |
branch | stable |
changeset 2290 | 47b5236774a0 |
parent 2107 | 6c4a4c514ac2 |
child 2394 | 92bba46b853f |
child 2417 | 18a14c23413c |
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 |
||
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
|
13 |
from logilab.common.configuration import 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 |
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
|
20 |
from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration |
0 | 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 |
|
180 |
class RepositoryDeleteHandler(CommandHandler): |
|
181 |
cmdname = 'delete' |
|
182 |
cfgname = 'repository' |
|
183 |
||
184 |
def cleanup(self): |
|
185 |
"""remove application's configuration and database""" |
|
186 |
from logilab.common.adbh import get_adv_func_helper |
|
187 |
source = self.config.sources()['system'] |
|
188 |
dbname = source['db-name'] |
|
189 |
helper = get_adv_func_helper(source['db-driver']) |
|
190 |
if confirm('delete database %s ?' % dbname): |
|
191 |
user = source['db-user'] or None |
|
192 |
cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
|
193 |
cursor = cnx.cursor() |
|
194 |
try: |
|
195 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
196 |
print 'database %s dropped' % dbname |
|
197 |
# XXX should check we are not connected as user |
|
198 |
if user and helper.users_support and \ |
|
199 |
confirm('delete user %s ?' % user, default_is_yes=False): |
|
200 |
cursor.execute('DROP USER %s' % user) |
|
201 |
print 'user %s dropped' % user |
|
202 |
cnx.commit() |
|
203 |
except: |
|
204 |
cnx.rollback() |
|
205 |
raise |
|
206 |
||
1469 | 207 |
|
0 | 208 |
class RepositoryStartHandler(CommandHandler): |
209 |
cmdname = 'start' |
|
210 |
cfgname = 'repository' |
|
211 |
||
212 |
def start_command(self, ctlconf, debug): |
|
213 |
command = ['cubicweb-ctl start-repository '] |
|
214 |
if debug: |
|
215 |
command.append('--debug') |
|
216 |
command.append(self.config.appid) |
|
217 |
return ' '.join(command) |
|
1469 | 218 |
|
0 | 219 |
|
220 |
class RepositoryStopHandler(CommandHandler): |
|
221 |
cmdname = 'stop' |
|
222 |
cfgname = 'repository' |
|
223 |
||
224 |
def poststop(self): |
|
225 |
"""if pyro is enabled, ensure the repository is correctly |
|
226 |
unregistered |
|
227 |
""" |
|
228 |
if self.config.pyro_enabled(): |
|
229 |
from cubicweb.server.repository import pyro_unregister |
|
230 |
pyro_unregister(self.config) |
|
1469 | 231 |
|
0 | 232 |
|
233 |
# repository specific commands ################################################ |
|
234 |
class CreateApplicationDBCommand(Command): |
|
235 |
"""Create the system database of an application (run after 'create'). |
|
1469 | 236 |
|
0 | 237 |
You will be prompted for a login / password to use to connect to |
238 |
the system database. The given user should have almost all rights |
|
239 |
on the database (ie a super user on the dbms allowed to create |
|
240 |
database, users, languages...). |
|
241 |
||
242 |
<application> |
|
243 |
the identifier of the application to initialize. |
|
244 |
""" |
|
245 |
name = 'db-create' |
|
246 |
arguments = '<application>' |
|
1469 | 247 |
|
0 | 248 |
options = ( |
249 |
("create-db", |
|
250 |
{'short': 'c', 'type': "yn", 'metavar': '<y or n>', |
|
251 |
'default': True, |
|
252 |
'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
|
253 |
("verbose", |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
254 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
255 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
256 |
'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
|
257 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
258 |
), |
0 | 259 |
) |
260 |
def run(self, args): |
|
261 |
"""run the command with its specific arguments""" |
|
262 |
from logilab.common.adbh import get_adv_func_helper |
|
263 |
from indexer import get_indexer |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
264 |
verbose = self.get('verbose') |
0 | 265 |
appid = pop_arg(args, msg="No application specified !") |
266 |
config = ServerConfiguration.config_for(appid) |
|
267 |
create_db = self.config.create_db |
|
268 |
source = config.sources()['system'] |
|
269 |
driver = source['db-driver'] |
|
270 |
helper = get_adv_func_helper(driver) |
|
271 |
if create_db: |
|
272 |
# 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
|
273 |
dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
0 | 274 |
cursor = dbcnx.cursor() |
275 |
try: |
|
276 |
if helper.users_support: |
|
277 |
user = source['db-user'] |
|
278 |
if not helper.user_exists(cursor, user) and \ |
|
279 |
confirm('create db user %s ?' % user, default_is_yes=False): |
|
280 |
helper.create_user(source['db-user'], source['db-password']) |
|
281 |
print 'user %s created' % user |
|
282 |
dbname = source['db-name'] |
|
283 |
if dbname in helper.list_databases(cursor): |
|
284 |
if confirm('DB %s already exists -- do you want to drop it ?' % dbname): |
|
285 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
286 |
else: |
|
287 |
return |
|
288 |
if dbcnx.logged_user != source['db-user']: |
|
289 |
helper.create_database(cursor, dbname, source['db-user'], |
|
290 |
source['db-encoding']) |
|
291 |
else: |
|
292 |
helper.create_database(cursor, dbname, |
|
293 |
encoding=source['db-encoding']) |
|
294 |
dbcnx.commit() |
|
295 |
print 'database %s created' % source['db-name'] |
|
296 |
except: |
|
297 |
dbcnx.rollback() |
|
298 |
raise |
|
1469 | 299 |
cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
0 | 300 |
cursor = cnx.cursor() |
301 |
indexer = get_indexer(driver) |
|
302 |
indexer.init_extensions(cursor) |
|
1469 | 303 |
# postgres specific stuff |
0 | 304 |
if driver == 'postgres': |
305 |
# install plpythonu/plpgsql language if not installed by the cube |
|
306 |
for extlang in ('plpythonu', 'plpgsql'): |
|
307 |
helper.create_language(cursor, extlang) |
|
308 |
cursor.close() |
|
309 |
cnx.commit() |
|
310 |
print 'database for application %s created and necessary extensions installed' % appid |
|
311 |
print |
|
312 |
if confirm('do you want to initialize the system database?'): |
|
313 |
cmd_run('db-init', config.appid) |
|
314 |
else: |
|
315 |
print 'nevermind, you can do it later using the db-init command' |
|
316 |
||
1469 | 317 |
|
0 | 318 |
class InitApplicationCommand(Command): |
319 |
"""Initialize the system database of an application (run after 'db-create'). |
|
1469 | 320 |
|
0 | 321 |
You will be prompted for a login / password to use to connect to |
322 |
the system database. The given user should have the create tables, |
|
323 |
and grant permissions. |
|
324 |
||
325 |
<application> |
|
326 |
the identifier of the application to initialize. |
|
327 |
""" |
|
328 |
name = 'db-init' |
|
329 |
arguments = '<application>' |
|
1469 | 330 |
|
0 | 331 |
options = ( |
332 |
("drop", |
|
333 |
{'short': 'd', 'action': 'store_true', |
|
334 |
'default': False, |
|
335 |
'help': 'insert drop statements to remove previously existant \ |
|
336 |
tables, indexes... (no by default)'}), |
|
337 |
) |
|
338 |
||
339 |
def run(self, args): |
|
340 |
from cubicweb.server import init_repository |
|
341 |
appid = pop_arg(args, msg="No application specified !") |
|
342 |
config = ServerConfiguration.config_for(appid) |
|
343 |
init_repository(config, drop=self.config.drop) |
|
344 |
||
345 |
||
346 |
class GrantUserOnApplicationCommand(Command): |
|
347 |
"""Grant a database user on a repository system database. |
|
1469 | 348 |
|
0 | 349 |
<application> |
350 |
the identifier of the application |
|
351 |
<user> |
|
352 |
the database's user requiring grant access |
|
353 |
""" |
|
354 |
name = 'db-grant-user' |
|
355 |
arguments = '<application> <user>' |
|
356 |
||
357 |
options = ( |
|
358 |
("set-owner", |
|
1469 | 359 |
{'short': 'o', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 360 |
'default' : False, |
361 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
362 |
), |
|
363 |
) |
|
364 |
def run(self, args): |
|
365 |
"""run the command with its specific arguments""" |
|
366 |
from cubicweb.server.sqlutils import sqlexec, sqlgrants |
|
367 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
368 |
user = pop_arg(args, msg="No user specified !") |
|
369 |
config = ServerConfiguration.config_for(appid) |
|
370 |
source = config.sources()['system'] |
|
371 |
set_owner = self.config.set_owner |
|
372 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
373 |
cursor = cnx.cursor() |
|
374 |
schema = config.load_schema() |
|
375 |
try: |
|
376 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
377 |
set_owner=set_owner), cursor) |
|
378 |
except Exception, ex: |
|
379 |
cnx.rollback() |
|
380 |
import traceback |
|
381 |
traceback.print_exc() |
|
382 |
print 'An error occured:', ex |
|
383 |
else: |
|
384 |
cnx.commit() |
|
385 |
print 'grants given to %s on application %s' % (appid, user) |
|
386 |
||
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
387 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
388 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
389 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
390 |
<application> |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
391 |
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
|
392 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
393 |
name = 'reset-admin-pwd' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
394 |
arguments = '<application>' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
395 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
396 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
397 |
"""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
|
398 |
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
|
399 |
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
|
400 |
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
|
401 |
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
|
402 |
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
|
403 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
404 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
405 |
except KeyError: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
406 |
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
|
407 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
408 |
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
|
409 |
cursor = cnx.cursor() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
410 |
_, 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
|
411 |
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
|
412 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
413 |
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
|
414 |
% {'sp': SQL_PREFIX, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
415 |
'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
|
416 |
cursor, withpb=False) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
417 |
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
|
418 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
419 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
420 |
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
|
421 |
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
|
422 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
423 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
424 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
425 |
traceback.print_exc() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
426 |
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
|
427 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
428 |
cnx.commit() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
429 |
print 'password reset, sources file regenerated' |
0 | 430 |
|
1469 | 431 |
|
0 | 432 |
class StartRepositoryCommand(Command): |
433 |
"""Start an CubicWeb RQL server for a given application. |
|
1469 | 434 |
|
0 | 435 |
The server will be accessible through pyro |
436 |
||
437 |
<application> |
|
438 |
the identifier of the application to initialize. |
|
439 |
""" |
|
440 |
name = 'start-repository' |
|
441 |
arguments = '<application>' |
|
1469 | 442 |
|
0 | 443 |
options = ( |
444 |
("debug", |
|
445 |
{'short': 'D', 'action' : 'store_true', |
|
446 |
'help': 'start server in debug mode.'}), |
|
447 |
) |
|
448 |
||
449 |
def run(self, args): |
|
450 |
from cubicweb.server.server import RepositoryServer |
|
451 |
appid = pop_arg(args, msg="No application specified !") |
|
452 |
config = ServerConfiguration.config_for(appid) |
|
453 |
debug = self.config.debug |
|
454 |
# create the server |
|
455 |
server = RepositoryServer(config, debug) |
|
456 |
# go ! (don't daemonize in debug mode) |
|
457 |
if not debug and server.daemonize(config['pid-file']) == -1: |
|
458 |
return |
|
459 |
uid = config['uid'] |
|
460 |
if uid is not None: |
|
461 |
try: |
|
462 |
uid = int(uid) |
|
463 |
except ValueError: |
|
464 |
from pwd import getpwnam |
|
465 |
uid = getpwnam(uid).pw_uid |
|
466 |
os.setuid(uid) |
|
467 |
server.install_sig_handlers() |
|
468 |
server.connect(config['host'], 0) |
|
469 |
server.run() |
|
470 |
||
471 |
||
472 |
def _remote_dump(host, appid, output, sudo=False): |
|
473 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s.dump %s' % (appid, appid) |
|
474 |
if sudo: |
|
475 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
476 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
477 |
print dmpcmd |
|
478 |
if os.system(dmpcmd): |
|
479 |
raise ExecutionError('Error while dumping the database') |
|
480 |
if output is None: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
481 |
from datetime import date |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
482 |
date = date.today().strftime('%Y-%m-%d') |
0 | 483 |
output = '%s-%s.dump' % (appid, date) |
484 |
cmd = 'scp %s:/tmp/%s.dump %s' % (host, appid, output) |
|
485 |
print cmd |
|
486 |
if os.system(cmd): |
|
487 |
raise ExecutionError('Error while retrieving the dump') |
|
488 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s.dump"' % (host, appid) |
|
489 |
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
|
490 |
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
|
491 |
'an error occured while deleting remote dump. Continue anyway?'): |
0 | 492 |
raise ExecutionError('Error while deleting remote dump') |
493 |
||
494 |
def _local_dump(appid, output): |
|
495 |
config = ServerConfiguration.config_for(appid) |
|
496 |
# schema=1 to avoid unnecessary schema loading |
|
497 |
mih = config.migration_handler(connect=False, schema=1) |
|
498 |
mih.backup_database(output, askconfirm=False) |
|
499 |
||
500 |
def _local_restore(appid, backupfile, drop): |
|
501 |
config = ServerConfiguration.config_for(appid) |
|
502 |
# schema=1 to avoid unnecessary schema loading |
|
503 |
mih = config.migration_handler(connect=False, schema=1) |
|
504 |
mih.restore_database(backupfile, drop) |
|
505 |
repo = mih.repo_connect() |
|
506 |
# version of the database |
|
507 |
dbversions = repo.get_versions() |
|
508 |
mih.shutdown() |
|
509 |
if not dbversions: |
|
510 |
print "bad or missing version information in the database, don't upgrade file system" |
|
511 |
return |
|
512 |
# version of installed software |
|
513 |
eversion = dbversions['cubicweb'] |
|
514 |
status = application_status(config, eversion, dbversions) |
|
515 |
# * database version > installed software |
|
516 |
if status == 'needsoftupgrade': |
|
517 |
print "database is using some earlier version than installed software!" |
|
518 |
print "please upgrade your software and then upgrade the instance" |
|
519 |
print "using command 'cubicweb-ctl upgrade %s'" % config.appid |
|
520 |
return |
|
521 |
# * database version < installed software, an upgrade will be necessary |
|
522 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
523 |
if status == 'needapplupgrade': |
|
524 |
print "database is using some older version than installed software." |
|
525 |
print "You'll have to upgrade the instance using command" |
|
526 |
print "'cubicweb-ctl upgrade %s'" % config.appid |
|
527 |
return |
|
528 |
# * database version = installed software, database version = instance fs version |
|
529 |
# ok! |
|
530 |
||
531 |
||
532 |
def application_status(config, cubicwebapplversion, vcconf): |
|
533 |
cubicwebversion = config.cubicweb_version() |
|
534 |
if cubicwebapplversion > cubicwebversion: |
|
535 |
return 'needsoftupgrade' |
|
536 |
if cubicwebapplversion < cubicwebversion: |
|
537 |
return 'needapplupgrade' |
|
538 |
for cube in config.cubes(): |
|
539 |
try: |
|
540 |
softversion = config.cube_version(cube) |
|
541 |
except ConfigurationError: |
|
542 |
print "no cube version information for %s, is the cube installed?" % cube |
|
543 |
continue |
|
544 |
try: |
|
545 |
applversion = vcconf[cube] |
|
546 |
except KeyError: |
|
547 |
print "no cube version information for %s in version configuration" % cube |
|
1469 | 548 |
continue |
0 | 549 |
if softversion == applversion: |
550 |
continue |
|
551 |
if softversion > applversion: |
|
552 |
return 'needsoftupgrade' |
|
553 |
elif softversion < applversion: |
|
554 |
return 'needapplupgrade' |
|
555 |
return None |
|
1469 | 556 |
|
0 | 557 |
|
558 |
class DBDumpCommand(Command): |
|
559 |
"""Backup the system database of an application. |
|
1469 | 560 |
|
0 | 561 |
<application> |
562 |
the identifier of the application to backup |
|
563 |
format [[user@]host:]appname |
|
564 |
""" |
|
565 |
name = 'db-dump' |
|
566 |
arguments = '<application>' |
|
567 |
||
568 |
options = ( |
|
569 |
("output", |
|
1469 | 570 |
{'short': 'o', 'type' : "string", 'metavar' : '<file>', |
0 | 571 |
'default' : None, |
572 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
573 |
), |
|
574 |
('sudo', |
|
575 |
{'short': 's', 'action' : 'store_true', |
|
576 |
'default' : False, |
|
577 |
'help': 'Use sudo on the remote host.'} |
|
578 |
), |
|
579 |
) |
|
580 |
||
581 |
def run(self, args): |
|
582 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
583 |
if ':' in appid: |
|
584 |
host, appid = appid.split(':') |
|
585 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
586 |
else: |
|
587 |
_local_dump(appid, self.config.output) |
|
588 |
||
589 |
||
590 |
class DBRestoreCommand(Command): |
|
591 |
"""Restore the system database of an application. |
|
1469 | 592 |
|
0 | 593 |
<application> |
594 |
the identifier of the application to restore |
|
595 |
""" |
|
596 |
name = 'db-restore' |
|
597 |
arguments = '<application> <backupfile>' |
|
598 |
||
599 |
options = ( |
|
600 |
("no-drop", |
|
1469 | 601 |
{'short': 'n', 'action' : 'store_true', |
0 | 602 |
'default' : False, |
603 |
'help': 'for some reason the database doesn\'t exist and so ' |
|
604 |
'should not be dropped.'} |
|
605 |
), |
|
606 |
) |
|
607 |
||
608 |
def run(self, args): |
|
609 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
610 |
backupfile = pop_arg(args, msg="No backup file specified !") |
|
611 |
_local_restore(appid, backupfile, not self.config.no_drop) |
|
612 |
||
613 |
||
614 |
class DBCopyCommand(Command): |
|
615 |
"""Copy the system database of an application (backup and restore). |
|
1469 | 616 |
|
0 | 617 |
<src-application> |
618 |
the identifier of the application to backup |
|
619 |
format [[user@]host:]appname |
|
620 |
||
621 |
<dest-application> |
|
622 |
the identifier of the application to restore |
|
623 |
""" |
|
624 |
name = 'db-copy' |
|
625 |
arguments = '<src-application> <dest-application>' |
|
626 |
||
627 |
options = ( |
|
628 |
("no-drop", |
|
1469 | 629 |
{'short': 'n', 'action' : 'store_true', |
0 | 630 |
'default' : False, |
631 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
632 |
'should not be dropped.'} |
|
633 |
), |
|
634 |
("keep-dump", |
|
635 |
{'short': 'k', 'action' : 'store_true', |
|
636 |
'default' : False, |
|
637 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
638 |
), |
|
639 |
('sudo', |
|
640 |
{'short': 's', 'action' : 'store_true', |
|
641 |
'default' : False, |
|
642 |
'help': 'Use sudo on the remote host.'} |
|
643 |
), |
|
644 |
) |
|
645 |
||
646 |
def run(self, args): |
|
647 |
import tempfile |
|
648 |
srcappid = pop_arg(args, 1, msg="No source application specified !") |
|
649 |
destappid = pop_arg(args, msg="No destination application specified !") |
|
650 |
output = tempfile.mktemp() |
|
651 |
if ':' in srcappid: |
|
652 |
host, srcappid = srcappid.split(':') |
|
653 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
654 |
else: |
|
655 |
_local_dump(srcappid, output) |
|
656 |
_local_restore(destappid, output, not self.config.no_drop) |
|
657 |
if self.config.keep_dump: |
|
658 |
print 'you can get the dump file at', output |
|
659 |
else: |
|
660 |
os.remove(output) |
|
661 |
||
1469 | 662 |
|
0 | 663 |
class CheckRepositoryCommand(Command): |
664 |
"""Check integrity of the system database of an application. |
|
1469 | 665 |
|
0 | 666 |
<application> |
667 |
the identifier of the application to check |
|
668 |
""" |
|
669 |
name = 'db-check' |
|
670 |
arguments = '<application>' |
|
671 |
||
672 |
options = ( |
|
673 |
("checks", |
|
1469 | 674 |
{'short': 'c', 'type' : "csv", 'metavar' : '<check list>', |
0 | 675 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
676 |
'help': 'Comma separated list of check to run. By default run all \ |
|
677 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
678 |
), |
|
1469 | 679 |
|
0 | 680 |
("autofix", |
1469 | 681 |
{'short': 'a', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 682 |
'default' : False, |
683 |
'help': 'Automatically correct integrity problems if this option \ |
|
684 |
is set to "y" or "yes", else only display them'} |
|
685 |
), |
|
686 |
("reindex", |
|
1469 | 687 |
{'short': 'r', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 688 |
'default' : False, |
689 |
'help': 're-indexes the database for full text search if this \ |
|
690 |
option is set to "y" or "yes" (may be long for large database).'} |
|
691 |
), |
|
1469 | 692 |
|
0 | 693 |
) |
694 |
||
695 |
def run(self, args): |
|
696 |
from cubicweb.server.checkintegrity import check |
|
697 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
698 |
config = ServerConfiguration.config_for(appid) |
|
699 |
repo, cnx = repo_cnx(config) |
|
700 |
check(repo, cnx, |
|
701 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
702 |
||
703 |
||
704 |
class RebuildFTICommand(Command): |
|
705 |
"""Rebuild the full-text index of the system database of an application. |
|
1469 | 706 |
|
0 | 707 |
<application> |
708 |
the identifier of the application to rebuild |
|
709 |
""" |
|
710 |
name = 'db-rebuild-fti' |
|
711 |
arguments = '<application>' |
|
712 |
||
713 |
options = () |
|
714 |
||
715 |
def run(self, args): |
|
716 |
from cubicweb.server.checkintegrity import reindex_entities |
|
717 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
718 |
config = ServerConfiguration.config_for(appid) |
|
719 |
repo, cnx = repo_cnx(config) |
|
720 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
721 |
reindex_entities(repo.schema, session) |
|
722 |
cnx.commit() |
|
723 |
||
1469 | 724 |
|
0 | 725 |
class SynchronizeApplicationSchemaCommand(Command): |
726 |
"""Synchronize persistent schema with cube schema. |
|
1469 | 727 |
|
0 | 728 |
Will synchronize common stuff between the cube schema and the |
729 |
actual persistent schema, but will not add/remove any entity or relation. |
|
730 |
||
731 |
<application> |
|
732 |
the identifier of the application to synchronize. |
|
733 |
""" |
|
734 |
name = 'schema-sync' |
|
735 |
arguments = '<application>' |
|
736 |
||
737 |
def run(self, args): |
|
738 |
appid = pop_arg(args, msg="No application specified !") |
|
739 |
config = ServerConfiguration.config_for(appid) |
|
740 |
mih = config.migration_handler() |
|
741 |
mih.cmd_synchronize_schema() |
|
742 |
||
743 |
||
1469 | 744 |
register_commands( (CreateApplicationDBCommand, |
0 | 745 |
InitApplicationCommand, |
746 |
GrantUserOnApplicationCommand, |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
747 |
ResetAdminPasswordCommand, |
0 | 748 |
StartRepositoryCommand, |
749 |
DBDumpCommand, |
|
750 |
DBRestoreCommand, |
|
751 |
DBCopyCommand, |
|
752 |
CheckRepositoryCommand, |
|
753 |
RebuildFTICommand, |
|
754 |
SynchronizeApplicationSchemaCommand, |
|
755 |
) ) |