image/image: tolerate pub-key copying to fail during image build

This commit is contained in:
InsanePrawn 2024-03-20 20:56:17 +01:00 committed by Prawn
parent a176fad05a
commit a28550825f
2 changed files with 10 additions and 3 deletions

View file

@ -335,6 +335,7 @@ def install_rootfs(
copy_ssh_keys( copy_ssh_keys(
chroot, chroot,
user=user, user=user,
allow_fail=True,
) )
files = { files = {
'etc/pacman.conf': get_base_distro(arch).get_pacman_conf( 'etc/pacman.conf': get_base_distro(arch).get_pacman_conf(

View file

@ -85,7 +85,7 @@ def find_ssh_keys():
return 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']) check_programs_wrap(['ssh-keygen'])
ssh_dir_relative = os.path.join('/home', user, '.ssh') ssh_dir_relative = os.path.join('/home', user, '.ssh')
ssh_dir = chroot.get_path(ssh_dir_relative) ssh_dir = chroot.get_path(ssh_dir_relative)
@ -134,7 +134,13 @@ def copy_ssh_keys(chroot: Chroot, user: str):
continue continue
if not os.path.exists(ssh_dir): 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) chroot.run_cmd(["mkdir", "-p", "-m", "700", ssh_dir_relative], switch_user=user)
logging.info(f"Writing SSH pub keys to {authorized_keys_file}") 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