-
Notifications
You must be signed in to change notification settings - Fork 4
/
sandbox.py
69 lines (53 loc) · 1.85 KB
/
sandbox.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
# Make a sandbox for running a command.
# Sandbox could be: a baserock chroot, for the time being.
# Image layout: /rootfs, /manifest
import json
import os
import shutil
import subprocess
import sys
import tarfile
import tempfile
def appc_manifest_for_command(command):
'''Fake an appc manifest.'''
manifest = {
'acKind': 'ImageManifest',
'acVersion': '0.5.2',
'name': 'temp/temp1',
'labels': [],
'app': {
'exec': command,
'user': 'root',
'group': 'root',
'workingDirectory': '/temp.build',
}
}
return json.dumps(manifest)
def make_sandbox_for_command(command, source_tar, target,
actool='/home/shared/baserock/appc-spec/actool/actool'):
'''Fake an appc image.
This is a dumb idea, because you have to unpack a tar, create a tar, then
unpack it again to run it.
Better to have the executor take manifest and rootfs separately.
'''
tempdir = tempfile.mkdtemp()
try:
manifest_path = os.path.join(tempdir, 'manifest')
rootfs_path = os.path.join(tempdir, 'rootfs')
with open(manifest_path, 'w') as f:
f.write(appc_manifest_for_command(command))
os.mkdir(rootfs_path)
# FIXME: You've probably got to run this as root.
with tarfile.TarFile(source_tar, 'r') as tf:
tf.extractall(path=rootfs_path)
subprocess.check_call(
[actool, 'build', tempdir, target],
stdout=sys.stdout,
stderr=sys.stderr)
print 'Created %s' % target
finally:
shutil.rmtree(tempdir)
make_sandbox_for_command(
command=['/bin/sh', '-c', '"echo foo && exit 1"'],
source_tar='/home/shared/baserock-chroot-src/definitions/baserock-minimal.tar',
target='/home/shared/baserock/baserock-minimal.aci')