author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 10 Aug 2009 09:12:21 +0200 | |
changeset 2749 | e6a8cd8cc910 |
parent 2743 | b0e79a77ad67 |
child 2790 | 968108e16066 |
child 2905 | b23bbb31368c |
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 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
106 |
if not ASK.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 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
140 |
if not ASK.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) |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
310 |
if not ASK.confirm('error while compiling message catalogs, ' |
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
311 |
'continue anyway ?'): |
0 | 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 |
}), |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
385 |
('loglevel', |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
386 |
{'short': 'l', 'type' : 'choice', 'metavar': '<log level>', |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
387 |
'default': None, 'choices': ('debug', 'info', 'warning', 'error'), |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
388 |
'help': 'debug if -D is set, error otherwise', |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
389 |
}), |
0 | 390 |
) |
391 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
392 |
def start_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
393 |
"""start the instance's server""" |
0 | 394 |
# use get() since start may be used from other commands (eg upgrade) |
395 |
# without all options defined |
|
396 |
debug = self.get('debug') |
|
397 |
force = self.get('force') |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
398 |
loglevel = self.get('loglevel') |
1132 | 399 |
config = cwcfg.config_for(appid) |
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
400 |
if loglevel is not None: |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
401 |
loglevel = 'LOG_%s' % loglevel.upper() |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
402 |
config.global_set_option('log-threshold', loglevel) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
403 |
config.init_log(loglevel, debug=debug, force=True) |
0 | 404 |
if self.get('profile'): |
405 |
config.global_set_option('profile', self.config.profile) |
|
406 |
helper = self.config_helper(config, cmdname='start') |
|
407 |
pidf = config['pid-file'] |
|
408 |
if exists(pidf) and not force: |
|
409 |
msg = "%s seems to be running. Remove %s by hand if necessary or use \ |
|
410 |
the --force option." |
|
411 |
raise ExecutionError(msg % (appid, pidf)) |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
412 |
helper.start_command(config, debug) |
0 | 413 |
return True |
414 |
||
415 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
416 |
class StopInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
417 |
"""Stop the given instances. |
1446 | 418 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
419 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
420 |
identifiers of the instances to stop. If no instance is |
0 | 421 |
given, stop them all. |
422 |
""" |
|
423 |
name = 'stop' |
|
424 |
actionverb = 'stopped' |
|
1446 | 425 |
|
0 | 426 |
def ordered_instances(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
427 |
instances = super(StopInstanceCommand, self).ordered_instances() |
0 | 428 |
instances.reverse() |
429 |
return instances |
|
1446 | 430 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
431 |
def stop_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
432 |
"""stop the instance's server""" |
1132 | 433 |
config = cwcfg.config_for(appid) |
0 | 434 |
helper = self.config_helper(config, cmdname='stop') |
435 |
helper.poststop() # do this anyway |
|
436 |
pidf = config['pid-file'] |
|
437 |
if not exists(pidf): |
|
438 |
print >> sys.stderr, "%s doesn't exist." % pidf |
|
439 |
return |
|
440 |
import signal |
|
441 |
pid = int(open(pidf).read().strip()) |
|
442 |
try: |
|
443 |
kill(pid, signal.SIGTERM) |
|
444 |
except: |
|
445 |
print >> sys.stderr, "process %s seems already dead." % pid |
|
446 |
else: |
|
447 |
try: |
|
448 |
wait_process_end(pid) |
|
449 |
except ExecutionError, ex: |
|
450 |
print >> sys.stderr, ex |
|
451 |
print >> sys.stderr, 'trying SIGKILL' |
|
452 |
try: |
|
453 |
kill(pid, signal.SIGKILL) |
|
454 |
except: |
|
455 |
# probably dead now |
|
456 |
pass |
|
457 |
wait_process_end(pid) |
|
458 |
try: |
|
459 |
remove(pidf) |
|
460 |
except OSError: |
|
461 |
# already removed by twistd |
|
462 |
pass |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
463 |
print 'instance %s stopped' % appid |
1446 | 464 |
|
0 | 465 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
466 |
class RestartInstanceCommand(StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
467 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
468 |
"""Restart the given instances. |
1446 | 469 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
470 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
471 |
identifiers of the instances to restart. If no instance is |
0 | 472 |
given, restart them all. |
473 |
""" |
|
474 |
name = 'restart' |
|
475 |
actionverb = 'restarted' |
|
476 |
||
477 |
def run_args(self, args, askconfirm): |
|
1132 | 478 |
regdir = cwcfg.registry_dir() |
0 | 479 |
if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
480 |
# no specific startorder |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
481 |
super(RestartInstanceCommand, self).run_args(args, askconfirm) |
0 | 482 |
return |
483 |
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
|
484 |
'instances then restart them.') |
0 | 485 |
# get instances in startorder |
486 |
stopped = [] |
|
487 |
for appid in args: |
|
488 |
if askconfirm: |
|
489 |
print '*'*72 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
490 |
if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
0 | 491 |
continue |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
492 |
self.stop_instance(appid) |
0 | 493 |
stopped.append(appid) |
494 |
forkcmd = [w for w in sys.argv if not w in args] |
|
495 |
forkcmd[1] = 'start' |
|
496 |
forkcmd = ' '.join(forkcmd) |
|
497 |
for appid in reversed(args): |
|
498 |
status = system('%s %s' % (forkcmd, appid)) |
|
499 |
if status: |
|
500 |
sys.exit(status) |
|
1446 | 501 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
502 |
def restart_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
503 |
self.stop_instance(appid) |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
504 |
if self.start_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
505 |
print 'instance %s %s' % (appid, self.actionverb) |
0 | 506 |
|
1446 | 507 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
508 |
class ReloadConfigurationCommand(RestartInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
509 |
"""Reload the given instances. This command is equivalent to a |
0 | 510 |
restart for now. |
1446 | 511 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
512 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
513 |
identifiers of the instances to reload. If no instance is |
0 | 514 |
given, reload them all. |
515 |
""" |
|
516 |
name = 'reload' |
|
1446 | 517 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
518 |
def reload_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
519 |
self.restart_instance(appid) |
1446 | 520 |
|
0 | 521 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
522 |
class StatusCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
523 |
"""Display status information about the given instances. |
1446 | 524 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
526 |
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
|
527 |
given, get status information about all registered instances. |
0 | 528 |
""" |
529 |
name = 'status' |
|
530 |
options = () |
|
531 |
||
1132 | 532 |
@staticmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
533 |
def status_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
534 |
"""print running status information for an instance""" |
1132 | 535 |
for mode in cwcfg.possible_configurations(appid): |
536 |
config = cwcfg.config_for(appid, mode) |
|
0 | 537 |
print '[%s-%s]' % (appid, mode), |
538 |
try: |
|
539 |
pidf = config['pid-file'] |
|
540 |
except KeyError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
541 |
print 'buggy instance, pid file not specified' |
0 | 542 |
continue |
543 |
if not exists(pidf): |
|
544 |
print "doesn't seem to be running" |
|
545 |
continue |
|
546 |
pid = int(open(pidf).read().strip()) |
|
547 |
# trick to guess whether or not the process is running |
|
548 |
try: |
|
549 |
getpgid(pid) |
|
550 |
except OSError: |
|
551 |
print "should be running with pid %s but the process can not be found" % pid |
|
552 |
continue |
|
553 |
print "running with pid %s" % (pid) |
|
554 |
||
555 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
556 |
class UpgradeInstanceCommand(InstanceCommandFork, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
557 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
558 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
559 |
"""Upgrade an instance after cubicweb and/or component(s) upgrade. |
0 | 560 |
|
561 |
For repository update, you will be prompted for a login / password to use |
|
562 |
to connect to the system database. For some upgrades, the given user |
|
563 |
should have create or alter table permissions. |
|
564 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
565 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
566 |
identifiers of the instances to upgrade. If no instance is |
0 | 567 |
given, upgrade them all. |
568 |
""" |
|
569 |
name = 'upgrade' |
|
570 |
actionverb = 'upgraded' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
571 |
options = InstanceCommand.options + ( |
0 | 572 |
('force-componant-version', |
573 |
{'short': 't', 'type' : 'csv', 'metavar': 'cube1=X.Y.Z,cube2=X.Y.Z', |
|
574 |
'default': None, |
|
575 |
'help': 'force migration from the indicated version for the specified cube.'}), |
|
576 |
('force-cubicweb-version', |
|
577 |
{'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z', |
|
578 |
'default': None, |
|
579 |
'help': 'force migration from the indicated cubicweb version.'}), |
|
1446 | 580 |
|
0 | 581 |
('fs-only', |
582 |
{'short': 's', 'action' : 'store_true', |
|
583 |
'default': False, |
|
584 |
'help': 'only upgrade files on the file system, not the database.'}), |
|
585 |
||
586 |
('nostartstop', |
|
587 |
{'short': 'n', 'action' : 'store_true', |
|
588 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
589 |
'help': 'don\'t try to stop instance before migration and to restart it after.'}), |
1446 | 590 |
|
0 | 591 |
('verbosity', |
592 |
{'short': 'v', 'type' : 'int', 'metavar': '<0..2>', |
|
593 |
'default': 1, |
|
594 |
'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \ |
|
595 |
for everything."}), |
|
1446 | 596 |
|
0 | 597 |
('backup-db', |
598 |
{'short': 'b', 'type' : 'yn', 'metavar': '<y or n>', |
|
599 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
600 |
'help': "Backup the instance database before upgrade.\n"\ |
0 | 601 |
"If the option is ommitted, confirmation will be ask.", |
602 |
}), |
|
603 |
||
604 |
('ext-sources', |
|
605 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
606 |
'default': None, |
|
607 |
'help': "For multisources instances, specify to which sources the \ |
|
608 |
repository should connect to for upgrading. When unspecified or 'migration' is \ |
|
609 |
given, appropriate sources for migration will be automatically selected \ |
|
610 |
(recommended). If 'all' is given, will connect to all defined sources.", |
|
611 |
}), |
|
612 |
) |
|
613 |
||
614 |
def ordered_instances(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
615 |
# 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
|
616 |
return InstanceCommand.ordered_instances(self) |
1446 | 617 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
618 |
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
|
619 |
print '\n' + underline_title('Upgrading the instance %s' % appid) |
0 | 620 |
from logilab.common.changelog import Version |
1132 | 621 |
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
|
622 |
config.repairing = True # notice we're not starting the server |
0 | 623 |
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
|
624 |
try: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
625 |
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
|
626 |
except AttributeError: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
627 |
# not a server config |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
628 |
pass |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
629 |
# get instance and installed versions for the server and the componants |
0 | 630 |
mih = config.migration_handler() |
631 |
repo = mih.repo_connect() |
|
632 |
vcconf = repo.get_versions() |
|
633 |
if self.config.force_componant_version: |
|
634 |
packversions = {} |
|
635 |
for vdef in self.config.force_componant_version: |
|
636 |
componant, version = vdef.split('=') |
|
637 |
packversions[componant] = Version(version) |
|
638 |
vcconf.update(packversions) |
|
639 |
toupgrade = [] |
|
640 |
for cube in config.cubes(): |
|
641 |
installedversion = config.cube_version(cube) |
|
642 |
try: |
|
643 |
applversion = vcconf[cube] |
|
644 |
except KeyError: |
|
645 |
config.error('no version information for %s' % cube) |
|
646 |
continue |
|
647 |
if installedversion > applversion: |
|
648 |
toupgrade.append( (cube, applversion, installedversion) ) |
|
1446 | 649 |
cubicwebversion = config.cubicweb_version() |
0 | 650 |
if self.config.force_cubicweb_version: |
651 |
applcubicwebversion = Version(self.config.force_cubicweb_version) |
|
652 |
vcconf['cubicweb'] = applcubicwebversion |
|
653 |
else: |
|
654 |
applcubicwebversion = vcconf.get('cubicweb') |
|
655 |
if cubicwebversion > applcubicwebversion: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
656 |
toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion)) |
0 | 657 |
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
|
658 |
print '-> no software migration needed for instance %s.' % appid |
0 | 659 |
return |
660 |
for cube, fromversion, toversion in toupgrade: |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
661 |
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
|
662 |
# only stop once we're sure we have something to do |
1477 | 663 |
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
|
664 |
self.stop_instance(appid) |
0 | 665 |
# run cubicweb/componants migration scripts |
666 |
mih.migrate(vcconf, reversed(toupgrade), self.config) |
|
667 |
# rewrite main configuration file |
|
668 |
mih.rewrite_configuration() |
|
669 |
# handle i18n upgrade: |
|
670 |
# * install new languages |
|
671 |
# * recompile catalogs |
|
672 |
# in the first componant given |
|
673 |
from cubicweb.common import i18n |
|
1132 | 674 |
templdir = cwcfg.cube_dir(config.cubes()[0]) |
0 | 675 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))] |
676 |
errors = config.i18ncompile(langs) |
|
677 |
if errors: |
|
678 |
print '\n'.join(errors) |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
679 |
if not ASK.confirm('Error while compiling message catalogs, ' |
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
680 |
'continue anyway ?'): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
681 |
print '-> migration not completed.' |
0 | 682 |
return |
683 |
mih.shutdown() |
|
684 |
print |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
685 |
print '-> instance migrated.' |
1132 | 686 |
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
|
687 |
self.start_instance(appid) |
0 | 688 |
print |
689 |
||
690 |
||
691 |
class ShellCommand(Command): |
|
692 |
"""Run an interactive migration shell. This is a python shell with |
|
693 |
enhanced migration commands predefined in the namespace. An additional |
|
694 |
argument may be given corresponding to a file containing commands to |
|
695 |
execute in batch mode. |
|
696 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
697 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
698 |
the identifier of the instance to connect. |
0 | 699 |
""" |
700 |
name = 'shell' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
701 |
arguments = '<instance> [batch command file]' |
0 | 702 |
options = ( |
703 |
('system-only', |
|
704 |
{'short': 'S', 'action' : 'store_true', |
|
705 |
'default': False, |
|
706 |
'help': 'only connect to the system source when the instance is ' |
|
707 |
'using multiple sources. You can\'t use this option and the ' |
|
708 |
'--ext-sources option at the same time.'}), |
|
1446 | 709 |
|
0 | 710 |
('ext-sources', |
711 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
712 |
'default': None, |
|
713 |
'help': "For multisources instances, specify to which sources the \ |
|
714 |
repository should connect to for upgrading. When unspecified or 'all' given, \ |
|
715 |
will connect to all defined sources. If 'migration' is given, appropriate \ |
|
716 |
sources for migration will be automatically selected.", |
|
717 |
}), |
|
1446 | 718 |
|
0 | 719 |
) |
720 |
def run(self, args): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
721 |
appid = pop_arg(args, 99, msg="No instance specified !") |
1132 | 722 |
config = cwcfg.config_for(appid) |
0 | 723 |
if self.config.ext_sources: |
724 |
assert not self.config.system_only |
|
725 |
sources = self.config.ext_sources |
|
726 |
elif self.config.system_only: |
|
727 |
sources = ('system',) |
|
728 |
else: |
|
729 |
sources = ('all',) |
|
730 |
config.set_sources_mode(sources) |
|
731 |
mih = config.migration_handler() |
|
732 |
if args: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
733 |
for arg in args: |
2318
17f7c3990f9b
Fix shell script execution command.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
2275
diff
changeset
|
734 |
mih.process_script(arg) |
0 | 735 |
else: |
736 |
mih.interactive_shell() |
|
1446 | 737 |
mih.shutdown() |
0 | 738 |
|
739 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
740 |
class RecompileInstanceCatalogsCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
741 |
"""Recompile i18n catalogs for instances. |
1446 | 742 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
743 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
744 |
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
|
745 |
given, recompile for all registered instances. |
0 | 746 |
""" |
1898
39b37f90a8a4
[cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1477
diff
changeset
|
747 |
name = 'i18ninstance' |
1132 | 748 |
|
749 |
@staticmethod |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
750 |
def i18ninstance_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
751 |
"""recompile instance's messages catalogs""" |
1132 | 752 |
config = cwcfg.config_for(appid) |
0 | 753 |
try: |
754 |
config.bootstrap_cubes() |
|
755 |
except IOError, ex: |
|
756 |
import errno |
|
757 |
if ex.errno != errno.ENOENT: |
|
758 |
raise |
|
759 |
# 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
|
760 |
# 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
|
761 |
config.repairing = True |
0 | 762 |
# create an in-memory repository, will call config.init_cubes() |
763 |
config.repository() |
|
764 |
except AttributeError: |
|
765 |
# web only config |
|
766 |
config.init_cubes(config.repository().get_cubes()) |
|
767 |
errors = config.i18ncompile() |
|
768 |
if errors: |
|
769 |
print '\n'.join(errors) |
|
770 |
||
771 |
||
772 |
class ListInstancesCommand(Command): |
|
773 |
"""list available instances, useful for bash completion.""" |
|
774 |
name = 'listinstances' |
|
775 |
hidden = True |
|
1446 | 776 |
|
0 | 777 |
def run(self, args): |
778 |
"""run the command with its specific arguments""" |
|
1132 | 779 |
regdir = cwcfg.registry_dir() |
0 | 780 |
for appid in sorted(listdir(regdir)): |
781 |
print appid |
|
782 |
||
783 |
||
784 |
class ListCubesCommand(Command): |
|
785 |
"""list available componants, useful for bash completion.""" |
|
786 |
name = 'listcubes' |
|
787 |
hidden = True |
|
1446 | 788 |
|
0 | 789 |
def run(self, args): |
790 |
"""run the command with its specific arguments""" |
|
1132 | 791 |
for cube in cwcfg.available_cubes(): |
0 | 792 |
print cube |
793 |
||
794 |
register_commands((ListCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
795 |
CreateInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
796 |
DeleteInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
797 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
798 |
StopInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
799 |
RestartInstanceCommand, |
0 | 800 |
ReloadConfigurationCommand, |
801 |
StatusCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
802 |
UpgradeInstanceCommand, |
0 | 803 |
ShellCommand, |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
804 |
RecompileInstanceCatalogsCommand, |
0 | 805 |
ListInstancesCommand, ListCubesCommand, |
806 |
)) |
|
807 |
||
1446 | 808 |
|
0 | 809 |
def run(args): |
810 |
"""command line tool""" |
|
1132 | 811 |
cwcfg.load_cwctl_plugins() |
0 | 812 |
main_run(args, __doc__) |
813 |
||
814 |
if __name__ == '__main__': |
|
815 |
run(sys.argv[1:]) |