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