-
Notifications
You must be signed in to change notification settings - Fork 109
/
org.osbuild.ostree.passwd
executable file
·65 lines (49 loc) · 2.53 KB
/
org.osbuild.ostree.passwd
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
#!/usr/bin/python3
import contextlib
import os
import sys
import osbuild.api
from osbuild.util import ostree
SUBUID_PATH = "etc/subuid"
SUBGID_PATH = "etc/subgid"
# pylint: disable=too-many-statements
def main(tree, inputs, _options):
source_root, refs = ostree.parse_input_commits(inputs["commits"])
assert refs, "Need at least one commit"
assert len(refs) == 1, "Only one commit is currently supported"
os.makedirs(os.path.join(tree, "etc"), exist_ok=True)
subuids = ostree.SubIdsDB()
subgids = ostree.SubIdsDB()
# Only once ref (commit) is currently supported, so this loop will run exactly once
for commit, data in refs.items():
ref = data.get("path", commit).lstrip("/")
checkout_root = os.path.join(source_root, ref)
# Merge /usr/etc/passwd with /usr/lib/passwd from the checkout and store it in the buildroot
# "tree" directory. Entries in /usr/etc/passwd have a precedence, but the file does not
# necessarily exist.
passwd = ostree.PasswdLike.from_file(os.path.join(checkout_root, "usr/etc/passwd"), allow_missing_file=True)
passwd.merge_with_file(os.path.join(checkout_root, "usr/lib/passwd"), allow_missing_file=False)
passwd.dump_to_file(os.path.join(tree, "etc/passwd"))
# Merge /usr/etc/group with /usr/lib/group from the checkout and store it in the buildroot
# "tree" directory. Entries in /usr/etc/group have a precedence, but the file does not
# necessarily exist.
passwd = ostree.PasswdLike.from_file(os.path.join(checkout_root, "usr/etc/group"), allow_missing_file=True)
passwd.merge_with_file(os.path.join(checkout_root, "usr/lib/group"), allow_missing_file=False)
passwd.dump_to_file(os.path.join(tree, "etc/group"))
# Merge /etc/sub{g,u}id with /usr/etc/sub{g,u}id from the checkout and store it in the buildroot
with contextlib.suppress(FileNotFoundError):
subuids.read_from(os.path.join(checkout_root, "usr", SUBUID_PATH))
with contextlib.suppress(FileNotFoundError):
subgids.read_from(os.path.join(checkout_root, "usr", SUBGID_PATH))
# If we have entries in the subordinate id files, write them to the tree
if subuids:
subuids.write_to(os.path.join(tree, SUBUID_PATH))
if subgids:
subgids.write_to(os.path.join(tree, SUBGID_PATH))
return 0
if __name__ == '__main__':
stage_args = osbuild.api.arguments()
r = main(stage_args["tree"],
stage_args["inputs"],
stage_args["options"])
sys.exit(r)