author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 27 Jul 2009 18:43:47 +0200 | |
changeset 2525 | 19eaaf9aebf2 |
parent 2512 | 106b2a05dc88 |
child 2532 | f7ca29d75183 |
permissions | -rw-r--r-- |
0 | 1 |
"""%%prog %s [options] %s |
2 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
3 |
CubicWeb main instances controller. |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1898
diff
changeset
|
4 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 5 |
%s""" |
6 |
||
7 |
import sys |
|
1342
4273e44852cb
no more cubes_dir class method
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
8 |
from os import remove, listdir, system, kill, getpgid, pathsep |
0 | 9 |
from os.path import exists, join, isfile, isdir |
10 |
||
1132 | 11 |
from logilab.common.clcommands import register_commands, pop_arg |
12 |
||
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
13 |
from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage, underline_title |
1132 | 14 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CONFIGURATIONS |
15 |
from cubicweb.toolsutils import Command, main_run, rm, create_dir, confirm |
|
1446 | 16 |
|
0 | 17 |
def wait_process_end(pid, maxtry=10, waittime=1): |
18 |
"""wait for a process to actually die""" |
|
19 |
import signal |
|
20 |
from time import sleep |
|
21 |
nbtry = 0 |
|
22 |
while nbtry < maxtry: |
|
23 |
try: |
|
24 |
kill(pid, signal.SIGUSR1) |
|
25 |
except OSError: |
|
26 |
break |
|
27 |
nbtry += 1 |
|
28 |
sleep(waittime) |
|
29 |
else: |
|
30 |
raise ExecutionError('can\'t kill process %s' % pid) |
|
31 |
||
32 |
def list_instances(regdir): |
|
33 |
return sorted(idir for idir in listdir(regdir) if isdir(join(regdir, idir))) |
|
34 |
||
35 |
def detect_available_modes(templdir): |
|
36 |
modes = [] |
|
37 |
for fname in ('schema', 'schema.py'): |
|
38 |
if exists(join(templdir, fname)): |
|
39 |
modes.append('repository') |
|
40 |
break |
|
41 |
for fname in ('data', 'views', 'views.py'): |
|
42 |
if exists(join(templdir, fname)): |
|
43 |
modes.append('web ui') |
|
44 |
break |
|
45 |
return modes |
|
1446 | 46 |
|
47 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
48 |
class InstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
49 |
"""base class for command taking 0 to n instance id as arguments |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
50 |
(0 meaning all registered instances) |
0 | 51 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
52 |
arguments = '[<instance>...]' |
0 | 53 |
options = ( |
54 |
("force", |
|
55 |
{'short': 'f', 'action' : 'store_true', |
|
56 |
'default': False, |
|
57 |
'help': 'force command without asking confirmation', |
|
58 |
} |
|
59 |
), |
|
60 |
) |
|
61 |
actionverb = None |
|
1446 | 62 |
|
0 | 63 |
def ordered_instances(self): |
64 |
"""return instances in the order in which they should be started, |
|
65 |
considering $REGISTRY_DIR/startorder file if it exists (useful when |
|
66 |
some instances depends on another as external source |
|
67 |
""" |
|
1132 | 68 |
regdir = cwcfg.registry_dir() |
0 | 69 |
_allinstances = list_instances(regdir) |
70 |
if isfile(join(regdir, 'startorder')): |
|
71 |
allinstances = [] |
|
72 |
for line in file(join(regdir, 'startorder')): |
|
73 |
line = line.strip() |
|
74 |
if line and not line.startswith('#'): |
|
75 |
try: |
|
76 |
_allinstances.remove(line) |
|
77 |
allinstances.append(line) |
|
78 |
except ValueError: |
|
1132 | 79 |
print ('ERROR: startorder file contains unexistant ' |
80 |
'instance %s' % line) |
|
0 | 81 |
allinstances += _allinstances |
82 |
else: |
|
83 |
allinstances = _allinstances |
|
84 |
return allinstances |
|
1446 | 85 |
|
0 | 86 |
def run(self, args): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
87 |
"""run the <command>_method on each argument (a list of instance |
0 | 88 |
identifiers) |
89 |
""" |
|
90 |
if not args: |
|
91 |
args = self.ordered_instances() |
|
92 |
try: |
|
93 |
askconfirm = not self.config.force |
|
94 |
except AttributeError: |
|
95 |
# no force option |
|
96 |
askconfirm = False |
|
97 |
else: |
|
98 |
askconfirm = False |
|
99 |
self.run_args(args, askconfirm) |
|
1446 | 100 |
|
0 | 101 |
def run_args(self, args, askconfirm): |
102 |
for appid in args: |
|
103 |
if askconfirm: |
|
104 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
105 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 106 |
continue |
107 |
self.run_arg(appid) |
|
1446 | 108 |
|
0 | 109 |
def run_arg(self, appid): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
110 |
cmdmeth = getattr(self, '%s_instance' % self.name) |
0 | 111 |
try: |
112 |
cmdmeth(appid) |
|
113 |
except (KeyboardInterrupt, SystemExit): |
|
114 |
print >> sys.stderr, '%s aborted' % self.name |
|
115 |
sys.exit(2) # specific error code |
|
116 |
except (ExecutionError, ConfigurationError), ex: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
117 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 118 |
appid, self.actionverb, ex) |
119 |
except Exception, ex: |
|
120 |
import traceback |
|
121 |
traceback.print_exc() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
122 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 123 |
appid, self.actionverb, ex) |
124 |
||
125 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
126 |
class InstanceCommandFork(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
127 |
"""Same as `InstanceCommand`, but command is forked in a new environment |
0 | 128 |
for each argument |
129 |
""" |
|
130 |
||
131 |
def run_args(self, args, askconfirm): |
|
132 |
if len(args) > 1: |
|
133 |
forkcmd = ' '.join(w for w in sys.argv if not w in args) |
|
134 |
else: |
|
135 |
forkcmd = None |
|
136 |
for appid in args: |
|
137 |
if askconfirm: |
|
138 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
139 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 140 |
continue |
141 |
if forkcmd: |
|
142 |
status = system('%s %s' % (forkcmd, appid)) |
|
143 |
if status: |
|
2156
fadb86b040a9
continue to run command on other instances, do not exit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2104
diff
changeset
|
144 |
print '%s exited with status %s' % (forkcmd, status) |
0 | 145 |
else: |
146 |
self.run_arg(appid) |
|
1446 | 147 |
|
0 | 148 |
# base commands ############################################################### |
149 |
||
150 |
class ListCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
151 |
"""List configurations, cubes and instances. |
0 | 152 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
153 |
list available configurations, installed cubes, and registered instances |
0 | 154 |
""" |
155 |
name = 'list' |
|
156 |
options = ( |
|
157 |
('verbose', |
|
1446 | 158 |
{'short': 'v', 'action' : 'store_true', |
159 |
'help': "display more information."}), |
|
0 | 160 |
) |
1446 | 161 |
|
0 | 162 |
def run(self, args): |
163 |
"""run the command with its specific arguments""" |
|
164 |
if args: |
|
165 |
raise BadCommandUsage('Too much arguments') |
|
1132 | 166 |
print 'CubicWeb version:', cwcfg.cubicweb_version() |
167 |
print 'Detected mode:', cwcfg.mode |
|
0 | 168 |
print |
169 |
print 'Available configurations:' |
|
170 |
for config in CONFIGURATIONS: |
|
171 |
print '*', config.name |
|
172 |
for line in config.__doc__.splitlines(): |
|
173 |
line = line.strip() |
|
174 |
if not line: |
|
175 |
continue |
|
176 |
print ' ', line |
|
1446 | 177 |
print |
0 | 178 |
try: |
1342
4273e44852cb
no more cubes_dir class method
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
179 |
cubesdir = pathsep.join(cwcfg.cubes_search_path()) |
1132 | 180 |
namesize = max(len(x) for x in cwcfg.available_cubes()) |
0 | 181 |
except ConfigurationError, ex: |
182 |
print 'No cubes available:', ex |
|
183 |
except ValueError: |
|
184 |
print 'No cubes available in %s' % cubesdir |
|
185 |
else: |
|
186 |
print 'Available cubes (%s):' % cubesdir |
|
1132 | 187 |
for cube in cwcfg.available_cubes(): |
0 | 188 |
if cube in ('CVS', '.svn', 'shared', '.hg'): |
189 |
continue |
|
190 |
try: |
|
1132 | 191 |
tinfo = cwcfg.cube_pkginfo(cube) |
0 | 192 |
tversion = tinfo.version |
193 |
except ConfigurationError: |
|
194 |
tinfo = None |
|
195 |
tversion = '[missing cube information]' |
|
196 |
print '* %s %s' % (cube.ljust(namesize), tversion) |
|
197 |
if self.config.verbose: |
|
198 |
shortdesc = tinfo and (getattr(tinfo, 'short_desc', '') |
|
199 |
or tinfo.__doc__) |
|
200 |
if shortdesc: |
|
201 |
print ' '+ ' \n'.join(shortdesc.splitlines()) |
|
2024
82128fe6798c
redo juj's fix in stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1898
diff
changeset
|
202 |
modes = detect_available_modes(cwcfg.cube_dir(cube)) |
0 | 203 |
print ' available modes: %s' % ', '.join(modes) |
204 |
print |
|
205 |
try: |
|
1132 | 206 |
regdir = cwcfg.registry_dir() |
0 | 207 |
except ConfigurationError, ex: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
208 |
print 'No instance available:', ex |
0 | 209 |
print |
210 |
return |
|
211 |
instances = list_instances(regdir) |
|
212 |
if instances: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
213 |
print 'Available instances (%s):' % regdir |
0 | 214 |
for appid in instances: |
1132 | 215 |
modes = cwcfg.possible_configurations(appid) |
0 | 216 |
if not modes: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
217 |
print '* %s (BROKEN instance, no configuration found)' % appid |
0 | 218 |
continue |
219 |
print '* %s (%s)' % (appid, ', '.join(modes)) |
|
220 |
try: |
|
1132 | 221 |
config = cwcfg.config_for(appid, modes[0]) |
1446 | 222 |
except Exception, exc: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
223 |
print ' (BROKEN instance, %s)' % exc |
0 | 224 |
continue |
225 |
else: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
226 |
print 'No instance available in %s' % regdir |
0 | 227 |
print |
228 |
||
229 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
230 |
class CreateInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
231 |
"""Create an instance from a cube. This is an unified |
0 | 232 |
command which can handle web / server / all-in-one installation |
233 |
according to available parts of the software library and of the |
|
234 |
desired cube. |
|
235 |
||
236 |
<cube> |
|
237 |
the name of cube to use (list available cube names using |
|
238 |
the "list" command). You can use several cubes by separating |
|
239 |
them using comma (e.g. 'jpl,eemail') |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
240 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
241 |
an identifier for the instance to create |
0 | 242 |
""" |
243 |
name = 'create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
244 |
arguments = '<cube> <instance>' |
0 | 245 |
options = ( |
246 |
("config-level", |
|
247 |
{'short': 'l', 'type' : 'int', 'metavar': '<level>', |
|
248 |
'default': 0, |
|
249 |
'help': 'configuration level (0..2): 0 will ask for essential \ |
|
250 |
configuration parameters only while 2 will ask for all parameters', |
|
251 |
} |
|
252 |
), |
|
253 |
("config", |
|
254 |
{'short': 'c', 'type' : 'choice', 'metavar': '<install type>', |
|
255 |
'choices': ('all-in-one', 'repository', 'twisted'), |
|
256 |
'default': 'all-in-one', |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
257 |
'help': 'installation type, telling which part of an instance \ |
0 | 258 |
should be installed. You can list available configurations using the "list" \ |
259 |
command. Default to "all-in-one", e.g. an installation embedding both the RQL \ |
|
260 |
repository and the web server.', |
|
261 |
} |
|
262 |
), |
|
263 |
) |
|
1446 | 264 |
|
0 | 265 |
def run(self, args): |
266 |
"""run the command with its specific arguments""" |
|
267 |
from logilab.common.textutils import get_csv |
|
268 |
configname = self.config.config |
|
269 |
cubes = get_csv(pop_arg(args, 1)) |
|
270 |
appid = pop_arg(args) |
|
271 |
# get the configuration and helper |
|
1132 | 272 |
cwcfg.creating = True |
273 |
config = cwcfg.config_for(appid, configname) |
|
0 | 274 |
config.set_language = False |
2104
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
275 |
cubes = config.expand_cubes(cubes) |
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
276 |
config.init_cubes(cubes) |
0 | 277 |
helper = self.config_helper(config) |
278 |
# check the cube exists |
|
279 |
try: |
|
1132 | 280 |
templdirs = [cwcfg.cube_dir(cube) |
0 | 281 |
for cube in cubes] |
282 |
except ConfigurationError, ex: |
|
283 |
print ex |
|
284 |
print '\navailable cubes:', |
|
1132 | 285 |
print ', '.join(cwcfg.available_cubes()) |
0 | 286 |
return |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
287 |
# create the registry directory for this instance |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
288 |
print '\n'+underline_title('Creating the instance %s' % appid) |
0 | 289 |
create_dir(config.apphome) |
290 |
# load site_cubicweb from the cubes dir (if any) |
|
291 |
config.load_site_cubicweb() |
|
292 |
# cubicweb-ctl configuration |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
293 |
print '\n'+underline_title('Configuring the instance (%s.conf)' % configname) |
0 | 294 |
config.input_config('main', self.config.config_level) |
295 |
# configuration'specific stuff |
|
296 |
print |
|
297 |
helper.bootstrap(cubes, self.config.config_level) |
|
298 |
# write down configuration |
|
299 |
config.save() |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
300 |
print '-> generated %s' % config.main_config_file() |
0 | 301 |
# handle i18n files structure |
302 |
# in the first cube given |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
303 |
print '-> preparing i18n catalogs' |
0 | 304 |
from cubicweb.common import i18n |
305 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdirs[0], 'i18n'))] |
|
306 |
errors = config.i18ncompile(langs) |
|
307 |
if errors: |
|
308 |
print '\n'.join(errors) |
|
309 |
if not confirm('error while compiling message catalogs, ' |
|
310 |
'continue anyway ?'): |
|
311 |
print 'creation not completed' |
|
312 |
return |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
313 |
# create the additional data directory for this instance |
0 | 314 |
if config.appdatahome != config.apphome: # true in dev mode |
315 |
create_dir(config.appdatahome) |
|
2489
37a747ad6fd4
#344772: instance backups should be done in instance_data_dir
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
316 |
create_dir(join(config.appdatahome, 'backup')) |
0 | 317 |
if config['uid']: |
318 |
from logilab.common.shellutils import chown |
|
319 |
# this directory should be owned by the uid of the server process |
|
320 |
print 'set %s as owner of the data directory' % config['uid'] |
|
321 |
chown(config.appdatahome, config['uid']) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
322 |
print '\n-> creation done for %r.\n' % config.apphome |
0 | 323 |
helper.postcreate() |
324 |
||
1446 | 325 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
326 |
class DeleteInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
327 |
"""Delete an instance. Will remove instance's files and |
0 | 328 |
unregister it. |
329 |
""" |
|
330 |
name = 'delete' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
331 |
arguments = '<instance>' |
1446 | 332 |
|
0 | 333 |
options = () |
334 |
||
335 |
def run(self, args): |
|
336 |
"""run the command with its specific arguments""" |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
337 |
appid = pop_arg(args, msg="No instance specified !") |
1132 | 338 |
configs = [cwcfg.config_for(appid, configname) |
339 |
for configname in cwcfg.possible_configurations(appid)] |
|
0 | 340 |
if not configs: |
341 |
raise ExecutionError('unable to guess configuration for %s' % appid) |
|
342 |
for config in configs: |
|
343 |
helper = self.config_helper(config, required=False) |
|
344 |
if helper: |
|
345 |
helper.cleanup() |
|
346 |
# remove home |
|
347 |
rm(config.apphome) |
|
348 |
# remove instance data directory |
|
349 |
try: |
|
350 |
rm(config.appdatahome) |
|
351 |
except OSError, ex: |
|
352 |
import errno |
|
353 |
if ex.errno != errno.ENOENT: |
|
354 |
raise |
|
355 |
confignames = ', '.join([config.name for config in configs]) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
356 |
print 'instance %s (%s) deleted' % (appid, confignames) |
0 | 357 |
|
358 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
359 |
# instance commands ######################################################## |
0 | 360 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
361 |
class StartInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
362 |
"""Start the given instances. If no instance is given, start them all. |
1446 | 363 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
364 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
365 |
identifiers of the instances to start. If no instance is |
0 | 366 |
given, start them all. |
367 |
""" |
|
368 |
name = 'start' |
|
369 |
actionverb = 'started' |
|
370 |
options = ( |
|
371 |
("debug", |
|
372 |
{'short': 'D', 'action' : 'store_true', |
|
373 |
'help': 'start server in debug mode.'}), |
|
374 |
("force", |
|
375 |
{'short': 'f', 'action' : 'store_true', |
|
376 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
377 |
'help': 'start the instance even if it seems to be already \ |
0 | 378 |
running.'}), |
379 |
('profile', |
|
380 |
{'short': 'P', 'type' : 'string', 'metavar': '<stat file>', |
|
381 |
'default': None, |
|
382 |
'help': 'profile code and use the specified file to store stats', |
|
383 |
}), |
|
384 |
) |
|
385 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
386 |
def start_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
387 |
"""start the instance's server""" |
0 | 388 |
# use get() since start may be used from other commands (eg upgrade) |
389 |
# without all options defined |
|
390 |
debug = self.get('debug') |
|
391 |
force = self.get('force') |
|
1132 | 392 |
config = cwcfg.config_for(appid) |
0 | 393 |
if self.get('profile'): |
394 |
config.global_set_option('profile', self.config.profile) |
|
395 |
helper = self.config_helper(config, cmdname='start') |
|
396 |
pidf = config['pid-file'] |
|
397 |
if exists(pidf) and not force: |
|
398 |
msg = "%s seems to be running. Remove %s by hand if necessary or use \ |
|
399 |
the --force option." |
|
400 |
raise ExecutionError(msg % (appid, pidf)) |
|
401 |
command = helper.start_command(config, debug) |
|
402 |
if debug: |
|
403 |
print "starting server with command :" |
|
404 |
print command |
|
405 |
if system(command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
406 |
print 'an error occured while starting the instance, not started' |
0 | 407 |
print |
408 |
return False |
|
409 |
if not debug: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
410 |
print 'instance %s started' % appid |
0 | 411 |
return True |
412 |
||
413 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
414 |
class StopInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
415 |
"""Stop the given instances. |
1446 | 416 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
417 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
418 |
identifiers of the instances to stop. If no instance is |
0 | 419 |
given, stop them all. |
420 |
""" |
|
421 |
name = 'stop' |
|
422 |
actionverb = 'stopped' |
|
1446 | 423 |
|
0 | 424 |
def ordered_instances(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
425 |
instances = super(StopInstanceCommand, self).ordered_instances() |
0 | 426 |
instances.reverse() |
427 |
return instances |
|
1446 | 428 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
429 |
def stop_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
430 |
"""stop the instance's server""" |
1132 | 431 |
config = cwcfg.config_for(appid) |
0 | 432 |
helper = self.config_helper(config, cmdname='stop') |
433 |
helper.poststop() # do this anyway |
|
434 |
pidf = config['pid-file'] |
|
435 |
if not exists(pidf): |
|
436 |
print >> sys.stderr, "%s doesn't exist." % pidf |
|
437 |
return |
|
438 |
import signal |
|
439 |
pid = int(open(pidf).read().strip()) |
|
440 |
try: |
|
441 |
kill(pid, signal.SIGTERM) |
|
442 |
except: |
|
443 |
print >> sys.stderr, "process %s seems already dead." % pid |
|
444 |
else: |
|
445 |
try: |
|
446 |
wait_process_end(pid) |
|
447 |
except ExecutionError, ex: |
|
448 |
print >> sys.stderr, ex |
|
449 |
print >> sys.stderr, 'trying SIGKILL' |
|
450 |
try: |
|
451 |
kill(pid, signal.SIGKILL) |
|
452 |
except: |
|
453 |
# probably dead now |
|
454 |
pass |
|
455 |
wait_process_end(pid) |
|
456 |
try: |
|
457 |
remove(pidf) |
|
458 |
except OSError: |
|
459 |
# already removed by twistd |
|
460 |
pass |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
461 |
print 'instance %s stopped' % appid |
1446 | 462 |
|
0 | 463 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
464 |
class RestartInstanceCommand(StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
465 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
466 |
"""Restart the given instances. |
1446 | 467 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
468 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
469 |
identifiers of the instances to restart. If no instance is |
0 | 470 |
given, restart them all. |
471 |
""" |
|
472 |
name = 'restart' |
|
473 |
actionverb = 'restarted' |
|
474 |
||
475 |
def run_args(self, args, askconfirm): |
|
1132 | 476 |
regdir = cwcfg.registry_dir() |
0 | 477 |
if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
478 |
# no specific startorder |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
479 |
super(RestartInstanceCommand, self).run_args(args, askconfirm) |
0 | 480 |
return |
481 |
print ('some specific start order is specified, will first stop all ' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
482 |
'instances then restart them.') |
0 | 483 |
# get instances in startorder |
484 |
stopped = [] |
|
485 |
for appid in args: |
|
486 |
if askconfirm: |
|
487 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
488 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 489 |
continue |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
490 |
self.stop_instance(appid) |
0 | 491 |
stopped.append(appid) |
492 |
forkcmd = [w for w in sys.argv if not w in args] |
|
493 |
forkcmd[1] = 'start' |
|
494 |
forkcmd = ' '.join(forkcmd) |
|
495 |
for appid in reversed(args): |
|
496 |
status = system('%s %s' % (forkcmd, appid)) |
|
497 |
if status: |
|
498 |
sys.exit(status) |
|
1446 | 499 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
500 |
def restart_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
501 |
self.stop_instance(appid) |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
502 |
if self.start_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
503 |
print 'instance %s %s' % (appid, self.actionverb) |
0 | 504 |
|
1446 | 505 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
506 |
class ReloadConfigurationCommand(RestartInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
507 |
"""Reload the given instances. This command is equivalent to a |
0 | 508 |
restart for now. |
1446 | 509 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
510 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
511 |
identifiers of the instances to reload. If no instance is |
0 | 512 |
given, reload them all. |
513 |
""" |
|
514 |
name = 'reload' |
|
1446 | 515 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
516 |
def reload_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
517 |
self.restart_instance(appid) |
1446 | 518 |
|
0 | 519 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
520 |
class StatusCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
521 |
"""Display status information about the given instances. |
1446 | 522 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
523 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
524 |
identifiers of the instances to status. If no instance is |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
given, get status information about all registered instances. |
0 | 526 |
""" |
527 |
name = 'status' |
|
528 |
options = () |
|
529 |
||
1132 | 530 |
@staticmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
531 |
def status_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
532 |
"""print running status information for an instance""" |
1132 | 533 |
for mode in cwcfg.possible_configurations(appid): |
534 |
config = cwcfg.config_for(appid, mode) |
|
0 | 535 |
print '[%s-%s]' % (appid, mode), |
536 |
try: |
|
537 |
pidf = config['pid-file'] |
|
538 |
except KeyError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
539 |
print 'buggy instance, pid file not specified' |
0 | 540 |
continue |
541 |
if not exists(pidf): |
|
542 |
print "doesn't seem to be running" |
|
543 |
continue |
|
544 |
pid = int(open(pidf).read().strip()) |
|
545 |
# trick to guess whether or not the process is running |
|
546 |
try: |
|
547 |
getpgid(pid) |
|
548 |
except OSError: |
|
549 |
print "should be running with pid %s but the process can not be found" % pid |
|
550 |
continue |
|
551 |
print "running with pid %s" % (pid) |
|
552 |
||
553 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
554 |
class UpgradeInstanceCommand(InstanceCommandFork, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
555 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
556 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
557 |
"""Upgrade an instance after cubicweb and/or component(s) upgrade. |
0 | 558 |
|
559 |
For repository update, you will be prompted for a login / password to use |
|
560 |
to connect to the system database. For some upgrades, the given user |
|
561 |
should have create or alter table permissions. |
|
562 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
563 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
564 |
identifiers of the instances to upgrade. If no instance is |
0 | 565 |
given, upgrade them all. |
566 |
""" |
|
567 |
name = 'upgrade' |
|
568 |
actionverb = 'upgraded' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
569 |
options = InstanceCommand.options + ( |
0 | 570 |
('force-componant-version', |
571 |
{'short': 't', 'type' : 'csv', 'metavar': 'cube1=X.Y.Z,cube2=X.Y.Z', |
|
572 |
'default': None, |
|
573 |
'help': 'force migration from the indicated version for the specified cube.'}), |
|
574 |
('force-cubicweb-version', |
|
575 |
{'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z', |
|
576 |
'default': None, |
|
577 |
'help': 'force migration from the indicated cubicweb version.'}), |
|
1446 | 578 |
|
0 | 579 |
('fs-only', |
580 |
{'short': 's', 'action' : 'store_true', |
|
581 |
'default': False, |
|
582 |
'help': 'only upgrade files on the file system, not the database.'}), |
|
583 |
||
584 |
('nostartstop', |
|
585 |
{'short': 'n', 'action' : 'store_true', |
|
586 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
587 |
'help': 'don\'t try to stop instance before migration and to restart it after.'}), |
1446 | 588 |
|
0 | 589 |
('verbosity', |
590 |
{'short': 'v', 'type' : 'int', 'metavar': '<0..2>', |
|
591 |
'default': 1, |
|
592 |
'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \ |
|
593 |
for everything."}), |
|
1446 | 594 |
|
0 | 595 |
('backup-db', |
596 |
{'short': 'b', 'type' : 'yn', 'metavar': '<y or n>', |
|
597 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
598 |
'help': "Backup the instance database before upgrade.\n"\ |
0 | 599 |
"If the option is ommitted, confirmation will be ask.", |
600 |
}), |
|
601 |
||
602 |
('ext-sources', |
|
603 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
604 |
'default': None, |
|
605 |
'help': "For multisources instances, specify to which sources the \ |
|
606 |
repository should connect to for upgrading. When unspecified or 'migration' is \ |
|
607 |
given, appropriate sources for migration will be automatically selected \ |
|
608 |
(recommended). If 'all' is given, will connect to all defined sources.", |
|
609 |
}), |
|
610 |
) |
|
611 |
||
612 |
def ordered_instances(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
613 |
# need this since mro return StopInstanceCommand implementation |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
614 |
return InstanceCommand.ordered_instances(self) |
1446 | 615 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
616 |
def upgrade_instance(self, appid): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
617 |
print '\n' + underline_title('Upgrading the instance %s' % appid) |
0 | 618 |
from logilab.common.changelog import Version |
1132 | 619 |
config = cwcfg.config_for(appid) |
2473
490f88fb99b6
new distinguish repairing/creating from regular start.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2395
diff
changeset
|
620 |
config.repairing = True # notice we're not starting the server |
0 | 621 |
config.verbosity = self.config.verbosity |
1219
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
622 |
try: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
623 |
config.set_sources_mode(self.config.ext_sources or ('migration',)) |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
624 |
except AttributeError: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
625 |
# not a server config |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
626 |
pass |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
627 |
# get instance and installed versions for the server and the componants |
0 | 628 |
mih = config.migration_handler() |
629 |
repo = mih.repo_connect() |
|
630 |
vcconf = repo.get_versions() |
|
631 |
if self.config.force_componant_version: |
|
632 |
packversions = {} |
|
633 |
for vdef in self.config.force_componant_version: |
|
634 |
componant, version = vdef.split('=') |
|
635 |
packversions[componant] = Version(version) |
|
636 |
vcconf.update(packversions) |
|
637 |
toupgrade = [] |
|
638 |
for cube in config.cubes(): |
|
639 |
installedversion = config.cube_version(cube) |
|
640 |
try: |
|
641 |
applversion = vcconf[cube] |
|
642 |
except KeyError: |
|
643 |
config.error('no version information for %s' % cube) |
|
644 |
continue |
|
645 |
if installedversion > applversion: |
|
646 |
toupgrade.append( (cube, applversion, installedversion) ) |
|
1446 | 647 |
cubicwebversion = config.cubicweb_version() |
0 | 648 |
if self.config.force_cubicweb_version: |
649 |
applcubicwebversion = Version(self.config.force_cubicweb_version) |
|
650 |
vcconf['cubicweb'] = applcubicwebversion |
|
651 |
else: |
|
652 |
applcubicwebversion = vcconf.get('cubicweb') |
|
653 |
if cubicwebversion > applcubicwebversion: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
654 |
toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion)) |
0 | 655 |
if not self.config.fs_only and not toupgrade: |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
656 |
print '-> no software migration needed for instance %s.' % appid |
0 | 657 |
return |
658 |
for cube, fromversion, toversion in toupgrade: |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
659 |
print '\n' + underline_title('%s migration %s -> %s' % |
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
660 |
(cube, fromversion, toversion)) |
1404
971b19de6b85
stop application only once we're sure we've something to do, fixing #342689
sylvain.thenault@logilab.fr
parents:
1219
diff
changeset
|
661 |
# only stop once we're sure we have something to do |
1477 | 662 |
if not (cwcfg.mode == 'dev' or self.config.nostartstop): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
663 |
self.stop_instance(appid) |
0 | 664 |
# run cubicweb/componants migration scripts |
665 |
mih.migrate(vcconf, reversed(toupgrade), self.config) |
|
666 |
# rewrite main configuration file |
|
667 |
mih.rewrite_configuration() |
|
668 |
# handle i18n upgrade: |
|
669 |
# * install new languages |
|
670 |
# * recompile catalogs |
|
671 |
# in the first componant given |
|
672 |
from cubicweb.common import i18n |
|
1132 | 673 |
templdir = cwcfg.cube_dir(config.cubes()[0]) |
0 | 674 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))] |
675 |
errors = config.i18ncompile(langs) |
|
676 |
if errors: |
|
677 |
print '\n'.join(errors) |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
678 |
if not confirm('Error while compiling message catalogs, ' |
0 | 679 |
'continue anyway ?'): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
680 |
print '-> migration not completed.' |
0 | 681 |
return |
682 |
mih.shutdown() |
|
683 |
print |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
684 |
print '-> instance migrated.' |
1132 | 685 |
if not (cwcfg.mode == 'dev' or self.config.nostartstop): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
686 |
self.start_instance(appid) |
0 | 687 |
print |
688 |
||
689 |
||
690 |
class ShellCommand(Command): |
|
691 |
"""Run an interactive migration shell. This is a python shell with |
|
692 |
enhanced migration commands predefined in the namespace. An additional |
|
693 |
argument may be given corresponding to a file containing commands to |
|
694 |
execute in batch mode. |
|
695 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
696 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
697 |
the identifier of the instance to connect. |
0 | 698 |
""" |
699 |
name = 'shell' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
700 |
arguments = '<instance> [batch command file]' |
0 | 701 |
options = ( |
702 |
('system-only', |
|
703 |
{'short': 'S', 'action' : 'store_true', |
|
704 |
'default': False, |
|
705 |
'help': 'only connect to the system source when the instance is ' |
|
706 |
'using multiple sources. You can\'t use this option and the ' |
|
707 |
'--ext-sources option at the same time.'}), |
|
1446 | 708 |
|
0 | 709 |
('ext-sources', |
710 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
711 |
'default': None, |
|
712 |
'help': "For multisources instances, specify to which sources the \ |
|
713 |
repository should connect to for upgrading. When unspecified or 'all' given, \ |
|
714 |
will connect to all defined sources. If 'migration' is given, appropriate \ |
|
715 |
sources for migration will be automatically selected.", |
|
716 |
}), |
|
1446 | 717 |
|
0 | 718 |
) |
719 |
def run(self, args): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
720 |
appid = pop_arg(args, 99, msg="No instance specified !") |
1132 | 721 |
config = cwcfg.config_for(appid) |
0 | 722 |
if self.config.ext_sources: |
723 |
assert not self.config.system_only |
|
724 |
sources = self.config.ext_sources |
|
725 |
elif self.config.system_only: |
|
726 |
sources = ('system',) |
|
727 |
else: |
|
728 |
sources = ('all',) |
|
729 |
config.set_sources_mode(sources) |
|
730 |
mih = config.migration_handler() |
|
731 |
if args: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
732 |
for arg in args: |
2318
17f7c3990f9b
Fix shell script execution command.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
2275
diff
changeset
|
733 |
mih.process_script(arg) |
0 | 734 |
else: |
735 |
mih.interactive_shell() |
|
1446 | 736 |
mih.shutdown() |
0 | 737 |
|
738 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
739 |
class RecompileInstanceCatalogsCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
740 |
"""Recompile i18n catalogs for instances. |
1446 | 741 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
742 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
743 |
identifiers of the instances to consider. If no instance is |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
744 |
given, recompile for all registered instances. |
0 | 745 |
""" |
1898
39b37f90a8a4
[cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1477
diff
changeset
|
746 |
name = 'i18ninstance' |
1132 | 747 |
|
748 |
@staticmethod |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
749 |
def i18ninstance_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
750 |
"""recompile instance's messages catalogs""" |
1132 | 751 |
config = cwcfg.config_for(appid) |
0 | 752 |
try: |
753 |
config.bootstrap_cubes() |
|
754 |
except IOError, ex: |
|
755 |
import errno |
|
756 |
if ex.errno != errno.ENOENT: |
|
757 |
raise |
|
758 |
# bootstrap_cubes files doesn't exist |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
759 |
# notify this is not a regular start |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
760 |
config.repairing = True |
0 | 761 |
# create an in-memory repository, will call config.init_cubes() |
762 |
config.repository() |
|
763 |
except AttributeError: |
|
764 |
# web only config |
|
765 |
config.init_cubes(config.repository().get_cubes()) |
|
766 |
errors = config.i18ncompile() |
|
767 |
if errors: |
|
768 |
print '\n'.join(errors) |
|
769 |
||
770 |
||
771 |
class ListInstancesCommand(Command): |
|
772 |
"""list available instances, useful for bash completion.""" |
|
773 |
name = 'listinstances' |
|
774 |
hidden = True |
|
1446 | 775 |
|
0 | 776 |
def run(self, args): |
777 |
"""run the command with its specific arguments""" |
|
1132 | 778 |
regdir = cwcfg.registry_dir() |
0 | 779 |
for appid in sorted(listdir(regdir)): |
780 |
print appid |
|
781 |
||
782 |
||
783 |
class ListCubesCommand(Command): |
|
784 |
"""list available componants, useful for bash completion.""" |
|
785 |
name = 'listcubes' |
|
786 |
hidden = True |
|
1446 | 787 |
|
0 | 788 |
def run(self, args): |
789 |
"""run the command with its specific arguments""" |
|
1132 | 790 |
for cube in cwcfg.available_cubes(): |
0 | 791 |
print cube |
792 |
||
793 |
register_commands((ListCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
794 |
CreateInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
795 |
DeleteInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
796 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
797 |
StopInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
798 |
RestartInstanceCommand, |
0 | 799 |
ReloadConfigurationCommand, |
800 |
StatusCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
801 |
UpgradeInstanceCommand, |
0 | 802 |
ShellCommand, |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
803 |
RecompileInstanceCatalogsCommand, |
0 | 804 |
ListInstancesCommand, ListCubesCommand, |
805 |
)) |
|
806 |
||
1446 | 807 |
|
0 | 808 |
def run(args): |
809 |
"""command line tool""" |
|
1132 | 810 |
cwcfg.load_cwctl_plugins() |
0 | 811 |
main_run(args, __doc__) |
812 |
||
813 |
if __name__ == '__main__': |
|
814 |
run(sys.argv[1:]) |