forked from omec-project/bess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer_build.py
executable file
·117 lines (84 loc) · 2.75 KB
/
container_build.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
#!/usr/bin/env python
import sys
import subprocess
import os
import os.path
import time
IMAGE = 'nefelinetworks/bess_build:latest' + os.getenv('TAG_SUFFIX', '')
BESS_DIR_HOST = os.path.dirname(os.path.abspath(__file__))
BESS_DIR_CONTAINER = '/build/bess'
BUILD_SCRIPT = './build.py'
def run_cmd(cmd):
proc = subprocess.Popen(cmd, shell=True)
proc.communicate()
if proc.returncode:
print >> sys.stderr, 'Error has occured running host command: %s' % cmd
sys.exit(proc.returncode)
def shell_quote(cmd):
return "'" + cmd.replace("'", "'\\''") + "'"
def run_docker_cmd(cmd):
run_cmd('docker run -e CXX -e DEBUG -e SANITIZE --rm -t ' \
'-u %d:%d -v %s:%s %s sh -c %s' %
(os.getuid(), os.getgid(), BESS_DIR_HOST, BESS_DIR_CONTAINER,
IMAGE, shell_quote(cmd)))
def run_shell():
run_cmd('docker run -e CXX -e DEBUG -e SANITIZE --rm -it -v %s:%s %s' %
(BESS_DIR_HOST, BESS_DIR_CONTAINER, IMAGE))
def build_bess():
run_docker_cmd('%s bess' % BUILD_SCRIPT)
def build_kmod():
subprocess.check_output('uname -r', shell=True).strip()
try:
run_docker_cmd('%s kmod' % BUILD_SCRIPT)
except:
print >> sys.stderr, '*** module build has failed.'
def build_kmod_buildtest():
kernels_to_test = '/lib/modules/*/build'
kmod_build = 'KERNELDIR=$0 %s kmod' % BUILD_SCRIPT
run_docker_cmd('ls -x -d %s | xargs -n 1 sh -c %s' %
(kernels_to_test, shell_quote(kmod_build)))
def build_all():
build_bess()
build_kmod()
def do_clean():
run_docker_cmd('%s clean' % BUILD_SCRIPT)
def do_dist_clean():
run_docker_cmd('%s dist_clean' % BUILD_SCRIPT)
def print_usage():
print >> sys.stderr, \
'Usage: %s ' \
'[all|bess|kmod|kmod_buildtest|clean|dist_clean|shell||help]' % \
sys.argv[0]
def main():
os.chdir(BESS_DIR_HOST)
if len(sys.argv) == 1:
build_bess()
build_kmod()
elif len(sys.argv) == 2:
if sys.argv[1] == 'all':
build_all()
elif sys.argv[1] == 'bess':
build_bess()
elif sys.argv[1] == 'kmod':
build_kmod()
elif sys.argv[1] == 'kmod_buildtest':
build_kmod_buildtest()
elif sys.argv[1] == 'clean':
do_clean()
elif sys.argv[1] == 'dist_clean':
do_dist_clean()
elif sys.argv[1] == 'shell':
run_shell()
elif sys.argv[1] == 'help':
print_usage()
sys.exit(0)
else:
print >> sys.stderr, 'Error - unknown command "%s".' % sys.argv[1]
print_usage()
sys.exit(2)
else:
print_usage()
sys.exit(2)
if __name__ == '__main__':
main()
print 'Done.'