author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sun, 02 Aug 2009 11:54:02 +0200 | |
changeset 2642 | 2d30de60a8ff |
parent 2633 | bc9386c3b2c9 |
child 2654 | 6512522860aa |
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 |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2532
diff
changeset
|
12 |
from logilab.common.shellutils import ASK |
1132 | 13 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
14 |
from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage, underline_title |
1132 | 15 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CONFIGURATIONS |
2615
1ea41b7c0836
F [dialog] offer to create backup. refactor to use l.c.shellutils.ASK
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2532
diff
changeset
|
16 |
from cubicweb.toolsutils import Command, main_run, rm, create_dir |
1446 | 17 |
|
0 | 18 |
def wait_process_end(pid, maxtry=10, waittime=1): |
19 |
"""wait for a process to actually die""" |
|
20 |
import signal |
|
21 |
from time import sleep |
|
22 |
nbtry = 0 |
|
23 |
while nbtry < maxtry: |
|
24 |
try: |
|
25 |
kill(pid, signal.SIGUSR1) |
|
26 |
except OSError: |
|
27 |
break |
|
28 |
nbtry += 1 |
|
29 |
sleep(waittime) |
|
30 |
else: |
|
31 |
raise ExecutionError('can\'t kill process %s' % pid) |
|
32 |
||
33 |
def list_instances(regdir): |
|
34 |
return sorted(idir for idir in listdir(regdir) if isdir(join(regdir, idir))) |
|
35 |
||
36 |
def detect_available_modes(templdir): |
|
37 |
modes = [] |
|
38 |
for fname in ('schema', 'schema.py'): |
|
39 |
if exists(join(templdir, fname)): |
|
40 |
modes.append('repository') |
|
41 |
break |
|
42 |
for fname in ('data', 'views', 'views.py'): |
|
43 |
if exists(join(templdir, fname)): |
|
44 |
modes.append('web ui') |
|
45 |
break |
|
46 |
return modes |
|
1446 | 47 |
|
48 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
49 |
class InstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
50 |
"""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
|
51 |
(0 meaning all registered instances) |
0 | 52 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
53 |
arguments = '[<instance>...]' |
0 | 54 |
options = ( |
55 |
("force", |
|
56 |
{'short': 'f', 'action' : 'store_true', |
|
57 |
'default': False, |
|
58 |
'help': 'force command without asking confirmation', |
|
59 |
} |
|
60 |
), |
|
61 |
) |
|
62 |
actionverb = None |
|
1446 | 63 |
|
0 | 64 |
def ordered_instances(self): |
65 |
"""return instances in the order in which they should be started, |
|
66 |
considering $REGISTRY_DIR/startorder file if it exists (useful when |
|
67 |
some instances depends on another as external source |
|
68 |
""" |
|
1132 | 69 |
regdir = cwcfg.registry_dir() |
0 | 70 |
_allinstances = list_instances(regdir) |
71 |
if isfile(join(regdir, 'startorder')): |
|
72 |
allinstances = [] |
|
73 |
for line in file(join(regdir, 'startorder')): |
|
74 |
line = line.strip() |
|
75 |
if line and not line.startswith('#'): |
|
76 |
try: |
|
77 |
_allinstances.remove(line) |
|
78 |
allinstances.append(line) |
|
79 |
except ValueError: |
|
1132 | 80 |
print ('ERROR: startorder file contains unexistant ' |
81 |
'instance %s' % line) |
|
0 | 82 |
allinstances += _allinstances |
83 |
else: |
|
84 |
allinstances = _allinstances |
|
85 |
return allinstances |
|
1446 | 86 |
|
0 | 87 |
def run(self, args): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
88 |
"""run the <command>_method on each argument (a list of instance |
0 | 89 |
identifiers) |
90 |
""" |
|
91 |
if not args: |
|
92 |
args = self.ordered_instances() |
|
93 |
try: |
|
94 |
askconfirm = not self.config.force |
|
95 |
except AttributeError: |
|
96 |
# no force option |
|
97 |
askconfirm = False |
|
98 |
else: |
|
99 |
askconfirm = False |
|
100 |
self.run_args(args, askconfirm) |
|
1446 | 101 |
|
0 | 102 |
def run_args(self, args, askconfirm): |
103 |
for appid in args: |
|
104 |
if askconfirm: |
|
105 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
106 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 107 |
continue |
108 |
self.run_arg(appid) |
|
1446 | 109 |
|
0 | 110 |
def run_arg(self, appid): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
111 |
cmdmeth = getattr(self, '%s_instance' % self.name) |
0 | 112 |
try: |
113 |
cmdmeth(appid) |
|
114 |
except (KeyboardInterrupt, SystemExit): |
|
115 |
print >> sys.stderr, '%s aborted' % self.name |
|
116 |
sys.exit(2) # specific error code |
|
117 |
except (ExecutionError, ConfigurationError), ex: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
118 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 119 |
appid, self.actionverb, ex) |
120 |
except Exception, ex: |
|
121 |
import traceback |
|
122 |
traceback.print_exc() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
123 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 124 |
appid, self.actionverb, ex) |
125 |
||
126 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
127 |
class InstanceCommandFork(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
128 |
"""Same as `InstanceCommand`, but command is forked in a new environment |
0 | 129 |
for each argument |
130 |
""" |
|
131 |
||
132 |
def run_args(self, args, askconfirm): |
|
133 |
if len(args) > 1: |
|
134 |
forkcmd = ' '.join(w for w in sys.argv if not w in args) |
|
135 |
else: |
|
136 |
forkcmd = None |
|
137 |
for appid in args: |
|
138 |
if askconfirm: |
|
139 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
140 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 141 |
continue |
142 |
if forkcmd: |
|
143 |
status = system('%s %s' % (forkcmd, appid)) |
|
144 |
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
|
145 |
print '%s exited with status %s' % (forkcmd, status) |
0 | 146 |
else: |
147 |
self.run_arg(appid) |
|
1446 | 148 |
|
0 | 149 |
# base commands ############################################################### |
150 |
||
151 |
class ListCommand(Command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
152 |
"""List configurations, cubes and instances. |
0 | 153 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
154 |
list available configurations, installed cubes, and registered instances |
0 | 155 |
""" |
156 |
name = 'list' |
|
157 |
options = ( |
|
158 |
('verbose', |
|
1446 | 159 |
{'short': 'v', 'action' : 'store_true', |
160 |
'help': "display more information."}), |
|
0 | 161 |
) |
1446 | 162 |
|
0 | 163 |
def run(self, args): |
164 |
"""run the command with its specific arguments""" |
|
165 |
if args: |
|
166 |
raise BadCommandUsage('Too much arguments') |
|
1132 | 167 |
print 'CubicWeb version:', cwcfg.cubicweb_version() |
168 |
print 'Detected mode:', cwcfg.mode |
|
0 | 169 |
print |
170 |
print 'Available configurations:' |
|
171 |
for config in CONFIGURATIONS: |
|
172 |
print '*', config.name |
|
173 |
for line in config.__doc__.splitlines(): |
|
174 |
line = line.strip() |
|
175 |
if not line: |
|
176 |
continue |
|
177 |
print ' ', line |
|
1446 | 178 |
print |
0 | 179 |
try: |
1342
4273e44852cb
no more cubes_dir class method
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
180 |
cubesdir = pathsep.join(cwcfg.cubes_search_path()) |
1132 | 181 |
namesize = max(len(x) for x in cwcfg.available_cubes()) |
0 | 182 |
except ConfigurationError, ex: |
183 |
print 'No cubes available:', ex |
|
184 |
except ValueError: |
|
185 |
print 'No cubes available in %s' % cubesdir |
|
186 |
else: |
|
187 |
print 'Available cubes (%s):' % cubesdir |
|
1132 | 188 |
for cube in cwcfg.available_cubes(): |
0 | 189 |
if cube in ('CVS', '.svn', 'shared', '.hg'): |
190 |
continue |
|
191 |
try: |
|
1132 | 192 |
tinfo = cwcfg.cube_pkginfo(cube) |
0 | 193 |
tversion = tinfo.version |
194 |
except ConfigurationError: |
|
195 |
tinfo = None |
|
196 |
tversion = '[missing cube information]' |
|
197 |
print '* %s %s' % (cube.ljust(namesize), tversion) |
|
198 |
if self.config.verbose: |
|
199 |
shortdesc = tinfo and (getattr(tinfo, 'short_desc', '') |
|
200 |
or tinfo.__doc__) |
|
201 |
if shortdesc: |
|
202 |
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
|
203 |
modes = detect_available_modes(cwcfg.cube_dir(cube)) |
0 | 204 |
print ' available modes: %s' % ', '.join(modes) |
205 |
print |
|
206 |
try: |
|
1132 | 207 |
regdir = cwcfg.registry_dir() |
0 | 208 |
except ConfigurationError, ex: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
209 |
print 'No instance available:', ex |
0 | 210 |
print |
211 |
return |
|
212 |
instances = list_instances(regdir) |
|
213 |
if instances: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
214 |
print 'Available instances (%s):' % regdir |
0 | 215 |
for appid in instances: |
1132 | 216 |
modes = cwcfg.possible_configurations(appid) |
0 | 217 |
if not modes: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
218 |
print '* %s (BROKEN instance, no configuration found)' % appid |
0 | 219 |
continue |
220 |
print '* %s (%s)' % (appid, ', '.join(modes)) |
|
221 |
try: |
|
1132 | 222 |
config = cwcfg.config_for(appid, modes[0]) |
1446 | 223 |
except Exception, exc: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
224 |
print ' (BROKEN instance, %s)' % exc |
0 | 225 |
continue |
226 |
else: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
227 |
print 'No instance available in %s' % regdir |
0 | 228 |
print |
229 |
||
230 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
231 |
class CreateInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
232 |
"""Create an instance from a cube. This is an unified |
0 | 233 |
command which can handle web / server / all-in-one installation |
234 |
according to available parts of the software library and of the |
|
235 |
desired cube. |
|
236 |
||
237 |
<cube> |
|
238 |
the name of cube to use (list available cube names using |
|
239 |
the "list" command). You can use several cubes by separating |
|
240 |
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
|
241 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
242 |
an identifier for the instance to create |
0 | 243 |
""" |
244 |
name = 'create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
245 |
arguments = '<cube> <instance>' |
0 | 246 |
options = ( |
247 |
("config-level", |
|
248 |
{'short': 'l', 'type' : 'int', 'metavar': '<level>', |
|
249 |
'default': 0, |
|
250 |
'help': 'configuration level (0..2): 0 will ask for essential \ |
|
251 |
configuration parameters only while 2 will ask for all parameters', |
|
252 |
} |
|
253 |
), |
|
254 |
("config", |
|
255 |
{'short': 'c', 'type' : 'choice', 'metavar': '<install type>', |
|
256 |
'choices': ('all-in-one', 'repository', 'twisted'), |
|
257 |
'default': 'all-in-one', |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
258 |
'help': 'installation type, telling which part of an instance \ |
0 | 259 |
should be installed. You can list available configurations using the "list" \ |
260 |
command. Default to "all-in-one", e.g. an installation embedding both the RQL \ |
|
261 |
repository and the web server.', |
|
262 |
} |
|
263 |
), |
|
264 |
) |
|
1446 | 265 |
|
0 | 266 |
def run(self, args): |
267 |
"""run the command with its specific arguments""" |
|
2633
bc9386c3b2c9
get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
268 |
from logilab.common.textutils import splitstrip |
0 | 269 |
configname = self.config.config |
2633
bc9386c3b2c9
get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
270 |
cubes = splitstrip(pop_arg(args, 1)) |
0 | 271 |
appid = pop_arg(args) |
272 |
# get the configuration and helper |
|
1132 | 273 |
cwcfg.creating = True |
274 |
config = cwcfg.config_for(appid, configname) |
|
0 | 275 |
config.set_language = False |
2104
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
276 |
cubes = config.expand_cubes(cubes) |
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
277 |
config.init_cubes(cubes) |
0 | 278 |
helper = self.config_helper(config) |
279 |
# check the cube exists |
|
280 |
try: |
|
1132 | 281 |
templdirs = [cwcfg.cube_dir(cube) |
0 | 282 |
for cube in cubes] |
283 |
except ConfigurationError, ex: |
|
284 |
print ex |
|
285 |
print '\navailable cubes:', |
|
1132 | 286 |
print ', '.join(cwcfg.available_cubes()) |
0 | 287 |
return |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
288 |
# 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
|
289 |
print '\n'+underline_title('Creating the instance %s' % appid) |
0 | 290 |
create_dir(config.apphome) |
291 |
# load site_cubicweb from the cubes dir (if any) |
|
292 |
config.load_site_cubicweb() |
|
293 |
# cubicweb-ctl configuration |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
294 |
print '\n'+underline_title('Configuring the instance (%s.conf)' % configname) |
0 | 295 |
config.input_config('main', self.config.config_level) |
296 |
# configuration'specific stuff |
|
297 |
print |
|
298 |
helper.bootstrap(cubes, self.config.config_level) |
|
299 |
# write down configuration |
|
300 |
config.save() |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
301 |
print '-> generated %s' % config.main_config_file() |
0 | 302 |
# handle i18n files structure |
303 |
# in the first cube given |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
304 |
print '-> preparing i18n catalogs' |
0 | 305 |
from cubicweb.common import i18n |
306 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdirs[0], 'i18n'))] |
|
307 |
errors = config.i18ncompile(langs) |
|
308 |
if errors: |
|
309 |
print '\n'.join(errors) |
|
310 |
if not confirm('error while compiling message catalogs, ' |
|
311 |
'continue anyway ?'): |
|
312 |
print 'creation not completed' |
|
313 |
return |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
314 |
# create the additional data directory for this instance |
0 | 315 |
if config.appdatahome != config.apphome: # true in dev mode |
316 |
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
|
317 |
create_dir(join(config.appdatahome, 'backup')) |
0 | 318 |
if config['uid']: |
319 |
from logilab.common.shellutils import chown |
|
320 |
# this directory should be owned by the uid of the server process |
|
321 |
print 'set %s as owner of the data directory' % config['uid'] |
|
322 |
chown(config.appdatahome, config['uid']) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
323 |
print '\n-> creation done for %r.\n' % config.apphome |
0 | 324 |
helper.postcreate() |
325 |
||
1446 | 326 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
327 |
class DeleteInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
328 |
"""Delete an instance. Will remove instance's files and |
0 | 329 |
unregister it. |
330 |
""" |
|
331 |
name = 'delete' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
332 |
arguments = '<instance>' |
1446 | 333 |
|
0 | 334 |
options = () |
335 |
||
336 |
def run(self, args): |
|
337 |
"""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
|
338 |
appid = pop_arg(args, msg="No instance specified !") |
1132 | 339 |
configs = [cwcfg.config_for(appid, configname) |
340 |
for configname in cwcfg.possible_configurations(appid)] |
|
0 | 341 |
if not configs: |
342 |
raise ExecutionError('unable to guess configuration for %s' % appid) |
|
343 |
for config in configs: |
|
344 |
helper = self.config_helper(config, required=False) |
|
345 |
if helper: |
|
346 |
helper.cleanup() |
|
347 |
# remove home |
|
348 |
rm(config.apphome) |
|
349 |
# remove instance data directory |
|
350 |
try: |
|
351 |
rm(config.appdatahome) |
|
352 |
except OSError, ex: |
|
353 |
import errno |
|
354 |
if ex.errno != errno.ENOENT: |
|
355 |
raise |
|
356 |
confignames = ', '.join([config.name for config in configs]) |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
357 |
print '-> instance %s (%s) deleted.' % (appid, confignames) |
0 | 358 |
|
359 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
360 |
# instance commands ######################################################## |
0 | 361 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
362 |
class StartInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
363 |
"""Start the given instances. If no instance is given, start them all. |
1446 | 364 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
365 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
366 |
identifiers of the instances to start. If no instance is |
0 | 367 |
given, start them all. |
368 |
""" |
|
369 |
name = 'start' |
|
370 |
actionverb = 'started' |
|
371 |
options = ( |
|
372 |
("debug", |
|
373 |
{'short': 'D', 'action' : 'store_true', |
|
374 |
'help': 'start server in debug mode.'}), |
|
375 |
("force", |
|
376 |
{'short': 'f', 'action' : 'store_true', |
|
377 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
378 |
'help': 'start the instance even if it seems to be already \ |
0 | 379 |
running.'}), |
380 |
('profile', |
|
381 |
{'short': 'P', 'type' : 'string', 'metavar': '<stat file>', |
|
382 |
'default': None, |
|
383 |
'help': 'profile code and use the specified file to store stats', |
|
384 |
}), |
|
385 |
) |
|
386 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
387 |
def start_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
388 |
"""start the instance's server""" |
0 | 389 |
# use get() since start may be used from other commands (eg upgrade) |
390 |
# without all options defined |
|
391 |
debug = self.get('debug') |
|
392 |
force = self.get('force') |
|
1132 | 393 |
config = cwcfg.config_for(appid) |
0 | 394 |
if self.get('profile'): |
395 |
config.global_set_option('profile', self.config.profile) |
|
396 |
helper = self.config_helper(config, cmdname='start') |
|
397 |
pidf = config['pid-file'] |
|
398 |
if exists(pidf) and not force: |
|
399 |
msg = "%s seems to be running. Remove %s by hand if necessary or use \ |
|
400 |
the --force option." |
|
401 |
raise ExecutionError(msg % (appid, pidf)) |
|
402 |
command = helper.start_command(config, debug) |
|
403 |
if debug: |
|
404 |
print "starting server with command :" |
|
405 |
print command |
|
406 |
if system(command): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
407 |
print 'an error occured while starting the instance, not started' |
0 | 408 |
print |
409 |
return False |
|
410 |
if not debug: |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
411 |
print '-> instance %s started.' % appid |
0 | 412 |
return True |
413 |
||
414 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
415 |
class StopInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
416 |
"""Stop the given instances. |
1446 | 417 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
418 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
419 |
identifiers of the instances to stop. If no instance is |
0 | 420 |
given, stop them all. |
421 |
""" |
|
422 |
name = 'stop' |
|
423 |
actionverb = 'stopped' |
|
1446 | 424 |
|
0 | 425 |
def ordered_instances(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
426 |
instances = super(StopInstanceCommand, self).ordered_instances() |
0 | 427 |
instances.reverse() |
428 |
return instances |
|
1446 | 429 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
430 |
def stop_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
431 |
"""stop the instance's server""" |
1132 | 432 |
config = cwcfg.config_for(appid) |
0 | 433 |
helper = self.config_helper(config, cmdname='stop') |
434 |
helper.poststop() # do this anyway |
|
435 |
pidf = config['pid-file'] |
|
436 |
if not exists(pidf): |
|
437 |
print >> sys.stderr, "%s doesn't exist." % pidf |
|
438 |
return |
|
439 |
import signal |
|
440 |
pid = int(open(pidf).read().strip()) |
|
441 |
try: |
|
442 |
kill(pid, signal.SIGTERM) |
|
443 |
except: |
|
444 |
print >> sys.stderr, "process %s seems already dead." % pid |
|
445 |
else: |
|
446 |
try: |
|
447 |
wait_process_end(pid) |
|
448 |
except ExecutionError, ex: |
|
449 |
print >> sys.stderr, ex |
|
450 |
print >> sys.stderr, 'trying SIGKILL' |
|
451 |
try: |
|
452 |
kill(pid, signal.SIGKILL) |
|
453 |
except: |
|
454 |
# probably dead now |
|
455 |
pass |
|
456 |
wait_process_end(pid) |
|
457 |
try: |
|
458 |
remove(pidf) |
|
459 |
except OSError: |
|
460 |
# already removed by twistd |
|
461 |
pass |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
462 |
print 'instance %s stopped' % appid |
1446 | 463 |
|
0 | 464 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
465 |
class RestartInstanceCommand(StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
466 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
467 |
"""Restart the given instances. |
1446 | 468 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
469 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
470 |
identifiers of the instances to restart. If no instance is |
0 | 471 |
given, restart them all. |
472 |
""" |
|
473 |
name = 'restart' |
|
474 |
actionverb = 'restarted' |
|
475 |
||
476 |
def run_args(self, args, askconfirm): |
|
1132 | 477 |
regdir = cwcfg.registry_dir() |
0 | 478 |
if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
479 |
# no specific startorder |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
480 |
super(RestartInstanceCommand, self).run_args(args, askconfirm) |
0 | 481 |
return |
482 |
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
|
483 |
'instances then restart them.') |
0 | 484 |
# get instances in startorder |
485 |
stopped = [] |
|
486 |
for appid in args: |
|
487 |
if askconfirm: |
|
488 |
print '*'*72 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
489 |
if not confirm('%s instance %r ?' % (self.name, appid)): |
0 | 490 |
continue |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
491 |
self.stop_instance(appid) |
0 | 492 |
stopped.append(appid) |
493 |
forkcmd = [w for w in sys.argv if not w in args] |
|
494 |
forkcmd[1] = 'start' |
|
495 |
forkcmd = ' '.join(forkcmd) |
|
496 |
for appid in reversed(args): |
|
497 |
status = system('%s %s' % (forkcmd, appid)) |
|
498 |
if status: |
|
499 |
sys.exit(status) |
|
1446 | 500 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
501 |
def restart_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
502 |
self.stop_instance(appid) |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
503 |
if self.start_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
504 |
print 'instance %s %s' % (appid, self.actionverb) |
0 | 505 |
|
1446 | 506 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
507 |
class ReloadConfigurationCommand(RestartInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
508 |
"""Reload the given instances. This command is equivalent to a |
0 | 509 |
restart for now. |
1446 | 510 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
511 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
512 |
identifiers of the instances to reload. If no instance is |
0 | 513 |
given, reload them all. |
514 |
""" |
|
515 |
name = 'reload' |
|
1446 | 516 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
517 |
def reload_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
518 |
self.restart_instance(appid) |
1446 | 519 |
|
0 | 520 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
521 |
class StatusCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
522 |
"""Display status information about the given instances. |
1446 | 523 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
524 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
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
|
526 |
given, get status information about all registered instances. |
0 | 527 |
""" |
528 |
name = 'status' |
|
529 |
options = () |
|
530 |
||
1132 | 531 |
@staticmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
532 |
def status_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
533 |
"""print running status information for an instance""" |
1132 | 534 |
for mode in cwcfg.possible_configurations(appid): |
535 |
config = cwcfg.config_for(appid, mode) |
|
0 | 536 |
print '[%s-%s]' % (appid, mode), |
537 |
try: |
|
538 |
pidf = config['pid-file'] |
|
539 |
except KeyError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
540 |
print 'buggy instance, pid file not specified' |
0 | 541 |
continue |
542 |
if not exists(pidf): |
|
543 |
print "doesn't seem to be running" |
|
544 |
continue |
|
545 |
pid = int(open(pidf).read().strip()) |
|
546 |
# trick to guess whether or not the process is running |
|
547 |
try: |
|
548 |
getpgid(pid) |
|
549 |
except OSError: |
|
550 |
print "should be running with pid %s but the process can not be found" % pid |
|
551 |
continue |
|
552 |
print "running with pid %s" % (pid) |
|
553 |
||
554 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
555 |
class UpgradeInstanceCommand(InstanceCommandFork, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
556 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
557 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
558 |
"""Upgrade an instance after cubicweb and/or component(s) upgrade. |
0 | 559 |
|
560 |
For repository update, you will be prompted for a login / password to use |
|
561 |
to connect to the system database. For some upgrades, the given user |
|
562 |
should have create or alter table permissions. |
|
563 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
564 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
565 |
identifiers of the instances to upgrade. If no instance is |
0 | 566 |
given, upgrade them all. |
567 |
""" |
|
568 |
name = 'upgrade' |
|
569 |
actionverb = 'upgraded' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
570 |
options = InstanceCommand.options + ( |
0 | 571 |
('force-componant-version', |
572 |
{'short': 't', 'type' : 'csv', 'metavar': 'cube1=X.Y.Z,cube2=X.Y.Z', |
|
573 |
'default': None, |
|
574 |
'help': 'force migration from the indicated version for the specified cube.'}), |
|
575 |
('force-cubicweb-version', |
|
576 |
{'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z', |
|
577 |
'default': None, |
|
578 |
'help': 'force migration from the indicated cubicweb version.'}), |
|
1446 | 579 |
|
0 | 580 |
('fs-only', |
581 |
{'short': 's', 'action' : 'store_true', |
|
582 |
'default': False, |
|
583 |
'help': 'only upgrade files on the file system, not the database.'}), |
|
584 |
||
585 |
('nostartstop', |
|
586 |
{'short': 'n', 'action' : 'store_true', |
|
587 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
588 |
'help': 'don\'t try to stop instance before migration and to restart it after.'}), |
1446 | 589 |
|
0 | 590 |
('verbosity', |
591 |
{'short': 'v', 'type' : 'int', 'metavar': '<0..2>', |
|
592 |
'default': 1, |
|
593 |
'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \ |
|
594 |
for everything."}), |
|
1446 | 595 |
|
0 | 596 |
('backup-db', |
597 |
{'short': 'b', 'type' : 'yn', 'metavar': '<y or n>', |
|
598 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
599 |
'help': "Backup the instance database before upgrade.\n"\ |
0 | 600 |
"If the option is ommitted, confirmation will be ask.", |
601 |
}), |
|
602 |
||
603 |
('ext-sources', |
|
604 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
605 |
'default': None, |
|
606 |
'help': "For multisources instances, specify to which sources the \ |
|
607 |
repository should connect to for upgrading. When unspecified or 'migration' is \ |
|
608 |
given, appropriate sources for migration will be automatically selected \ |
|
609 |
(recommended). If 'all' is given, will connect to all defined sources.", |
|
610 |
}), |
|
611 |
) |
|
612 |
||
613 |
def ordered_instances(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
614 |
# 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
|
615 |
return InstanceCommand.ordered_instances(self) |
1446 | 616 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
617 |
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
|
618 |
print '\n' + underline_title('Upgrading the instance %s' % appid) |
0 | 619 |
from logilab.common.changelog import Version |
1132 | 620 |
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
|
621 |
config.repairing = True # notice we're not starting the server |
0 | 622 |
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
|
623 |
try: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
624 |
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
|
625 |
except AttributeError: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
626 |
# not a server config |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
627 |
pass |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
628 |
# get instance and installed versions for the server and the componants |
0 | 629 |
mih = config.migration_handler() |
630 |
repo = mih.repo_connect() |
|
631 |
vcconf = repo.get_versions() |
|
632 |
if self.config.force_componant_version: |
|
633 |
packversions = {} |
|
634 |
for vdef in self.config.force_componant_version: |
|
635 |
componant, version = vdef.split('=') |
|
636 |
packversions[componant] = Version(version) |
|
637 |
vcconf.update(packversions) |
|
638 |
toupgrade = [] |
|
639 |
for cube in config.cubes(): |
|
640 |
installedversion = config.cube_version(cube) |
|
641 |
try: |
|
642 |
applversion = vcconf[cube] |
|
643 |
except KeyError: |
|
644 |
config.error('no version information for %s' % cube) |
|
645 |
continue |
|
646 |
if installedversion > applversion: |
|
647 |
toupgrade.append( (cube, applversion, installedversion) ) |
|
1446 | 648 |
cubicwebversion = config.cubicweb_version() |
0 | 649 |
if self.config.force_cubicweb_version: |
650 |
applcubicwebversion = Version(self.config.force_cubicweb_version) |
|
651 |
vcconf['cubicweb'] = applcubicwebversion |
|
652 |
else: |
|
653 |
applcubicwebversion = vcconf.get('cubicweb') |
|
654 |
if cubicwebversion > applcubicwebversion: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
655 |
toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion)) |
0 | 656 |
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
|
657 |
print '-> no software migration needed for instance %s.' % appid |
0 | 658 |
return |
659 |
for cube, fromversion, toversion in toupgrade: |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
660 |
print '-> migration needed from %s to %s for %s' % (fromversion, toversion, cube) |
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:]) |