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