-
Notifications
You must be signed in to change notification settings - Fork 59
/
esoap.py
432 lines (354 loc) · 13.1 KB
/
esoap.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# ELBE - Debian Based Embedded Rootfilesystem Builder
# Copyright (c) 2014-2018 Manuel Traut <manut@linutronix.de>
# Copyright (c) 2015-2016, 2018 Torben Hohn <torben.hohn@linutronix.de>
# Copyright (c) 2016 Claudius Heine <ch@denx.de>
# Copyright (c) 2017 Philipp Arras <philipp.arras@linutronix.de>
#
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import print_function
import binascii
import os
import tarfile
import fnmatch
import sys
from tempfile import NamedTemporaryFile
from elbepack.shellhelper import system, command_out
from elbepack.version import elbe_version, is_devel
from elbepack.elbexml import ValidationMode
from elbepack.filesystem import hostfs
from .faults import soap_faults
from .datatypes import SoapProject, SoapFile, SoapCmdReply
from .authentication import authenticated_admin, authenticated_uid
try:
from spyne.service import ServiceBase
from spyne.decorator import rpc
from spyne.model.primitive import String, Boolean, Integer
from spyne.model.complex import Array
except ImportError as e:
print("failed to import spyne", file=sys.stderr)
print("please install python(3)-spyne", file=sys.stderr)
sys.exit(20)
class ESoap (ServiceBase):
# pylint: disable=too-many-public-methods
__name__ = 'soap'
def __init__(self):
self.app = None
self.transport = None
@rpc(_returns=String)
@soap_faults
def get_version(self):
# pylint: disable=no-self-use
return elbe_version
@rpc(String, String, _returns=Boolean)
@soap_faults
def login(self, user, passwd):
s = self.transport.req_env['beaker.session']
s['userid'] = self.app.pm.db.validate_login(user, passwd)
s.save()
return True
@rpc(_returns=Array(String))
@soap_faults
@authenticated_admin
def list_users(self):
return [u.name for u in self.app.pm.db.list_users()]
@rpc(String, String(max_occurs='unbounded'), _returns=SoapCmdReply)
@soap_faults
@authenticated_admin
def install_elbe_version(self, version, pkglist):
# pylint: disable=no-self-use
if is_devel:
return SoapCmdReply(10,
'Initvm is in devel mode: installing another\n'
'elbe version will not have any effect.\n')
pkgs = ['"%s=%s*"' % (p, version) for p in pkglist]
# Prevent, that elbe daemon is restarted by the
# prerm/postinst scripts.
# elbe daemon does it itself, because cherrypy
# notices that.
hostfs.write_file("usr/sbin/policy-rc.d",
0o755, "#!/bin/sh\nexit 101\n")
try:
env = {'LANG': 'C',
'LANGUAGE': 'C',
'LC_ALL': 'C',
'DEBIAN_FRONTEND': 'noninteractive',
'DEBCONF_NONINTERACTIVE_SEEN': 'true'}
cmd = 'apt-get install -y --force-yes %s' % ' '.join(pkgs)
ret, out = command_out(cmd, env_add=env)
finally:
hostfs.remove('usr/sbin/policy-rc.d')
return SoapCmdReply(ret, out)
@rpc(String, String, String, String, Boolean)
@soap_faults
@authenticated_admin
def add_user(self, name, fullname, password, email, admin):
# pylint: disable=too-many-arguments
self.app.pm.db.add_user(name, fullname, password, email, admin)
@rpc(_returns=Array(SoapProject))
@soap_faults
@authenticated_admin
def list_projects(self):
return self.app.pm.db.list_projects()
@rpc(String, _returns=SoapProject)
@soap_faults
@authenticated_uid
def get_project(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
return self.app.pm.db.get_project_data(builddir)
@rpc(String, _returns=Array(SoapFile))
@authenticated_uid
@soap_faults
def get_files(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
files = self.app.pm.db.get_project_files(builddir)
return [SoapFile(f) for f in files]
@rpc(String, String, String, Integer, _returns=Integer)
@authenticated_uid
@soap_faults
def upload_file(self, uid, builddir, fname, blob, part):
# pylint: disable=too-many-arguments
fn = os.path.join(builddir, fname)
if part == 0:
if self.app.pm.db.is_busy(builddir):
return -1
self.app.pm.db.set_busy(builddir, ["empty_project", "needs_build",
"has_changes", "build_done",
"build_failed"])
# truncate file
with open(fn, 'w') as fp:
fp.write('')
if part == -1:
with open(fn, 'a') as fp:
fp.flush()
self.app.pm.db.reset_busy(builddir, "has_changes")
if fname == "source.xml":
# ensure that the project cache is reloaded
self.app.pm.close_current_project(uid)
self.app.pm.open_project(
uid, builddir, url_validation=ValidationMode.NO_CHECK)
self.app.pm.set_current_project_xml(uid, fn)
return -2
with open(fn, 'ab') as fp:
fp.write(binascii.a2b_base64(blob))
return part + 1
@rpc(String, String, Integer, _returns=String)
@authenticated_uid
@soap_faults
def get_file(self, uid, builddir, filename, part):
self.app.pm.open_project(uid, builddir)
size = 1024 * 1024 * 5
pos = size * part
file_name = builddir + "/" + filename
file_stat = os.stat(file_name)
if pos >= file_stat.st_size:
return "EndOfFile"
with open(file_name, 'rb') as fp:
if not fp:
return "FileNotFound"
try:
fp.seek(pos)
data = fp.read(size)
return binascii.b2a_base64(data)
except BaseException:
return "EndOfFile"
@rpc(String)
@authenticated_uid
@soap_faults
def build_chroot_tarball(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_chroot_tarball(uid)
@rpc(String)
@authenticated_uid
@soap_faults
def build_sysroot(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_sysroot(uid)
@rpc(String)
@authenticated_uid
@soap_faults
def build_sdk(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_sdk(uid)
@rpc(String, Boolean, Boolean)
@authenticated_uid
@soap_faults
def build_cdroms(self, uid, builddir, build_bin, build_src):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_cdroms(uid, build_bin, build_src)
@rpc(String, Boolean, Boolean, Boolean)
@authenticated_uid
@soap_faults
def build(self, uid, builddir, build_bin, build_src, skip_pbuilder):
# pylint: disable=too-many-arguments
self.app.pm.open_project(uid, builddir)
self.app.pm.build_current_project(uid, build_bin, build_src,
skip_pbuilder)
@rpc(String, Boolean, Boolean, String)
@authenticated_uid
@soap_faults
def build_pbuilder(self, uid, builddir, cross, noccache, ccachesize):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_pbuilder(uid, cross, noccache, ccachesize)
@rpc(String)
@authenticated_uid
@soap_faults
def update_pbuilder(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.update_pbuilder(uid)
@rpc(String)
@authenticated_uid
@soap_faults
def start_cdrom(self, uid, builddir):
self.app.pm.open_project(
uid, builddir, url_validation=ValidationMode.NO_CHECK)
cdrom_fname = os.path.join(builddir, "uploaded_cdrom.iso")
# Now write empty File
fp = open(cdrom_fname, "w")
fp.close()
@rpc(String, String)
@authenticated_uid
@soap_faults
def append_cdrom(self, uid, builddir, data):
self.app.pm.open_project(
uid, builddir, url_validation=ValidationMode.NO_CHECK)
cdrom_fname = os.path.join(builddir, "uploaded_cdrom.iso")
# Now append data to cdrom_file
fp = open(cdrom_fname, "ab")
fp.write(binascii.a2b_base64(data))
fp.close()
@rpc(String)
@authenticated_uid
@soap_faults
def finish_cdrom(self, uid, builddir):
self.app.pm.open_project(
uid, builddir, url_validation=ValidationMode.NO_CHECK)
self.app.pm.set_current_project_upload_cdrom(uid)
@rpc(String)
@authenticated_uid
@soap_faults
def start_pdebuild(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
pdebuild_fname = os.path.join(builddir, "current_pdebuild.tar.gz")
# Now write empty File
fp = open(pdebuild_fname, "w")
fp.close()
@rpc(String, String)
@authenticated_uid
@soap_faults
def append_pdebuild(self, uid, builddir, data):
self.app.pm.open_project(uid, builddir)
pdebuild_fname = os.path.join(builddir, "current_pdebuild.tar.gz")
# Now write empty File
fp = open(pdebuild_fname, "ab")
fp.write(binascii.a2b_base64(data))
fp.close()
@rpc(String, Integer, String, Boolean)
@authenticated_uid
@soap_faults
# pylint: disable=too-many-arguments
def finish_pdebuild(self, uid, builddir, cpuset, profile, cross):
self.app.pm.open_project(uid, builddir)
self.app.pm.build_current_pdebuild(uid, cpuset, profile, cross)
@rpc(String, String)
@authenticated_uid
@soap_faults
def start_upload_orig(self, uid, builddir, fname):
self.app.pm.open_project(uid, builddir)
orig_fname = os.path.join(builddir, fname)
# Now write empty File
fp = open(orig_fname, "w")
fp.close()
self.app.pm.set_orig_fname(uid, fname)
@rpc(String, String)
@authenticated_uid
@soap_faults
def append_upload_orig(self, uid, builddir, data):
self.app.pm.open_project(uid, builddir)
orig_fname = os.path.join(builddir, self.app.pm.get_orig_fname(uid))
# Now append to File
fp = open(orig_fname, "ab")
fp.write(binascii.a2b_base64(data))
fp.close()
@rpc(String)
@authenticated_uid
@soap_faults
def finish_upload_orig(self, uid, builddir):
# If we support more than one orig, we need to put the orig_files into
# some list here.
# We still need the notion of a "current" orig during file upload.
pass
@rpc(String)
@authenticated_uid
@soap_faults
def reset_project(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.db.reset_project(builddir, True)
@rpc(String)
@authenticated_uid
@soap_faults
def del_project(self, uid, builddir):
self.app.pm.del_project(uid, builddir)
@rpc(String, String, _returns=String)
@authenticated_uid
@soap_faults
def create_project(self, uid, xml, url_validation):
with NamedTemporaryFile() as fp:
fp.write(binascii.a2b_base64(xml))
fp.flush()
prjid = self.app.pm.create_project(
uid, fp.name, url_validation=url_validation)
return prjid
@rpc(_returns=String)
@authenticated_uid
@soap_faults
def new_project(self, uid):
return self.app.pm.new_project(uid)
@rpc(String, _returns=String)
@authenticated_uid
@soap_faults
def get_project_busy(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
ret, msg = self.app.pm.current_project_is_busy(uid)
if not msg and not ret:
return 'ELBE-FINISH'
return msg
@rpc()
@authenticated_admin
@soap_faults
def shutdown_initvm(self):
# pylint: disable=no-self-use
system("systemctl --no-block poweroff")
@rpc(String)
@authenticated_uid
@soap_faults
def rm_log(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
self.app.pm.rm_log(uid)
@rpc(String, _returns=String)
@authenticated_uid
@soap_faults
def list_packages(self, uid, builddir):
self.app.pm.open_project(uid, builddir)
s = ''
for _, _, filenames in os.walk(
os.path.join(builddir, "repo/pool/main")):
for filename in fnmatch.filter(filenames, '*.deb'):
s += filename + '\n'
return s
@rpc(String, String)
@authenticated_uid
@soap_faults
def tar_prjrepo(self, uid, builddir, filename):
self.app.pm.open_project(uid, builddir)
with tarfile.open(os.path.join(builddir, filename), "w:gz") as tar:
tar.add(
os.path.join(
builddir, "repo"), arcname=os.path.basename(
os.path.join(
builddir, "repo")))
@rpc(String, String)
@authenticated_uid
@soap_faults
def include_package(self, uid, builddir, filename):
self.app.pm.open_project(uid, builddir)
self.app.pm.add_deb_package(uid, filename)