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