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