forked from hforge/usine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules_instance.py
377 lines (296 loc) · 12.2 KB
/
modules_instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: UTF-8 -*-
# Copyright (C) 2009-2010 Juan David Ibáñez Palomar <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from os.path import expanduser
# Import from pygobject
from glib import GError
# Import from itools
from itools.core import freeze, lazy
from itools.fs import lfs, vfs
# Import from usine
from config import config
from hosts import local, get_remote_host
from modules import module, register_module
cmd_vhosts = """
from itools.database import Catalog, get_register_fields
catalog = Catalog('./%s/catalog', get_register_fields(), read_only=True)
vhosts = list(catalog.get_unique_values('vhosts'))
vhosts.sort()
for vhost in vhosts:
print vhost
"""
class instance(module):
@lazy
def location(self):
location = self.options['location']
if location[:10] == 'localhost:':
# Case 1: local
user, server, path = None, 'localhost', location[10:]
else:
# Case 2: remote
user, location = location.split('@', 1)
server, path = location.split(':', 1)
if path[0] != '/':
path = '~/%s' % path
return user, server, path
@lazy
def bin_python(self):
return '%s/bin/python' % self.location[2]
def get_host(self):
user, server, path = self.location
if server == 'localhost':
return local
if config.options.offline:
print 'Error: this action is not available in offline mode'
exit(1)
server = config.get_section('server', server)
host = server.options['host']
shell = bool(int(self.options.get('shell', '0')))
return get_remote_host(host, user, shell)
def get_source(self, name):
source = config.get_section('pysrc', name)
if source:
return source
raise ValueError, 'the source "%s" is not found' % name
class pyenv(instance):
class_title = u'Manage Python environments'
def get_actions(self):
if self.location[1] == 'localhost':
return ['build', 'install', 'restart', 'deploy', 'deploy_reindex',
'reindex']
return ['build', 'upload', 'install', 'restart', 'deploy',
'deploy_reindex', 'test', 'vhosts', 'reindex']
def get_action(self, name):
if self.location[1] == 'localhost':
if name == 'install':
return self.action_install_local
elif name == 'upload':
return None
return super(pyenv, self).get_action(name)
def get_packages(self):
packages = self.options['packages'].split()
return [ x.split(':') for x in packages ]
build_title = u'Build the source code this Python environment requires'
def action_build(self):
"""Make a source distribution for every required Python package.
"""
path = expanduser('~/.usine/cache')
if not lfs.exists(path):
lfs.make_folder(path)
print '**********************************************************'
print ' BUILD'
print '**********************************************************'
for name, branch in self.get_packages():
config.options.branch = branch
source = self.get_source(name)
source.action_dist()
upload_title = u'Upload the source code to the remote server'
def action_upload(self):
"""Upload every required package to the remote host.
"""
host = self.get_host()
print '**********************************************************'
print ' UPLOAD'
print '**********************************************************'
for name, branch in self.get_packages():
source = self.get_source(name)
# Upload
pkgname = source.get_pkgname()
l_path = '%s/dist/%s.tar.gz' % (source.get_path(), pkgname)
host.put(l_path, '/tmp')
install_title = u'Install the source code into the Python environment'
def action_install(self):
"""Installs every required package into the remote virtual
environment.
"""
host = self.get_host()
print '**********************************************************'
print ' INSTALL'
print '**********************************************************'
command = '%s setup.py --quiet install --force' % self.bin_python
prefix = self.options.get('prefix')
if prefix:
command += ' --prefix=%s' % prefix
for name, branch in self.get_packages():
source = self.get_source(name)
pkgname = source.get_pkgname()
# Untar
host.run('tar xzf %s.tar.gz' % pkgname, '/tmp')
pkg_path = '/tmp/%s' % pkgname
# Install
host.run(command, pkg_path)
# Clean
host.run('rm -rf %s' % pkg_path, '/tmp')
def action_install_local(self):
print '**********************************************************'
print ' INSTALL'
print '**********************************************************'
bin_python = expanduser(self.bin_python)
command = [bin_python, 'setup.py', 'install', '--force']
for name, branch in self.get_packages():
source = self.get_source(name)
cwd = source.get_path()
local.run(command, cwd=cwd)
restart_title = u'Restart the ikaaro instances that use this environment'
def action_restart(self):
"""Restarts every ikaaro instance.
"""
print '**********************************************************'
print ' RESTART'
print '**********************************************************'
for ikaaro in config.get_sections_by_type('ikaaro'):
if ikaaro.options['pyenv'] == self.name:
ikaaro.stop()
ikaaro.start()
reindex_title = u'Reindex the ikaaro instances that use this environment'
def action_reindex(self):
"""Reindex every ikaaro instance.
"""
print '**********************************************************'
print ' REINDEX'
print '**********************************************************'
for ikaaro in config.get_sections_by_type('ikaaro'):
if ikaaro.options['pyenv'] == self.name:
ikaaro.stop()
ikaaro.update_catalog()
ikaaro.start()
deploy_title = u'All of the above'
def action_deploy(self):
"""Deploy (build, upload, install, restart) the required Python
packages in the remote virtual environment, and restart all the ikaaro
instances.
"""
actions = ['build', 'upload', 'install', 'restart']
for name in actions:
action = self.get_action(name)
if action:
action()
deploy_reindex_title = (
u'Build, upload, install, reindex and start the ikaaro instances')
def action_deploy_reindex(self):
"""
Build, upload, install the required Python packages
in the remote virtual environment and stop, reindex and start all the
ikaaro instances.
"""
actions = ['build', 'upload', 'install', 'stop', 'reindex', 'start']
for name in actions:
action = self.get_action(name)
if action:
action()
test_title = (
u'Test if ikaaro instances of this Python environment are alive')
def action_test(self):
""" Test if ikaaro instances of this Python environment are alive"""
print '**********************************************************'
print ' TEST'
print '**********************************************************'
for ikaaro in config.get_sections_by_type('ikaaro'):
if ikaaro.options['pyenv'] == self.name:
uri = ikaaro.options['uri']
try:
vfs.open('%s/;_ctrl' % uri)
except GError:
print '[ERROR] ', uri
else:
print '[OK]', uri
vhosts_title = (
u'List vhosts of all ikaaro instances of this Python environment')
def action_vhosts(self):
"""List vhosts of all ikaaro instances of this Python environment"""
print '**********************************************************'
print ' LIST VHOSTS'
print '**********************************************************'
for ikaaro in config.get_sections_by_type('ikaaro'):
if ikaaro.options['pyenv'] == self.name:
ikaaro.vhosts()
class ikaaro(instance):
class_title = u'Manage Ikaaro instances'
class_actions = freeze(['start', 'stop', 'restart', 'reindex', 'vhosts'])
@lazy
def pyenv(self):
pyenv = self.options['pyenv']
return config.get_section('pyenv', pyenv)
@lazy
def bin_icms(self):
pyenv = self.pyenv
prefix = pyenv.options.get('prefix') or pyenv.location[2]
return '%s/bin' % prefix
def get_host(self):
pyenv = self.pyenv
host = pyenv.get_host()
cwd = pyenv.location[2]
host.chdir(cwd)
return host
def stop(self):
path = self.options['path']
host = self.get_host()
host.run('%s/icms-stop.py %s' % (self.bin_icms, path))
host.run('%s/icms-stop.py --force %s' % (self.bin_icms, path))
def start(self, readonly=False):
path = self.options['path']
cmd = '%s/icms-start.py -d %s' % (self.bin_icms, path)
readonly = readonly or self.options.get('readonly', False)
if readonly:
cmd = cmd + ' -r'
host = self.get_host()
host.run(cmd)
def update_catalog(self):
path = self.options['path']
cmd = '{0}/icms-update-catalog.py -y {1} --quiet'.format(self.bin_icms, path)
host = self.get_host()
host.run(cmd)
def vhosts(self):
path = self.options['path']
host = self.get_host()
cmd = cmd_vhosts % path
host.run('./bin/python -c "%s"' % cmd, quiet=True)
start_title = u'Start an ikaaro instance'
def action_start(self):
print '**********************************************************'
print ' START'
print '**********************************************************'
self.start()
stop_title = u'Stop an ikaaro instance'
def action_stop(self):
print '**********************************************************'
print ' STOP'
print '**********************************************************'
self.stop()
restart_title = u'(Re)Start an ikaaro instance'
def action_restart(self):
print '**********************************************************'
print ' RESTART'
print '**********************************************************'
self.stop()
self.start()
reindex_title = u'Update catalog of an ikaaro instance'
def action_reindex(self):
print '**********************************************************'
print ' REINDEX'
print '**********************************************************'
self.stop()
self.update_catalog()
self.start()
vhosts_title = u'List vhosts of ikaaro instance'
def action_vhosts(self):
print '**********************************************************'
print ' List Vhosts'
print '**********************************************************'
self.vhosts()
# Register
register_module('ikaaro', ikaaro)
register_module('pyenv', pyenv)