From 587b240dcc1c978e5ab10863ef41a493a964c4ad Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:26:47 +0200 Subject: [PATCH] Implement ynh_lxc_reset --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd | 28 ++++++++++++++++++++-------- package_check.sh | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 79115ef..dfd1961 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -254,7 +254,7 @@ run_all_tests() { # Reset and create a fresh container to work with check_lxd_setup - ynh_lxc_reset + ynh_lxc_reset --name=$LXC_NAME ynh_lxc_pc_create --image=$LXC_BASE --name=$LXC_NAME # Be sure that the container is running ynh_lxc_pc_exec --name=$LXC_NAME --command="true" diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 388a68c..7867cf7 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -208,20 +208,32 @@ ynh_lxc_snapshot_load () { _ynh_lxc_start_and_wait --name=$name } +# Reset an LXC container +# +# usage: ynh_lxc_reset --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_reset () { + # Declare an array to define the options of this helper. + local legacy_args=n + local -A args_array=([n]=name=) + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + # If the container exists - if lxc info $LXC_NAME >/dev/null 2>/dev/null; then + if lxc info $name >/dev/null 2>/dev/null; then # Remove swap files before deletting the continer - ynh_lxc_swapfiles_clean --name=$LXC_NAME + ynh_lxc_swapfiles_clean --name=$name fi - ynh_lxc_stop --name=$LXC_NAME + ynh_lxc_stop --name=$name - if lxc info $LXC_NAME >/dev/null 2>/dev/null; then - local current_storage=$(lxc list $LXC_NAME --format json --columns b | jq '.[].expanded_devices.root.pool') - swapoff "$(lxc storage get $current_storage source)/containers/$LXC_NAME/rootfs/swap" 2>/dev/null + if lxc info $name >/dev/null 2>/dev/null; then + local current_storage=$(lxc list $name --format json --columns b | jq '.[].expanded_devices.root.pool') + swapoff "$(lxc storage get $current_storage source)/containers/$name/rootfs/swap" 2>/dev/null fi - lxc delete $LXC_NAME --force 2>/dev/null + lxc delete $name --force 2>/dev/null } - diff --git a/package_check.sh b/package_check.sh index c98931f..c4130c7 100755 --- a/package_check.sh +++ b/package_check.sh @@ -124,7 +124,7 @@ parse_args function cleanup() { trap '' SIGINT # Disable ctrl+c in this function - ynh_lxc_reset + ynh_lxc_reset --name=$LXC_NAME [ -n "$TEST_CONTEXT" ] && rm -rf "$TEST_CONTEXT" [ -n "$lock_file" ] && rm -f "$lock_file"