Implement ynh_lxc_snapshot_exists

This commit is contained in:
yalh76 2022-10-24 01:18:07 +02:00
parent d3883f6b69
commit 72a3afd02b
3 changed files with 20 additions and 9 deletions

View file

@ -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

View file

@ -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 () {

View file

@ -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