From 41f0704d772320dad5c4dce96e849bcb1a49b864 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 5 Nov 2022 19:48:07 +0100 Subject: [PATCH] Few fixes --- lib/tests.sh | 4 +- lib/tests_coordination.sh | 1 + lib/ynh_lxd | 77 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 7 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index e167a14..3b9a9e0 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -132,7 +132,7 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 \ && _PREINSTALL \ && _INSTALL_APP "path=$check_path" \ - && ynh_lxc_pc_snapshot_create --name=LXC_NAME --snapname=$snapname + && ynh_lxc_pc_snapshot_create --name=$LXC_NAME --snapname=$snapname else # Or uses an existing snapshot log_info "(Reusing existing snapshot $snapname)" \ @@ -372,7 +372,7 @@ TEST_INSTALL () { [ "$install_type" != "private" ] \ && ! 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 + && ynh_lxc_pc_snapshot_create --name=$LXC_NAME --snapname=$snapname # Remove and reinstall the application _REMOVE_APP \ diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index dfd1961..61f7d7d 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -3,6 +3,7 @@ source lib/ynh_lxd source lib/ynh_lxd_package_check source lib/tests.sh +source /usr/share/yunohost/helpers readonly complete_log="./Complete-${WORKER_ID}.log" diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 22b1a09..0025bd1 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -178,6 +178,37 @@ ynh_lxc_run_inside () { lxc exec $name -- /bin/bash -c "$command" } +# Check an LXC container can start +# +# usage: ynh_lxc_check_container_start --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_check_container_start () { + # 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 "$@" + + ynh_print_info --message="Test du conteneur $name" + ynh_lxc_start --name=$name # Démarre le conteneur + + wait_period=0 + while ! ynh_lxc_is_started --name=$name + do + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 20 ];then + break + else + sleep 5 + fi + done + + ynh_lxc_is_started --name=$name +} + # Restart a container # # usage: _ynh_lxc_restart_container --name=name @@ -293,18 +324,16 @@ ynh_lxc_launch (){ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ - -c limits.cpu.allowance=80% \ - >>/proc/self/fd/3 + -c limits.cpu.allowance=80% | tee -a /proc/self/fd/3 # Check if we can launch container from a local image elif lxc image list $image | grep -q -w $image; then lxc launch $image $name \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ - -c limits.cpu.allowance=80% \ - >>/proc/self/fd/3 + -c limits.cpu.allowance=80% | tee -a /proc/self/fd/3 else - log_critical "Can't find base image $image, run ./package_check.sh --rebuild" + log_critical "Can't find base image $image" fi } @@ -472,6 +501,44 @@ ynh_lxc_snapshot_load () { fi } +# Clone an LXC container +# +# usage: ynh_lxc_clone --source=source --destination=destination +# | arg: -s, --source= - source LXC +# | arg: -d, --destination= - destination LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_clone () { + # Declare an array to define the options of this helper. + local legacy_args=sd + local -A args_array=([s]=source= [d]=destination=) + local source + local destination + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_exists --name=$destination + then + ynh_print_info --message="Deleting LXC container $destination" + ynh_secure_remove --file="/var/lib/lxd/snapshots/$destination/snap0.tar.gz" + ynh_lxc_reset --name=$destination + fi + + ynh_print_info --message="Cloning LXC container from $source to $destination" + lxc copy "$source" "$destination" + + ynh_lxc_check_container_start --name=$destination + STATUS=$? + if [ $STATUS -eq 1 ]; then + ynh_print_info --message="LXC container $destination is broken." + else + ynh_print_info --message=" LXC container $destination is working." + ynh_print_info --message= "Creating snapshot of LXC container $destination" + ynh_lxc_snapshot_create --name="$destination" --snapname="snap0" + fi + return $STATUS +} + # Reset an LXC container # # usage: ynh_lxc_reset --name=name