-
Notifications
You must be signed in to change notification settings - Fork 5
/
on_premise.py
executable file
·137 lines (114 loc) · 4.59 KB
/
on_premise.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
#!/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']
provisioners = repos['provisioners']
app_info = repos['app_info']
post_processors = repos['post_processors']
if "metadata" in repos:
metadata = repos['metadata']
if vendor != "on_premise":
sys.exit("Vendor is NOT supported! ")
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!")
with open('spec.json') as d:
spec = json.load(d)
# Xilinx Base Runtim Image Url
image_url = ""
target_platforms = []
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()
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
path = "build_history/" + timestamp
try:
os.mkdir(path)
except OSError:
sys.exit("[Error]: Can NOT create folder " + path)
commands = []
labels = {}
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 + "! ")
with open(path + "/Dockerfile", "w") as d:
d.write("From " + image_url + "\n")
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")
if metadata and "entrypoint" in metadata:
d.write("ENTRYPOINT " + metadata['entrypoint'])
#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)
print("Build history: " + path)
print("Build successfully!")
exit(0)