author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 20 Aug 2009 17:57:31 +0200 | |
branch | stable |
changeset 2930 | d7c23b2c7538 |
parent 2851 | d3b80a4faf3f |
child 2867 | e8581a4f1bae |
child 2969 | d95f23a0fc3b |
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 |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
15 |
from logilab.common.shellutils import ASK |
0 | 16 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
17 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, underline_title |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
18 |
from cubicweb.toolsutils import Command, CommandHandler |
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
|
19 |
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
|
20 |
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
|
21 |
from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration |
0 | 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'] |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
35 |
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
|
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', '')) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
51 |
user = raw_input('Connect as user ? [%r]: ' % default_user) |
0 | 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): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
62 |
"""shortcut to get a connextion to the instance system database |
0 | 63 |
defined in the given config. If <dbms_system_base> is True, |
64 |
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
|
65 |
create/drop the instance database) |
0 | 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: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
111 |
print '-> Error: 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): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
123 |
"""create an instance by copying files from the given cube and by |
0 | 124 |
asking information necessary to build required configuration files |
125 |
""" |
|
126 |
config = self.config |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
127 |
print underline_title('Configuring the repository') |
0 | 128 |
config.input_config('email', inputlevel) |
129 |
if config.pyro_enabled(): |
|
130 |
config.input_config('pyro-server', inputlevel) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
131 |
print '\n'+underline_title('Configuring the sources') |
0 | 132 |
sourcesfile = config.sources_file() |
133 |
sconfig = Configuration(options=SOURCE_TYPES['native'].options) |
|
134 |
sconfig.adapter = 'native' |
|
135 |
sconfig.input_config(inputlevel=inputlevel) |
|
136 |
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
|
137 |
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
|
138 |
# 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
|
139 |
# 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
|
140 |
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
|
141 |
sourcescfg[cube] = ask_source_config(cube, inputlevel) |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
142 |
print |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
143 |
while ASK.confirm('Enter another source ?', default_is_yes=False): |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
144 |
available = sorted(stype for stype in SOURCE_TYPES |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
145 |
if not stype in cubes) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
146 |
while True: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
147 |
sourcetype = raw_input('source type (%s): ' % ', '.join(available)) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
148 |
if sourcetype in available: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
149 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
150 |
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
|
151 |
while True: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
152 |
sourceuri = raw_input('source uri: ').strip() |
2106
2295f2aba61d
check not using "admin" as uri (reserved)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2105
diff
changeset
|
153 |
if sourceuri != 'admin' and sourceuri not in sourcescfg: |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
154 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
155 |
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
|
156 |
sourcescfg[sourceuri] = ask_source_config(sourcetype) |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
157 |
sourcemodule = SOURCE_TYPES[sourcetype].module |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
158 |
if not sourcemodule.startswith('cubicweb.'): |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
159 |
# module names look like cubes.mycube.themodule |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
160 |
sourcecube = SOURCE_TYPES[sourcetype].module.split('.', 2)[1] |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
161 |
# if the source adapter is coming from an external component, |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
162 |
# ensure it's specified in used cubes |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
163 |
if not sourcecube in cubes: |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
164 |
cubes.append(sourcecube) |
0 | 165 |
sconfig = Configuration(options=USER_OPTIONS) |
166 |
sconfig.input_config(inputlevel=inputlevel) |
|
167 |
sourcescfg['admin'] = sconfig |
|
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
168 |
config.write_sources_file(sourcescfg) |
0 | 169 |
# remember selected cubes for later initialization of the database |
170 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 171 |
|
0 | 172 |
def postcreate(self): |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
173 |
if ASK.confirm('Run db-create to create the system database ?'): |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
174 |
verbosity = (self.config.mode == 'installed') and 'y' or 'n' |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
175 |
cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity) |
0 | 176 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
177 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
178 |
'"cubicweb-ctl db-create %s".' % self.config.appid) |
1469 | 179 |
|
0 | 180 |
|
181 |
class RepositoryDeleteHandler(CommandHandler): |
|
182 |
cmdname = 'delete' |
|
183 |
cfgname = 'repository' |
|
184 |
||
185 |
def cleanup(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
186 |
"""remove instance's configuration and database""" |
0 | 187 |
from logilab.common.adbh import get_adv_func_helper |
188 |
source = self.config.sources()['system'] |
|
189 |
dbname = source['db-name'] |
|
190 |
helper = get_adv_func_helper(source['db-driver']) |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
191 |
if ASK.confirm('Delete database %s ?' % dbname): |
0 | 192 |
user = source['db-user'] or None |
193 |
cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
|
194 |
cursor = cnx.cursor() |
|
195 |
try: |
|
196 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
197 |
print '-> database %s dropped.' % dbname |
0 | 198 |
# XXX should check we are not connected as user |
199 |
if user and helper.users_support and \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
200 |
ASK.confirm('Delete user %s ?' % user, default_is_yes=False): |
0 | 201 |
cursor.execute('DROP USER %s' % user) |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
202 |
print '-> user %s dropped.' % user |
0 | 203 |
cnx.commit() |
204 |
except: |
|
205 |
cnx.rollback() |
|
206 |
raise |
|
207 |
||
1469 | 208 |
|
0 | 209 |
class RepositoryStartHandler(CommandHandler): |
210 |
cmdname = 'start' |
|
211 |
cfgname = 'repository' |
|
212 |
||
213 |
def start_command(self, ctlconf, debug): |
|
214 |
command = ['cubicweb-ctl start-repository '] |
|
215 |
if debug: |
|
216 |
command.append('--debug') |
|
217 |
command.append(self.config.appid) |
|
218 |
return ' '.join(command) |
|
1469 | 219 |
|
0 | 220 |
|
221 |
class RepositoryStopHandler(CommandHandler): |
|
222 |
cmdname = 'stop' |
|
223 |
cfgname = 'repository' |
|
224 |
||
225 |
def poststop(self): |
|
226 |
"""if pyro is enabled, ensure the repository is correctly |
|
227 |
unregistered |
|
228 |
""" |
|
229 |
if self.config.pyro_enabled(): |
|
230 |
from cubicweb.server.repository import pyro_unregister |
|
231 |
pyro_unregister(self.config) |
|
1469 | 232 |
|
0 | 233 |
|
234 |
# repository specific commands ################################################ |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
235 |
class CreateInstanceDBCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
236 |
"""Create the system database of an instance (run after 'create'). |
1469 | 237 |
|
0 | 238 |
You will be prompted for a login / password to use to connect to |
239 |
the system database. The given user should have almost all rights |
|
240 |
on the database (ie a super user on the dbms allowed to create |
|
241 |
database, users, languages...). |
|
242 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
243 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
244 |
the identifier of the instance to initialize. |
0 | 245 |
""" |
246 |
name = 'db-create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
247 |
arguments = '<instance>' |
1469 | 248 |
|
0 | 249 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
250 |
('create-db', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
251 |
{'short': 'c', 'type': 'yn', 'metavar': '<y or n>', |
0 | 252 |
'default': True, |
253 |
'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
|
254 |
('verbose', |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
255 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
256 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
257 |
'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
|
258 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
259 |
), |
0 | 260 |
) |
261 |
def run(self, args): |
|
262 |
"""run the command with its specific arguments""" |
|
263 |
from logilab.common.adbh import get_adv_func_helper |
|
264 |
from indexer import get_indexer |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
265 |
verbose = self.get('verbose') |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
266 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 267 |
config = ServerConfiguration.config_for(appid) |
268 |
create_db = self.config.create_db |
|
269 |
source = config.sources()['system'] |
|
270 |
driver = source['db-driver'] |
|
271 |
helper = get_adv_func_helper(driver) |
|
272 |
if create_db: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
273 |
print '\n'+underline_title('Creating the system database') |
0 | 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 \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
281 |
ASK.confirm('Create db user %s ?' % user, default_is_yes=False): |
0 | 282 |
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
|
283 |
print '-> user %s created.' % user |
0 | 284 |
dbname = source['db-name'] |
285 |
if dbname in helper.list_databases(cursor): |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
286 |
if ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
0 | 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() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
297 |
print '-> database %s created.' % source['db-name'] |
0 | 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() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
312 |
print '-> database for instance %s created and necessary extensions installed.' % appid |
0 | 313 |
print |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
314 |
if ASK.confirm('Run db-init to initialize the system database ?'): |
0 | 315 |
cmd_run('db-init', config.appid) |
316 |
else: |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
317 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
318 |
'"cubicweb-ctl db-init %s".' % self.config.appid) |
0 | 319 |
|
1469 | 320 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
321 |
class InitInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
322 |
"""Initialize the system database of an instance (run after 'db-create'). |
1469 | 323 |
|
0 | 324 |
You will be prompted for a login / password to use to connect to |
325 |
the system database. The given user should have the create tables, |
|
326 |
and grant permissions. |
|
327 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
328 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
329 |
the identifier of the instance to initialize. |
0 | 330 |
""" |
331 |
name = 'db-init' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
332 |
arguments = '<instance>' |
1469 | 333 |
|
0 | 334 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
335 |
('drop', |
0 | 336 |
{'short': 'd', 'action': 'store_true', |
337 |
'default': False, |
|
338 |
'help': 'insert drop statements to remove previously existant \ |
|
339 |
tables, indexes... (no by default)'}), |
|
340 |
) |
|
341 |
||
342 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
343 |
print '\n'+underline_title('Initializing the system database') |
0 | 344 |
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
|
345 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 346 |
config = ServerConfiguration.config_for(appid) |
347 |
init_repository(config, drop=self.config.drop) |
|
348 |
||
349 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
350 |
class GrantUserOnInstanceCommand(Command): |
0 | 351 |
"""Grant a database user on a repository system database. |
1469 | 352 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
353 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
354 |
the identifier of the instance |
0 | 355 |
<user> |
356 |
the database's user requiring grant access |
|
357 |
""" |
|
358 |
name = 'db-grant-user' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
359 |
arguments = '<instance> <user>' |
0 | 360 |
|
361 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
362 |
('set-owner', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
363 |
{'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 364 |
'default' : False, |
365 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
366 |
), |
|
367 |
) |
|
368 |
def run(self, args): |
|
369 |
"""run the command with its specific arguments""" |
|
370 |
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
|
371 |
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
|
372 |
user = pop_arg(args, msg='No user specified !') |
0 | 373 |
config = ServerConfiguration.config_for(appid) |
374 |
source = config.sources()['system'] |
|
375 |
set_owner = self.config.set_owner |
|
376 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
377 |
cursor = cnx.cursor() |
|
378 |
schema = config.load_schema() |
|
379 |
try: |
|
380 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
381 |
set_owner=set_owner), cursor) |
|
382 |
except Exception, ex: |
|
383 |
cnx.rollback() |
|
384 |
import traceback |
|
385 |
traceback.print_exc() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
386 |
print '-> an error occured:', ex |
0 | 387 |
else: |
388 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
389 |
print '-> rights granted to %s on instance %s.' % (appid, user) |
0 | 390 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
391 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
392 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
393 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
394 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
395 |
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
|
396 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
397 |
name = 'reset-admin-pwd' |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
398 |
arguments = '<instance>' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
399 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
400 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
401 |
"""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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
408 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
409 |
except KeyError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
410 |
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
|
411 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
412 |
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
|
413 |
cursor = cnx.cursor() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
414 |
_, 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
|
415 |
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
|
416 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
417 |
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
|
418 |
% {'sp': SQL_PREFIX, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
419 |
'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
|
420 |
cursor, withpb=False) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
421 |
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
|
422 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
423 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
424 |
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
|
425 |
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
|
426 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
427 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
428 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
429 |
traceback.print_exc() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
430 |
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
|
431 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
432 |
cnx.commit() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
433 |
print '-> password reset, sources file regenerated.' |
0 | 434 |
|
1469 | 435 |
|
0 | 436 |
class StartRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
437 |
"""Start an CubicWeb RQL server for a given instance. |
1469 | 438 |
|
0 | 439 |
The server will be accessible through pyro |
440 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
441 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
442 |
the identifier of the instance to initialize. |
0 | 443 |
""" |
444 |
name = 'start-repository' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
445 |
arguments = '<instance>' |
1469 | 446 |
|
0 | 447 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
448 |
('debug', |
0 | 449 |
{'short': 'D', 'action' : 'store_true', |
450 |
'help': 'start server in debug mode.'}), |
|
451 |
) |
|
452 |
||
453 |
def run(self, args): |
|
454 |
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
|
455 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 456 |
config = ServerConfiguration.config_for(appid) |
457 |
debug = self.config.debug |
|
458 |
# create the server |
|
459 |
server = RepositoryServer(config, debug) |
|
460 |
# go ! (don't daemonize in debug mode) |
|
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
461 |
pidfile = config['pid-file'] |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
462 |
# ensure the directory where the pid-file should be set exists (for |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
463 |
# instance /var/run/cubicweb may be deleted on computer restart) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
464 |
piddir = os.path.dirname(pidfile) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
465 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
466 |
os.makedirs(piddir) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
467 |
if not debug and server.daemonize(pidfile) == -1: |
0 | 468 |
return |
469 |
uid = config['uid'] |
|
470 |
if uid is not None: |
|
471 |
try: |
|
472 |
uid = int(uid) |
|
473 |
except ValueError: |
|
474 |
from pwd import getpwnam |
|
475 |
uid = getpwnam(uid).pw_uid |
|
476 |
os.setuid(uid) |
|
477 |
server.install_sig_handlers() |
|
478 |
server.connect(config['host'], 0) |
|
479 |
server.run() |
|
480 |
||
481 |
||
482 |
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
|
483 |
# XXX generate unique/portable file name |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
484 |
from datetime import date |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
485 |
filename = '%s-%s.tgz' % (appid, date.today().strftime('%Y-%m-%d')) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
486 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid) |
0 | 487 |
if sudo: |
488 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
489 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
490 |
print dmpcmd |
|
491 |
if os.system(dmpcmd): |
|
492 |
raise ExecutionError('Error while dumping the database') |
|
493 |
if output is None: |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
494 |
output = filename |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
495 |
cmd = 'scp %s:/tmp/%s %s' % (host, filename, output) |
0 | 496 |
print cmd |
497 |
if os.system(cmd): |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
498 |
raise ExecutionError('Error while retrieving the dump at /tmp/%s' % filename) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
499 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename) |
0 | 500 |
print rmcmd |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
501 |
if os.system(rmcmd) and not ASK.confirm( |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
502 |
'An error occured while deleting remote dump at /tmp/%s. ' |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
503 |
'Continue anyway?' % filename): |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
504 |
raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename) |
0 | 505 |
|
506 |
def _local_dump(appid, output): |
|
507 |
config = ServerConfiguration.config_for(appid) |
|
508 |
# 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
|
509 |
mih = config.migration_handler(connect=False, schema=1, verbosity=1) |
0 | 510 |
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
|
511 |
mih.shutdown() |
0 | 512 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
513 |
def _local_restore(appid, backupfile, drop, systemonly=True): |
0 | 514 |
config = ServerConfiguration.config_for(appid) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
515 |
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
|
516 |
config.repairing = 1 # don't check versions |
0 | 517 |
# 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
|
518 |
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
|
519 |
mih.restore_database(backupfile, drop, systemonly, askconfirm=False) |
0 | 520 |
repo = mih.repo_connect() |
521 |
# version of the database |
|
522 |
dbversions = repo.get_versions() |
|
523 |
mih.shutdown() |
|
524 |
if not dbversions: |
|
525 |
print "bad or missing version information in the database, don't upgrade file system" |
|
526 |
return |
|
527 |
# version of installed software |
|
528 |
eversion = dbversions['cubicweb'] |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
529 |
status = instance_status(config, eversion, dbversions) |
0 | 530 |
# * database version > installed software |
531 |
if status == 'needsoftupgrade': |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
532 |
print "** The database of %s is more recent than the installed software!" % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
533 |
print "** Upgrade your software, then migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
534 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 535 |
return |
536 |
# * database version < installed software, an upgrade will be necessary |
|
537 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
538 |
elif status == 'needapplupgrade': |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
539 |
print "** The database of %s is older than the installed software." % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
540 |
print "** Migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
541 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 542 |
return |
543 |
# * database version = installed software, database version = instance fs version |
|
544 |
# ok! |
|
545 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
546 |
def instance_status(config, cubicwebapplversion, vcconf): |
0 | 547 |
cubicwebversion = config.cubicweb_version() |
548 |
if cubicwebapplversion > cubicwebversion: |
|
549 |
return 'needsoftupgrade' |
|
550 |
if cubicwebapplversion < cubicwebversion: |
|
551 |
return 'needapplupgrade' |
|
552 |
for cube in config.cubes(): |
|
553 |
try: |
|
554 |
softversion = config.cube_version(cube) |
|
555 |
except ConfigurationError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
556 |
print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube |
0 | 557 |
continue |
558 |
try: |
|
559 |
applversion = vcconf[cube] |
|
560 |
except KeyError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
561 |
print '-> Error: no cube version information for %s in version configuration.' % cube |
1469 | 562 |
continue |
0 | 563 |
if softversion == applversion: |
564 |
continue |
|
565 |
if softversion > applversion: |
|
566 |
return 'needsoftupgrade' |
|
567 |
elif softversion < applversion: |
|
568 |
return 'needapplupgrade' |
|
569 |
return None |
|
1469 | 570 |
|
0 | 571 |
|
572 |
class DBDumpCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
573 |
"""Backup the system database of an instance. |
1469 | 574 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
575 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
576 |
the identifier of the instance to backup |
0 | 577 |
format [[user@]host:]appname |
578 |
""" |
|
579 |
name = 'db-dump' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
580 |
arguments = '<instance>' |
0 | 581 |
|
582 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
583 |
('output', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
584 |
{'short': 'o', 'type' : 'string', 'metavar' : '<file>', |
0 | 585 |
'default' : None, |
586 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
587 |
), |
|
588 |
('sudo', |
|
589 |
{'short': 's', 'action' : 'store_true', |
|
590 |
'default' : False, |
|
591 |
'help': 'Use sudo on the remote host.'} |
|
592 |
), |
|
593 |
) |
|
594 |
||
595 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
596 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 597 |
if ':' in appid: |
598 |
host, appid = appid.split(':') |
|
599 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
600 |
else: |
|
601 |
_local_dump(appid, self.config.output) |
|
602 |
||
603 |
||
604 |
class DBRestoreCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
605 |
"""Restore the system database of an instance. |
1469 | 606 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
607 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
608 |
the identifier of the instance to restore |
0 | 609 |
""" |
610 |
name = 'db-restore' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
611 |
arguments = '<instance> <backupfile>' |
0 | 612 |
|
613 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
614 |
('no-drop', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
615 |
{'short': 'n', 'action' : 'store_true', 'default' : False, |
0 | 616 |
'help': 'for some reason the database doesn\'t exist and so ' |
617 |
'should not be dropped.'} |
|
618 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
619 |
('restore-all', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
620 |
{'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
|
621 |
'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
|
622 |
'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
|
623 |
'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
|
624 |
'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
|
625 |
), |
0 | 626 |
) |
627 |
||
628 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
629 |
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
|
630 |
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
|
631 |
_local_restore(appid, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
632 |
drop=not self.config.no_drop, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
633 |
systemonly=not self.config.restore_all) |
0 | 634 |
|
635 |
||
636 |
class DBCopyCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
637 |
"""Copy the system database of an instance (backup and restore). |
1469 | 638 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
639 |
<src-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
640 |
the identifier of the instance to backup |
0 | 641 |
format [[user@]host:]appname |
642 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
643 |
<dest-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
644 |
the identifier of the instance to restore |
0 | 645 |
""" |
646 |
name = 'db-copy' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
647 |
arguments = '<src-instance> <dest-instance>' |
0 | 648 |
|
649 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
650 |
('no-drop', |
1469 | 651 |
{'short': 'n', 'action' : 'store_true', |
0 | 652 |
'default' : False, |
653 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
654 |
'should not be dropped.'} |
|
655 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
656 |
('keep-dump', |
0 | 657 |
{'short': 'k', 'action' : 'store_true', |
658 |
'default' : False, |
|
659 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
660 |
), |
|
661 |
('sudo', |
|
662 |
{'short': 's', 'action' : 'store_true', |
|
663 |
'default' : False, |
|
664 |
'help': 'Use sudo on the remote host.'} |
|
665 |
), |
|
666 |
) |
|
667 |
||
668 |
def run(self, args): |
|
669 |
import tempfile |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
670 |
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
|
671 |
destappid = pop_arg(args, msg='No destination instance specified !') |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
672 |
output = tempfile.mkstemp(dir='/tmp/')[1] |
0 | 673 |
if ':' in srcappid: |
674 |
host, srcappid = srcappid.split(':') |
|
675 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
676 |
else: |
|
677 |
_local_dump(srcappid, output) |
|
678 |
_local_restore(destappid, output, not self.config.no_drop) |
|
679 |
if self.config.keep_dump: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
680 |
print '-> you can get the dump file at', output |
0 | 681 |
else: |
682 |
os.remove(output) |
|
683 |
||
1469 | 684 |
|
0 | 685 |
class CheckRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
686 |
"""Check integrity of the system database of an instance. |
1469 | 687 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
688 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
689 |
the identifier of the instance to check |
0 | 690 |
""" |
691 |
name = 'db-check' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
692 |
arguments = '<instance>' |
0 | 693 |
|
694 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
695 |
('checks', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
696 |
{'short': 'c', 'type' : 'csv', 'metavar' : '<check list>', |
0 | 697 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
698 |
'help': 'Comma separated list of check to run. By default run all \ |
|
699 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
700 |
), |
|
1469 | 701 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
702 |
('autofix', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
703 |
{'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 704 |
'default' : False, |
705 |
'help': 'Automatically correct integrity problems if this option \ |
|
706 |
is set to "y" or "yes", else only display them'} |
|
707 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
708 |
('reindex', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
709 |
{'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 710 |
'default' : False, |
711 |
'help': 're-indexes the database for full text search if this \ |
|
712 |
option is set to "y" or "yes" (may be long for large database).'} |
|
713 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
714 |
('force', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
715 |
{'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
|
716 |
'default' : False, |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
717 |
'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
|
718 |
), |
1469 | 719 |
|
0 | 720 |
) |
721 |
||
722 |
def run(self, args): |
|
723 |
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
|
724 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 725 |
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
|
726 |
config.repairing = self.config.force |
0 | 727 |
repo, cnx = repo_cnx(config) |
728 |
check(repo, cnx, |
|
729 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
730 |
||
731 |
||
732 |
class RebuildFTICommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
733 |
"""Rebuild the full-text index of the system database of an instance. |
1469 | 734 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
735 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
736 |
the identifier of the instance to rebuild |
0 | 737 |
""" |
738 |
name = 'db-rebuild-fti' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
739 |
arguments = '<instance>' |
0 | 740 |
|
741 |
options = () |
|
742 |
||
743 |
def run(self, args): |
|
744 |
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
|
745 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 746 |
config = ServerConfiguration.config_for(appid) |
747 |
repo, cnx = repo_cnx(config) |
|
748 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
749 |
reindex_entities(repo.schema, session) |
|
750 |
cnx.commit() |
|
751 |
||
1469 | 752 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
753 |
class SynchronizeInstanceSchemaCommand(Command): |
0 | 754 |
"""Synchronize persistent schema with cube schema. |
1469 | 755 |
|
0 | 756 |
Will synchronize common stuff between the cube schema and the |
757 |
actual persistent schema, but will not add/remove any entity or relation. |
|
758 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
759 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
760 |
the identifier of the instance to synchronize. |
0 | 761 |
""" |
762 |
name = 'schema-sync' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
763 |
arguments = '<instance>' |
0 | 764 |
|
765 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
766 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 767 |
config = ServerConfiguration.config_for(appid) |
768 |
mih = config.migration_handler() |
|
769 |
mih.cmd_synchronize_schema() |
|
770 |
||
771 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
772 |
register_commands( (CreateInstanceDBCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
773 |
InitInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
774 |
GrantUserOnInstanceCommand, |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
775 |
ResetAdminPasswordCommand, |
0 | 776 |
StartRepositoryCommand, |
777 |
DBDumpCommand, |
|
778 |
DBRestoreCommand, |
|
779 |
DBCopyCommand, |
|
780 |
CheckRepositoryCommand, |
|
781 |
RebuildFTICommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
782 |
SynchronizeInstanceSchemaCommand, |
0 | 783 |
) ) |