Gentoo Archives: gentoo-commits

From: Matt Turner <mattst88@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:master commit in: catalyst/base/
Date: Sat, 02 Apr 2022 23:50:52
Message-Id: 1648943416.5be6069bcbd5a7fa3f114f28366597bc5ddbb891.mattst88@gentoo
1 commit: 5be6069bcbd5a7fa3f114f28366597bc5ddbb891
2 Author: Patrice Clement <monsieurp <AT> gentoo <DOT> org>
3 AuthorDate: Tue Mar 8 21:02:55 2022 +0000
4 Commit: Matt Turner <mattst88 <AT> gentoo <DOT> org>
5 CommitDate: Sat Apr 2 23:50:16 2022 +0000
6 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=5be6069b
7
8 catalyst: support 3 new options
9
10 * stage4/groups: create a a list of groups.
11 * stage4/users: create a list of users. users can also be added to
12 groups using the "foo.bar=wheel,audio,baz" format.
13 * stage4/ssh_public_keys: copy an SSH public key into the stage4 user's home
14 (.ssh/authorized_keys) and set the file permission to 0644.
15
16 Bug: https://bugs.gentoo.org/236905
17 Signed-off-by: Patrice Clement <monsieurp <AT> gentoo.org>
18 Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>
19
20 catalyst/base/stagebase.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++
21 1 file changed, 70 insertions(+)
22
23 diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py
24 index de1e30ef..76feb5f0 100644
25 --- a/catalyst/base/stagebase.py
26 +++ b/catalyst/base/stagebase.py
27 @@ -201,6 +201,9 @@ class StageBase(TargetBase, ClearBase, GenBase):
28 self.set_packages()
29 self.set_rm()
30 self.set_linuxrc()
31 + self.set_groups()
32 + self.set_users()
33 + self.set_ssh_public_keys()
34 self.set_busybox_config()
35 self.set_overlay()
36 self.set_repos()
37 @@ -583,6 +586,39 @@ class StageBase(TargetBase, ClearBase, GenBase):
38 self.settings[self.settings["spec_prefix"] + "/linuxrc"]
39 del self.settings[self.settings["spec_prefix"] + "/linuxrc"]
40
41 + def set_groups(self):
42 + groups = self.settings["spec_prefix"] + "/groups"
43 + if groups in self.settings:
44 + if isinstance(self.settings[groups], str):
45 + self.settings["groups"] = self.settings[groups].split(",")
46 + self.settings["groups"] = self.settings[groups]
47 + del self.settings[groups]
48 + else:
49 + self.settings["groups"] = []
50 + log.info('groups to create: %s' % self.settings["groups"])
51 +
52 + def set_users(self):
53 + users = self.settings["spec_prefix"] + "/users"
54 + if users in self.settings:
55 + if isinstance(self.settings[users], str):
56 + self.settings["users"] = self.settings[users].split(",")
57 + self.settings["users"] = self.settings[users]
58 + del self.settings[users]
59 + else:
60 + self.settings["users"] = []
61 + log.info('users to create: %s' % self.settings["users"])
62 +
63 + def set_ssh_public_keys(self):
64 + ssh_public_keys = self.settings["spec_prefix"] + "/ssh_public_keys"
65 + if ssh_public_keys in self.settings:
66 + if isinstance(self.settings[ssh_public_keys], str):
67 + self.settings["ssh_public_keys"] = self.settings[ssh_public_keys].split(",")
68 + self.settings["ssh_public_keys"] = self.settings[ssh_public_keys]
69 + del self.settings[ssh_public_keys]
70 + else:
71 + self.settings["ssh_public_keys"] = []
72 + log.info('ssh public keys to copy: %s' % self.settings["ssh_public_keys"])
73 +
74 def set_busybox_config(self):
75 if self.settings["spec_prefix"] + "/busybox_config" in self.settings:
76 if isinstance(self.settings[self.settings['spec_prefix'] + '/busybox_config'], str):
77 @@ -894,6 +930,40 @@ class StageBase(TargetBase, ClearBase, GenBase):
78 cmd(['rsync', '-a', x + '/', self.settings['stage_path']],
79 env=self.env)
80
81 + def groups(self):
82 + for x in self.settings["groups"].split():
83 + log.notice("Creating group: '%s'", x)
84 + cmd(["groupadd", "-R", self.settings['chroot_path'], x], env=self.env)
85 +
86 + def users(self):
87 + for x in self.settings["users"]:
88 + usr, grp = '', ''
89 + try:
90 + usr, grp = x.split("=")
91 + except ValueError:
92 + usr = x
93 + log.debug("users: '=' separator not found on line " + x)
94 + log.debug("users: missing separator means no groups found")
95 + uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", x]
96 + if grp != '':
97 + uacmd = ["useradd", "-R", self.settings['chroot_path'], "-m", "-G", grp, usr]
98 + log.notice("Creating user: '%s'", f"{usr}={grp}")
99 + cmd(uacmd, env=self.env)
100 +
101 + def ssh_public_keys(self):
102 + for x in self.settings["ssh_public_keys"]:
103 + usr, pub_key_src = '', ''
104 + try:
105 + usr, pub_key_src = x.split("=")
106 + except ValueError:
107 + raise CatalystError(f"ssh_public_keys: '=' separator not found on line {x}")
108 + log.notice("Copying SSH public key for user: '%s'", usr)
109 + pub_key_dest = self.settings['chroot_path'] + f"/home/{usr}/.ssh/authorized_keys"
110 + cpcmd = ["cp", "-av", pub_key_src, pub_key_dest]
111 + cmd(cpcmd, env=self.env)
112 + chcmd = ["chmod", "0644", pub_key_dest]
113 + cmd(chcmd, env=self.env)
114 +
115 def bind(self):
116 for x in [x for x in self.mount if self.mount[x]['enable']]:
117 if str(self.mount[x]['source']) == 'config':