-
Notifications
You must be signed in to change notification settings - Fork 77
/
build.py
251 lines (196 loc) · 7.62 KB
/
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
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
import urllib.request
import json
from pathlib import Path
import re
from shutil import rmtree
import subprocess
import logging
from rq import get_current_job
from .common import get_packages_hash, verify_usign, get_file_hash
log = logging.getLogger("rq.worker")
log.setLevel(logging.DEBUG)
def build(request: dict):
"""Build image request and setup ImageBuilders automatically
The `request` dict contains properties of the requested image.
Args:
request (dict): Contains all properties of requested image
"""
if "packages" in request:
assert isinstance(
request["packages"], set
), "packages must be type set not list"
else:
request["packages"] = set()
assert (request["store_path"]).is_dir(), "store_path must be existing directory"
job = get_current_job()
log.debug(f"Building {request}")
cache = (request["cache_path"] / request["version"] / request["target"]).parent
target, subtarget = request["target"].split("/")
sums_file = Path(cache / f"{subtarget}_sums")
sig_file = Path(cache / f"{subtarget}_sums.sig")
def setup_ib():
"""Setup ImageBuilder based on `request`
This function downloads and verifies the ImageBuilder archive. Existing
setups are automatically updated if newer version are available
upstream.
"""
log.debug("Setting up ImageBuilder")
if (cache / subtarget).is_dir():
rmtree(cache / subtarget)
download_file("sha256sums.sig", sig_file)
download_file("sha256sums", sums_file)
assert verify_usign(
sig_file, sums_file, request["version_data"]["pubkey"]
), "Bad signature for cheksums"
# openwrt-imagebuilder-ath79-generic.Linux-x86_64.tar.xz
ib_search = re.search(
r"^(.{64}) \*(openwrt-imagebuilder-.+?\.Linux-x86_64\.tar\.xz)$",
sums_file.read_text(),
re.MULTILINE,
)
assert ib_search, "No ImageBuilder in checksums found"
ib_hash, ib_archive = ib_search.groups()
download_file(ib_archive)
assert ib_hash == get_file_hash(
cache / ib_archive
), "Wrong ImageBuilder archive checksum"
(cache / subtarget).mkdir(parents=True, exist_ok=True)
extract_archive = subprocess.run(
["tar", "--strip-components=1", "-xf", ib_archive, "-C", subtarget],
cwd=cache,
)
assert not extract_archive.returncode, "Extracting ImageBuilder archive failed"
log.debug(f"Extracted TAR {ib_archive}")
(cache / ib_archive).unlink()
extra_repos = request["version_data"].get("extra_repos")
if extra_repos:
log.debug("Found extra repos")
repos_path = cache / subtarget / "repositories.conf"
repos = repos_path.read_text()
for name, repo in extra_repos.items():
repos += f"\nsrc/gz {name} {repo}"
repos_path.write_text(repos)
log.debug(f"Repos:\n{repos}")
def download_file(filename: str, dest: str = None):
"""Download file from upstream target path
The URL points automatically to the targets folder upstream
Args:
filename (str): File in upstream target folder
dest (str): Optional path to store the file, default to target
cache folder
"""
log.debug(f"Downloading {filename}")
urllib.request.urlretrieve(
request["upstream_url"]
+ "/"
+ request["version_data"]["path"]
+ "/targets/"
+ request["target"]
+ "/"
+ filename,
dest or (cache / filename),
)
cache.mkdir(parents=True, exist_ok=True)
stamp_file = cache / f"{subtarget}_stamp"
sig_file_headers = urllib.request.urlopen(
request["upstream_url"]
+ "/"
+ request["version_data"]["path"]
+ "/targets/"
+ request["target"]
+ "/sha256sums.sig"
).info()
log.debug(f"sig_file_headers: \n{sig_file_headers}")
origin_modified = sig_file_headers.get("Last-Modified")
log.info("Origin %s", origin_modified)
if stamp_file.is_file():
local_modified = stamp_file.read_text()
log.info("Local %s", local_modified)
else:
local_modified = ""
if origin_modified != local_modified:
log.debug("New ImageBuilder upstream available")
setup_ib()
stamp_file.write_text(origin_modified)
if request.get("diff_packages", False) and request.get("packages"):
info_run = subprocess.run(
["make", "info"], text=True, capture_output=True, cwd=cache / subtarget
)
default_packages = set(
re.search(r"Default Packages: (.*)\n", info_run.stdout).group(1).split()
)
profile_packages = set(
re.search(
r"{}:\n .+\n Packages: (.+?)\n".format(request["profile"]),
info_run.stdout,
re.MULTILINE,
)
.group(1)
.split()
)
remove_packages = (default_packages | profile_packages) - request["packages"]
request["packages"] = request["packages"] | set(
map(lambda p: f"-{p}", remove_packages)
)
manifest_run = subprocess.run(
[
"make",
"manifest",
f"PROFILE={request['profile']}",
f"PACKAGES={' '.join(request['packages'])}",
],
text=True,
capture_output=True,
cwd=cache / subtarget,
)
if manifest_run.returncode:
log.error(f"Manifest stdout {manifest_run.stdout}")
log.error(f"Manifest stderr {manifest_run.stderr}")
manifest = dict(map(lambda pv: pv.split(" - "), manifest_run.stdout.splitlines()))
manifest_packages = manifest.keys()
log.debug(f"Manifest Packages: {manifest_packages}")
packages_hash = get_packages_hash(manifest_packages)
log.debug(f"Packages Hash {packages_hash}")
bin_dir = (
Path(request["version"])
/ request["target"]
/ request["profile"]
/ packages_hash
)
(request["store_path"] / bin_dir).mkdir(parents=True, exist_ok=True)
image_build = subprocess.run(
[
"make",
"image",
f"PROFILE={request['profile']}",
f"PACKAGES={' '.join(request['packages'])}",
f"EXTRA_IMAGE_NAME={packages_hash}",
f"BIN_DIR={request['store_path'] / bin_dir}",
],
text=True,
capture_output=True,
cwd=cache / subtarget,
)
(request["store_path"] / bin_dir / "buildlog.txt").write_text(
f"### STDOUT\n\n{image_build.stdout}\n\n### STDERR\n\n{image_build.stderr}"
)
# check if running as job or within pytest
if job:
job.meta["bin_dir"] = str(bin_dir)
job.meta["buildlog"] = True
job.save_meta()
if image_build.returncode:
log.error(f"Build stdout {image_build.stdout}")
log.error(f"Build stderr {image_build.stderr}")
assert not image_build.returncode, "ImageBuilder failed"
json_file = Path(request["store_path"] / bin_dir / "profiles.json")
assert json_file.is_file(), "Image built but no profiles.json file created"
json_content = json.loads(json_file.read_text())
assert (
request["profile"] in json_content["profiles"]
), "Requested profile not in created profiles.json"
json_content.update({"manifest": manifest})
json_content.update(json_content["profiles"][request["profile"]])
json_content["id"] = request["profile"]
json_content.pop("profiles")
return json_content