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