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