author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 10 Sep 2009 08:13:22 +0200 | |
changeset 3163 | edfe43ceaa35 |
parent 2968 | 0e3460341023 |
parent 3117 | 32686ae66c75 |
child 3185 | bd0126d17e83 |
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 |
|
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2743
diff
changeset
|
20 |
from cubicweb import ConfigurationError, ExecutionError, BadCommandUsage |
1132 | 21 |
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg, CONFIGURATIONS |
2790
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2743
diff
changeset
|
22 |
from cubicweb.toolsutils import Command, main_run, rm, create_dir, underline_title |
968108e16066
move underline_title to toolsutils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2743
diff
changeset
|
23 |
|
1446 | 24 |
|
0 | 25 |
def wait_process_end(pid, maxtry=10, waittime=1): |
26 |
"""wait for a process to actually die""" |
|
27 |
import signal |
|
28 |
from time import sleep |
|
29 |
nbtry = 0 |
|
30 |
while nbtry < maxtry: |
|
31 |
try: |
|
32 |
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
|
33 |
except (OSError, AttributeError): # XXX win32 |
0 | 34 |
break |
35 |
nbtry += 1 |
|
36 |
sleep(waittime) |
|
37 |
else: |
|
38 |
raise ExecutionError('can\'t kill process %s' % pid) |
|
39 |
||
40 |
def list_instances(regdir): |
|
41 |
return sorted(idir for idir in listdir(regdir) if isdir(join(regdir, idir))) |
|
42 |
||
43 |
def detect_available_modes(templdir): |
|
44 |
modes = [] |
|
45 |
for fname in ('schema', 'schema.py'): |
|
46 |
if exists(join(templdir, fname)): |
|
47 |
modes.append('repository') |
|
48 |
break |
|
49 |
for fname in ('data', 'views', 'views.py'): |
|
50 |
if exists(join(templdir, fname)): |
|
51 |
modes.append('web ui') |
|
52 |
break |
|
53 |
return modes |
|
1446 | 54 |
|
55 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
56 |
class InstanceCommand(Command): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
57 |
"""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
|
58 |
(0 meaning all registered instances) |
0 | 59 |
""" |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
60 |
arguments = '[<instance>...]' |
0 | 61 |
options = ( |
62 |
("force", |
|
63 |
{'short': 'f', 'action' : 'store_true', |
|
64 |
'default': False, |
|
65 |
'help': 'force command without asking confirmation', |
|
66 |
} |
|
67 |
), |
|
68 |
) |
|
69 |
actionverb = None |
|
1446 | 70 |
|
0 | 71 |
def ordered_instances(self): |
72 |
"""return instances in the order in which they should be started, |
|
73 |
considering $REGISTRY_DIR/startorder file if it exists (useful when |
|
74 |
some instances depends on another as external source |
|
75 |
""" |
|
1132 | 76 |
regdir = cwcfg.registry_dir() |
0 | 77 |
_allinstances = list_instances(regdir) |
78 |
if isfile(join(regdir, 'startorder')): |
|
79 |
allinstances = [] |
|
80 |
for line in file(join(regdir, 'startorder')): |
|
81 |
line = line.strip() |
|
82 |
if line and not line.startswith('#'): |
|
83 |
try: |
|
84 |
_allinstances.remove(line) |
|
85 |
allinstances.append(line) |
|
86 |
except ValueError: |
|
1132 | 87 |
print ('ERROR: startorder file contains unexistant ' |
88 |
'instance %s' % line) |
|
0 | 89 |
allinstances += _allinstances |
90 |
else: |
|
91 |
allinstances = _allinstances |
|
92 |
return allinstances |
|
1446 | 93 |
|
0 | 94 |
def run(self, args): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
95 |
"""run the <command>_method on each argument (a list of instance |
0 | 96 |
identifiers) |
97 |
""" |
|
98 |
if not args: |
|
99 |
args = self.ordered_instances() |
|
100 |
try: |
|
101 |
askconfirm = not self.config.force |
|
102 |
except AttributeError: |
|
103 |
# no force option |
|
104 |
askconfirm = False |
|
105 |
else: |
|
106 |
askconfirm = False |
|
107 |
self.run_args(args, askconfirm) |
|
1446 | 108 |
|
0 | 109 |
def run_args(self, args, askconfirm): |
110 |
for appid in args: |
|
111 |
if askconfirm: |
|
112 |
print '*'*72 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
113 |
if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
0 | 114 |
continue |
115 |
self.run_arg(appid) |
|
1446 | 116 |
|
0 | 117 |
def run_arg(self, appid): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
118 |
cmdmeth = getattr(self, '%s_instance' % self.name) |
0 | 119 |
try: |
120 |
cmdmeth(appid) |
|
121 |
except (KeyboardInterrupt, SystemExit): |
|
122 |
print >> sys.stderr, '%s aborted' % self.name |
|
123 |
sys.exit(2) # specific error code |
|
124 |
except (ExecutionError, ConfigurationError), ex: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
125 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 126 |
appid, self.actionverb, ex) |
127 |
except Exception, ex: |
|
128 |
import traceback |
|
129 |
traceback.print_exc() |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
130 |
print >> sys.stderr, 'instance %s not %s: %s' % ( |
0 | 131 |
appid, self.actionverb, ex) |
132 |
||
133 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
134 |
class InstanceCommandFork(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
135 |
"""Same as `InstanceCommand`, but command is forked in a new environment |
0 | 136 |
for each argument |
137 |
""" |
|
138 |
||
139 |
def run_args(self, args, askconfirm): |
|
140 |
if len(args) > 1: |
|
141 |
forkcmd = ' '.join(w for w in sys.argv if not w in args) |
|
142 |
else: |
|
143 |
forkcmd = None |
|
144 |
for appid in args: |
|
145 |
if askconfirm: |
|
146 |
print '*'*72 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
147 |
if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
0 | 148 |
continue |
149 |
if forkcmd: |
|
150 |
status = system('%s %s' % (forkcmd, appid)) |
|
151 |
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
|
152 |
print '%s exited with status %s' % (forkcmd, status) |
0 | 153 |
else: |
154 |
self.run_arg(appid) |
|
1446 | 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 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
369 |
class StartInstanceCommand(InstanceCommand): |
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""" |
0 | 401 |
# use get() since start may be used from other commands (eg upgrade) |
402 |
# without all options defined |
|
403 |
debug = self.get('debug') |
|
404 |
force = self.get('force') |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
405 |
loglevel = self.get('loglevel') |
1132 | 406 |
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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
config.init_log(loglevel, debug=debug, force=True) |
0 | 411 |
if self.get('profile'): |
412 |
config.global_set_option('profile', self.config.profile) |
|
413 |
helper = self.config_helper(config, cmdname='start') |
|
414 |
pidf = config['pid-file'] |
|
415 |
if exists(pidf) and not force: |
|
416 |
msg = "%s seems to be running. Remove %s by hand if necessary or use \ |
|
417 |
the --force option." |
|
418 |
raise ExecutionError(msg % (appid, pidf)) |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2633
diff
changeset
|
419 |
helper.start_command(config, debug) |
0 | 420 |
return True |
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 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
473 |
class RestartInstanceCommand(StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
474 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
475 |
"""Restart the given instances. |
1446 | 476 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
477 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
478 |
identifiers of the instances to restart. If no instance is |
0 | 479 |
given, restart them all. |
480 |
""" |
|
481 |
name = 'restart' |
|
482 |
actionverb = 'restarted' |
|
483 |
||
484 |
def run_args(self, args, askconfirm): |
|
1132 | 485 |
regdir = cwcfg.registry_dir() |
0 | 486 |
if not isfile(join(regdir, 'startorder')) or len(args) <= 1: |
487 |
# no specific startorder |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
488 |
super(RestartInstanceCommand, self).run_args(args, askconfirm) |
0 | 489 |
return |
490 |
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
|
491 |
'instances then restart them.') |
0 | 492 |
# get instances in startorder |
493 |
stopped = [] |
|
494 |
for appid in args: |
|
495 |
if askconfirm: |
|
496 |
print '*'*72 |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
497 |
if not ASK.confirm('%s instance %r ?' % (self.name, appid)): |
0 | 498 |
continue |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
499 |
self.stop_instance(appid) |
0 | 500 |
stopped.append(appid) |
501 |
forkcmd = [w for w in sys.argv if not w in args] |
|
502 |
forkcmd[1] = 'start' |
|
503 |
forkcmd = ' '.join(forkcmd) |
|
504 |
for appid in reversed(args): |
|
505 |
status = system('%s %s' % (forkcmd, appid)) |
|
506 |
if status: |
|
507 |
sys.exit(status) |
|
1446 | 508 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
509 |
def restart_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
510 |
self.stop_instance(appid) |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
511 |
if self.start_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
512 |
print 'instance %s %s' % (appid, self.actionverb) |
0 | 513 |
|
1446 | 514 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
515 |
class ReloadConfigurationCommand(RestartInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
516 |
"""Reload the given instances. This command is equivalent to a |
0 | 517 |
restart for now. |
1446 | 518 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
519 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
520 |
identifiers of the instances to reload. If no instance is |
0 | 521 |
given, reload them all. |
522 |
""" |
|
523 |
name = 'reload' |
|
1446 | 524 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
525 |
def reload_instance(self, appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
526 |
self.restart_instance(appid) |
1446 | 527 |
|
0 | 528 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
529 |
class StatusCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
530 |
"""Display status information about the given instances. |
1446 | 531 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
532 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
533 |
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
|
534 |
given, get status information about all registered instances. |
0 | 535 |
""" |
536 |
name = 'status' |
|
537 |
options = () |
|
538 |
||
1132 | 539 |
@staticmethod |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
540 |
def status_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
541 |
"""print running status information for an instance""" |
1132 | 542 |
for mode in cwcfg.possible_configurations(appid): |
543 |
config = cwcfg.config_for(appid, mode) |
|
0 | 544 |
print '[%s-%s]' % (appid, mode), |
545 |
try: |
|
546 |
pidf = config['pid-file'] |
|
547 |
except KeyError: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
548 |
print 'buggy instance, pid file not specified' |
0 | 549 |
continue |
550 |
if not exists(pidf): |
|
551 |
print "doesn't seem to be running" |
|
552 |
continue |
|
553 |
pid = int(open(pidf).read().strip()) |
|
554 |
# trick to guess whether or not the process is running |
|
555 |
try: |
|
556 |
getpgid(pid) |
|
557 |
except OSError: |
|
558 |
print "should be running with pid %s but the process can not be found" % pid |
|
559 |
continue |
|
560 |
print "running with pid %s" % (pid) |
|
561 |
||
562 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
563 |
class UpgradeInstanceCommand(InstanceCommandFork, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
564 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
565 |
StopInstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
566 |
"""Upgrade an instance after cubicweb and/or component(s) upgrade. |
0 | 567 |
|
568 |
For repository update, you will be prompted for a login / password to use |
|
569 |
to connect to the system database. For some upgrades, the given user |
|
570 |
should have create or alter table permissions. |
|
571 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
572 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
573 |
identifiers of the instances to upgrade. If no instance is |
0 | 574 |
given, upgrade them all. |
575 |
""" |
|
576 |
name = 'upgrade' |
|
577 |
actionverb = 'upgraded' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
578 |
options = InstanceCommand.options + ( |
0 | 579 |
('force-componant-version', |
580 |
{'short': 't', 'type' : 'csv', 'metavar': 'cube1=X.Y.Z,cube2=X.Y.Z', |
|
581 |
'default': None, |
|
582 |
'help': 'force migration from the indicated version for the specified cube.'}), |
|
583 |
('force-cubicweb-version', |
|
584 |
{'short': 'e', 'type' : 'string', 'metavar': 'X.Y.Z', |
|
585 |
'default': None, |
|
586 |
'help': 'force migration from the indicated cubicweb version.'}), |
|
1446 | 587 |
|
0 | 588 |
('fs-only', |
589 |
{'short': 's', 'action' : 'store_true', |
|
590 |
'default': False, |
|
591 |
'help': 'only upgrade files on the file system, not the database.'}), |
|
592 |
||
593 |
('nostartstop', |
|
594 |
{'short': 'n', 'action' : 'store_true', |
|
595 |
'default': False, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
596 |
'help': 'don\'t try to stop instance before migration and to restart it after.'}), |
1446 | 597 |
|
0 | 598 |
('verbosity', |
599 |
{'short': 'v', 'type' : 'int', 'metavar': '<0..2>', |
|
600 |
'default': 1, |
|
601 |
'help': "0: no confirmation, 1: only main commands confirmed, 2 ask \ |
|
602 |
for everything."}), |
|
1446 | 603 |
|
0 | 604 |
('backup-db', |
605 |
{'short': 'b', 'type' : 'yn', 'metavar': '<y or n>', |
|
606 |
'default': None, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
607 |
'help': "Backup the instance database before upgrade.\n"\ |
0 | 608 |
"If the option is ommitted, confirmation will be ask.", |
609 |
}), |
|
610 |
||
611 |
('ext-sources', |
|
612 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
613 |
'default': None, |
|
614 |
'help': "For multisources instances, specify to which sources the \ |
|
615 |
repository should connect to for upgrading. When unspecified or 'migration' is \ |
|
616 |
given, appropriate sources for migration will be automatically selected \ |
|
617 |
(recommended). If 'all' is given, will connect to all defined sources.", |
|
618 |
}), |
|
619 |
) |
|
620 |
||
621 |
def ordered_instances(self): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
622 |
# need this since mro return StopInstanceCommand implementation |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
623 |
return InstanceCommand.ordered_instances(self) |
1446 | 624 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
625 |
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
|
626 |
print '\n' + underline_title('Upgrading the instance %s' % appid) |
0 | 627 |
from logilab.common.changelog import Version |
1132 | 628 |
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
|
629 |
config.repairing = True # notice we're not starting the server |
0 | 630 |
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
|
631 |
try: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
632 |
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
|
633 |
except AttributeError: |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
634 |
# not a server config |
054bb575c013
take care, self.config may not be a server config
sylvain.thenault@logilab.fr
parents:
1015
diff
changeset
|
635 |
pass |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
636 |
# get instance and installed versions for the server and the componants |
0 | 637 |
mih = config.migration_handler() |
638 |
repo = mih.repo_connect() |
|
639 |
vcconf = repo.get_versions() |
|
640 |
if self.config.force_componant_version: |
|
641 |
packversions = {} |
|
642 |
for vdef in self.config.force_componant_version: |
|
643 |
componant, version = vdef.split('=') |
|
644 |
packversions[componant] = Version(version) |
|
645 |
vcconf.update(packversions) |
|
646 |
toupgrade = [] |
|
647 |
for cube in config.cubes(): |
|
648 |
installedversion = config.cube_version(cube) |
|
649 |
try: |
|
650 |
applversion = vcconf[cube] |
|
651 |
except KeyError: |
|
652 |
config.error('no version information for %s' % cube) |
|
653 |
continue |
|
654 |
if installedversion > applversion: |
|
655 |
toupgrade.append( (cube, applversion, installedversion) ) |
|
1446 | 656 |
cubicwebversion = config.cubicweb_version() |
0 | 657 |
if self.config.force_cubicweb_version: |
658 |
applcubicwebversion = Version(self.config.force_cubicweb_version) |
|
659 |
vcconf['cubicweb'] = applcubicwebversion |
|
660 |
else: |
|
661 |
applcubicwebversion = vcconf.get('cubicweb') |
|
662 |
if cubicwebversion > applcubicwebversion: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
663 |
toupgrade.append(('cubicweb', applcubicwebversion, cubicwebversion)) |
0 | 664 |
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
|
665 |
print '-> no software migration needed for instance %s.' % appid |
0 | 666 |
return |
667 |
for cube, fromversion, toversion in toupgrade: |
|
2532
f7ca29d75183
[cleanup] improve dialog message consistency
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2512
diff
changeset
|
668 |
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
|
669 |
# only stop once we're sure we have something to do |
1477 | 670 |
if not (cwcfg.mode == 'dev' or self.config.nostartstop): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
671 |
self.stop_instance(appid) |
0 | 672 |
# run cubicweb/componants migration scripts |
673 |
mih.migrate(vcconf, reversed(toupgrade), self.config) |
|
674 |
# rewrite main configuration file |
|
675 |
mih.rewrite_configuration() |
|
676 |
# handle i18n upgrade: |
|
677 |
# * install new languages |
|
678 |
# * recompile catalogs |
|
679 |
# in the first componant given |
|
680 |
from cubicweb.common import i18n |
|
1132 | 681 |
templdir = cwcfg.cube_dir(config.cubes()[0]) |
0 | 682 |
langs = [lang for lang, _ in i18n.available_catalogs(join(templdir, 'i18n'))] |
683 |
errors = config.i18ncompile(langs) |
|
684 |
if errors: |
|
685 |
print '\n'.join(errors) |
|
2743
b0e79a77ad67
[c-c] fix confirm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
686 |
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
|
687 |
'continue anyway ?'): |
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
688 |
print '-> migration not completed.' |
0 | 689 |
return |
690 |
mih.shutdown() |
|
691 |
print |
|
2512
106b2a05dc88
[cleanup] started to improve cubicweb-ctl dialog messages consistency
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2489
diff
changeset
|
692 |
print '-> instance migrated.' |
1132 | 693 |
if not (cwcfg.mode == 'dev' or self.config.nostartstop): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
694 |
self.start_instance(appid) |
0 | 695 |
print |
696 |
||
697 |
||
698 |
class ShellCommand(Command): |
|
699 |
"""Run an interactive migration shell. This is a python shell with |
|
700 |
enhanced migration commands predefined in the namespace. An additional |
|
701 |
argument may be given corresponding to a file containing commands to |
|
702 |
execute in batch mode. |
|
703 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
704 |
<instance> |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
705 |
the identifier of the instance to connect. |
0 | 706 |
""" |
707 |
name = 'shell' |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
708 |
arguments = '<instance> [batch command file]' |
0 | 709 |
options = ( |
710 |
('system-only', |
|
711 |
{'short': 'S', 'action' : 'store_true', |
|
712 |
'default': False, |
|
713 |
'help': 'only connect to the system source when the instance is ' |
|
714 |
'using multiple sources. You can\'t use this option and the ' |
|
715 |
'--ext-sources option at the same time.'}), |
|
1446 | 716 |
|
0 | 717 |
('ext-sources', |
718 |
{'short': 'E', 'type' : 'csv', 'metavar': '<sources>', |
|
719 |
'default': None, |
|
720 |
'help': "For multisources instances, specify to which sources the \ |
|
721 |
repository should connect to for upgrading. When unspecified or 'all' given, \ |
|
722 |
will connect to all defined sources. If 'migration' is given, appropriate \ |
|
723 |
sources for migration will be automatically selected.", |
|
724 |
}), |
|
1446 | 725 |
|
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
|
726 |
('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
|
727 |
{'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
|
728 |
'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
|
729 |
'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
|
730 |
), |
0 | 731 |
) |
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
|
732 |
|
0 | 733 |
def run(self, args): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
734 |
appid = pop_arg(args, 99, msg="No instance specified !") |
1132 | 735 |
config = cwcfg.config_for(appid) |
0 | 736 |
if self.config.ext_sources: |
737 |
assert not self.config.system_only |
|
738 |
sources = self.config.ext_sources |
|
739 |
elif self.config.system_only: |
|
740 |
sources = ('system',) |
|
741 |
else: |
|
742 |
sources = ('all',) |
|
743 |
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
|
744 |
config.repairing = self.config.force |
0 | 745 |
mih = config.migration_handler() |
746 |
if args: |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2156
diff
changeset
|
747 |
for arg in args: |
2318
17f7c3990f9b
Fix shell script execution command.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
2275
diff
changeset
|
748 |
mih.process_script(arg) |
0 | 749 |
else: |
750 |
mih.interactive_shell() |
|
1446 | 751 |
mih.shutdown() |
0 | 752 |
|
753 |
||
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
754 |
class RecompileInstanceCatalogsCommand(InstanceCommand): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
755 |
"""Recompile i18n catalogs for instances. |
1446 | 756 |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
757 |
<instance>... |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
758 |
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
|
759 |
given, recompile for all registered instances. |
0 | 760 |
""" |
1898
39b37f90a8a4
[cw-ctl] rename i18n commands (see #342889)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1477
diff
changeset
|
761 |
name = 'i18ninstance' |
1132 | 762 |
|
763 |
@staticmethod |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
764 |
def i18ninstance_instance(appid): |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
765 |
"""recompile instance's messages catalogs""" |
1132 | 766 |
config = cwcfg.config_for(appid) |
0 | 767 |
try: |
768 |
config.bootstrap_cubes() |
|
769 |
except IOError, ex: |
|
770 |
import errno |
|
771 |
if ex.errno != errno.ENOENT: |
|
772 |
raise |
|
773 |
# 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
|
774 |
# 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
|
775 |
config.repairing = True |
0 | 776 |
# create an in-memory repository, will call config.init_cubes() |
777 |
config.repository() |
|
778 |
except AttributeError: |
|
779 |
# web only config |
|
780 |
config.init_cubes(config.repository().get_cubes()) |
|
781 |
errors = config.i18ncompile() |
|
782 |
if errors: |
|
783 |
print '\n'.join(errors) |
|
784 |
||
785 |
||
786 |
class ListInstancesCommand(Command): |
|
787 |
"""list available instances, useful for bash completion.""" |
|
788 |
name = 'listinstances' |
|
789 |
hidden = True |
|
1446 | 790 |
|
0 | 791 |
def run(self, args): |
792 |
"""run the command with its specific arguments""" |
|
1132 | 793 |
regdir = cwcfg.registry_dir() |
0 | 794 |
for appid in sorted(listdir(regdir)): |
795 |
print appid |
|
796 |
||
797 |
||
798 |
class ListCubesCommand(Command): |
|
799 |
"""list available componants, useful for bash completion.""" |
|
800 |
name = 'listcubes' |
|
801 |
hidden = True |
|
1446 | 802 |
|
0 | 803 |
def run(self, args): |
804 |
"""run the command with its specific arguments""" |
|
1132 | 805 |
for cube in cwcfg.available_cubes(): |
0 | 806 |
print cube |
807 |
||
808 |
register_commands((ListCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
809 |
CreateInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
810 |
DeleteInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
811 |
StartInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
812 |
StopInstanceCommand, |
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
813 |
RestartInstanceCommand, |
0 | 814 |
ReloadConfigurationCommand, |
815 |
StatusCommand, |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
816 |
UpgradeInstanceCommand, |
0 | 817 |
ShellCommand, |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2473
diff
changeset
|
818 |
RecompileInstanceCatalogsCommand, |
0 | 819 |
ListInstancesCommand, ListCubesCommand, |
820 |
)) |
|
821 |
||
1446 | 822 |
|
0 | 823 |
def run(args): |
824 |
"""command line tool""" |
|
1132 | 825 |
cwcfg.load_cwctl_plugins() |
0 | 826 |
main_run(args, __doc__) |
827 |
||
828 |
if __name__ == '__main__': |
|
829 |
run(sys.argv[1:]) |