Cleanup jail if lxc download script fails

This commit is contained in:
Jip-Hop 2024-07-09 12:48:36 +02:00
parent be51af67dc
commit 29cb1ee77e
1 changed files with 29 additions and 25 deletions

View File

@ -870,10 +870,7 @@ def run_lxc_download_script(
stat_chmod(lxc_download_script, 0o700) stat_chmod(lxc_download_script, 0o700)
check_exit_code = False
if None not in [jail_name, jail_path, jail_rootfs_path, distro, release]: if None not in [jail_name, jail_path, jail_rootfs_path, distro, release]:
check_exit_code = True
cmd = [ cmd = [
lxc_download_script, lxc_download_script,
f"--name={jail_name}", f"--name={jail_name}",
@ -883,27 +880,35 @@ def run_lxc_download_script(
f"--dist={distro}", f"--dist={distro}",
f"--release={release}", f"--release={release}",
] ]
if rc := subprocess.run(cmd, env={"LXC_CACHE_PATH": lxc_cache}).returncode != 0:
eprint("Aborting...")
return rc
else: else:
# List images
cmd = [lxc_download_script, "--list", f"--arch={arch}"] cmd = [lxc_download_script, "--list", f"--arch={arch}"]
p1 = subprocess.Popen( p1 = subprocess.Popen(
cmd, stdout=subprocess.PIPE, env={"LXC_CACHE_PATH": lxc_cache} cmd, stdout=subprocess.PIPE, env={"LXC_CACHE_PATH": lxc_cache}
) )
for line in iter(p1.stdout.readline, b""): for line in iter(p1.stdout.readline, b""):
line = line.decode().strip() line = line.decode().strip()
# Filter out the known incompatible distros # Filter out the known incompatible distros
if not re.match( if not re.match(
r"^(alpine|amazonlinux|busybox|devuan|funtoo|openwrt|plamo|voidlinux)\s", r"^(alpine|amazonlinux|busybox|devuan|funtoo|openwrt|plamo|voidlinux)\s",
line, line,
): ):
print(line) print(line)
rc = p1.wait()
# Currently --list will always return a non-zero exit code, even when listing the images was successful
# https://github.com/lxc/lxc/pull/4462
# Therefore we must currently return 0, to prevent aborting the interactive create process
p1.wait() # return rc
if check_exit_code and p1.returncode != 0:
eprint("Aborting...")
return p1.returncode
return 0 return 0
@ -1118,9 +1123,8 @@ def interactive_config():
input("Press Enter to continue...") input("Press Enter to continue...")
print() print()
returncode = run_lxc_download_script() if run_lxc_download_script() != 0:
if returncode != 0: fail("Failed to list images. Aborting...")
return returncode
print( print(
dedent( dedent(
@ -1358,10 +1362,10 @@ def create_jail(**kwargs):
# but we don't need it so we will remove it later # but we don't need it so we will remove it later
open(jail_config_path, "a").close() open(jail_config_path, "a").close()
returncode = run_lxc_download_script( if returncode := run_lxc_download_script(
jail_name, jail_path, jail_rootfs_path, distro, release jail_name, jail_path, jail_rootfs_path, distro, release
) ) != 0:
if returncode != 0: cleanup(jail_path)
return returncode return returncode
# Assuming the name of your jail is "myjail" # Assuming the name of your jail is "myjail"