author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Mon, 16 Nov 2009 08:58:35 +0100 | |
branch | stable |
changeset 3851 | 3a18a0a24411 |
parent 3835 | a191b3b9e455 |
child 3890 | d7a270f50f54 |
child 3893 | 74f216b69628 |
permissions | -rw-r--r-- |
0 | 1 |
"""cubicweb-ctl commands and command handlers specific to the server.serverconfig |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1912
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
8 |
__docformat__ = 'restructuredtext en' |
0 | 9 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
10 |
import sys |
0 | 11 |
import os |
12 |
||
2107
6c4a4c514ac2
add source to sources configuration when adding a cube defining a source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2106
diff
changeset
|
13 |
from logilab.common.configuration import Configuration |
1132 | 14 |
from logilab.common.clcommands import register_commands, cmd_run, pop_arg |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
15 |
from logilab.common.shellutils import ASK |
0 | 16 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
17 |
from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, underline_title |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
18 |
from cubicweb.toolsutils import Command, CommandHandler |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
19 |
from cubicweb.server import SOURCE_TYPES |
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
20 |
from cubicweb.server.utils import ask_source_config |
2107
6c4a4c514ac2
add source to sources configuration when adding a cube defining a source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2106
diff
changeset
|
21 |
from cubicweb.server.serverconfig import USER_OPTIONS, ServerConfiguration |
0 | 22 |
|
23 |
# utility functions ########################################################### |
|
24 |
||
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
25 |
def source_cnx(source, dbname=None, special_privs=False, verbose=True): |
0 | 26 |
"""open and return a connection to the system database defined in the |
27 |
given server.serverconfig |
|
28 |
""" |
|
29 |
from getpass import getpass |
|
30 |
from logilab.common.db import get_connection |
|
31 |
dbhost = source['db-host'] |
|
32 |
if dbname is None: |
|
33 |
dbname = source['db-name'] |
|
34 |
driver = source['db-driver'] |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
35 |
print '-> connecting to %s database %s@%s' % (driver, dbname, dbhost or 'localhost'), |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
36 |
if not verbose or (not special_privs and source.get('db-user')): |
0 | 37 |
user = source['db-user'] |
38 |
print 'as', user |
|
39 |
if source.get('db-password'): |
|
40 |
password = source['db-password'] |
|
41 |
else: |
|
42 |
password = getpass('password: ') |
|
43 |
else: |
|
44 |
print |
|
45 |
if special_privs: |
|
46 |
print 'WARNING' |
|
47 |
print 'the user will need the following special access rights on the database:' |
|
48 |
print special_privs |
|
49 |
print |
|
50 |
default_user = source.get('db-user', os.environ.get('USER', '')) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
51 |
user = raw_input('Connect as user ? [%r]: ' % default_user) |
0 | 52 |
user = user or default_user |
53 |
if user == source.get('db-user') and source.get('db-password'): |
|
54 |
password = source['db-password'] |
|
55 |
else: |
|
56 |
password = getpass('password: ') |
|
57 |
return get_connection(driver, dbhost, dbname, user, password=password, |
|
58 |
port=source.get('db-port')) |
|
59 |
||
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
60 |
def system_source_cnx(source, dbms_system_base=False, |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
61 |
special_privs='CREATE/DROP DATABASE', verbose=True): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
62 |
"""shortcut to get a connextion to the instance system database |
0 | 63 |
defined in the given config. If <dbms_system_base> is True, |
64 |
connect to the dbms system database instead (for task such as |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
65 |
create/drop the instance database) |
0 | 66 |
""" |
67 |
if dbms_system_base: |
|
68 |
from logilab.common.adbh import get_adv_func_helper |
|
69 |
system_db = get_adv_func_helper(source['db-driver']).system_database() |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
70 |
return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose) |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
71 |
return source_cnx(source, special_privs=special_privs, verbose=verbose) |
0 | 72 |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
73 |
def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
0 | 74 |
"""return a connection on the RDMS system table (to create/drop a user |
75 |
or a database |
|
76 |
""" |
|
2417
18a14c23413c
should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2107
diff
changeset
|
77 |
import logilab.common as lgp |
0 | 78 |
from logilab.common.adbh import get_adv_func_helper |
2417
18a14c23413c
should ensure lgc is configured to not use mx to avoid error if backend module is missing mx support
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2107
diff
changeset
|
79 |
lgp.USE_MX_DATETIME = False |
0 | 80 |
special_privs = '' |
81 |
driver = source['db-driver'] |
|
82 |
helper = get_adv_func_helper(driver) |
|
83 |
if user is not None and helper.users_support: |
|
84 |
special_privs += '%s USER' % what |
|
85 |
if db is not None: |
|
86 |
special_privs += ' %s DATABASE' % what |
|
87 |
# connect on the dbms system base to create our base |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
88 |
cnx = system_source_cnx(source, True, special_privs=special_privs, verbose=verbose) |
0 | 89 |
# disable autocommit (isolation_level(1)) because DROP and |
90 |
# CREATE DATABASE can't be executed in a transaction |
|
91 |
try: |
|
92 |
cnx.set_isolation_level(0) |
|
93 |
except AttributeError: |
|
94 |
# set_isolation_level() is psycopg specific |
|
95 |
pass |
|
96 |
return cnx |
|
1469 | 97 |
|
0 | 98 |
def repo_cnx(config): |
99 |
"""return a in-memory repository and a db api connection it""" |
|
100 |
from cubicweb.dbapi import in_memory_cnx |
|
101 |
from cubicweb.server.utils import manager_userpasswd |
|
102 |
try: |
|
103 |
login = config.sources()['admin']['login'] |
|
104 |
pwd = config.sources()['admin']['password'] |
|
105 |
except KeyError: |
|
106 |
login, pwd = manager_userpasswd() |
|
107 |
while True: |
|
108 |
try: |
|
109 |
return in_memory_cnx(config, login, pwd) |
|
110 |
except AuthenticationError: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
111 |
print '-> Error: wrong user/password.' |
1682
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
112 |
# reset cubes else we'll have an assertion error on next retry |
36bd5cba09de
must reset cubes before next authentication
Graziella Toutoungis <graziella.toutoungis@logilab.fr>
parents:
1469
diff
changeset
|
113 |
config._cubes = None |
0 | 114 |
login, pwd = manager_userpasswd() |
1469 | 115 |
|
0 | 116 |
# repository specific command handlers ######################################## |
117 |
||
118 |
class RepositoryCreateHandler(CommandHandler): |
|
119 |
cmdname = 'create' |
|
120 |
cfgname = 'repository' |
|
121 |
||
122 |
def bootstrap(self, cubes, inputlevel=0): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
123 |
"""create an instance by copying files from the given cube and by |
0 | 124 |
asking information necessary to build required configuration files |
125 |
""" |
|
126 |
config = self.config |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
127 |
print underline_title('Configuring the repository') |
0 | 128 |
config.input_config('email', inputlevel) |
3539
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
129 |
# ask for pyro configuration if pyro is activated and we're not using a |
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
130 |
# all-in-one config, in which case this is done by the web side command |
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
131 |
# handler |
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
132 |
if config.pyro_enabled() and config.name != 'all-in-one': |
f3b14d052798
[pyro] merge pyro-id / pyro-instance-id options, put all pyro options in the same section of the configuration file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3316
diff
changeset
|
133 |
config.input_config('pyro', inputlevel) |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
134 |
print '\n'+underline_title('Configuring the sources') |
0 | 135 |
sourcesfile = config.sources_file() |
3835
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3786
diff
changeset
|
136 |
# XXX hack to make Method('default_instance_id') usable in db option |
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3786
diff
changeset
|
137 |
# defs (in native.py) |
a191b3b9e455
more sensible default values to c-c "create" inputs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3786
diff
changeset
|
138 |
Configuration.default_instance_id = staticmethod(lambda: config.appid) |
0 | 139 |
sconfig = Configuration(options=SOURCE_TYPES['native'].options) |
140 |
sconfig.adapter = 'native' |
|
141 |
sconfig.input_config(inputlevel=inputlevel) |
|
142 |
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
|
143 |
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
|
144 |
# 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
|
145 |
# 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
|
146 |
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
|
147 |
sourcescfg[cube] = ask_source_config(cube, inputlevel) |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
148 |
print |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
149 |
while ASK.confirm('Enter another source ?', default_is_yes=False): |
2105
92ea410806fe
refactor sources configuration, add source to sources when using a cube defining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1980
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
156 |
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
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
break |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
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
|
165 |
# 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
|
166 |
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
|
167 |
# 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
|
168 |
# 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
|
169 |
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
|
170 |
cubes.append(sourcecube) |
0 | 171 |
sconfig = Configuration(options=USER_OPTIONS) |
172 |
sconfig.input_config(inputlevel=inputlevel) |
|
173 |
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
|
174 |
config.write_sources_file(sourcescfg) |
0 | 175 |
# remember selected cubes for later initialization of the database |
176 |
config.write_bootstrap_cubes_file(cubes) |
|
1469 | 177 |
|
0 | 178 |
def postcreate(self): |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
179 |
if ASK.confirm('Run db-create to create the system database ?'): |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
180 |
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
|
181 |
cmd_run('db-create', self.config.appid, '--verbose=%s' % verbosity) |
0 | 182 |
else: |
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
183 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
184 |
'"cubicweb-ctl db-create %s".' % self.config.appid) |
1469 | 185 |
|
0 | 186 |
|
187 |
class RepositoryDeleteHandler(CommandHandler): |
|
188 |
cmdname = 'delete' |
|
189 |
cfgname = 'repository' |
|
190 |
||
191 |
def cleanup(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
192 |
"""remove instance's configuration and database""" |
0 | 193 |
from logilab.common.adbh import get_adv_func_helper |
194 |
source = self.config.sources()['system'] |
|
195 |
dbname = source['db-name'] |
|
196 |
helper = get_adv_func_helper(source['db-driver']) |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
197 |
if ASK.confirm('Delete database %s ?' % dbname): |
0 | 198 |
user = source['db-user'] or None |
199 |
cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
|
200 |
cursor = cnx.cursor() |
|
201 |
try: |
|
202 |
cursor.execute('DROP DATABASE %s' % dbname) |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
203 |
print '-> database %s dropped.' % dbname |
0 | 204 |
# XXX should check we are not connected as user |
205 |
if user and helper.users_support and \ |
|
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
206 |
ASK.confirm('Delete user %s ?' % user, default_is_yes=False): |
0 | 207 |
cursor.execute('DROP USER %s' % user) |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
208 |
print '-> user %s dropped.' % user |
0 | 209 |
cnx.commit() |
210 |
except: |
|
211 |
cnx.rollback() |
|
212 |
raise |
|
213 |
||
1469 | 214 |
|
0 | 215 |
class RepositoryStartHandler(CommandHandler): |
216 |
cmdname = 'start' |
|
217 |
cfgname = 'repository' |
|
218 |
||
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2969
diff
changeset
|
219 |
def start_server(self, ctlconf, debug): |
0 | 220 |
command = ['cubicweb-ctl start-repository '] |
221 |
if debug: |
|
222 |
command.append('--debug') |
|
223 |
command.append(self.config.appid) |
|
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2969
diff
changeset
|
224 |
os.system(' '.join(command)) |
1469 | 225 |
|
0 | 226 |
|
227 |
class RepositoryStopHandler(CommandHandler): |
|
228 |
cmdname = 'stop' |
|
229 |
cfgname = 'repository' |
|
230 |
||
231 |
def poststop(self): |
|
232 |
"""if pyro is enabled, ensure the repository is correctly |
|
233 |
unregistered |
|
234 |
""" |
|
235 |
if self.config.pyro_enabled(): |
|
236 |
from cubicweb.server.repository import pyro_unregister |
|
237 |
pyro_unregister(self.config) |
|
1469 | 238 |
|
0 | 239 |
|
240 |
# repository specific commands ################################################ |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
241 |
class CreateInstanceDBCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
242 |
"""Create the system database of an instance (run after 'create'). |
1469 | 243 |
|
0 | 244 |
You will be prompted for a login / password to use to connect to |
245 |
the system database. The given user should have almost all rights |
|
246 |
on the database (ie a super user on the dbms allowed to create |
|
247 |
database, users, languages...). |
|
248 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
249 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
250 |
the identifier of the instance to initialize. |
0 | 251 |
""" |
252 |
name = 'db-create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
253 |
arguments = '<instance>' |
1469 | 254 |
|
0 | 255 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
256 |
('create-db', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
257 |
{'short': 'c', 'type': 'yn', 'metavar': '<y or n>', |
0 | 258 |
'default': True, |
259 |
'help': 'create the database (yes by default)'}), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
260 |
('verbose', |
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
261 |
{'short': 'v', 'type' : 'yn', 'metavar': '<verbose>', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
262 |
'default': 'n', |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
263 |
'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
|
264 |
} |
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
265 |
), |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
266 |
('automatic', |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
267 |
{'short': 'a', 'type' : 'yn', 'metavar': '<auto>', |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
268 |
'default': 'n', |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
269 |
'help': 'automatic mode: never ask and use default answer to every question', |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
270 |
} |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
271 |
), |
0 | 272 |
) |
273 |
def run(self, args): |
|
274 |
"""run the command with its specific arguments""" |
|
275 |
from logilab.common.adbh import get_adv_func_helper |
|
276 |
from indexer import get_indexer |
|
136
ff51a18c66a3
ask less questions on instance creation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
277 |
verbose = self.get('verbose') |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
278 |
automatic = self.get('automatic') |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
279 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 280 |
config = ServerConfiguration.config_for(appid) |
281 |
create_db = self.config.create_db |
|
282 |
source = config.sources()['system'] |
|
283 |
driver = source['db-driver'] |
|
284 |
helper = get_adv_func_helper(driver) |
|
285 |
if create_db: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
286 |
print '\n'+underline_title('Creating the system database') |
0 | 287 |
# 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
|
288 |
dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
0 | 289 |
cursor = dbcnx.cursor() |
290 |
try: |
|
291 |
if helper.users_support: |
|
292 |
user = source['db-user'] |
|
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
293 |
if not helper.user_exists(cursor, user) and (automatic or \ |
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
294 |
ASK.confirm('Create db user %s ?' % user, default_is_yes=False)): |
0 | 295 |
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
|
296 |
print '-> user %s created.' % user |
0 | 297 |
dbname = source['db-name'] |
298 |
if dbname in helper.list_databases(cursor): |
|
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
299 |
if automatic or ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
0 | 300 |
cursor.execute('DROP DATABASE %s' % dbname) |
301 |
else: |
|
302 |
return |
|
303 |
if dbcnx.logged_user != source['db-user']: |
|
304 |
helper.create_database(cursor, dbname, source['db-user'], |
|
305 |
source['db-encoding']) |
|
306 |
else: |
|
307 |
helper.create_database(cursor, dbname, |
|
308 |
encoding=source['db-encoding']) |
|
309 |
dbcnx.commit() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
310 |
print '-> database %s created.' % source['db-name'] |
0 | 311 |
except: |
312 |
dbcnx.rollback() |
|
313 |
raise |
|
1469 | 314 |
cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
0 | 315 |
cursor = cnx.cursor() |
316 |
indexer = get_indexer(driver) |
|
317 |
indexer.init_extensions(cursor) |
|
1469 | 318 |
# postgres specific stuff |
0 | 319 |
if driver == 'postgres': |
320 |
# install plpythonu/plpgsql language if not installed by the cube |
|
3303 | 321 |
langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
3115
29262ba01464
minimal steps to have cw running on windows
Aurélien Campéas
parents:
2969
diff
changeset
|
322 |
for extlang in langs: |
0 | 323 |
helper.create_language(cursor, extlang) |
324 |
cursor.close() |
|
325 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
326 |
print '-> database for instance %s created and necessary extensions installed.' % appid |
0 | 327 |
print |
2969
d95f23a0fc3b
F add --automatic/-a option to run db-create without user input
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2851
diff
changeset
|
328 |
if automatic or ASK.confirm('Run db-init to initialize the system database ?'): |
0 | 329 |
cmd_run('db-init', config.appid) |
330 |
else: |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
331 |
print ('-> nevermind, you can do it later with ' |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2394
diff
changeset
|
332 |
'"cubicweb-ctl db-init %s".' % self.config.appid) |
0 | 333 |
|
1469 | 334 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
335 |
class InitInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
336 |
"""Initialize the system database of an instance (run after 'db-create'). |
1469 | 337 |
|
0 | 338 |
You will be prompted for a login / password to use to connect to |
339 |
the system database. The given user should have the create tables, |
|
340 |
and grant permissions. |
|
341 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
342 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
343 |
the identifier of the instance to initialize. |
0 | 344 |
""" |
345 |
name = 'db-init' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
346 |
arguments = '<instance>' |
1469 | 347 |
|
0 | 348 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
349 |
('drop', |
0 | 350 |
{'short': 'd', 'action': 'store_true', |
351 |
'default': False, |
|
352 |
'help': 'insert drop statements to remove previously existant \ |
|
353 |
tables, indexes... (no by default)'}), |
|
354 |
) |
|
355 |
||
356 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
357 |
print '\n'+underline_title('Initializing the system database') |
0 | 358 |
from cubicweb.server import init_repository |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
359 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 360 |
config = ServerConfiguration.config_for(appid) |
361 |
init_repository(config, drop=self.config.drop) |
|
362 |
||
363 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
364 |
class GrantUserOnInstanceCommand(Command): |
0 | 365 |
"""Grant a database user on a repository system database. |
1469 | 366 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
367 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
368 |
the identifier of the instance |
0 | 369 |
<user> |
370 |
the database's user requiring grant access |
|
371 |
""" |
|
372 |
name = 'db-grant-user' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
373 |
arguments = '<instance> <user>' |
0 | 374 |
|
375 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
376 |
('set-owner', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
377 |
{'short': 'o', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 378 |
'default' : False, |
379 |
'help': 'Set the user as tables owner if yes (no by default).'} |
|
380 |
), |
|
381 |
) |
|
382 |
def run(self, args): |
|
383 |
"""run the command with its specific arguments""" |
|
384 |
from cubicweb.server.sqlutils import sqlexec, sqlgrants |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
385 |
appid = pop_arg(args, 1, msg='No instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
386 |
user = pop_arg(args, msg='No user specified !') |
0 | 387 |
config = ServerConfiguration.config_for(appid) |
388 |
source = config.sources()['system'] |
|
389 |
set_owner = self.config.set_owner |
|
390 |
cnx = system_source_cnx(source, special_privs='GRANT') |
|
391 |
cursor = cnx.cursor() |
|
392 |
schema = config.load_schema() |
|
393 |
try: |
|
394 |
sqlexec(sqlgrants(schema, source['db-driver'], user, |
|
395 |
set_owner=set_owner), cursor) |
|
396 |
except Exception, ex: |
|
397 |
cnx.rollback() |
|
398 |
import traceback |
|
399 |
traceback.print_exc() |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
400 |
print '-> an error occured:', ex |
0 | 401 |
else: |
402 |
cnx.commit() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
403 |
print '-> rights granted to %s on instance %s.' % (appid, user) |
0 | 404 |
|
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
405 |
class ResetAdminPasswordCommand(Command): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
406 |
"""Reset the administrator password. |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
407 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
408 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
409 |
the identifier of the instance |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
410 |
""" |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
411 |
name = 'reset-admin-pwd' |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
412 |
arguments = '<instance>' |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
413 |
|
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
414 |
def run(self, args): |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
415 |
"""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
|
416 |
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
|
417 |
from cubicweb.server.utils import crypt_password, manager_userpasswd |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
418 |
appid = pop_arg(args, 1, msg='No instance specified !') |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
419 |
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
|
420 |
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
|
421 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
422 |
adminlogin = sourcescfg['admin']['login'] |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
423 |
except KeyError: |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
424 |
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
|
425 |
sys.exit(1) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
426 |
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
|
427 |
cursor = cnx.cursor() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
428 |
_, 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
|
429 |
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
|
430 |
try: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
431 |
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
|
432 |
% {'sp': SQL_PREFIX, |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
433 |
'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
|
434 |
cursor, withpb=False) |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
435 |
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
|
436 |
sconfig['login'] = adminlogin |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
437 |
sconfig['password'] = passwd |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
438 |
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
|
439 |
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
|
440 |
except Exception, ex: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
441 |
cnx.rollback() |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
442 |
import traceback |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
443 |
traceback.print_exc() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
444 |
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
|
445 |
else: |
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
446 |
cnx.commit() |
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
447 |
print '-> password reset, sources file regenerated.' |
0 | 448 |
|
1469 | 449 |
|
0 | 450 |
class StartRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
451 |
"""Start an CubicWeb RQL server for a given instance. |
1469 | 452 |
|
0 | 453 |
The server will be accessible through pyro |
454 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
455 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
456 |
the identifier of the instance to initialize. |
0 | 457 |
""" |
458 |
name = 'start-repository' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
459 |
arguments = '<instance>' |
1469 | 460 |
|
0 | 461 |
options = ( |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
462 |
('debug', |
0 | 463 |
{'short': 'D', 'action' : 'store_true', |
464 |
'help': 'start server in debug mode.'}), |
|
465 |
) |
|
466 |
||
467 |
def run(self, args): |
|
468 |
from cubicweb.server.server import RepositoryServer |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
469 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 470 |
config = ServerConfiguration.config_for(appid) |
3786
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
471 |
if sys.platform == 'win32': |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
472 |
if not self.config.debug: |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
473 |
from logging import getLogger |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
474 |
logger = getLogger('cubicweb.ctl') |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
475 |
logger.info('Forcing debug mode on win32 platform') |
11d98118f0eb
force debug mode on cubicweb-ctl start on win32
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
3539
diff
changeset
|
476 |
self.config.debug = True |
0 | 477 |
debug = self.config.debug |
478 |
# create the server |
|
479 |
server = RepositoryServer(config, debug) |
|
480 |
# go ! (don't daemonize in debug mode) |
|
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
481 |
pidfile = config['pid-file'] |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
482 |
# ensure the directory where the pid-file should be set exists (for |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
483 |
# instance /var/run/cubicweb may be deleted on computer restart) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
484 |
piddir = os.path.dirname(pidfile) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
485 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
486 |
os.makedirs(piddir) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2682
diff
changeset
|
487 |
if not debug and server.daemonize(pidfile) == -1: |
0 | 488 |
return |
489 |
uid = config['uid'] |
|
490 |
if uid is not None: |
|
491 |
try: |
|
492 |
uid = int(uid) |
|
493 |
except ValueError: |
|
494 |
from pwd import getpwnam |
|
495 |
uid = getpwnam(uid).pw_uid |
|
496 |
os.setuid(uid) |
|
497 |
server.install_sig_handlers() |
|
498 |
server.connect(config['host'], 0) |
|
499 |
server.run() |
|
500 |
||
501 |
||
502 |
def _remote_dump(host, appid, output, sudo=False): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
503 |
# XXX generate unique/portable file name |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
504 |
from datetime import date |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
505 |
filename = '%s-%s.tgz' % (appid, date.today().strftime('%Y-%m-%d')) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
506 |
dmpcmd = 'cubicweb-ctl db-dump -o /tmp/%s %s' % (filename, appid) |
0 | 507 |
if sudo: |
508 |
dmpcmd = 'sudo %s' % (dmpcmd) |
|
509 |
dmpcmd = 'ssh -t %s "%s"' % (host, dmpcmd) |
|
510 |
print dmpcmd |
|
511 |
if os.system(dmpcmd): |
|
512 |
raise ExecutionError('Error while dumping the database') |
|
513 |
if output is None: |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
514 |
output = filename |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
515 |
cmd = 'scp %s:/tmp/%s %s' % (host, filename, output) |
0 | 516 |
print cmd |
517 |
if os.system(cmd): |
|
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
518 |
raise ExecutionError('Error while retrieving the dump at /tmp/%s' % filename) |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
519 |
rmcmd = 'ssh -t %s "rm -f /tmp/%s"' % (host, filename) |
0 | 520 |
print rmcmd |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2507
diff
changeset
|
521 |
if os.system(rmcmd) and not ASK.confirm( |
2837
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
522 |
'An error occured while deleting remote dump at /tmp/%s. ' |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
523 |
'Continue anyway?' % filename): |
65df5df190a5
B [cwctl] fix db-{dump,copy,restore} commands
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
524 |
raise ExecutionError('Error while deleting remote dump at /tmp/%s' % filename) |
0 | 525 |
|
526 |
def _local_dump(appid, output): |
|
527 |
config = ServerConfiguration.config_for(appid) |
|
528 |
# schema=1 to avoid unnecessary schema loading |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
529 |
mih = config.migration_handler(connect=False, schema=1, verbosity=1) |
0 | 530 |
mih.backup_database(output, askconfirm=False) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
531 |
mih.shutdown() |
0 | 532 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
533 |
def _local_restore(appid, backupfile, drop, systemonly=True): |
0 | 534 |
config = ServerConfiguration.config_for(appid) |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
535 |
config.verbosity = 1 # else we won't be asked for confirmation on problems |
2507
45248d0ad8a0
don't check versions on db-restore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
536 |
config.repairing = 1 # don't check versions |
0 | 537 |
# schema=1 to avoid unnecessary schema loading |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
538 |
mih = config.migration_handler(connect=False, schema=1, verbosity=1) |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
539 |
mih.restore_database(backupfile, drop, systemonly, askconfirm=False) |
0 | 540 |
repo = mih.repo_connect() |
541 |
# version of the database |
|
542 |
dbversions = repo.get_versions() |
|
543 |
mih.shutdown() |
|
544 |
if not dbversions: |
|
545 |
print "bad or missing version information in the database, don't upgrade file system" |
|
546 |
return |
|
547 |
# version of installed software |
|
548 |
eversion = dbversions['cubicweb'] |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
549 |
status = instance_status(config, eversion, dbversions) |
0 | 550 |
# * database version > installed software |
551 |
if status == 'needsoftupgrade': |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
552 |
print "** The database of %s is more recent than the installed software!" % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
553 |
print "** Upgrade your software, then migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
554 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 555 |
return |
556 |
# * database version < installed software, an upgrade will be necessary |
|
557 |
# anyway, just rewrite vc.conf and warn user he has to upgrade |
|
2851
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
558 |
elif status == 'needapplupgrade': |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
559 |
print "** The database of %s is older than the installed software." % config.appid |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
560 |
print "** Migrate the database by running the command" |
d3b80a4faf3f
F [cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2837
diff
changeset
|
561 |
print "** 'cubicweb-ctl upgrade %s'" % config.appid |
0 | 562 |
return |
563 |
# * database version = installed software, database version = instance fs version |
|
564 |
# ok! |
|
565 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
566 |
def instance_status(config, cubicwebapplversion, vcconf): |
0 | 567 |
cubicwebversion = config.cubicweb_version() |
568 |
if cubicwebapplversion > cubicwebversion: |
|
569 |
return 'needsoftupgrade' |
|
570 |
if cubicwebapplversion < cubicwebversion: |
|
571 |
return 'needapplupgrade' |
|
572 |
for cube in config.cubes(): |
|
573 |
try: |
|
574 |
softversion = config.cube_version(cube) |
|
575 |
except ConfigurationError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
576 |
print '-> Error: no cube version information for %s, please check that the cube is installed.' % cube |
0 | 577 |
continue |
578 |
try: |
|
579 |
applversion = vcconf[cube] |
|
580 |
except KeyError: |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
581 |
print '-> Error: no cube version information for %s in version configuration.' % cube |
1469 | 582 |
continue |
0 | 583 |
if softversion == applversion: |
584 |
continue |
|
585 |
if softversion > applversion: |
|
586 |
return 'needsoftupgrade' |
|
587 |
elif softversion < applversion: |
|
588 |
return 'needapplupgrade' |
|
589 |
return None |
|
1469 | 590 |
|
0 | 591 |
|
592 |
class DBDumpCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
593 |
"""Backup the system database of an instance. |
1469 | 594 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
595 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
596 |
the identifier of the instance to backup |
0 | 597 |
format [[user@]host:]appname |
598 |
""" |
|
599 |
name = 'db-dump' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
600 |
arguments = '<instance>' |
0 | 601 |
|
602 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
603 |
('output', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
604 |
{'short': 'o', 'type' : 'string', 'metavar' : '<file>', |
0 | 605 |
'default' : None, |
606 |
'help': 'Specify the backup file where the backup will be stored.'} |
|
607 |
), |
|
608 |
('sudo', |
|
609 |
{'short': 's', 'action' : 'store_true', |
|
610 |
'default' : False, |
|
611 |
'help': 'Use sudo on the remote host.'} |
|
612 |
), |
|
613 |
) |
|
614 |
||
615 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
616 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 617 |
if ':' in appid: |
618 |
host, appid = appid.split(':') |
|
619 |
_remote_dump(host, appid, self.config.output, self.config.sudo) |
|
620 |
else: |
|
621 |
_local_dump(appid, self.config.output) |
|
622 |
||
623 |
||
624 |
class DBRestoreCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
625 |
"""Restore the system database of an instance. |
1469 | 626 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
627 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
628 |
the identifier of the instance to restore |
0 | 629 |
""" |
630 |
name = 'db-restore' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
631 |
arguments = '<instance> <backupfile>' |
0 | 632 |
|
633 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
634 |
('no-drop', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
635 |
{'short': 'n', 'action' : 'store_true', 'default' : False, |
0 | 636 |
'help': 'for some reason the database doesn\'t exist and so ' |
637 |
'should not be dropped.'} |
|
638 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
639 |
('restore-all', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
640 |
{'short': 'r', 'action' : 'store_true', 'default' : False, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
641 |
'help': 'restore everything, eg not only the system source database ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
642 |
'but also data for all sources supporting backup/restore and custom ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
643 |
'instance data. In that case, <backupfile> is expected to be the ' |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
644 |
'timestamp of the backup to restore, not a file'} |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
645 |
), |
0 | 646 |
) |
647 |
||
648 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
649 |
appid = pop_arg(args, 1, msg='No instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
650 |
backupfile = pop_arg(args, msg='No backup file or timestamp specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
651 |
_local_restore(appid, backupfile, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
652 |
drop=not self.config.no_drop, |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
653 |
systemonly=not self.config.restore_all) |
0 | 654 |
|
655 |
||
656 |
class DBCopyCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
657 |
"""Copy the system database of an instance (backup and restore). |
1469 | 658 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
659 |
<src-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
660 |
the identifier of the instance to backup |
0 | 661 |
format [[user@]host:]appname |
662 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
663 |
<dest-instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
664 |
the identifier of the instance to restore |
0 | 665 |
""" |
666 |
name = 'db-copy' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
667 |
arguments = '<src-instance> <dest-instance>' |
0 | 668 |
|
669 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
670 |
('no-drop', |
1469 | 671 |
{'short': 'n', 'action' : 'store_true', |
0 | 672 |
'default' : False, |
673 |
'help': 'For some reason the database doesn\'t exist and so ' |
|
674 |
'should not be dropped.'} |
|
675 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
676 |
('keep-dump', |
0 | 677 |
{'short': 'k', 'action' : 'store_true', |
678 |
'default' : False, |
|
679 |
'help': 'Specify that the dump file should not be automatically removed.'} |
|
680 |
), |
|
681 |
('sudo', |
|
682 |
{'short': 's', 'action' : 'store_true', |
|
683 |
'default' : False, |
|
684 |
'help': 'Use sudo on the remote host.'} |
|
685 |
), |
|
686 |
) |
|
687 |
||
688 |
def run(self, args): |
|
689 |
import tempfile |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
690 |
srcappid = pop_arg(args, 1, msg='No source instance specified !') |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
691 |
destappid = pop_arg(args, msg='No destination instance specified !') |
3148 | 692 |
fd, output = tempfile.mkstemp() |
693 |
os.close(fd) |
|
0 | 694 |
if ':' in srcappid: |
695 |
host, srcappid = srcappid.split(':') |
|
696 |
_remote_dump(host, srcappid, output, self.config.sudo) |
|
697 |
else: |
|
698 |
_local_dump(srcappid, output) |
|
699 |
_local_restore(destappid, output, not self.config.no_drop) |
|
700 |
if self.config.keep_dump: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2107
diff
changeset
|
701 |
print '-> you can get the dump file at', output |
0 | 702 |
else: |
703 |
os.remove(output) |
|
704 |
||
1469 | 705 |
|
0 | 706 |
class CheckRepositoryCommand(Command): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
707 |
"""Check integrity of the system database of an instance. |
1469 | 708 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
709 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
710 |
the identifier of the instance to check |
0 | 711 |
""" |
712 |
name = 'db-check' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
713 |
arguments = '<instance>' |
0 | 714 |
|
715 |
options = ( |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
716 |
('checks', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
717 |
{'short': 'c', 'type' : 'csv', 'metavar' : '<check list>', |
0 | 718 |
'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'), |
719 |
'help': 'Comma separated list of check to run. By default run all \ |
|
720 |
checks, i.e. entities, relations, text_index and metadata.'} |
|
721 |
), |
|
1469 | 722 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
723 |
('autofix', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
724 |
{'short': 'a', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 725 |
'default' : False, |
726 |
'help': 'Automatically correct integrity problems if this option \ |
|
727 |
is set to "y" or "yes", else only display them'} |
|
728 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
729 |
('reindex', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
730 |
{'short': 'r', 'type' : 'yn', 'metavar' : '<yes or no>', |
0 | 731 |
'default' : False, |
732 |
'help': 're-indexes the database for full text search if this \ |
|
733 |
option is set to "y" or "yes" (may be long for large database).'} |
|
734 |
), |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
735 |
('force', |
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
736 |
{'short': 'f', 'action' : 'store_true', |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
737 |
'default' : False, |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
738 |
'help': 'don\'t check instance is up to date.'} |
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
739 |
), |
1469 | 740 |
|
0 | 741 |
) |
742 |
||
743 |
def run(self, args): |
|
744 |
from cubicweb.server.checkintegrity import check |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
745 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 746 |
config = ServerConfiguration.config_for(appid) |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2461
diff
changeset
|
747 |
config.repairing = self.config.force |
0 | 748 |
repo, cnx = repo_cnx(config) |
749 |
check(repo, cnx, |
|
750 |
self.config.checks, self.config.reindex, self.config.autofix) |
|
751 |
||
752 |
||
753 |
class RebuildFTICommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
754 |
"""Rebuild the full-text index of the system database of an instance. |
1469 | 755 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
756 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
757 |
the identifier of the instance to rebuild |
0 | 758 |
""" |
759 |
name = 'db-rebuild-fti' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
760 |
arguments = '<instance>' |
0 | 761 |
|
762 |
options = () |
|
763 |
||
764 |
def run(self, args): |
|
765 |
from cubicweb.server.checkintegrity import reindex_entities |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
766 |
appid = pop_arg(args, 1, msg='No instance specified !') |
0 | 767 |
config = ServerConfiguration.config_for(appid) |
768 |
repo, cnx = repo_cnx(config) |
|
769 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
770 |
reindex_entities(repo.schema, session) |
|
771 |
cnx.commit() |
|
772 |
||
1469 | 773 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
774 |
class SynchronizeInstanceSchemaCommand(Command): |
0 | 775 |
"""Synchronize persistent schema with cube schema. |
1469 | 776 |
|
0 | 777 |
Will synchronize common stuff between the cube schema and the |
778 |
actual persistent schema, but will not add/remove any entity or relation. |
|
779 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
780 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
781 |
the identifier of the instance to synchronize. |
0 | 782 |
""" |
783 |
name = 'schema-sync' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
784 |
arguments = '<instance>' |
0 | 785 |
|
786 |
def run(self, args): |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
787 |
appid = pop_arg(args, msg='No instance specified !') |
0 | 788 |
config = ServerConfiguration.config_for(appid) |
789 |
mih = config.migration_handler() |
|
790 |
mih.cmd_synchronize_schema() |
|
791 |
||
792 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
793 |
register_commands( (CreateInstanceDBCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
794 |
InitInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
795 |
GrantUserOnInstanceCommand, |
1912
2b9432262240
[server] provide a new reset-admin-pwd command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1682
diff
changeset
|
796 |
ResetAdminPasswordCommand, |
0 | 797 |
StartRepositoryCommand, |
798 |
DBDumpCommand, |
|
799 |
DBRestoreCommand, |
|
800 |
DBCopyCommand, |
|
801 |
CheckRepositoryCommand, |
|
802 |
RebuildFTICommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
803 |
SynchronizeInstanceSchemaCommand, |
0 | 804 |
) ) |