a lot: profiles, some more help strings. partial: exceptions instead of exit()

Signed-off-by: InsanePrawn <insane.prawny@gmail.com>
This commit is contained in:
InsanePrawn 2021-09-29 02:00:59 +02:00
parent e705af21f5
commit f09deaa9a5
9 changed files with 190 additions and 106 deletions

View file

@ -69,6 +69,22 @@ def create_chroot(
return chroot_path
def run_chroot_cmd(
script: str,
chroot_name,
chroot_base_path: str = None,
):
chroot_path = get_chroot_path(chroot_name, override_basepath=chroot_base_path)
result = subprocess.run([
'arch-chroot',
chroot_path,
'/bin/bash',
'-c',
script,
])
return result
def create_chroot_user(
chroot_name,
chroot_base_path: str = None,
@ -76,23 +92,18 @@ def create_chroot_user(
password='123456',
groups=['network', 'video', 'audio', 'optical', 'storage', 'input', 'scanner', 'games', 'lp', 'rfkill', 'wheel'],
):
chroot_path = get_chroot_path(chroot_name, override_basepath=chroot_base_path)
install_script = f'''
set -e
if ! id -u "{user}" >/dev/null 2>&1; then
useradd -m {user}
fi
usermod -a -G {",".join(groups)} {user}
echo "{user}:{password}" | chpasswd
chown {user}:{user} /home/{user} -R
'''
result = subprocess.run([
'arch-chroot',
chroot_path,
'/bin/bash',
'-c',
install_script,
])
if password:
install_script += f'echo "{user}:{password}" | chpasswd'
else:
install_script += 'passwd'
result = run_chroot_cmd(install_script, chroot_name=chroot_name, chroot_base_path=chroot_base_path)
if result.returncode != 0:
logging.fatal('Failed to setup user')
exit(1)
raise Exception('Failed to setup user')