-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzvapp
executable file
·363 lines (331 loc) · 14.3 KB
/
zvapp
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
#!/usr/bin/python
import shutil
import sys
import re
import tarfile
from tempfile import mkstemp, mkdtemp
from eventlet.green.subprocess import Popen, PIPE
from eventlet.green import os
from eventlet import GreenPool
from zvshlib.zvsh import ZvRunner, ZvArgs, ZvConfig
try:
import simplejson as json
except ImportError:
import json
try:
from zerocloud.configparser import ClusterConfigParser, \
ClusterConfigParsingError, NodeEncoder
from zerocloud.common import parse_location, \
CLUSTER_CONFIG_FILENAME, NODE_CONFIG_FILENAME, \
ACCESS_WRITABLE, ACCESS_READABLE, ACCESS_CDR, ACCESS_NETWORK, \
SwiftPath, ImagePath
from zerocloud.proxyquery import NameService
except ImportError:
sys.stderr.write('Please install python-zerocloud before running %s\n'
% sys.argv[0])
sys.exit(1)
class AppRunner(ZvRunner):
def __init__(self, command_line, report_file):
self.command = command_line
self.process = None
self.report = ''
self.report_file = report_file
def run(self):
try:
self.process = Popen(self.command, stdout=PIPE)
rep_reader = self.spawn(True, self.report_reader)
self.process.wait()
rep_reader.join()
except (KeyboardInterrupt, Exception):
pass
finally:
if self.process:
self.process.wait()
fd = open(self.report_file, 'wb')
fd.write(self.report)
fd.close()
class AppArgs(ZvArgs):
def add_agruments(self):
self.parser.add_argument('exec_file',
help='ZeroVM application archive '
'or map file\n')
self.parser.add_argument('--swift-root-path',
help='Root path for resolving swift:// urls, '
'ex.\n'
'swift://account/container/object ->'
' swift-root-path/account/container/'
'object\n')
self.parser.add_argument('--swift-account-path',
help='Account path for all urls\n'
'to serve all swift://account/ '
'urls from one path')
self.parser.add_argument('--sysimage-root-path',
help='Root path for system image files\n')
self.parser.add_argument('--zvm-save-dir',
help='Save ZeroVM environment files into '
'provided directory,\n'
'directory will be created/re-created\n')
self.parser.add_argument('--dry-run',
help='Print the resulting job descriptions '
'and exit\n',
action='store_true')
class ZvLocalFilesystem(object):
SYSIMAGE_MASK = re.compile(r'(.*?)(\.[^.]+)?$')
def __init__(self, sysimage_root_path=None,
root_path=None, account_path=None, config=None, savedir=None):
if not root_path:
root_path = config.get('root_path', None) or ''
self.image_path = None
self.root_path = os.path.abspath(root_path)
self.account_path = account_path
if not self.account_path:
self.account_path = config.get('account_path', None)
if self.account_path:
self.account_path = os.path.abspath(self.account_path)
self.savedir = savedir
if self.savedir:
self.tempdir = os.path.abspath(self.savedir)
if os.path.isdir(self.tempdir):
shutil.rmtree(self.tempdir)
os.makedirs(self.tempdir)
else:
self.tempdir = mkdtemp()
self.sysimage_devices = {}
if not sysimage_root_path:
sysimage_root_path = config.get('sysimage_path', None)
if sysimage_root_path:
self.sysimage_devices = \
self._list_sysimage_devices(
os.path.abspath(sysimage_root_path))
self.immediate_responses = {}
def list_account(self, account, mask=None):
account_path = self.account_path
if not account_path:
account_path = os.path.join(self.root_path, account)
ret = []
for fname in sorted(os.listdir(account_path)):
if not mask or mask.match(fname):
ret.append(fname)
return ret
def list_container(self, account, container, mask=None):
account_path = self.account_path
if not account_path:
account_path = os.path.join(self.root_path, account)
container_path = os.path.join(account_path, container)
ret = []
for dirpath, _junk, filenames in os.walk(container_path):
dirpath = dirpath.replace(container_path, '', 1)[1:]
for fname in sorted(filenames):
objpath = os.path.join(dirpath, fname)
if not mask or mask.match(objpath):
ret.append(objpath)
return ret
def _list_sysimage_devices(self, sysimage_path):
result = {}
if os.path.isdir(sysimage_path):
for fname in os.listdir(sysimage_path):
path = os.path.join(sysimage_path, fname)
if os.path.isfile(path):
base = os.path.basename(path)
sysimage_name = self.SYSIMAGE_MASK.search(base).group(1)
result[sysimage_name] = path
return result
def get_local_path(self, device, path, access, node_name=None):
sysimage = self.sysimage_devices.get(device, None)
if sysimage:
return sysimage
if 'image' == device and not path:
return self.image_path
loc = parse_location(path)
if not loc and access & ACCESS_WRITABLE:
temp_file = self.create_temp_file()
self.immediate_responses[node_name] = temp_file
return temp_file
elif isinstance(loc, SwiftPath):
account_path = self.account_path
if not account_path:
account_path = os.path.join(self.root_path, loc.account)
local_path = os.path.join(account_path, loc.container, loc.obj)
return local_path
elif isinstance(loc, ImagePath) and access & (ACCESS_READABLE |
ACCESS_CDR):
if 'image' == loc.image:
if os.path.isdir(self.image_path):
return os.path.join(os.path.abspath(self.image_path),
loc.path)
else:
return self._extract_file(self.image_path, loc.path)
sysimage = self.sysimage_devices.get(loc.image, None)
return self._extract_file(sysimage, loc.path)
elif access & ACCESS_NETWORK:
return path
def _extract_file(self, image, file_name):
tar = tarfile.open(name=image)
efile = None
try:
efile = tar.extractfile(file_name)
except KeyError:
pass
if not efile:
return None
(fd, fn) = mkstemp(dir=self.tempdir)
reader = iter(lambda: efile.read(4096), '')
for chunk in reader:
os.write(fd, chunk)
os.close(fd)
return fn
def resolve_local_paths(self, node_config):
for ch in node_config['channels']:
ch['lpath'] = self.get_local_path(ch['device'],
ch['path'],
ch['access'],
node_config['name'])
def cleanup(self):
if not self.savedir:
shutil.rmtree(self.tempdir)
def create_temp_file(self):
(fd, fn) = mkstemp(dir=self.tempdir)
os.close(fd)
return fn
def get_responses(self):
data = ''
for fname in sorted(self.immediate_responses.keys()):
data += open(self.immediate_responses[fname]).read()
return data
def create_temp_files(self, node_name):
session_dir = os.path.join(self.tempdir, node_name)
os.makedirs(session_dir)
result = []
for n in ['nvram', 'manifest', 'report']:
result.append(os.path.join(session_dir, n))
#result.append(session_dir)
return result
if __name__ == '__main__':
threadpool = GreenPool()
nspool = GreenPool(1)
app_args = AppArgs()
app_args.parse(sys.argv[1:])
zvsh_config = ['zvsh.cfg',
os.path.expanduser('~/.zvsh.cfg'),
'/etc/zvsh.cfg']
zvconfig = ZvConfig()
zvconfig.read(zvsh_config)
cluster_config = None
local_fs = ZvLocalFilesystem(app_args.args.sysimage_root_path,
app_args.args.swift_root_path,
app_args.args.swift_account_path,
zvconfig['zvapp'])
image_path = None
try:
if os.path.isdir(app_args.args.exec_file):
# user tries to run an extracted application
# we will need to create image on the fly
# to load it in zerovm as a channel
app_dir = app_args.args.exec_file
tar_file = local_fs.create_temp_file()
tar = tarfile.TarFile.open(name=tar_file, mode='w')
try:
tar.add(os.path.join(app_dir, CLUSTER_CONFIG_FILENAME),
arcname=CLUSTER_CONFIG_FILENAME)
boot = CLUSTER_CONFIG_FILENAME
except OSError:
try:
tar.add(os.path.join(app_dir, NODE_CONFIG_FILENAME),
arcname=NODE_CONFIG_FILENAME)
boot = NODE_CONFIG_FILENAME
except OSError:
sys.stderr.write('Cannot find boot map anywhere in %s\n'
% app_dir)
sys.exit(1)
def tar_filter(tar_info):
if tar_info.name in [CLUSTER_CONFIG_FILENAME,
NODE_CONFIG_FILENAME]:
return None
return tar_info
for item in os.listdir(app_dir):
tar.add(os.path.join(app_dir, item), arcname=item,
filter=tar_filter)
tar.close()
image_path = os.path.abspath(tar_file)
cluster_config = json.load(open(os.path.join(app_dir, boot), 'rb'))
else:
try:
cluster_config_fd = open(app_args.args.exec_file, 'rb')
except IOError, e:
sys.stderr.write(str(e) + '\n')
sys.exit(1)
try:
# let's load the file as a cluster map
cluster_config = json.load(cluster_config_fd)
image_path = os.path.dirname(
os.path.abspath(app_args.args.exec_file))
except Exception:
# it's not a cluster map file
# try to load it as zvm app archive
try:
tar = tarfile.open(app_args.args.exec_file)
for name in tar.getnames():
if name in [CLUSTER_CONFIG_FILENAME,
NODE_CONFIG_FILENAME]:
cluster_config = json.load(tar.extractfile(name))
break
image_path = os.path.abspath(app_args.args.exec_file)
except tarfile.ReadError:
# it's not a tar file, bail out for now
sys.stderr.write('Cannot parse the input file %s\n'
% app_args.args.exec_file)
sys.exit(1)
local_fs.image_path = image_path
threads = {}
parser = ClusterConfigParser(local_fs.sysimage_devices,
'application/octet-stream',
zvconfig,
local_fs.list_account,
local_fs.list_container)
try:
parser.parse(cluster_config,
os.path.isfile(local_fs.image_path),
account_name='local')
except ClusterConfigParsingError, e:
sys.stderr.write(str(e) + '\n')
exit(1)
ns_server = None
if parser.total_count > 1:
ns_server = NameService(parser.total_count)
ns_server.start(nspool)
for node in parser.node_list:
node.name_service = 'udp:%s:%d' % ('127.0.0.1', ns_server.port)
parser.build_connect_string(node)
if app_args.args.dry_run:
print json.dumps(parser.node_list, cls=NodeEncoder, indent=2)
exit(0)
for node in parser.node_list:
node_config = json.loads(json.dumps(node, cls=NodeEncoder))
local_fs.resolve_local_paths(node_config)
nexe_path = local_fs.get_local_path('boot',
node_config['exe'],
ACCESS_READABLE)
nvram_file, manifest_file, report_file = \
local_fs.create_temp_files(node_config['name'])
manifest = parser.prepare_for_standalone(node_config, nvram_file,
nexe_path, None)
with open(manifest_file, 'wb') as fd:
fd.write(manifest)
command_line = ['zerovm', '-PQ', manifest_file]
runner = AppRunner(command_line, report_file)
threads[node_config['name']] = (report_file, node_config['id'],
runner)
for name in sorted(threads.keys()):
runner = threads[name][2]
threadpool.spawn_n(runner.run)
threadpool.waitall()
if ns_server:
ns_server.stop()
for name in sorted(threads.keys()):
rfile, node_id, runner = threads[name]
print "---------- Node: %s id: %s ---------" % (name, node_id)
print open(rfile).read()
print "========== Result =========="
print local_fs.get_responses()
finally:
local_fs.cleanup()