From a28550825f89673635e36456da06c5a23c43ece8 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Wed, 20 Mar 2024 20:56:17 +0100 Subject: [PATCH] image/image: tolerate pub-key copying to fail during image build --- image/image.py | 1 + net/ssh.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/image/image.py b/image/image.py index afb3ddb..6532af7 100644 --- a/image/image.py +++ b/image/image.py @@ -335,6 +335,7 @@ def install_rootfs( copy_ssh_keys( chroot, user=user, + allow_fail=True, ) files = { 'etc/pacman.conf': get_base_distro(arch).get_pacman_conf( diff --git a/net/ssh.py b/net/ssh.py index cf1ed37..6eb7294 100644 --- a/net/ssh.py +++ b/net/ssh.py @@ -85,7 +85,7 @@ def find_ssh_keys(): return keys -def copy_ssh_keys(chroot: Chroot, user: str): +def copy_ssh_keys(chroot: Chroot, user: str, allow_fail: bool = False): check_programs_wrap(['ssh-keygen']) ssh_dir_relative = os.path.join('/home', user, '.ssh') ssh_dir = chroot.get_path(ssh_dir_relative) @@ -134,7 +134,13 @@ def copy_ssh_keys(chroot: Chroot, user: str): continue if not os.path.exists(ssh_dir): - logging.info(f"Creating {ssh_dir_relative} dir in chroot {chroot.path}") + logging.info(f"Creating {ssh_dir_relative!r} dir in chroot {chroot.path!r}") chroot.run_cmd(["mkdir", "-p", "-m", "700", ssh_dir_relative], switch_user=user) logging.info(f"Writing SSH pub keys to {authorized_keys_file}") - write_file(authorized_keys_file, "\n".join(auth_key_lines), user=chroot.get_uid(user), mode="644") + try: + write_file(authorized_keys_file, "\n".join(auth_key_lines), user=str(chroot.get_uid(user)), mode="644") + except Exception as ex: + logging.error(f"Failed to write SSH authorized_keys_file at {authorized_keys_file!r}:", exc_info=ex) + if allow_fail: + return + raise ex from ex