author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:33:03 +0200 | |
changeset 2458 | 4d114865098f |
parent 2446 | 440cb4ea7e5c |
parent 2395 | e3093fc12a00 |
child 2461 | 1f38b65cbd20 |
permissions | -rw-r--r-- |
0 | 1 |
"""cubicweb-ctl commands and command handlers specific to the server.serverconfig |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
10 |
import sys |
0 | 11 |
import os |
12 |
||
2107
6c4a4c514ac2
add source to sources configuration when adding a cube defining a source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2106
diff
changeset
|
13 |
from logilab.common.configuration import Configuration |
1132 | 14 |
from logilab.common.clcommands import register_commands, cmd_run, pop_arg |
0 | 15 |
|
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): |
0 | 61 |
"""shortcut to get a connextion to the application system database |
62 |
defined in the given config. If <dbms_system_base> is True, |
|
63 |
connect to the dbms system database instead (for task such as |
|
64 |
create/drop the application database) |
|
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): |
|
122 |
"""create an application by copying files from the given cube and by |
|
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): |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
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): |
|
185 |
"""remove application's configuration and database""" |
|
186 |
from logilab.common.adbh import get_adv_func_helper |
|
187 |
source = self.config.sources()['system'] |
|
188 |
dbname = source['db-name'] |
|
189 |
helper = get_adv_func_helper(source['db-driver']) |
|
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 ################################################ |
|
234 |
class CreateApplicationDBCommand(Command): |
|
235 |
"""Create the system database of an application (run after 'create'). |
|
1469 | 236 |
|
0 | 237 |
You will be prompted for a login / password to use to connect to |
238 |
the system database. The given user should have almost all rights |
|
239 |
on the database (ie a super user on the dbms allowed to create |
|
240 |
database, users, languages...). |
|
241 |
||
242 |
<application> |
|
243 |
the identifier of the application to initialize. |
|
244 |
""" |
|
245 |
name = 'db-create' |
|
246 |
arguments = '<application>' |
|
1469 | 247 |
|
0 | 248 |
options = ( |
249 |
("create-db", |
|
250 |
{'short': 'c', 'type': "yn", 'metavar': '<y or n>', |
|
251 |
'default': True, |
|
252 |
'help': 'create the database (yes by default)'}), |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
253 |
("verbose", |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
254 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
255 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
256 |
'help': 'verbose mode: will ask all possible configuration questions', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
257 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
258 |
), |
0 | 259 |
) |
260 |
def run(self, args): |
|
261 |
"""run the command with its specific arguments""" |
|
262 |
from logilab.common.adbh import get_adv_func_helper |
|
263 |
from indexer import get_indexer |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
264 |
verbose = self.get('verbose') |
0 | 265 |
appid = pop_arg(args, msg="No application specified !") |
266 |
config = ServerConfiguration.config_for(appid) |
|
267 |
create_db = self.config.create_db |
|
268 |
source = config.sources()['system'] |
|
269 |
driver = source['db-driver'] |
|
270 |
helper = get_adv_func_helper(driver) |
|
271 |
if create_db: |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
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() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
311 |
print '-> database for application %s created and necessary extensions installed.' % appid |
0 | 312 |
print |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
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 |
|
0 | 320 |
class InitApplicationCommand(Command): |
321 |
"""Initialize the system database of an application (run after 'db-create'). |
|
1469 | 322 |
|
0 | 323 |
You will be prompted for a login / password to use to connect to |
324 |
the system database. The given user should have the create tables, |
|
325 |
and grant permissions. |
|
326 |
||
327 |
<application> |
|
328 |
the identifier of the application to initialize. |
|
329 |
""" |
|
330 |
name = 'db-init' |
|
331 |
arguments = '<application>' |
|
1469 | 332 |
|
0 | 333 |
options = ( |
334 |
("drop", |
|
335 |
{'short': 'd', 'action': 'store_true', |
|
336 |
'default': False, |
|
337 |
'help': 'insert drop statements to remove previously existant \ |
|
338 |
tables, indexes... (no by default)'}), |
|
339 |
) |
|
340 |
||
341 |
def run(self, args): |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
342 |
print '\n'+underline_title('Initializing the "system database"') |
0 | 343 |
from cubicweb.server import init_repository |
344 |
appid = pop_arg(args, msg="No application specified !") |
|
345 |
config = ServerConfiguration.config_for(appid) |
|
346 |
init_repository(config, drop=self.config.drop) |
|
347 |
||
348 |
||
349 |
class GrantUserOnApplicationCommand(Command): |
|
350 |
"""Grant a database user on a repository system database. |
|
1469 | 351 |
|
0 | 352 |
<application> |
353 |
the identifier of the application |
|
354 |
<user> |
|
355 |
the database's user requiring grant access |
|
356 |
""" |
|
357 |
name = 'db-grant-user' |
|
358 |
arguments = '<application> <user>' |
|
359 |
||
360 |
options = ( |
|
361 |
("set-owner", |
|
1469 | 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 |
|
370 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
371 |
user = pop_arg(args, msg="No user specified !") |
|
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() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
388 |
print '-> rights granted to %s on application %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 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
393 |
<application> |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
394 |
the identifier of the application |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
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' |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
397 |
arguments = '<application>' |
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 |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
403 |
appid = pop_arg(args, 1, msg="No application specified !") |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
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): |
436 |
"""Start an CubicWeb RQL server for a given application. |
|
1469 | 437 |
|
0 | 438 |
The server will be accessible through pyro |
439 |
||
440 |
<application> |
|
441 |
the identifier of the application to initialize. |
|
442 |
""" |
|
443 |
name = 'start-repository' |
|
444 |
arguments = '<application>' |
|
1469 | 445 |
|
0 | 446 |
options = ( |
447 |
("debug", |
|
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 |
|
454 |
appid = pop_arg(args, msg="No application specified !") |
|
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): |
|
476 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s.dump %s' % (appid, appid) |
|
477 |
if sudo: |
|
478 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
479 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
480 |
print dmpcmd |
|
481 |
if os.system(dmpcmd): |
|
482 |
raise ExecutionError('Error while dumping the database') |
|
483 |
if output is None: |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
484 |
from datetime import date |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
136
diff
changeset
|
485 |
date = date.today().strftime('%Y-%m-%d') |
0 | 486 |
output = '%s-%s.dump' % (appid, date) |
487 |
cmd = 'scp %s:/tmp/%s.dump %s' % (host, appid, output) |
|
488 |
print cmd |
|
489 |
if os.system(cmd): |
|
490 |
raise ExecutionError('Error while retrieving the dump') |
|
491 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s.dump"' % (host, appid) |
|
492 |
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
|
493 |
if os.system(rmcmd) and not confirm( |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
494 |
'An error occured while deleting remote dump. Continue anyway?'): |
0 | 495 |
raise ExecutionError('Error while deleting remote dump') |
496 |
||
497 |
def _local_dump(appid, output): |
|
498 |
config = ServerConfiguration.config_for(appid) |
|
499 |
# schema=1 to avoid unnecessary schema loading |
|
500 |
mih = config.migration_handler(connect=False, schema=1) |
|
501 |
mih.backup_database(output, askconfirm=False) |
|
502 |
||
503 |
def _local_restore(appid, backupfile, drop): |
|
504 |
config = ServerConfiguration.config_for(appid) |
|
505 |
# schema=1 to avoid unnecessary schema loading |
|
506 |
mih = config.migration_handler(connect=False, schema=1) |
|
507 |
mih.restore_database(backupfile, drop) |
|
508 |
repo = mih.repo_connect() |
|
509 |
# version of the database |
|
510 |
dbversions = repo.get_versions() |
|
511 |
mih.shutdown() |
|
512 |
if not dbversions: |
|
513 |
print "bad or missing version information in the database, don't upgrade file system" |
|
514 |
return |
|
515 |
# version of installed software |
|
516 |
eversion = dbversions['cubicweb'] |
|
517 |
status = application_status(config, eversion, dbversions) |
|
518 |
# * database version > installed software |
|
519 |
if status == 'needsoftupgrade': |
|
520 |
print "database is using some earlier version than installed software!" |
|
521 |
print "please upgrade your software and then upgrade the instance" |
|
522 |
print "using command 'cubicweb-ctl upgrade %s'" % config.appid |
|
523 |
return |
|
524 |
# * database version < installed software, an upgrade will be necessary |
|
525 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
526 |
if status == 'needapplupgrade': |
|
527 |
print "database is using some older version than installed software." |
|
528 |
print "You'll have to upgrade the instance using command" |
|
529 |
print "'cubicweb-ctl upgrade %s'" % config.appid |
|
530 |
return |
|
531 |
# * database version = installed software, database version = instance fs version |
|
532 |
# ok! |
|
533 |
||
534 |
||
535 |
def application_status(config, cubicwebapplversion, vcconf): |
|
536 |
cubicwebversion = config.cubicweb_version() |
|
537 |
if cubicwebapplversion > cubicwebversion: |
|
538 |
return 'needsoftupgrade' |
|
539 |
if cubicwebapplversion < cubicwebversion: |
|
540 |
return 'needapplupgrade' |
|
541 |
for cube in config.cubes(): |
|
542 |
try: |
|
543 |
softversion = config.cube_version(cube) |
|
544 |
except ConfigurationError: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
545 |
print "-> Error: no cube version information for %s, please check that the cube is installed." % cube |
0 | 546 |
continue |
547 |
try: |
|
548 |
applversion = vcconf[cube] |
|
549 |
except KeyError: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
550 |
print "-> Error: no cube version information for %s in version configuration." % cube |
1469 | 551 |
continue |
0 | 552 |
if softversion == applversion: |
553 |
continue |
|
554 |
if softversion > applversion: |
|
555 |
return 'needsoftupgrade' |
|
556 |
elif softversion < applversion: |
|
557 |
return 'needapplupgrade' |
|
558 |
return None |
|
1469 | 559 |
|
0 | 560 |
|
561 |
class DBDumpCommand(Command): |
|
562 |
"""Backup the system database of an application. |
|
1469 | 563 |
|
0 | 564 |
<application> |
565 |
the identifier of the application to backup |
|
566 |
format [[user@]host:]appname |
|
567 |
""" |
|
568 |
name = 'db-dump' |
|
569 |
arguments = '<application>' |
|
570 |
||
571 |
options = ( |
|
572 |
("output", |
|
1469 | 573 |
{'short': 'o', 'type' : "string", 'metavar' : '<file>', |
0 | 574 |
'default' : None, |
575 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
576 |
), |
|
577 |
('sudo', |
|
578 |
{'short': 's', 'action' : 'store_true', |
|
579 |
'default' : False, |
|
580 |
'help': 'Use sudo on the remote host.'} |
|
581 |
), |
|
582 |
) |
|
583 |
||
584 |
def run(self, args): |
|
585 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
586 |
if ':' in appid: |
|
587 |
host, appid = appid.split(':') |
|
588 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
589 |
else: |
|
590 |
_local_dump(appid, self.config.output) |
|
591 |
||
592 |
||
593 |
class DBRestoreCommand(Command): |
|
594 |
"""Restore the system database of an application. |
|
1469 | 595 |
|
0 | 596 |
<application> |
597 |
the identifier of the application to restore |
|
598 |
""" |
|
599 |
name = 'db-restore' |
|
600 |
arguments = '<application> <backupfile>' |
|
601 |
||
602 |
options = ( |
|
603 |
("no-drop", |
|
1469 | 604 |
{'short': 'n', 'action' : 'store_true', |
0 | 605 |
'default' : False, |
606 |
'help': 'for some reason the database doesn\'t exist and so ' |
|
607 |
'should not be dropped.'} |
|
608 |
), |
|
609 |
) |
|
610 |
||
611 |
def run(self, args): |
|
612 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
613 |
backupfile = pop_arg(args, msg="No backup file specified !") |
|
614 |
_local_restore(appid, backupfile, not self.config.no_drop) |
|
615 |
||
616 |
||
617 |
class DBCopyCommand(Command): |
|
618 |
"""Copy the system database of an application (backup and restore). |
|
1469 | 619 |
|
0 | 620 |
<src-application> |
621 |
the identifier of the application to backup |
|
622 |
format [[user@]host:]appname |
|
623 |
||
624 |
<dest-application> |
|
625 |
the identifier of the application to restore |
|
626 |
""" |
|
627 |
name = 'db-copy' |
|
628 |
arguments = '<src-application> <dest-application>' |
|
629 |
||
630 |
options = ( |
|
631 |
("no-drop", |
|
1469 | 632 |
{'short': 'n', 'action' : 'store_true', |
0 | 633 |
'default' : False, |
634 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
635 |
'should not be dropped.'} |
|
636 |
), |
|
637 |
("keep-dump", |
|
638 |
{'short': 'k', 'action' : 'store_true', |
|
639 |
'default' : False, |
|
640 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
641 |
), |
|
642 |
('sudo', |
|
643 |
{'short': 's', 'action' : 'store_true', |
|
644 |
'default' : False, |
|
645 |
'help': 'Use sudo on the remote host.'} |
|
646 |
), |
|
647 |
) |
|
648 |
||
649 |
def run(self, args): |
|
650 |
import tempfile |
|
651 |
srcappid = pop_arg(args, 1, msg="No source application specified !") |
|
652 |
destappid = pop_arg(args, msg="No destination application specified !") |
|
2446
440cb4ea7e5c
[refactor] #342855: replace uses of (deprecated) mktemp
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2417
diff
changeset
|
653 |
output = tempfile.mkstemp() |
0 | 654 |
if ':' in srcappid: |
655 |
host, srcappid = srcappid.split(':') |
|
656 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
657 |
else: |
|
658 |
_local_dump(srcappid, output) |
|
659 |
_local_restore(destappid, output, not self.config.no_drop) |
|
660 |
if self.config.keep_dump: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
661 |
print '-> you can get the dump file at', output |
0 | 662 |
else: |
663 |
os.remove(output) |
|
664 |
||
1469 | 665 |
|
0 | 666 |
class CheckRepositoryCommand(Command): |
667 |
"""Check integrity of the system database of an application. |
|
1469 | 668 |
|
0 | 669 |
<application> |
670 |
the identifier of the application to check |
|
671 |
""" |
|
672 |
name = 'db-check' |
|
673 |
arguments = '<application>' |
|
674 |
||
675 |
options = ( |
|
676 |
("checks", |
|
1469 | 677 |
{'short': 'c', 'type' : "csv", 'metavar' : '<check list>', |
0 | 678 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
679 |
'help': 'Comma separated list of check to run. By default run all \ |
|
680 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
681 |
), |
|
1469 | 682 |
|
0 | 683 |
("autofix", |
1469 | 684 |
{'short': 'a', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 685 |
'default' : False, |
686 |
'help': 'Automatically correct integrity problems if this option \ |
|
687 |
is set to "y" or "yes", else only display them'} |
|
688 |
), |
|
689 |
("reindex", |
|
1469 | 690 |
{'short': 'r', 'type' : "yn", 'metavar' : '<yes or no>', |
0 | 691 |
'default' : False, |
692 |
'help': 're-indexes the database for full text search if this \ |
|
693 |
option is set to "y" or "yes" (may be long for large database).'} |
|
694 |
), |
|
1469 | 695 |
|
0 | 696 |
) |
697 |
||
698 |
def run(self, args): |
|
699 |
from cubicweb.server.checkintegrity import check |
|
700 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
701 |
config = ServerConfiguration.config_for(appid) |
|
702 |
repo, cnx = repo_cnx(config) |
|
703 |
check(repo, cnx, |
|
704 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
705 |
||
706 |
||
707 |
class RebuildFTICommand(Command): |
|
708 |
"""Rebuild the full-text index of the system database of an application. |
|
1469 | 709 |
|
0 | 710 |
<application> |
711 |
the identifier of the application to rebuild |
|
712 |
""" |
|
713 |
name = 'db-rebuild-fti' |
|
714 |
arguments = '<application>' |
|
715 |
||
716 |
options = () |
|
717 |
||
718 |
def run(self, args): |
|
719 |
from cubicweb.server.checkintegrity import reindex_entities |
|
720 |
appid = pop_arg(args, 1, msg="No application specified !") |
|
721 |
config = ServerConfiguration.config_for(appid) |
|
722 |
repo, cnx = repo_cnx(config) |
|
723 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
724 |
reindex_entities(repo.schema, session) |
|
725 |
cnx.commit() |
|
726 |
||
1469 | 727 |
|
0 | 728 |
class SynchronizeApplicationSchemaCommand(Command): |
729 |
"""Synchronize persistent schema with cube schema. |
|
1469 | 730 |
|
0 | 731 |
Will synchronize common stuff between the cube schema and the |
732 |
actual persistent schema, but will not add/remove any entity or relation. |
|
733 |
||
734 |
<application> |
|
735 |
the identifier of the application to synchronize. |
|
736 |
""" |
|
737 |
name = 'schema-sync' |
|
738 |
arguments = '<application>' |
|
739 |
||
740 |
def run(self, args): |
|
741 |
appid = pop_arg(args, msg="No application specified !") |
|
742 |
config = ServerConfiguration.config_for(appid) |
|
743 |
mih = config.migration_handler() |
|
744 |
mih.cmd_synchronize_schema() |
|
745 |
||
746 |
||
1469 | 747 |
register_commands( (CreateApplicationDBCommand, |
0 | 748 |
InitApplicationCommand, |
749 |
GrantUserOnApplicationCommand, |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
750 |
ResetAdminPasswordCommand, |
0 | 751 |
StartRepositoryCommand, |
752 |
DBDumpCommand, |
|
753 |
DBRestoreCommand, |
|
754 |
DBCopyCommand, |
|
755 |
CheckRepositoryCommand, |
|
756 |
RebuildFTICommand, |
|
757 |
SynchronizeApplicationSchemaCommand, |
|
758 |
) ) |