-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoconf_imagebuilder.py
88 lines (73 loc) · 3.21 KB
/
autoconf_imagebuilder.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
import json
import logging
import generate
import subprocess
from pathlib import Path
autoconf_template_file = "99-random-pws-autoconf"
preinstalled_packages = ["luci"]
class AutoconfImageBuilder:
def __init__(self, args) -> None:
self.openwrt_profile = args.profile
self.imagebuilder_path = Path("imagebuilder")
self.out_path = Path(args.output, self.openwrt_profile)
def build_openwrt_image(self):
ssid = generate.ssid()
wifi_password = generate.password(16)
root_password = generate.password(8)
self.outfilename_without_extension = f"openwrt_{ssid}"
self.out_path.mkdir(parents=True, exist_ok=True)
self._generate_uci_defaults_file(ssid, wifi_password, root_password)
self._imagebuilder_run_make()
self._generate_metadata_file(ssid, wifi_password, root_password)
self._rename_and_move_image()
def _generate_uci_defaults_file(self, ssid, wifi_password, root_password):
uci_defaults_path = self.imagebuilder_path / "files" / "etc" / "uci-defaults"
uci_defaults_path.mkdir(parents=True, exist_ok=True)
with open(autoconf_template_file, "r") as template_file:
template = template_file.read()
autoconf_content = template.format(
ssid=ssid, wifi_password=wifi_password, root_password=root_password
)
autoconf_file = uci_defaults_path / autoconf_template_file
with open(autoconf_file, "w") as autoconf:
autoconf.write(autoconf_content)
def _imagebuilder_run_make(self):
try:
subprocess.run(
[
"make",
"image",
"-j8",
f"PROFILE={self.openwrt_profile}",
f"PACKAGES={' '.join(preinstalled_packages)}",
"FILES=files",
],
cwd=self.imagebuilder_path,
check=True,
)
except subprocess.CalledProcessError:
logging.error("Error while running make command in imagebuilder directory")
raise
def _generate_metadata_file(self, ssid, wifi_password, root_password):
metadata = {
"ssid": ssid,
"wifi_password": wifi_password,
"root_password": root_password,
}
metadata_file = self.out_path / f"{self.outfilename_without_extension}.json"
with open(metadata_file, "w") as file:
json.dump(metadata, file, indent=4)
logging.info(f"Metadata file generated: {metadata_file}")
def _rename_and_move_image(self):
targets_path = self.imagebuilder_path / "bin" / "targets"
# Find file matching "openwrt*{self.openwrt_profile}*squashfs-sysupgrade.bin"
# recursively in subdirectories to avoid needing to specify the target
# as additional command line parameter
sysupgrade_file = next(
targets_path.glob(
f"**/openwrt*{self.openwrt_profile}*squashfs-sysupgrade.bin"
)
)
new_image_file = self.out_path / f"{self.outfilename_without_extension}.bin"
sysupgrade_file.rename(new_image_file)
logging.info(f"Image file generated: {new_image_file}")