From 72a3afd02bf5e4ba086198904a1a313aa61094c9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:18:07 +0200 Subject: [PATCH] Implement ynh_lxc_snapshot_exists --- lib/tests.sh | 4 ++-- lib/ynh_lxd | 23 +++++++++++++++++------ lib/ynh_lxd_package_check | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 6c9c0ca..753116b 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -126,7 +126,7 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { local _install_type="$(path_to_install_type $check_path)" local snapname="snap_${_install_type}install" - if ! ynh_lxc_snapshot_exists $snapname + if ! ynh_lxc_snapshot_exists --name=$LXC_NAME --snapname=$snapname then log_warning "Expected to find an existing snapshot $snapname but it doesn't exist yet .. will attempt to create it" ynh_lxc_snapshot_load snap0 \ @@ -370,7 +370,7 @@ TEST_INSTALL () { # Create the snapshot that'll be used by other tests later [ "$install_type" != "private" ] \ - && ! ynh_lxc_snapshot_exists $snapname \ + && ! ynh_lxc_snapshot_exists --name=$LXC_NAME --snapname=$snapname \ && log_debug "Create a snapshot after app install" \ && ynh_lxc_pc_snapshot_create --name=LXC_NAME --snapname=$snapname diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 822b0bd..b1892c7 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -161,12 +161,23 @@ ynh_lxc_swapfiles_clean () { lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' } -ynh_lxc_snapshot_exists () { - local snapname=$1 - lxc list --format json \ - | jq -e --arg LXC_NAME $LXC_NAME --arg snapname $snapname \ - '.[] | select(.name==$LXC_NAME) | .snapshots[] | select(.name==$snapname)' \ - >/dev/null +# Check if a snapshot exist for an LXC container +# +# usage: ynh_lxc_snapshot_exists --name=$LXC_NAME --snapname=--name=name --snapname=snapname +# | arg: -n, --name= - name of the LXC +# | arg: -s, --snapname= - name of the snapshot +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_snapshot_exists --name=$LXC_NAME --snapname=() { + # Declare an array to define the options of this helper. + local legacy_args=ns + local -A args_array=([n]=name= [s]=snapname=) + local name + local snapname + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null } ynh_lxc_snapshot_load () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index eb7a190..88e1027 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -260,7 +260,7 @@ ynh_lxc_pc_snapshot_create () { ynh_lxc_stop --name=$name # Check if the snapshot already exist - if ! ynh_lxc_snapshot_exists "$snapname" + if ! ynh_lxc_snapshot_exists --name=$name --snapname="$snapname" then log_info "(Creating snapshot $snapname ...)" lxc snapshot $name $snapname