-
Notifications
You must be signed in to change notification settings - Fork 5
/
nimbix.py
executable file
·196 lines (162 loc) · 6.91 KB
/
nimbix.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
#!/usr/bin/env python
import json
import subprocess
import os
import sys
import datetime
import shutil, errno
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-path', help = 'the config file path, default as config.json')
args = parser.parse_args()
def copyanything(src, dst):
try:
shutil.copytree(src, dst)
except OSError as exc: # python >2.5
if exc.errno == errno.ENOTDIR:
shutil.copy(src, dst)
else: raise
def list_tags() :
sys.exit("XRT and platform do NOT match! \
Available platform and XRT combination:\
\
Platform XRT Version OS Version\
alveo-u200 2018.3 /2019.1 / 2019.2 / 2020.1 Ubuntu 16.04 / Ubuntu 18.04 / CentOS\
alveo-u250 2018.3 /2019.1 / 2019.2 / 2020.1 Ubuntu 16.04 / Ubuntu 18.04 / CentOS\
alveo-u280 2019.2 / 2020.1 Ubuntu 16.04 / Ubuntu 18.04 / CentOS\
alveo-u50 2019.2 / 2020.1 Ubuntu 16.04 / Ubuntu 18.04 / CentOS")
if args.path:
with open(args.path) as d:
repos = json.load(d)
else:
with open('config.json') as d:
repos = json.load(d)
vendor = repos['vendor']
metadata = repos['metadata']
provisioners = repos['provisioners']
app_info = repos['app_info']
post_processors = repos['post_processors']
example_path = "examples/nimbix/"
if vendor != "nimbix":
sys.exit("Vendor is NOT supported! ")
with open(example_path+'AppDef.json.example') as d:
appdef = json.load(d)
if not metadata['app_name']:
sys.exit("Application name can NOT be empty!")
if not app_info['os_version']:
sys.exit("OS version can NOT be empty!")
if not app_info['xrt_version']:
sys.exit("XRT version can NOT be empty!")
if not app_info['platform']:
sys.exit("Platform can NOT be empty!")
if not post_processors['repository']:
sys.exit("Repository can NOT be empty!")
if not post_processors['tag']:
sys.exit("Tag can NOT be empty!")
internal = False
if "internal" in metadata and metadata['internal']:
internal = True
with open('spec.json') as d:
spec = json.load(d)
commands = []
labels = {}
# Xilinx Base Runtim Image Url
image_url = ""
target_platforms = []
if internal:
image_url = "xdock.xilinx.com/base_runtime:" + post_processors['tag'] + "-" + app_info['os_version']
else:
if app_info['os_version'] in spec['os_version']:
if app_info['xrt_version'] in spec['os_version'][app_info['os_version']]['xrt_version']:
image_url = "xilinx/xilinx_runtime_base:" + "alveo" + "-" + app_info['xrt_version'] + "-" + app_info['os_version']
for platform in app_info['platform']:
if platform in spec['os_version'][app_info['os_version']]['xrt_version'][app_info['xrt_version']]['platform']:
target_platforms.append(spec['os_version'][app_info['os_version']]['xrt_version'][app_info['xrt_version']]['platform'][platform])
if platform == "alveo-u50" and app_info['xrt_version'] == "2019.2":
image_url += "-u50"
commands.append("ENV INTERNAL_BUILD=1")
else:
print(" [Warning] Invalide platform: " + platform)
if not image_url:
list_tags()
dockerfile_example = example_path + ("Dockerfile_Centos.example" if app_info['os_version'] == "centos" else "Dockerfile_Ubuntu.example")
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
path = "build_history/" + timestamp
try:
os.mkdir(path)
shutil.copy(example_path+'help.html.example', path + "/help.html")
shutil.copy(example_path+'xilinx_runtime.sh.example', path + "/xilinx_runtime.sh")
except OSError:
sys.exit("[Error]: Can NOT create folder " + path)
for pro in provisioners:
ctype = pro['type']
if ctype == 'shell':
commands.append("RUN " + " && ".join(pro['inline']))
elif ctype == 'file':
if not os.path.exists(pro['source']):
sys.exit(pro['source'] + " does NOT exists!")
filename = os.path.basename(os.path.normpath(pro['destination']))
copyanything(pro['source'], path + "/" + filename)
commands.append("COPY " + filename + " " + pro['destination'])
elif ctype == 'label':
labels[pro['key']] = pro['value']
else:
print("Warning: Unknown type: " + ctype + "! ")
if "app_cover_image" in metadata:
app_cover_image = metadata["app_cover_image"]
if not os.path.exists(app_cover_image):
print("[Warning]: " + app_cover_image + " is not exists! ")
elif not app_cover_image.lower().endswith('.png'):
print("[Warning]: Cover image must be PNG image! Skip adding cover image! ")
else:
copyanything(app_cover_image, path + "/" + "screenshot.png")
commands.append("COPY screenshot.png /etc/NAE/screenshot.png")
commands.append("RUN chmod 644 /etc/NAE/screenshot.png")
if "app_license" in metadata:
app_license = metadata["app_license"]
if not os.path.exists(app_license):
print("[Warning]: " + app_license + " is not exists! ")
elif not app_license.lower().endswith('.txt'):
print("[Warning]: License file must be txt file! Skip adding license file! ")
else:
copyanything(app_license, path + "/" + "license.txt")
commands.append("COPY license.txt /etc/NAE/license.txt")
with open(dockerfile_example, "r") as f:
s = f.read()
s = s.replace("__from_image__", image_url)
with open(path + "/Dockerfile", "w") as d:
d.write(s)
for command in commands:
d.write(command + "\n")
if labels:
label_str = 'LABEL '
for key in labels:
label_str += key + '="' + labels[key] + '" '
d.write(label_str + "\n")
appdef['name'] = metadata['app_name']
appdef['description'] = metadata['app_description']
if not metadata['desktop_mode']:
del appdef['commands']['server']
if not metadata['batch_mode']:
del appdef['commands']['batch']
appdef["machines"] = metadata["machines"]
for target_platform in target_platforms:
if target_platform not in appdef['machines']:
appdef['machines'].append(target_platform)
with open(path + '/AppDef.json', "w") as d:
json.dump(appdef, d, indent=4)
#Build application
print("Build docker image: " + post_processors['repository'] + ":" + post_processors["tag"])
subprocess.check_call(
"docker build -t " + post_processors['repository'] + ":" + post_processors["tag"] + " " + path,
stderr=subprocess.STDOUT, shell=True)
if post_processors['push_after_build']:
print("docker push " + post_processors['repository'] + ":" + post_processors["tag"])
subprocess.check_call("docker push " + post_processors['repository'] + ":" + post_processors["tag"],
stderr=subprocess.STDOUT, shell=True)
else:
print("Push docker image by running:")
print(" docker push " + post_processors['repository'] + ":" + post_processors["tag"])
print("Build history: " + path)
print("Build successfully!")
exit(0)