author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Mon, 05 Oct 2009 19:11:48 +0200 | |
branch | stable |
changeset 3560 | 7d76775f965d |
parent 3486 | ea6bf6f9ba0c |
child 3562 | cff18f0d7c73 |
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: |
3560
7d76775f965d
fixlets on the workflow chapter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3486
diff
changeset
|
10 |
from os import kill, getpgi |
3115
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') |
|
3486
ea6bf6f9ba0c
[cwctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3198
diff
changeset
|
174 |
print 'CubicWeb %s (%s mode)' % (cwcfg.cubicweb_version(), cwcfg.mode) |
0 | 175 |
print |
176 |
print 'Available configurations:' |
|
177 |
for config in CONFIGURATIONS: |
|
178 |
print '*', config.name |
|
179 |
for line in config.__doc__.splitlines(): |
|
180 |
line = line.strip() |
|
181 |
if not line: |
|
182 |
continue |
|
183 |
print ' ', line |
|
1446 | 184 |
print |
0 | 185 |
try: |
1342
4273e44852cb
no more cubes_dir class method
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
186 |
cubesdir = pathsep.join(cwcfg.cubes_search_path()) |
1132 | 187 |
namesize = max(len(x) for x in cwcfg.available_cubes()) |
0 | 188 |
except ConfigurationError, ex: |
189 |
print 'No cubes available:', ex |
|
190 |
except ValueError: |
|
191 |
print 'No cubes available in %s' % cubesdir |
|
192 |
else: |
|
193 |
print 'Available cubes (%s):' % cubesdir |
|
1132 | 194 |
for cube in cwcfg.available_cubes(): |
0 | 195 |
if cube in ('CVS', '.svn', 'shared', '.hg'): |
196 |
continue |
|
197 |
try: |
|
1132 | 198 |
tinfo = cwcfg.cube_pkginfo(cube) |
0 | 199 |
tversion = tinfo.version |
200 |
except ConfigurationError: |
|
201 |
tinfo = None |
|
202 |
tversion = '[missing cube information]' |
|
203 |
print '* %s %s' % (cube.ljust(namesize), tversion) |
|
204 |
if self.config.verbose: |
|
205 |
shortdesc = tinfo and (getattr(tinfo, 'short_desc', '') |
|
206 |
or tinfo.__doc__) |
|
207 |
if shortdesc: |
|
208 |
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
|
209 |
modes = detect_available_modes(cwcfg.cube_dir(cube)) |
0 | 210 |
print ' available modes: %s' % ', '.join(modes) |
211 |
print |
|
212 |
try: |
|
1132 | 213 |
regdir = cwcfg.registry_dir() |
0 | 214 |
except ConfigurationError, ex: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
215 |
print 'No instance available:', ex |
0 | 216 |
print |
217 |
return |
|
218 |
instances = list_instances(regdir) |
|
219 |
if instances: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
220 |
print 'Available instances (%s):' % regdir |
0 | 221 |
for appid in instances: |
1132 | 222 |
modes = cwcfg.possible_configurations(appid) |
0 | 223 |
if not modes: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
224 |
print '* %s (BROKEN instance, no configuration found)' % appid |
0 | 225 |
continue |
226 |
print '* %s (%s)' % (appid, ', '.join(modes)) |
|
227 |
try: |
|
1132 | 228 |
config = cwcfg.config_for(appid, modes[0]) |
1446 | 229 |
except Exception, exc: |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
230 |
print ' (BROKEN instance, %s)' % exc |
0 | 231 |
continue |
232 |
else: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
233 |
print 'No instance available in %s' % regdir |
0 | 234 |
print |
235 |
||
236 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
237 |
class CreateInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
238 |
"""Create an instance from a cube. This is an unified |
0 | 239 |
command which can handle web / server / all-in-one installation |
240 |
according to available parts of the software library and of the |
|
241 |
desired cube. |
|
242 |
||
243 |
<cube> |
|
244 |
the name of cube to use (list available cube names using |
|
245 |
the "list" command). You can use several cubes by separating |
|
246 |
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
|
247 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
248 |
an identifier for the instance to create |
0 | 249 |
""" |
250 |
name = 'create' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
251 |
arguments = '<cube> <instance>' |
0 | 252 |
options = ( |
253 |
("config-level", |
|
254 |
{'short': 'l', 'type' : 'int', 'metavar': '<level>', |
|
255 |
'default': 0, |
|
256 |
'help': 'configuration level (0..2): 0 will ask for essential \ |
|
257 |
configuration parameters only while 2 will ask for all parameters', |
|
258 |
} |
|
259 |
), |
|
260 |
("config", |
|
261 |
{'short': 'c', 'type' : 'choice', 'metavar': '<install type>', |
|
262 |
'choices': ('all-in-one', 'repository', 'twisted'), |
|
263 |
'default': 'all-in-one', |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
264 |
'help': 'installation type, telling which part of an instance \ |
0 | 265 |
should be installed. You can list available configurations using the "list" \ |
266 |
command. Default to "all-in-one", e.g. an installation embedding both the RQL \ |
|
267 |
repository and the web server.', |
|
268 |
} |
|
269 |
), |
|
270 |
) |
|
1446 | 271 |
|
0 | 272 |
def run(self, args): |
273 |
"""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
|
274 |
from logilab.common.textutils import splitstrip |
0 | 275 |
configname = self.config.config |
2633
bc9386c3b2c9
get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2615
diff
changeset
|
276 |
cubes = splitstrip(pop_arg(args, 1)) |
0 | 277 |
appid = pop_arg(args) |
278 |
# get the configuration and helper |
|
1132 | 279 |
cwcfg.creating = True |
280 |
config = cwcfg.config_for(appid, configname) |
|
0 | 281 |
config.set_language = False |
2104
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
282 |
cubes = config.expand_cubes(cubes) |
b4ffcea3275b
change cubes into expanded cubes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2032
diff
changeset
|
283 |
config.init_cubes(cubes) |
0 | 284 |
helper = self.config_helper(config) |
285 |
# check the cube exists |
|
286 |
try: |
|
1132 | 287 |
templdirs = [cwcfg.cube_dir(cube) |
0 | 288 |
for cube in cubes] |
289 |
except ConfigurationError, ex: |
|
290 |
print ex |
|
291 |
print '\navailable cubes:', |
|
1132 | 292 |
print ', '.join(cwcfg.available_cubes()) |
0 | 293 |
return |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
294 |
# 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
|
295 |
print '\n'+underline_title('Creating the instance %s' % appid) |
0 | 296 |
create_dir(config.apphome) |
297 |
# load site_cubicweb from the cubes dir (if any) |
|
298 |
config.load_site_cubicweb() |
|
299 |
# cubicweb-ctl configuration |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
300 |
print '\n'+underline_title('Configuring the instance (%s.conf)' % configname) |
0 | 301 |
config.input_config('main', self.config.config_level) |
302 |
# configuration'specific stuff |
|
303 |
print |
|
304 |
helper.bootstrap(cubes, self.config.config_level) |
|
305 |
# write down configuration |
|
306 |
config.save() |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
307 |
print '-> generated %s' % config.main_config_file() |
0 | 308 |
# handle i18n files structure |
309 |
# in the first cube given |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
310 |
print '-> preparing i18n catalogs' |
0 | 311 |
from cubicweb.common import i18n |
312 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdirs[0], 'i18n'))] |
|
313 |
errors = config.i18ncompile(langs) |
|
314 |
if errors: |
|
315 |
print '\n'.join(errors) |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
316 |
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
|
317 |
'continue anyway ?'): |
0 | 318 |
print 'creation not completed' |
319 |
return |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
320 |
# create the additional data directory for this instance |
0 | 321 |
if config.appdatahome != config.apphome: # true in dev mode |
322 |
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
|
323 |
create_dir(join(config.appdatahome, 'backup')) |
0 | 324 |
if config['uid']: |
325 |
from logilab.common.shellutils import chown |
|
326 |
# this directory should be owned by the uid of the server process |
|
327 |
print 'set %s as owner of the data directory' % config['uid'] |
|
328 |
chown(config.appdatahome, config['uid']) |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2318
diff
changeset
|
329 |
print '\n-> creation done for %r.\n' % config.apphome |
0 | 330 |
helper.postcreate() |
331 |
||
1446 | 332 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
333 |
class DeleteInstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
334 |
"""Delete an instance. Will remove instance's files and |
0 | 335 |
unregister it. |
336 |
""" |
|
337 |
name = 'delete' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
338 |
arguments = '<instance>' |
1446 | 339 |
|
0 | 340 |
options = () |
341 |
||
342 |
def run(self, args): |
|
343 |
"""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
|
344 |
appid = pop_arg(args, msg="No instance specified !") |
1132 | 345 |
configs = [cwcfg.config_for(appid, configname) |
346 |
for configname in cwcfg.possible_configurations(appid)] |
|
0 | 347 |
if not configs: |
348 |
raise ExecutionError('unable to guess configuration for %s' % appid) |
|
349 |
for config in configs: |
|
350 |
helper = self.config_helper(config, required=False) |
|
351 |
if helper: |
|
352 |
helper.cleanup() |
|
353 |
# remove home |
|
354 |
rm(config.apphome) |
|
355 |
# remove instance data directory |
|
356 |
try: |
|
357 |
rm(config.appdatahome) |
|
358 |
except OSError, ex: |
|
359 |
import errno |
|
360 |
if ex.errno != errno.ENOENT: |
|
361 |
raise |
|
362 |
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
|
363 |
print '-> instance %s (%s) deleted.' % (appid, confignames) |
0 | 364 |
|
365 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
366 |
# instance commands ######################################################## |
0 | 367 |
|
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
368 |
class StartInstanceCommand(InstanceCommandFork): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
369 |
"""Start the given instances. If no instance is given, start them all. |
1446 | 370 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
371 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
372 |
identifiers of the instances to start. If no instance is |
0 | 373 |
given, start them all. |
374 |
""" |
|
375 |
name = 'start' |
|
376 |
actionverb = 'started' |
|
377 |
options = ( |
|
378 |
("debug", |
|
379 |
{'short': 'D', 'action' : 'store_true', |
|
380 |
'help': 'start server in debug mode.'}), |
|
381 |
("force", |
|
382 |
{'short': 'f', 'action' : 'store_true', |
|
383 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
384 |
'help': 'start the instance even if it seems to be already \ |
0 | 385 |
running.'}), |
386 |
('profile', |
|
387 |
{'short': 'P', 'type' : 'string', 'metavar': '<stat file>', |
|
388 |
'default': None, |
|
389 |
'help': 'profile code and use the specified file to store stats', |
|
390 |
}), |
|
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
|
391 |
('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
|
392 |
{'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
|
393 |
'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
|
394 |
'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
|
395 |
}), |
0 | 396 |
) |
397 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
398 |
def start_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
399 |
"""start the instance's server""" |
3182 | 400 |
debug = self['debug'] |
401 |
force = self['force'] |
|
402 |
loglevel = self['loglevel'] |
|
1132 | 403 |
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
|
404 |
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
|
405 |
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
|
406 |
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
|
407 |
config.init_log(loglevel, debug=debug, force=True) |
3182 | 408 |
if self['profile']: |
0 | 409 |
config.global_set_option('profile', self.config.profile) |
410 |
helper = self.config_helper(config, cmdname='start') |
|
411 |
pidf = config['pid-file'] |
|
412 |
if exists(pidf) and not force: |
|
413 |
msg = "%s seems to be running. Remove %s by hand if necessary or use \ |
|
414 |
the --force option." |
|
415 |
raise ExecutionError(msg % (appid, pidf)) |
|
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
416 |
helper.start_server(config, debug) |
0 | 417 |
|
418 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
419 |
class StopInstanceCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
420 |
"""Stop the given instances. |
1446 | 421 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
422 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
423 |
identifiers of the instances to stop. If no instance is |
0 | 424 |
given, stop them all. |
425 |
""" |
|
426 |
name = 'stop' |
|
427 |
actionverb = 'stopped' |
|
1446 | 428 |
|
0 | 429 |
def ordered_instances(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
430 |
instances = super(StopInstanceCommand, self).ordered_instances() |
0 | 431 |
instances.reverse() |
432 |
return instances |
|
1446 | 433 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
434 |
def stop_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
435 |
"""stop the instance's server""" |
1132 | 436 |
config = cwcfg.config_for(appid) |
0 | 437 |
helper = self.config_helper(config, cmdname='stop') |
438 |
helper.poststop() # do this anyway |
|
439 |
pidf = config['pid-file'] |
|
440 |
if not exists(pidf): |
|
441 |
print >> sys.stderr, "%s doesn't exist." % pidf |
|
442 |
return |
|
443 |
import signal |
|
444 |
pid = int(open(pidf).read().strip()) |
|
445 |
try: |
|
446 |
kill(pid, signal.SIGTERM) |
|
447 |
except: |
|
448 |
print >> sys.stderr, "process %s seems already dead." % pid |
|
449 |
else: |
|
450 |
try: |
|
451 |
wait_process_end(pid) |
|
452 |
except ExecutionError, ex: |
|
453 |
print >> sys.stderr, ex |
|
454 |
print >> sys.stderr, 'trying SIGKILL' |
|
455 |
try: |
|
456 |
kill(pid, signal.SIGKILL) |
|
457 |
except: |
|
458 |
# probably dead now |
|
459 |
pass |
|
460 |
wait_process_end(pid) |
|
461 |
try: |
|
462 |
remove(pidf) |
|
463 |
except OSError: |
|
464 |
# already removed by twistd |
|
465 |
pass |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
466 |
print 'instance %s stopped' % appid |
1446 | 467 |
|
0 | 468 |
|
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
469 |
class RestartInstanceCommand(StartInstanceCommand): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
470 |
"""Restart the given instances. |
1446 | 471 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
472 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
473 |
identifiers of the instances to restart. If no instance is |
0 | 474 |
given, restart them all. |
475 |
""" |
|
476 |
name = 'restart' |
|
477 |
actionverb = 'restarted' |
|
478 |
||
479 |
def run_args(self, args, askconfirm): |
|
1132 | 480 |
regdir = cwcfg.registry_dir() |
0 | 481 |
if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
482 |
# no specific startorder |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
483 |
super(RestartInstanceCommand, self).run_args(args, askconfirm) |
0 | 484 |
return |
485 |
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
|
486 |
'instances then restart them.') |
0 | 487 |
# get instances in startorder |
488 |
for appid in args: |
|
489 |
if askconfirm: |
|
490 |
print '*'*72 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
491 |
if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
0 | 492 |
continue |
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
493 |
StopInstanceCommand().stop_instance(appid) |
0 | 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): |
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
503 |
StopInstanceCommand().stop_instance(appid) |
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
504 |
self.start_instance(appid) |
0 | 505 |
|
1446 | 506 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
507 |
class ReloadConfigurationCommand(RestartInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
508 |
"""Reload the given instances. This command is equivalent to a |
0 | 509 |
restart for now. |
1446 | 510 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
511 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
512 |
identifiers of the instances to reload. If no instance is |
0 | 513 |
given, reload them all. |
514 |
""" |
|
515 |
name = 'reload' |
|
1446 | 516 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
517 |
def reload_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
518 |
self.restart_instance(appid) |
1446 | 519 |
|
0 | 520 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
521 |
class StatusCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
522 |
"""Display status information about the given instances. |
1446 | 523 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
524 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
identifiers of the instances to status. If no instance is |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
526 |
given, get status information about all registered instances. |
0 | 527 |
""" |
528 |
name = 'status' |
|
529 |
options = () |
|
530 |
||
1132 | 531 |
@staticmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
532 |
def status_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
533 |
"""print running status information for an instance""" |
1132 | 534 |
for mode in cwcfg.possible_configurations(appid): |
535 |
config = cwcfg.config_for(appid, mode) |
|
0 | 536 |
print '[%s-%s]' % (appid, mode), |
537 |
try: |
|
538 |
pidf = config['pid-file'] |
|
539 |
except KeyError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
540 |
print 'buggy instance, pid file not specified' |
0 | 541 |
continue |
542 |
if not exists(pidf): |
|
543 |
print "doesn't seem to be running" |
|
544 |
continue |
|
545 |
pid = int(open(pidf).read().strip()) |
|
546 |
# trick to guess whether or not the process is running |
|
547 |
try: |
|
548 |
getpgid(pid) |
|
549 |
except OSError: |
|
550 |
print "should be running with pid %s but the process can not be found" % pid |
|
551 |
continue |
|
552 |
print "running with pid %s" % (pid) |
|
553 |
||
554 |
||
3180
6bab5746ebf5
[c-c] fix start/restart commands
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2905
diff
changeset
|
555 |
class UpgradeInstanceCommand(InstanceCommandFork): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
556 |
"""Upgrade an instance after cubicweb and/or component(s) upgrade. |
0 | 557 |
|
558 |
For repository update, you will be prompted for a login / password to use |
|
559 |
to connect to the system database. For some upgrades, the given user |
|
560 |
should have create or alter table permissions. |
|
561 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
562 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
563 |
identifiers of the instances to upgrade. If no instance is |
0 | 564 |
given, upgrade them all. |
565 |
""" |
|
566 |
name = 'upgrade' |
|
567 |
actionverb = 'upgraded' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
568 |
options = InstanceCommand.options + ( |
0 | 569 |
('force-componant-version', |
570 |
{'short': 't', 'type' : 'csv', 'metavar': 'cube1=X.Y.Z,cube2=X.Y.Z', |
|
571 |
'default': None, |
|
572 |
'help': 'force migration from the indicated version for the specified cube.'}), |
|
573 |
('force-cubicweb-version', |
|
574 |
{'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z', |
|
575 |
'default': None, |
|
576 |
'help': 'force migration from the indicated cubicweb version.'}), |
|
1446 | 577 |
|
0 | 578 |
('fs-only', |
579 |
{'short': 's', 'action' : 'store_true', |
|
580 |
'default': False, |
|
581 |
'help': 'only upgrade files on the file system, not the database.'}), |
|
582 |
||
583 |
('nostartstop', |
|
584 |
{'short': 'n', 'action' : 'store_true', |
|
585 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
586 |
'help': 'don\'t try to stop instance before migration and to restart it after.'}), |
1446 | 587 |
|
0 | 588 |
('verbosity', |
589 |
{'short': 'v', 'type' : 'int', 'metavar': '<0..2>', |
|
590 |
'default': 1, |
|
591 |
'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \ |
|
592 |
for everything."}), |
|
1446 | 593 |
|
0 | 594 |
('backup-db', |
595 |
{'short': 'b', 'type' : 'yn', 'metavar': '<y or n>', |
|
596 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
597 |
'help': "Backup the instance database before upgrade.\n"\ |
0 | 598 |
"If the option is ommitted, confirmation will be ask.", |
599 |
}), |
|
600 |
||
601 |
('ext-sources', |
|
602 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
603 |
'default': None, |
|
604 |
'help': "For multisources instances, specify to which sources the \ |
|
605 |
repository should connect to for upgrading. When unspecified or 'migration' is \ |
|
606 |
given, appropriate sources for migration will be automatically selected \ |
|
607 |
(recommended). If 'all' is given, will connect to all defined sources.", |
|
608 |
}), |
|
609 |
) |
|
610 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
611 |
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
|
612 |
print '\n' + underline_title('Upgrading the instance %s' % appid) |
0 | 613 |
from logilab.common.changelog import Version |
1132 | 614 |
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
|
615 |
config.repairing = True # notice we're not starting the server |
0 | 616 |
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
|
617 |
try: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
618 |
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
|
619 |
except AttributeError: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
620 |
# not a server config |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
621 |
pass |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
622 |
# get instance and installed versions for the server and the componants |
0 | 623 |
mih = config.migration_handler() |
624 |
repo = mih.repo_connect() |
|
625 |
vcconf = repo.get_versions() |
|
626 |
if self.config.force_componant_version: |
|
627 |
packversions = {} |
|
628 |
for vdef in self.config.force_componant_version: |
|
629 |
componant, version = vdef.split('=') |
|
630 |
packversions[componant] = Version(version) |
|
631 |
vcconf.update(packversions) |
|
632 |
toupgrade = [] |
|
633 |
for cube in config.cubes(): |
|
634 |
installedversion = config.cube_version(cube) |
|
635 |
try: |
|
636 |
applversion = vcconf[cube] |
|
637 |
except KeyError: |
|
638 |
config.error('no version information for %s' % cube) |
|
639 |
continue |
|
640 |
if installedversion > applversion: |
|
641 |
toupgrade.append( (cube, applversion, installedversion) ) |
|
1446 | 642 |
cubicwebversion = config.cubicweb_version() |
0 | 643 |
if self.config.force_cubicweb_version: |
644 |
applcubicwebversion = Version(self.config.force_cubicweb_version) |
|
645 |
vcconf['cubicweb'] = applcubicwebversion |
|
646 |
else: |
|
647 |
applcubicwebversion = vcconf.get('cubicweb') |
|
648 |
if cubicwebversion > applcubicwebversion: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
649 |
toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion)) |
0 | 650 |
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
|
651 |
print '-> no software migration needed for instance %s.' % appid |
0 | 652 |
return |
653 |
for cube, fromversion, toversion in toupgrade: |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
654 |
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
|
655 |
# only stop once we're sure we have something to do |
1477 | 656 |
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
|
657 |
StopInstanceCommand().stop_instance(appid) |
0 | 658 |
# run cubicweb/componants migration scripts |
659 |
mih.migrate(vcconf, reversed(toupgrade), self.config) |
|
660 |
# rewrite main configuration file |
|
661 |
mih.rewrite_configuration() |
|
662 |
# handle i18n upgrade: |
|
663 |
# * install new languages |
|
664 |
# * recompile catalogs |
|
665 |
# in the first componant given |
|
666 |
from cubicweb.common import i18n |
|
1132 | 667 |
templdir = cwcfg.cube_dir(config.cubes()[0]) |
0 | 668 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))] |
669 |
errors = config.i18ncompile(langs) |
|
670 |
if errors: |
|
671 |
print '\n'.join(errors) |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
672 |
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
|
673 |
'continue anyway ?'): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
674 |
print '-> migration not completed.' |
0 | 675 |
return |
676 |
mih.shutdown() |
|
677 |
print |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
678 |
print '-> instance migrated.' |
1132 | 679 |
if not (cwcfg.mode == 'dev' or self.config.nostartstop): |
3190
75cf006babe4
another friend
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3188
diff
changeset
|
680 |
StartInstanceCommand().start_instance(appid) |
0 | 681 |
print |
682 |
||
683 |
||
684 |
class ShellCommand(Command): |
|
685 |
"""Run an interactive migration shell. This is a python shell with |
|
686 |
enhanced migration commands predefined in the namespace. An additional |
|
687 |
argument may be given corresponding to a file containing commands to |
|
688 |
execute in batch mode. |
|
689 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
690 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
691 |
the identifier of the instance to connect. |
0 | 692 |
""" |
693 |
name = 'shell' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
694 |
arguments = '<instance> [batch command file]' |
0 | 695 |
options = ( |
696 |
('system-only', |
|
697 |
{'short': 'S', 'action' : 'store_true', |
|
698 |
'default': False, |
|
699 |
'help': 'only connect to the system source when the instance is ' |
|
700 |
'using multiple sources. You can\'t use this option and the ' |
|
701 |
'--ext-sources option at the same time.'}), |
|
1446 | 702 |
|
0 | 703 |
('ext-sources', |
704 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
705 |
'default': None, |
|
706 |
'help': "For multisources instances, specify to which sources the \ |
|
707 |
repository should connect to for upgrading. When unspecified or 'all' given, \ |
|
708 |
will connect to all defined sources. If 'migration' is given, appropriate \ |
|
709 |
sources for migration will be automatically selected.", |
|
710 |
}), |
|
1446 | 711 |
|
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
|
712 |
('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
|
713 |
{'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
|
714 |
'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
|
715 |
'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
|
716 |
), |
0 | 717 |
) |
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
|
718 |
|
0 | 719 |
def run(self, args): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
720 |
appid = pop_arg(args, 99, msg="No instance specified !") |
1132 | 721 |
config = cwcfg.config_for(appid) |
0 | 722 |
if self.config.ext_sources: |
723 |
assert not self.config.system_only |
|
724 |
sources = self.config.ext_sources |
|
725 |
elif self.config.system_only: |
|
726 |
sources = ('system',) |
|
727 |
else: |
|
728 |
sources = ('all',) |
|
729 |
config.set_sources_mode(sources) |
|
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
|
730 |
config.repairing = self.config.force |
0 | 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:]) |