-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (61 loc) · 2.15 KB
/
main.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
import sys
import os
import yaml
import subprocess
from jinja2 import Environment, FileSystemLoader
class iso_profile:
def __init__(self, name, values):
"""
values is dict from config.yaml
"""
self.name = name
self.root_device = values["root_device"]
self.grub_device = self.root_device
self.root_size = values["root_size"]
try:
self.serial = values["serial"]
except KeyError:
pass
# /dev/sda1 or /dev/nvme0n1p1
if self.root_device.startswith("/dev/sd"):
self.root_partition = f"{self.root_device}1"
elif self.root_device.startswith("/dev/nvme"):
self.root_partition = f"{self.root_device}p1"
else:
raise ValueError("unable to determine root_partition")
def build(self):
print(f"building {self.name}")
file_loader = FileSystemLoader("src/")
env = Environment(loader=file_loader)
for f in os.listdir("src"):
file_path = os.path.join("src", f)
if os.path.isfile(file_path) and file_path.endswith(".nix"):
template = env.get_template(f)
output = template.render(vals=self)
output_file_path = f"artifacts/{f}"
with open(output_file_path, "w") as out:
out.write(output)
result = subprocess.run(
["nix-build", "artifacts"], capture_output=True, text=True
)
if result.returncode != 0:
print(result.stdout)
print(result.stderr)
raise ValueError
result = subprocess.run(f"cp result/iso/*.iso {self.name}.iso", shell=True)
if result.returncode != 0:
raise ValueError
os.chmod(f"{self.name}.iso", 0o644)
print(f"{self.name} finished")
def main():
try:
os.mkdir("artifacts")
except FileExistsError:
pass
with open("config.yaml", "r") as f:
yam = yaml.safe_load(f)
for profile_name, values in yam.items():
profile = iso_profile(profile_name, values)
profile.build()
if __name__ == "__main__":
sys.exit(main())