From 775d3540e1ade32696999957ec1919b9120ab8d3 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 23 Oct 2022 23:51:39 +0200 Subject: [PATCH 01/51] Reorder helper files --- lib/tests_coordination.sh | 4 +- lib/witness.sh | 90 -------------------------- lib/ynh_lxd | 6 ++ lib/{lxc.sh => ynh_lxd_package_check} | 91 ++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 93 deletions(-) delete mode 100644 lib/witness.sh create mode 100644 lib/ynh_lxd rename lib/{lxc.sh => ynh_lxd_package_check} (69%) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 4e90f56..8b391cb 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -1,8 +1,8 @@ #!/bin/bash -source lib/lxc.sh +source lib/ynh_lxd +source lib/ynh_lxd_package_check source lib/tests.sh -source lib/witness.sh readonly complete_log="./Complete-${WORKER_ID}.log" diff --git a/lib/witness.sh b/lib/witness.sh deleted file mode 100644 index ea31590..0000000 --- a/lib/witness.sh +++ /dev/null @@ -1,90 +0,0 @@ - -set_witness_files () { - # Create files to check if the remove script does not remove them accidentally - log_debug "Create witness files..." - - create_witness_file () { - [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - RUN_INSIDE_LXC $action $1 - } - - # Nginx conf - create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file - create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file - - # /etc - create_witness_file "/etc/witnessfile" file - - # /opt directory - create_witness_file "/opt/witnessdir" directory - - # /var/www directory - create_witness_file "/var/www/witnessdir" directory - - # /home/yunohost.app/ - create_witness_file "/home/yunohost.app/witnessdir" directory - - # /var/log - create_witness_file "/var/log/witnessfile" file - - # Config fpm - create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file - - # Config logrotate - create_witness_file "/etc/logrotate.d/witnessfile" file - - # Config systemd - create_witness_file "/etc/systemd/system/witnessfile.service" file - - # Database - RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 - echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 -} - -check_witness_files () { - # Check all the witness files, to verify if them still here - - check_file_exist () { - if RUN_INSIDE_LXC test ! -e "$1" - then - log_error "The file $1 is missing ! Something gone wrong !" - SET_RESULT "failure" witness - fi - } - - # Nginx conf - check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" - - # /etc - check_file_exist "/etc/witnessfile" - - # /opt directory - check_file_exist "/opt/witnessdir" - - # /var/www directory - check_file_exist "/var/www/witnessdir" - - # /home/yunohost.app/ - check_file_exist "/home/yunohost.app/witnessdir" - - # /var/log - check_file_exist "/var/log/witnessfile" - - # Config fpm - check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" - - # Config logrotate - check_file_exist "/etc/logrotate.d/witnessfile" - - # Config systemd - check_file_exist "/etc/systemd/system/witnessfile.service" - - # Database - if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 - then - log_error "The database witnessdb is missing ! Something gone wrong !" - SET_RESULT "failure" witness - return 1 - fi -} diff --git a/lib/ynh_lxd b/lib/ynh_lxd new file mode 100644 index 0000000..6f59efb --- /dev/null +++ b/lib/ynh_lxd @@ -0,0 +1,6 @@ +#!/bin/bash + +#================================================= +# LXD HELPERS +#================================================= + diff --git a/lib/lxc.sh b/lib/ynh_lxd_package_check similarity index 69% rename from lib/lxc.sh rename to lib/ynh_lxd_package_check index f52ab43..a1f16c4 100644 --- a/lib/lxc.sh +++ b/lib/ynh_lxd_package_check @@ -1,7 +1,7 @@ #!/bin/bash #================================================= -# RUNNING SNAPSHOT +# PACKAGE_CHECK HELPERS #================================================= LXC_CREATE () { @@ -221,3 +221,92 @@ RUN_INSIDE_LXC() { lxc exec $LXC_NAME -- "$@" } +set_witness_files () { + # Create files to check if the remove script does not remove them accidentally + log_debug "Create witness files..." + + create_witness_file () { + [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" + RUN_INSIDE_LXC $action $1 + } + + # Nginx conf + create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file + create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file + + # /etc + create_witness_file "/etc/witnessfile" file + + # /opt directory + create_witness_file "/opt/witnessdir" directory + + # /var/www directory + create_witness_file "/var/www/witnessdir" directory + + # /home/yunohost.app/ + create_witness_file "/home/yunohost.app/witnessdir" directory + + # /var/log + create_witness_file "/var/log/witnessfile" file + + # Config fpm + create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file + + # Config logrotate + create_witness_file "/etc/logrotate.d/witnessfile" file + + # Config systemd + create_witness_file "/etc/systemd/system/witnessfile.service" file + + # Database + RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 + echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 +} + +check_witness_files () { + # Check all the witness files, to verify if them still here + + check_file_exist () { + if RUN_INSIDE_LXC test ! -e "$1" + then + log_error "The file $1 is missing ! Something gone wrong !" + SET_RESULT "failure" witness + fi + } + + # Nginx conf + check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + + # /etc + check_file_exist "/etc/witnessfile" + + # /opt directory + check_file_exist "/opt/witnessdir" + + # /var/www directory + check_file_exist "/var/www/witnessdir" + + # /home/yunohost.app/ + check_file_exist "/home/yunohost.app/witnessdir" + + # /var/log + check_file_exist "/var/log/witnessfile" + + # Config fpm + check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + + # Config logrotate + check_file_exist "/etc/logrotate.d/witnessfile" + + # Config systemd + check_file_exist "/etc/systemd/system/witnessfile.service" + + # Database + if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 + then + log_error "The database witnessdb is missing ! Something gone wrong !" + SET_RESULT "failure" witness + return 1 + fi +} From 26530dd3de0ec19053c7a44125b3a48f0fccdcb7 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 23 Oct 2022 23:54:40 +0200 Subject: [PATCH 02/51] Spaces to tabs --- lib/ynh_lxd_package_check | 326 +++++++++++++++++++------------------- 1 file changed, 163 insertions(+), 163 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index a1f16c4..2f8c987 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -5,138 +5,138 @@ #================================================= LXC_CREATE () { - log_info "Launching new LXC $LXC_NAME ..." - # Check if we can launch container from YunoHost remote image - if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then - lxc launch yunohost:$LXC_BASE $LXC_NAME \ - -c security.nesting=true \ - -c security.privileged=true \ - -c limits.memory=80% \ - -c limits.cpu.allowance=80% \ - >>/proc/self/fd/3 - # Check if we can launch container from a local image - elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then - lxc launch $LXC_BASE $LXC_NAME \ - -c security.nesting=true \ - -c security.privileged=true \ - -c limits.memory=80% \ - -c limits.cpu.allowance=80% \ - >>/proc/self/fd/3 - else - log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" - fi - - pipestatus="${PIPESTATUS[0]}" - location=$(lxc list --format json | jq -e --arg LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"') - [[ "$location" != "none" ]] && log_info "... on $location" + log_info "Launching new LXC $LXC_NAME ..." + # Check if we can launch container from YunoHost remote image + if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then + lxc launch yunohost:$LXC_BASE $LXC_NAME \ + -c security.nesting=true \ + -c security.privileged=true \ + -c limits.memory=80% \ + -c limits.cpu.allowance=80% \ + >>/proc/self/fd/3 + # Check if we can launch container from a local image + elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then + lxc launch $LXC_BASE $LXC_NAME \ + -c security.nesting=true \ + -c security.privileged=true \ + -c limits.memory=80% \ + -c limits.cpu.allowance=80% \ + >>/proc/self/fd/3 + else + log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" + fi + + pipestatus="${PIPESTATUS[0]}" + location=$(lxc list --format json | jq -e --arg LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"') + [[ "$location" != "none" ]] && log_info "... on $location" - [[ "$pipestatus" -eq 0 ]] || exit 1 + [[ "$pipestatus" -eq 0 ]] || exit 1 - _LXC_START_AND_WAIT $LXC_NAME - set_witness_files - lxc snapshot $LXC_NAME snap0 + _LXC_START_AND_WAIT $LXC_NAME + set_witness_files + lxc snapshot $LXC_NAME snap0 } 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 + 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 } CREATE_LXC_SNAPSHOT () { - # Create a temporary snapshot + # Create a temporary snapshot - local snapname=$1 + local snapname=$1 - start_timer + start_timer - # Check all the witness files, to verify if them still here - check_witness_files >&2 + # Check all the witness files, to verify if them still here + check_witness_files >&2 - # Remove swap files to avoid killing the CI with huge snapshots. - CLEAN_SWAPFILES - - LXC_STOP $LXC_NAME + # Remove swap files to avoid killing the CI with huge snapshots. + CLEAN_SWAPFILES + + LXC_STOP $LXC_NAME - # Check if the snapshot already exist - if ! LXC_SNAPSHOT_EXISTS "$snapname" - then - log_info "(Creating snapshot $snapname ...)" - lxc snapshot $LXC_NAME $snapname - fi + # Check if the snapshot already exist + if ! LXC_SNAPSHOT_EXISTS "$snapname" + then + log_info "(Creating snapshot $snapname ...)" + lxc snapshot $LXC_NAME $snapname + fi - _LXC_START_AND_WAIT $LXC_NAME + _LXC_START_AND_WAIT $LXC_NAME - stop_timer 1 + stop_timer 1 } LOAD_LXC_SNAPSHOT () { - local snapname=$1 - log_debug "Loading snapshot $snapname ..." + local snapname=$1 + log_debug "Loading snapshot $snapname ..." - # Remove swap files before restoring the snapshot. - CLEAN_SWAPFILES + # Remove swap files before restoring the snapshot. + CLEAN_SWAPFILES - LXC_STOP $LXC_NAME + LXC_STOP $LXC_NAME - lxc restore $LXC_NAME $snapname - lxc start $LXC_NAME - _LXC_START_AND_WAIT $LXC_NAME + lxc restore $LXC_NAME $snapname + lxc start $LXC_NAME + _LXC_START_AND_WAIT $LXC_NAME } #================================================= LXC_EXEC () { - # Start the lxc container and execute the given command in it - local cmd=$1 + # Start the lxc container and execute the given command in it + local cmd=$1 - _LXC_START_AND_WAIT $LXC_NAME + _LXC_START_AND_WAIT $LXC_NAME - start_timer + start_timer - # Execute the command given in argument in the container and log its results. - lxc exec $LXC_NAME --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$cmd" | tee -a "$complete_log" $current_test_log + # Execute the command given in argument in the container and log its results. + lxc exec $LXC_NAME --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$cmd" | tee -a "$complete_log" $current_test_log - # Store the return code of the command - local returncode=${PIPESTATUS[0]} + # Store the return code of the command + local returncode=${PIPESTATUS[0]} - log_debug "Return code: $returncode" + log_debug "Return code: $returncode" - stop_timer 1 - # Return the exit code of the ssh command - return $returncode + stop_timer 1 + # Return the exit code of the ssh command + return $returncode } LXC_STOP () { - local container_to_stop=$1 - # (We also use timeout 30 in front of the command because sometime lxc - # commands can hang forever despite the --timeout >_>...) - timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null + local container_to_stop=$1 + # (We also use timeout 30 in front of the command because sometime lxc + # commands can hang forever despite the --timeout >_>...) + timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null - # If the command times out, then add the option --force - if [ $? -eq 124 ]; then - timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null - fi + # If the command times out, then add the option --force + if [ $? -eq 124 ]; then + timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null + fi } LXC_RESET () { - # If the container exists - if lxc info $LXC_NAME >/dev/null 2>/dev/null; then - # Remove swap files before deletting the continer - CLEAN_SWAPFILES - fi + # If the container exists + if lxc info $LXC_NAME >/dev/null 2>/dev/null; then + # Remove swap files before deletting the continer + CLEAN_SWAPFILES + fi - LXC_STOP $LXC_NAME + LXC_STOP $LXC_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 - fi + 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 + fi - lxc delete $LXC_NAME --force 2>/dev/null + lxc delete $LXC_NAME --force 2>/dev/null } @@ -144,7 +144,7 @@ _LXC_START_AND_WAIT() { restart_container() { - LXC_STOP $1 + LXC_STOP $1 lxc start "$1" } @@ -198,115 +198,115 @@ _LXC_START_AND_WAIT() { # Fail if the container failed to start if [ $i -eq $max_try ] && [ $failstart -eq 1 ] then - log_error "The container miserably failed to start or to connect to the internet" - lxc info --show-log $1 + log_error "The container miserably failed to start or to connect to the internet" + lxc info --show-log $1 return 1 fi done - LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") + LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") } CLEAN_SWAPFILES() { - # Restart it if needed - if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then - lxc start $LXC_NAME - _LXC_START_AND_WAIT $LXC_NAME - fi - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' + # Restart it if needed + if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then + lxc start $LXC_NAME + _LXC_START_AND_WAIT $LXC_NAME + fi + lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' + lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' } RUN_INSIDE_LXC() { - lxc exec $LXC_NAME -- "$@" + lxc exec $LXC_NAME -- "$@" } set_witness_files () { - # Create files to check if the remove script does not remove them accidentally - log_debug "Create witness files..." + # Create files to check if the remove script does not remove them accidentally + log_debug "Create witness files..." - create_witness_file () { - [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - RUN_INSIDE_LXC $action $1 - } + create_witness_file () { + [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" + RUN_INSIDE_LXC $action $1 + } - # Nginx conf - create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file - create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file + # Nginx conf + create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file + create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file - # /etc - create_witness_file "/etc/witnessfile" file + # /etc + create_witness_file "/etc/witnessfile" file - # /opt directory - create_witness_file "/opt/witnessdir" directory + # /opt directory + create_witness_file "/opt/witnessdir" directory - # /var/www directory - create_witness_file "/var/www/witnessdir" directory + # /var/www directory + create_witness_file "/var/www/witnessdir" directory - # /home/yunohost.app/ - create_witness_file "/home/yunohost.app/witnessdir" directory + # /home/yunohost.app/ + create_witness_file "/home/yunohost.app/witnessdir" directory - # /var/log - create_witness_file "/var/log/witnessfile" file + # /var/log + create_witness_file "/var/log/witnessfile" file - # Config fpm - create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file + # Config fpm + create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file - # Config logrotate - create_witness_file "/etc/logrotate.d/witnessfile" file + # Config logrotate + create_witness_file "/etc/logrotate.d/witnessfile" file - # Config systemd - create_witness_file "/etc/systemd/system/witnessfile.service" file + # Config systemd + create_witness_file "/etc/systemd/system/witnessfile.service" file - # Database - RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 - echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 + # Database + RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 + echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 } check_witness_files () { - # Check all the witness files, to verify if them still here + # Check all the witness files, to verify if them still here - check_file_exist () { - if RUN_INSIDE_LXC test ! -e "$1" - then - log_error "The file $1 is missing ! Something gone wrong !" - SET_RESULT "failure" witness - fi - } + check_file_exist () { + if RUN_INSIDE_LXC test ! -e "$1" + then + log_error "The file $1 is missing ! Something gone wrong !" + SET_RESULT "failure" witness + fi + } - # Nginx conf - check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + # Nginx conf + check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" - # /etc - check_file_exist "/etc/witnessfile" + # /etc + check_file_exist "/etc/witnessfile" - # /opt directory - check_file_exist "/opt/witnessdir" + # /opt directory + check_file_exist "/opt/witnessdir" - # /var/www directory - check_file_exist "/var/www/witnessdir" + # /var/www directory + check_file_exist "/var/www/witnessdir" - # /home/yunohost.app/ - check_file_exist "/home/yunohost.app/witnessdir" + # /home/yunohost.app/ + check_file_exist "/home/yunohost.app/witnessdir" - # /var/log - check_file_exist "/var/log/witnessfile" + # /var/log + check_file_exist "/var/log/witnessfile" - # Config fpm - check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + # Config fpm + check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" - # Config logrotate - check_file_exist "/etc/logrotate.d/witnessfile" + # Config logrotate + check_file_exist "/etc/logrotate.d/witnessfile" - # Config systemd - check_file_exist "/etc/systemd/system/witnessfile.service" + # Config systemd + check_file_exist "/etc/systemd/system/witnessfile.service" - # Database - if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 - then - log_error "The database witnessdb is missing ! Something gone wrong !" - SET_RESULT "failure" witness - return 1 - fi + # Database + if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 + then + log_error "The database witnessdb is missing ! Something gone wrong !" + SET_RESULT "failure" witness + return 1 + fi } From 984e4c272032b27f7673534ed728356f599c4e50 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 23 Oct 2022 23:56:52 +0200 Subject: [PATCH 03/51] LXC_EXEC to ynh_lxc_pc_exec --- lib/tests.sh | 22 ++++++++++---------- lib/tests_coordination.sh | 8 +++---- lib/ynh_lxd_package_check | 44 +++++++++++++++++++-------------------- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 5685fdf..cfca38e 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -13,7 +13,7 @@ _RUN_YUNOHOST_CMD() { lxc file push -p -r "$package_path" $LXC_NAME/app_folder --quiet # --output-as none is to disable the json-like output for some commands like backup create - LXC_EXEC "yunohost --output-as none --debug $1" \ + ynh_lxc_pc_exec "yunohost --output-as none --debug $1" \ | grep --line-buffered -v --extended-regexp '^[0-9]+\s+.{1,15}DEBUG' \ | grep --line-buffered -v 'processing action' @@ -39,7 +39,7 @@ _PREINSTALL () { # Copy the pre-install script into the container. lxc file push "$preinstall_script" "$LXC_NAME/preinstall.sh" # Then execute the script to execute the pre-install commands. - LXC_EXEC "bash /preinstall.sh" + ynh_lxc_pc_exec "bash /preinstall.sh" fi } @@ -63,7 +63,7 @@ _PREUPGRADE () { # Copy the pre-upgrade script into the container. lxc file push "$preupgrade_script" "$LXC_NAME/preupgrade.sh" # Then execute the script to execute the pre-upgrade commands. - LXC_EXEC "bash /preupgrade.sh" + ynh_lxc_pc_exec "bash /preupgrade.sh" return $? fi } @@ -111,7 +111,7 @@ _INSTALL_APP () { local ret=$? [ $ret -eq 0 ] && log_debug "Installation successful." || log_error "Installation failed." - if LXC_EXEC "su nobody -s /bin/bash -c \"test -r /var/www/$app_id || test -w /var/www/$app_id || test -x /var/www/$app_id\"" + if ynh_lxc_pc_exec "su nobody -s /bin/bash -c \"test -r /var/www/$app_id || test -w /var/www/$app_id || test -x /var/www/$app_id\"" then log_error "It looks like anybody can read/enter /var/www/$app_id, which ain't super great from a security point of view ... Config files or other files may contain secrets or information that should in most case not be world-readable. You should remove all 'others' permissions with 'chmod o-rwx', and setup appropriate, exclusive permissions to the appropriate owner/group with chmod/chown." SET_RESULT "failure" install_dir_permissions @@ -212,7 +212,7 @@ _VALIDATE_THAT_APP_CAN_BE_ACCESSED () { log_debug "Running curl $check_domain$curl_check_path" # Call cURL to try to access to the URL of the app - LXC_EXEC "curl --location --insecure --silent --show-error \ + ynh_lxc_pc_exec "curl --location --insecure --silent --show-error \ --header 'Host: $check_domain' \ --resolve $DOMAIN:80:$LXC_IP \ --resolve $DOMAIN:443:$LXC_IP \ @@ -223,7 +223,7 @@ _VALIDATE_THAT_APP_CAN_BE_ACCESSED () { $check_domain$curl_check_path" \ > "$TEST_CONTEXT/curl_print" - LXC_EXEC "cat ./curl_output" > $curl_output + ynh_lxc_pc_exec "cat ./curl_output" > $curl_output # Analyze the result of curl command if [ $? -ne 0 ] @@ -284,12 +284,12 @@ Page extract:\n$page_extract" > $TEST_CONTEXT/curl_result # If we had a 50x error, try to display service info and logs to help debugging if [[ $curl_error -ne 0 ]] && echo "5" | grep -q "${http_code:0:1}" then - LXC_EXEC "systemctl --all" | grep "$app_id_to_check.*service" - for SERVICE in $(LXC_EXEC "systemctl -all" | grep -o "$app_id_to_check.*service") + ynh_lxc_pc_exec "systemctl --all" | grep "$app_id_to_check.*service" + for SERVICE in $(ynh_lxc_pc_exec "systemctl -all" | grep -o "$app_id_to_check.*service") do - LXC_EXEC "journalctl --no-pager --no-hostname -n 30 -u $SERVICE"; + ynh_lxc_pc_exec "journalctl --no-pager --no-hostname -n 30 -u $SERVICE"; done - LXC_EXEC "tail -v -n 15 \$(find /var/log/{nginx/,php*,$app_id_to_check} -mmin -3)" + ynh_lxc_pc_exec "tail -v -n 15 \$(find /var/log/{nginx/,php*,$app_id_to_check} -mmin -3)" fi done @@ -490,7 +490,7 @@ TEST_PORT_ALREADY_USED () { lxc file push $TEST_CONTEXT/netcat.service $LXC_NAME/etc/systemd/system/netcat.service # Then start this service to block this port. - LXC_EXEC "systemctl enable --now netcat" + ynh_lxc_pc_exec "systemctl enable --now netcat" _PREINSTALL diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 8b391cb..fb956be 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -257,13 +257,13 @@ run_all_tests() { LXC_RESET LXC_CREATE # Be sure that the container is running - LXC_EXEC "true" + ynh_lxc_pc_exec "true" # Print the version of YunoHost from the LXC container log_small_title "YunoHost versions" - LXC_EXEC "yunohost --version" - LXC_EXEC "yunohost --version --output-as json" | jq -r .yunohost.version >> $TEST_CONTEXT/ynh_version - LXC_EXEC "yunohost --version --output-as json" | jq -r .yunohost.repo >> $TEST_CONTEXT/ynh_branch + ynh_lxc_pc_exec "yunohost --version" + ynh_lxc_pc_exec "yunohost --version --output-as json" | jq -r .yunohost.version >> $TEST_CONTEXT/ynh_version + ynh_lxc_pc_exec "yunohost --version --output-as json" | jq -r .yunohost.repo >> $TEST_CONTEXT/ynh_branch echo $ARCH > $TEST_CONTEXT/architecture echo $app_id > $TEST_CONTEXT/app_id diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 2f8c987..8c707b1 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -4,6 +4,27 @@ # PACKAGE_CHECK HELPERS #================================================= +ynh_lxc_pc_exec () { + # Start the lxc container and execute the given command in it + local cmd=$1 + + _LXC_START_AND_WAIT $LXC_NAME + + start_timer + + # Execute the command given in argument in the container and log its results. + lxc exec $LXC_NAME --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$cmd" | tee -a "$complete_log" $current_test_log + + # Store the return code of the command + local returncode=${PIPESTATUS[0]} + + log_debug "Return code: $returncode" + + stop_timer 1 + # Return the exit code of the ssh command + return $returncode +} + LXC_CREATE () { log_info "Launching new LXC $LXC_NAME ..." # Check if we can launch container from YunoHost remote image @@ -86,29 +107,6 @@ LOAD_LXC_SNAPSHOT () { _LXC_START_AND_WAIT $LXC_NAME } -#================================================= - -LXC_EXEC () { - # Start the lxc container and execute the given command in it - local cmd=$1 - - _LXC_START_AND_WAIT $LXC_NAME - - start_timer - - # Execute the command given in argument in the container and log its results. - lxc exec $LXC_NAME --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$cmd" | tee -a "$complete_log" $current_test_log - - # Store the return code of the command - local returncode=${PIPESTATUS[0]} - - log_debug "Return code: $returncode" - - stop_timer 1 - # Return the exit code of the ssh command - return $returncode -} - LXC_STOP () { local container_to_stop=$1 # (We also use timeout 30 in front of the command because sometime lxc From 053f07b9e9183235480f1b353d7a14c3819dbbc2 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 23 Oct 2022 23:59:18 +0200 Subject: [PATCH 04/51] set_witness_files to ynh_lxc_pc_witness_files_set --- lib/ynh_lxd_package_check | 86 +++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 8c707b1..038866b 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -25,6 +25,48 @@ ynh_lxc_pc_exec () { return $returncode } +ynh_lxc_pc_witness_files_set () { + # Create files to check if the remove script does not remove them accidentally + log_debug "Create witness files..." + + create_witness_file () { + [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" + RUN_INSIDE_LXC $action $1 + } + + # Nginx conf + create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file + create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file + + # /etc + create_witness_file "/etc/witnessfile" file + + # /opt directory + create_witness_file "/opt/witnessdir" directory + + # /var/www directory + create_witness_file "/var/www/witnessdir" directory + + # /home/yunohost.app/ + create_witness_file "/home/yunohost.app/witnessdir" directory + + # /var/log + create_witness_file "/var/log/witnessfile" file + + # Config fpm + create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file + + # Config logrotate + create_witness_file "/etc/logrotate.d/witnessfile" file + + # Config systemd + create_witness_file "/etc/systemd/system/witnessfile.service" file + + # Database + RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 + echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 +} + LXC_CREATE () { log_info "Launching new LXC $LXC_NAME ..." # Check if we can launch container from YunoHost remote image @@ -54,7 +96,7 @@ LXC_CREATE () { [[ "$pipestatus" -eq 0 ]] || exit 1 _LXC_START_AND_WAIT $LXC_NAME - set_witness_files + ynh_lxc_pc_witness_files_set lxc snapshot $LXC_NAME snap0 } @@ -219,48 +261,6 @@ RUN_INSIDE_LXC() { lxc exec $LXC_NAME -- "$@" } -set_witness_files () { - # Create files to check if the remove script does not remove them accidentally - log_debug "Create witness files..." - - create_witness_file () { - [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - RUN_INSIDE_LXC $action $1 - } - - # Nginx conf - create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file - create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file - - # /etc - create_witness_file "/etc/witnessfile" file - - # /opt directory - create_witness_file "/opt/witnessdir" directory - - # /var/www directory - create_witness_file "/var/www/witnessdir" directory - - # /home/yunohost.app/ - create_witness_file "/home/yunohost.app/witnessdir" directory - - # /var/log - create_witness_file "/var/log/witnessfile" file - - # Config fpm - create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file - - # Config logrotate - create_witness_file "/etc/logrotate.d/witnessfile" file - - # Config systemd - create_witness_file "/etc/systemd/system/witnessfile.service" file - - # Database - RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 - echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 -} - check_witness_files () { # Check all the witness files, to verify if them still here From cdcd0b6765bcafca12734f8701a3373f1c3150cc Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:01:24 +0200 Subject: [PATCH 05/51] create_witness_file to ynh_lxc_pc_witness_file_create --- lib/ynh_lxd_package_check | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 038866b..23ab41f 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -25,42 +25,42 @@ ynh_lxc_pc_exec () { return $returncode } +ynh_lxc_pc_witness_file_create () { + [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" + RUN_INSIDE_LXC $action $1 +} + ynh_lxc_pc_witness_files_set () { # Create files to check if the remove script does not remove them accidentally log_debug "Create witness files..." - create_witness_file () { - [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - RUN_INSIDE_LXC $action $1 - } - # Nginx conf - create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file - create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file + ynh_lxc_pc_witness_file_create "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file + ynh_lxc_pc_witness_file_create "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file # /etc - create_witness_file "/etc/witnessfile" file + ynh_lxc_pc_witness_file_create "/etc/witnessfile" file # /opt directory - create_witness_file "/opt/witnessdir" directory + ynh_lxc_pc_witness_file_create "/opt/witnessdir" directory # /var/www directory - create_witness_file "/var/www/witnessdir" directory + ynh_lxc_pc_witness_file_create "/var/www/witnessdir" directory # /home/yunohost.app/ - create_witness_file "/home/yunohost.app/witnessdir" directory + ynh_lxc_pc_witness_file_create "/home/yunohost.app/witnessdir" directory # /var/log - create_witness_file "/var/log/witnessfile" file + ynh_lxc_pc_witness_file_create "/var/log/witnessfile" file # Config fpm - create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file + ynh_lxc_pc_witness_file_create "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file # Config logrotate - create_witness_file "/etc/logrotate.d/witnessfile" file + ynh_lxc_pc_witness_file_create "/etc/logrotate.d/witnessfile" file # Config systemd - create_witness_file "/etc/systemd/system/witnessfile.service" file + ynh_lxc_pc_witness_file_create "/etc/systemd/system/witnessfile.service" file # Database RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 From 7c8e2c7c29883138f88b367e857d13530aa9ca0a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:05:41 +0200 Subject: [PATCH 06/51] check_witness_files to ynh_lxc_pc_witness_files_check --- lib/tests.sh | 2 +- lib/ynh_lxd_package_check | 98 +++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index cfca38e..68c58fc 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -18,7 +18,7 @@ _RUN_YUNOHOST_CMD() { | grep --line-buffered -v 'processing action' returncode=${PIPESTATUS[0]} - check_witness_files && return $returncode || return 2 + ynh_lxc_pc_witness_files_check && return $returncode || return 2 } _PREINSTALL () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 23ab41f..263a2b4 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -67,6 +67,54 @@ ynh_lxc_pc_witness_files_set () { echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 } +ynh_lxc_pc_witness_files_check () { + # Check all the witness files, to verify if them still here + + check_file_exist () { + if RUN_INSIDE_LXC test ! -e "$1" + then + log_error "The file $1 is missing ! Something gone wrong !" + SET_RESULT "failure" witness + fi + } + + # Nginx conf + check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + + # /etc + check_file_exist "/etc/witnessfile" + + # /opt directory + check_file_exist "/opt/witnessdir" + + # /var/www directory + check_file_exist "/var/www/witnessdir" + + # /home/yunohost.app/ + check_file_exist "/home/yunohost.app/witnessdir" + + # /var/log + check_file_exist "/var/log/witnessfile" + + # Config fpm + check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + + # Config logrotate + check_file_exist "/etc/logrotate.d/witnessfile" + + # Config systemd + check_file_exist "/etc/systemd/system/witnessfile.service" + + # Database + if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 + then + log_error "The database witnessdb is missing ! Something gone wrong !" + SET_RESULT "failure" witness + return 1 + fi +} + LXC_CREATE () { log_info "Launching new LXC $LXC_NAME ..." # Check if we can launch container from YunoHost remote image @@ -116,7 +164,7 @@ CREATE_LXC_SNAPSHOT () { start_timer # Check all the witness files, to verify if them still here - check_witness_files >&2 + ynh_lxc_pc_witness_files_check >&2 # Remove swap files to avoid killing the CI with huge snapshots. CLEAN_SWAPFILES @@ -260,51 +308,3 @@ CLEAN_SWAPFILES() { RUN_INSIDE_LXC() { lxc exec $LXC_NAME -- "$@" } - -check_witness_files () { - # Check all the witness files, to verify if them still here - - check_file_exist () { - if RUN_INSIDE_LXC test ! -e "$1" - then - log_error "The file $1 is missing ! Something gone wrong !" - SET_RESULT "failure" witness - fi - } - - # Nginx conf - check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" - - # /etc - check_file_exist "/etc/witnessfile" - - # /opt directory - check_file_exist "/opt/witnessdir" - - # /var/www directory - check_file_exist "/var/www/witnessdir" - - # /home/yunohost.app/ - check_file_exist "/home/yunohost.app/witnessdir" - - # /var/log - check_file_exist "/var/log/witnessfile" - - # Config fpm - check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" - - # Config logrotate - check_file_exist "/etc/logrotate.d/witnessfile" - - # Config systemd - check_file_exist "/etc/systemd/system/witnessfile.service" - - # Database - if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 - then - log_error "The database witnessdb is missing ! Something gone wrong !" - SET_RESULT "failure" witness - return 1 - fi -} From f0ce0e572e2562bdebb6c855b20c98ca19232a57 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:08:12 +0200 Subject: [PATCH 07/51] LXC_CREATE to ynh_lxc_pc_create --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd_package_check | 38 +++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index fb956be..2b7eb07 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -255,7 +255,7 @@ run_all_tests() { # Reset and create a fresh container to work with check_lxd_setup LXC_RESET - LXC_CREATE + ynh_lxc_pc_create # Be sure that the container is running ynh_lxc_pc_exec "true" diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 263a2b4..d6928e3 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -67,44 +67,44 @@ ynh_lxc_pc_witness_files_set () { echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 } +ynh_lxc_pc_witness_file_check () { + if RUN_INSIDE_LXC test ! -e "$1" + then + log_error "The file $1 is missing ! Something gone wrong !" + SET_RESULT "failure" witness + fi +} + ynh_lxc_pc_witness_files_check () { # Check all the witness files, to verify if them still here - check_file_exist () { - if RUN_INSIDE_LXC test ! -e "$1" - then - log_error "The file $1 is missing ! Something gone wrong !" - SET_RESULT "failure" witness - fi - } - # Nginx conf - check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" # /etc - check_file_exist "/etc/witnessfile" + ynh_lxc_pc_witness_file_check "/etc/witnessfile" # /opt directory - check_file_exist "/opt/witnessdir" + ynh_lxc_pc_witness_file_check "/opt/witnessdir" # /var/www directory - check_file_exist "/var/www/witnessdir" + ynh_lxc_pc_witness_file_check "/var/www/witnessdir" # /home/yunohost.app/ - check_file_exist "/home/yunohost.app/witnessdir" + ynh_lxc_pc_witness_file_check "/home/yunohost.app/witnessdir" # /var/log - check_file_exist "/var/log/witnessfile" + ynh_lxc_pc_witness_file_check "/var/log/witnessfile" # Config fpm - check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" # Config logrotate - check_file_exist "/etc/logrotate.d/witnessfile" + ynh_lxc_pc_witness_file_check "/etc/logrotate.d/witnessfile" # Config systemd - check_file_exist "/etc/systemd/system/witnessfile.service" + ynh_lxc_pc_witness_file_check "/etc/systemd/system/witnessfile.service" # Database if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 @@ -115,7 +115,7 @@ ynh_lxc_pc_witness_files_check () { fi } -LXC_CREATE () { +ynh_lxc_pc_create () { log_info "Launching new LXC $LXC_NAME ..." # Check if we can launch container from YunoHost remote image if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then From 9f5cf5bdb605ef5b76caa8effd5f090c5973ce3c Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:09:39 +0200 Subject: [PATCH 08/51] CREATE_LXC_SNAPSHOT to ynh_lxc_pc_snapshot_create --- lib/tests.sh | 4 ++-- lib/ynh_lxd_package_check | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 68c58fc..ee56fc8 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -132,7 +132,7 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { LOAD_LXC_SNAPSHOT snap0 \ && _PREINSTALL \ && _INSTALL_APP "path=$check_path" \ - && CREATE_LXC_SNAPSHOT $snapname + && ynh_lxc_pc_snapshot_create $snapname else # Or uses an existing snapshot log_info "(Reusing existing snapshot $snapname)" \ @@ -372,7 +372,7 @@ TEST_INSTALL () { [ "$install_type" != "private" ] \ && ! LXC_SNAPSHOT_EXISTS $snapname \ && log_debug "Create a snapshot after app install" \ - && CREATE_LXC_SNAPSHOT $snapname + && ynh_lxc_pc_snapshot_create $snapname # Remove and reinstall the application _REMOVE_APP \ diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index d6928e3..fda68bd 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -156,7 +156,7 @@ LXC_SNAPSHOT_EXISTS() { >/dev/null } -CREATE_LXC_SNAPSHOT () { +ynh_lxc_pc_snapshot_create () { # Create a temporary snapshot local snapname=$1 From efb7367e2ec743690f2a5cd330649700e469c137 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:13:06 +0200 Subject: [PATCH 09/51] LXC_SNAPSHOT_EXISTS to ynh_lxc_snapshot_exists --- lib/tests.sh | 4 ++-- lib/ynh_lxd | 8 ++++++++ lib/ynh_lxd_package_check | 10 +--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index ee56fc8..90ce154 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 ! LXC_SNAPSHOT_EXISTS $snapname + if ! ynh_lxc_snapshot_exists $snapname then log_warning "Expected to find an existing snapshot $snapname but it doesn't exist yet .. will attempt to create it" LOAD_LXC_SNAPSHOT snap0 \ @@ -370,7 +370,7 @@ TEST_INSTALL () { # Create the snapshot that'll be used by other tests later [ "$install_type" != "private" ] \ - && ! LXC_SNAPSHOT_EXISTS $snapname \ + && ! ynh_lxc_snapshot_exists $snapname \ && log_debug "Create a snapshot after app install" \ && ynh_lxc_pc_snapshot_create $snapname diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 6f59efb..ddd52bc 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -4,3 +4,11 @@ # LXD HELPERS #================================================= +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 +} + diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index fda68bd..27241cc 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -148,14 +148,6 @@ ynh_lxc_pc_create () { lxc snapshot $LXC_NAME snap0 } -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 -} - ynh_lxc_pc_snapshot_create () { # Create a temporary snapshot @@ -172,7 +164,7 @@ ynh_lxc_pc_snapshot_create () { LXC_STOP $LXC_NAME # Check if the snapshot already exist - if ! LXC_SNAPSHOT_EXISTS "$snapname" + if ! ynh_lxc_snapshot_exists "$snapname" then log_info "(Creating snapshot $snapname ...)" lxc snapshot $LXC_NAME $snapname From 0a0ef64ed6dbe392e0d88121654270bc09150b8f Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:14:29 +0200 Subject: [PATCH 10/51] LOAD_LXC_SNAPSHOT to ynh_lxc_snapshot_load --- lib/tests.sh | 14 +++++++------- lib/ynh_lxd | 14 ++++++++++++++ lib/ynh_lxd_package_check | 14 -------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 90ce154..bb72799 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -129,14 +129,14 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { if ! ynh_lxc_snapshot_exists $snapname then log_warning "Expected to find an existing snapshot $snapname but it doesn't exist yet .. will attempt to create it" - LOAD_LXC_SNAPSHOT snap0 \ + ynh_lxc_snapshot_load snap0 \ && _PREINSTALL \ && _INSTALL_APP "path=$check_path" \ && ynh_lxc_pc_snapshot_create $snapname else # Or uses an existing snapshot log_info "(Reusing existing snapshot $snapname)" \ - && LOAD_LXC_SNAPSHOT $snapname + && ynh_lxc_snapshot_load $snapname fi } @@ -356,7 +356,7 @@ TEST_INSTALL () { [ "$install_type" = "private" ] && { start_test "Installation in private mode"; local is_public="0"; } local snapname=snap_${install_type}install - LOAD_LXC_SNAPSHOT snap0 + ynh_lxc_snapshot_load snap0 _PREINSTALL @@ -392,7 +392,7 @@ _TEST_MULTI_INSTANCE () { local check_path="$(default_install_path)" - LOAD_LXC_SNAPSHOT snap0 + ynh_lxc_snapshot_load snap0 log_small_title "First installation: path=$SUBDOMAIN$check_path" \ && _LOAD_SNAPSHOT_OR_INSTALL_APP "$check_path" \ @@ -436,7 +436,7 @@ TEST_UPGRADE () { cp -a "$package_path" "${package_path}_back" (cd "$package_path"; git checkout --force --quiet "$commit") - LOAD_LXC_SNAPSHOT snap0 + ynh_lxc_snapshot_load snap0 _PREINSTALL @@ -481,7 +481,7 @@ TEST_PORT_ALREADY_USED () { local check_port="$1" local check_path="$(default_install_path)" - LOAD_LXC_SNAPSHOT snap0 + ynh_lxc_snapshot_load snap0 # Build a service with netcat for use this port before the app. echo -e "[Service]\nExecStart=/bin/netcat -l -k -p $check_port\n @@ -570,7 +570,7 @@ TEST_BACKUP_RESTORE () { elif [ $j -eq 1 ] then - LOAD_LXC_SNAPSHOT snap0 + ynh_lxc_snapshot_load snap0 # Remove the previous residual backups RUN_INSIDE_LXC rm -rf /home/yunohost.backup/archives diff --git a/lib/ynh_lxd b/lib/ynh_lxd index ddd52bc..94e71dc 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -12,3 +12,17 @@ ynh_lxc_snapshot_exists () { >/dev/null } +ynh_lxc_snapshot_load () { + local snapname=$1 + log_debug "Loading snapshot $snapname ..." + + # Remove swap files before restoring the snapshot. + CLEAN_SWAPFILES + + LXC_STOP $LXC_NAME + + lxc restore $LXC_NAME $snapname + lxc start $LXC_NAME + _LXC_START_AND_WAIT $LXC_NAME +} + diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 27241cc..6c2d0ff 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -175,20 +175,6 @@ ynh_lxc_pc_snapshot_create () { stop_timer 1 } -LOAD_LXC_SNAPSHOT () { - local snapname=$1 - log_debug "Loading snapshot $snapname ..." - - # Remove swap files before restoring the snapshot. - CLEAN_SWAPFILES - - LXC_STOP $LXC_NAME - - lxc restore $LXC_NAME $snapname - lxc start $LXC_NAME - _LXC_START_AND_WAIT $LXC_NAME -} - LXC_STOP () { local container_to_stop=$1 # (We also use timeout 30 in front of the command because sometime lxc From 01a6a7572f0b1529118dd3c1a143459d319b365f Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:16:04 +0200 Subject: [PATCH 11/51] LXC_STOP to ynh_lxc_stop --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd | 15 ++++++++++++++- lib/ynh_lxd_package_check | 19 +++---------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 2b7eb07..07e1512 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -347,7 +347,7 @@ TEST_LAUNCHER () { # End the timer for the test stop_timer 2 - LXC_STOP $LXC_NAME + ynh_lxc_stop $LXC_NAME # Update the lock file with the date of the last finished test. # $$ is the PID of package_check itself. diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 94e71dc..7c8691e 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -4,6 +4,19 @@ # LXD HELPERS #================================================= +ynh_lxc_stop () { + local container_to_stop=$1 + # (We also use timeout 30 in front of the command because sometime lxc + # commands can hang forever despite the --timeout >_>...) + timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null + + # If the command times out, then add the option --force + if [ $? -eq 124 ]; then + timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null + fi + +} + ynh_lxc_snapshot_exists () { local snapname=$1 lxc list --format json \ @@ -19,7 +32,7 @@ ynh_lxc_snapshot_load () { # Remove swap files before restoring the snapshot. CLEAN_SWAPFILES - LXC_STOP $LXC_NAME + ynh_lxc_stop $LXC_NAME lxc restore $LXC_NAME $snapname lxc start $LXC_NAME diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 6c2d0ff..10001dc 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -161,7 +161,7 @@ ynh_lxc_pc_snapshot_create () { # Remove swap files to avoid killing the CI with huge snapshots. CLEAN_SWAPFILES - LXC_STOP $LXC_NAME + ynh_lxc_stop $LXC_NAME # Check if the snapshot already exist if ! ynh_lxc_snapshot_exists "$snapname" @@ -175,19 +175,6 @@ ynh_lxc_pc_snapshot_create () { stop_timer 1 } -LXC_STOP () { - local container_to_stop=$1 - # (We also use timeout 30 in front of the command because sometime lxc - # commands can hang forever despite the --timeout >_>...) - timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null - - # If the command times out, then add the option --force - if [ $? -eq 124 ]; then - timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null - fi - -} - LXC_RESET () { # If the container exists if lxc info $LXC_NAME >/dev/null 2>/dev/null; then @@ -195,7 +182,7 @@ LXC_RESET () { CLEAN_SWAPFILES fi - LXC_STOP $LXC_NAME + ynh_lxc_stop $LXC_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') @@ -210,7 +197,7 @@ _LXC_START_AND_WAIT() { restart_container() { - LXC_STOP $1 + ynh_lxc_stop $1 lxc start "$1" } From b134ed4f867f07c1dc90836e1a9c4299afbb3043 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:17:19 +0200 Subject: [PATCH 12/51] LXC_RESET to ynh_lxc_reset --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd | 17 +++++++++++++++++ lib/ynh_lxd_package_check | 17 ----------------- package_check.sh | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 07e1512..5706478 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 - LXC_RESET + ynh_lxc_reset ynh_lxc_pc_create # Be sure that the container is running ynh_lxc_pc_exec "true" diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 7c8691e..a216345 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -39,3 +39,20 @@ ynh_lxc_snapshot_load () { _LXC_START_AND_WAIT $LXC_NAME } +ynh_lxc_reset () { + # If the container exists + if lxc info $LXC_NAME >/dev/null 2>/dev/null; then + # Remove swap files before deletting the continer + CLEAN_SWAPFILES + fi + + ynh_lxc_stop $LXC_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 + fi + + lxc delete $LXC_NAME --force 2>/dev/null +} + diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 10001dc..cbb5b35 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -175,23 +175,6 @@ ynh_lxc_pc_snapshot_create () { stop_timer 1 } -LXC_RESET () { - # If the container exists - if lxc info $LXC_NAME >/dev/null 2>/dev/null; then - # Remove swap files before deletting the continer - CLEAN_SWAPFILES - fi - - ynh_lxc_stop $LXC_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 - fi - - lxc delete $LXC_NAME --force 2>/dev/null -} - _LXC_START_AND_WAIT() { diff --git a/package_check.sh b/package_check.sh index 27dc1c4..c98931f 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 - LXC_RESET + ynh_lxc_reset [ -n "$TEST_CONTEXT" ] && rm -rf "$TEST_CONTEXT" [ -n "$lock_file" ] && rm -f "$lock_file" From 17a2403c9dfdaf4b068787115c50833a6117fa84 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:18:41 +0200 Subject: [PATCH 13/51] _LXC_START_AND_WAIT to _ynh_lxc_start_and_wait --- lib/ynh_lxd | 70 +++++++++++++++++++++++++++++++++++- lib/ynh_lxd_package_check | 76 +++------------------------------------ 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index a216345..a550188 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -17,6 +17,74 @@ ynh_lxc_stop () { } + +_ynh_lxc_start_and_wait () { + + restart_container() + { + ynh_lxc_stop $1 + lxc start "$1" + } + + # Try to start the container 3 times. + local max_try=3 + local i=0 + while [ $i -lt $max_try ] + do + i=$(( i +1 )) + local failstart=0 + + # Wait for container to start, we are using systemd to check this, + # for the sake of brevity. + for j in $(seq 1 10); do + if lxc exec "$1" -- systemctl isolate multi-user.target >/dev/null 2>/dev/null; then + break + fi + + if [ "$j" == "10" ]; then + log_debug 'Failed to start the container ... restarting ...' + failstart=1 + + restart_container "$1" + fi + + sleep 1s + done + + # Wait for container to access the internet + for j in $(seq 1 10); do + if lxc exec "$1" -- curl -s http://wikipedia.org > /dev/null 2>/dev/null; then + break + fi + + if [ "$j" == "10" ]; then + log_debug 'Failed to access the internet ... restarting' + failstart=1 + + restart_container "$1" + fi + + sleep 1s + done + + # Has started and has access to the internet + if [ $failstart -eq 0 ] + then + break + fi + + # Fail if the container failed to start + if [ $i -eq $max_try ] && [ $failstart -eq 1 ] + then + log_error "The container miserably failed to start or to connect to the internet" + lxc info --show-log $1 + return 1 + fi + done + + LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") +} + ynh_lxc_snapshot_exists () { local snapname=$1 lxc list --format json \ @@ -36,7 +104,7 @@ ynh_lxc_snapshot_load () { lxc restore $LXC_NAME $snapname lxc start $LXC_NAME - _LXC_START_AND_WAIT $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME } ynh_lxc_reset () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index cbb5b35..4e7e646 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -8,7 +8,7 @@ ynh_lxc_pc_exec () { # Start the lxc container and execute the given command in it local cmd=$1 - _LXC_START_AND_WAIT $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME start_timer @@ -143,7 +143,7 @@ ynh_lxc_pc_create () { [[ "$pipestatus" -eq 0 ]] || exit 1 - _LXC_START_AND_WAIT $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME ynh_lxc_pc_witness_files_set lxc snapshot $LXC_NAME snap0 } @@ -170,84 +170,16 @@ ynh_lxc_pc_snapshot_create () { lxc snapshot $LXC_NAME $snapname fi - _LXC_START_AND_WAIT $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME stop_timer 1 } - -_LXC_START_AND_WAIT() { - - restart_container() - { - ynh_lxc_stop $1 - lxc start "$1" - } - - # Try to start the container 3 times. - local max_try=3 - local i=0 - while [ $i -lt $max_try ] - do - i=$(( i +1 )) - local failstart=0 - - # Wait for container to start, we are using systemd to check this, - # for the sake of brevity. - for j in $(seq 1 10); do - if lxc exec "$1" -- systemctl isolate multi-user.target >/dev/null 2>/dev/null; then - break - fi - - if [ "$j" == "10" ]; then - log_debug 'Failed to start the container ... restarting ...' - failstart=1 - - restart_container "$1" - fi - - sleep 1s - done - - # Wait for container to access the internet - for j in $(seq 1 10); do - if lxc exec "$1" -- curl -s http://wikipedia.org > /dev/null 2>/dev/null; then - break - fi - - if [ "$j" == "10" ]; then - log_debug 'Failed to access the internet ... restarting' - failstart=1 - - restart_container "$1" - fi - - sleep 1s - done - - # Has started and has access to the internet - if [ $failstart -eq 0 ] - then - break - fi - - # Fail if the container failed to start - if [ $i -eq $max_try ] && [ $failstart -eq 1 ] - then - log_error "The container miserably failed to start or to connect to the internet" - lxc info --show-log $1 - return 1 - fi - done - - LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") -} - CLEAN_SWAPFILES() { # Restart it if needed if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then lxc start $LXC_NAME - _LXC_START_AND_WAIT $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME fi lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' From 52e814db28061f7f7a486a2361af43490dbf2407 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:19:46 +0200 Subject: [PATCH 14/51] CLEAN_SWAPFILES to ynh_lxc_swapfiles_clean --- lib/ynh_lxd | 14 ++++++++++++-- lib/ynh_lxd_package_check | 12 +----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index a550188..a48e270 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -85,6 +85,16 @@ _ynh_lxc_start_and_wait () { LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") } +ynh_lxc_swapfiles_clean () { + # Restart it if needed + if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then + lxc start $LXC_NAME + _ynh_lxc_start_and_wait $LXC_NAME + fi + lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' + lxc exec $LXC_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 \ @@ -98,7 +108,7 @@ ynh_lxc_snapshot_load () { log_debug "Loading snapshot $snapname ..." # Remove swap files before restoring the snapshot. - CLEAN_SWAPFILES + ynh_lxc_swapfiles_clean ynh_lxc_stop $LXC_NAME @@ -111,7 +121,7 @@ ynh_lxc_reset () { # If the container exists if lxc info $LXC_NAME >/dev/null 2>/dev/null; then # Remove swap files before deletting the continer - CLEAN_SWAPFILES + ynh_lxc_swapfiles_clean fi ynh_lxc_stop $LXC_NAME diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 4e7e646..ca82a18 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -159,7 +159,7 @@ ynh_lxc_pc_snapshot_create () { ynh_lxc_pc_witness_files_check >&2 # Remove swap files to avoid killing the CI with huge snapshots. - CLEAN_SWAPFILES + ynh_lxc_swapfiles_clean ynh_lxc_stop $LXC_NAME @@ -175,16 +175,6 @@ ynh_lxc_pc_snapshot_create () { stop_timer 1 } -CLEAN_SWAPFILES() { - # Restart it if needed - if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then - lxc start $LXC_NAME - _ynh_lxc_start_and_wait $LXC_NAME - fi - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' -} - RUN_INSIDE_LXC() { lxc exec $LXC_NAME -- "$@" } From be8035b4420435f8ec83c8879b3eb601dedd2927 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:21:50 +0200 Subject: [PATCH 15/51] RUN_INSIDE_LXC to ynh_lxc_run_inside --- lib/tests.sh | 4 ++-- lib/ynh_lxd | 4 ++++ lib/ynh_lxd_package_check | 14 +++++--------- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index bb72799..9690be0 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -530,7 +530,7 @@ TEST_BACKUP_RESTORE () { # Remove the previous residual backups rm -rf $TEST_CONTEXT/ynh_backups - RUN_INSIDE_LXC rm -rf /home/yunohost.backup/archives + ynh_lxc_run_inside rm -rf /home/yunohost.backup/archives # BACKUP # Made a backup if the installation succeed @@ -573,7 +573,7 @@ TEST_BACKUP_RESTORE () { ynh_lxc_snapshot_load snap0 # Remove the previous residual backups - RUN_INSIDE_LXC rm -rf /home/yunohost.backup/archives + ynh_lxc_run_inside rm -rf /home/yunohost.backup/archives # Place the copy of the backup archive in the container. lxc file push -r $TEST_CONTEXT/ynh_backups/archives $LXC_NAME/home/yunohost.backup/ diff --git a/lib/ynh_lxd b/lib/ynh_lxd index a48e270..7bbb1f1 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -17,6 +17,10 @@ ynh_lxc_stop () { } +ynh_lxc_run_inside () { + lxc exec $LXC_NAME -- "$@" +} + _ynh_lxc_start_and_wait () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index ca82a18..74723d8 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -27,7 +27,7 @@ ynh_lxc_pc_exec () { ynh_lxc_pc_witness_file_create () { [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - RUN_INSIDE_LXC $action $1 + ynh_lxc_run_inside $action $1 } ynh_lxc_pc_witness_files_set () { @@ -63,12 +63,12 @@ ynh_lxc_pc_witness_files_set () { ynh_lxc_pc_witness_file_create "/etc/systemd/system/witnessfile.service" file # Database - RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 - echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 + ynh_lxc_run_inside mysqladmin --wait status > /dev/null 2>&1 + echo "CREATE DATABASE witnessdb" | ynh_lxc_run_inside mysql --wait > /dev/null 2>&1 } ynh_lxc_pc_witness_file_check () { - if RUN_INSIDE_LXC test ! -e "$1" + if ynh_lxc_run_inside test ! -e "$1" then log_error "The file $1 is missing ! Something gone wrong !" SET_RESULT "failure" witness @@ -107,7 +107,7 @@ ynh_lxc_pc_witness_files_check () { ynh_lxc_pc_witness_file_check "/etc/systemd/system/witnessfile.service" # Database - if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 + if ! ynh_lxc_run_inside mysqlshow witnessdb > /dev/null 2>&1 then log_error "The database witnessdb is missing ! Something gone wrong !" SET_RESULT "failure" witness @@ -174,7 +174,3 @@ ynh_lxc_pc_snapshot_create () { stop_timer 1 } - -RUN_INSIDE_LXC() { - lxc exec $LXC_NAME -- "$@" -} From 3b085d60de606c95c9215965d57e06ca201c9d7c Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:26:04 +0200 Subject: [PATCH 16/51] Implement ynh_lxc_pc_exec --- lib/tests.sh | 22 +++++++++++----------- lib/tests_coordination.sh | 8 ++++---- lib/ynh_lxd_package_check | 20 ++++++++++++++++---- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 9690be0..bcb8dec 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -13,7 +13,7 @@ _RUN_YUNOHOST_CMD() { lxc file push -p -r "$package_path" $LXC_NAME/app_folder --quiet # --output-as none is to disable the json-like output for some commands like backup create - ynh_lxc_pc_exec "yunohost --output-as none --debug $1" \ + ynh_lxc_pc_exec --name=$LXC_NAME --command="yunohost --output-as none --debug $1" \ | grep --line-buffered -v --extended-regexp '^[0-9]+\s+.{1,15}DEBUG' \ | grep --line-buffered -v 'processing action' @@ -39,7 +39,7 @@ _PREINSTALL () { # Copy the pre-install script into the container. lxc file push "$preinstall_script" "$LXC_NAME/preinstall.sh" # Then execute the script to execute the pre-install commands. - ynh_lxc_pc_exec "bash /preinstall.sh" + ynh_lxc_pc_exec --name=$LXC_NAME --command="bash /preinstall.sh" fi } @@ -63,7 +63,7 @@ _PREUPGRADE () { # Copy the pre-upgrade script into the container. lxc file push "$preupgrade_script" "$LXC_NAME/preupgrade.sh" # Then execute the script to execute the pre-upgrade commands. - ynh_lxc_pc_exec "bash /preupgrade.sh" + ynh_lxc_pc_exec --name=$LXC_NAME --command="bash /preupgrade.sh" return $? fi } @@ -111,7 +111,7 @@ _INSTALL_APP () { local ret=$? [ $ret -eq 0 ] && log_debug "Installation successful." || log_error "Installation failed." - if ynh_lxc_pc_exec "su nobody -s /bin/bash -c \"test -r /var/www/$app_id || test -w /var/www/$app_id || test -x /var/www/$app_id\"" + if ynh_lxc_pc_exec --name=$LXC_NAME --command="su nobody -s /bin/bash -c \"test -r /var/www/$app_id || test -w /var/www/$app_id || test -x /var/www/$app_id\"" then log_error "It looks like anybody can read/enter /var/www/$app_id, which ain't super great from a security point of view ... Config files or other files may contain secrets or information that should in most case not be world-readable. You should remove all 'others' permissions with 'chmod o-rwx', and setup appropriate, exclusive permissions to the appropriate owner/group with chmod/chown." SET_RESULT "failure" install_dir_permissions @@ -212,7 +212,7 @@ _VALIDATE_THAT_APP_CAN_BE_ACCESSED () { log_debug "Running curl $check_domain$curl_check_path" # Call cURL to try to access to the URL of the app - ynh_lxc_pc_exec "curl --location --insecure --silent --show-error \ + ynh_lxc_pc_exec --name=$LXC_NAME --command="curl --location --insecure --silent --show-error \ --header 'Host: $check_domain' \ --resolve $DOMAIN:80:$LXC_IP \ --resolve $DOMAIN:443:$LXC_IP \ @@ -223,7 +223,7 @@ _VALIDATE_THAT_APP_CAN_BE_ACCESSED () { $check_domain$curl_check_path" \ > "$TEST_CONTEXT/curl_print" - ynh_lxc_pc_exec "cat ./curl_output" > $curl_output + ynh_lxc_pc_exec --name=$LXC_NAME --command="cat ./curl_output" > $curl_output # Analyze the result of curl command if [ $? -ne 0 ] @@ -284,12 +284,12 @@ Page extract:\n$page_extract" > $TEST_CONTEXT/curl_result # If we had a 50x error, try to display service info and logs to help debugging if [[ $curl_error -ne 0 ]] && echo "5" | grep -q "${http_code:0:1}" then - ynh_lxc_pc_exec "systemctl --all" | grep "$app_id_to_check.*service" - for SERVICE in $(ynh_lxc_pc_exec "systemctl -all" | grep -o "$app_id_to_check.*service") + ynh_lxc_pc_exec --name=$LXC_NAME --command="systemctl --all" | grep "$app_id_to_check.*service" + for SERVICE in $(ynh_lxc_pc_exec --name=$LXC_NAME --command="systemctl -all" | grep -o "$app_id_to_check.*service") do - ynh_lxc_pc_exec "journalctl --no-pager --no-hostname -n 30 -u $SERVICE"; + ynh_lxc_pc_exec --name=$LXC_NAME --command="journalctl --no-pager --no-hostname -n 30 -u $SERVICE"; done - ynh_lxc_pc_exec "tail -v -n 15 \$(find /var/log/{nginx/,php*,$app_id_to_check} -mmin -3)" + ynh_lxc_pc_exec --name=$LXC_NAME --command="tail -v -n 15 \$(find /var/log/{nginx/,php*,$app_id_to_check} -mmin -3)" fi done @@ -490,7 +490,7 @@ TEST_PORT_ALREADY_USED () { lxc file push $TEST_CONTEXT/netcat.service $LXC_NAME/etc/systemd/system/netcat.service # Then start this service to block this port. - ynh_lxc_pc_exec "systemctl enable --now netcat" + ynh_lxc_pc_exec --name=$LXC_NAME --command="systemctl enable --now netcat" _PREINSTALL diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 5706478..b4ea968 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -257,13 +257,13 @@ run_all_tests() { ynh_lxc_reset ynh_lxc_pc_create # Be sure that the container is running - ynh_lxc_pc_exec "true" + ynh_lxc_pc_exec --name=$LXC_NAME --command="true" # Print the version of YunoHost from the LXC container log_small_title "YunoHost versions" - ynh_lxc_pc_exec "yunohost --version" - ynh_lxc_pc_exec "yunohost --version --output-as json" | jq -r .yunohost.version >> $TEST_CONTEXT/ynh_version - ynh_lxc_pc_exec "yunohost --version --output-as json" | jq -r .yunohost.repo >> $TEST_CONTEXT/ynh_branch + ynh_lxc_pc_exec --name=$LXC_NAME --command="yunohost --version" + ynh_lxc_pc_exec --name=$LXC_NAME --command="yunohost --version --output-as json" | jq -r .yunohost.version >> $TEST_CONTEXT/ynh_version + ynh_lxc_pc_exec --name=$LXC_NAME --command="yunohost --version --output-as json" | jq -r .yunohost.repo >> $TEST_CONTEXT/ynh_branch echo $ARCH > $TEST_CONTEXT/architecture echo $app_id > $TEST_CONTEXT/app_id diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 74723d8..2445593 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -4,16 +4,28 @@ # PACKAGE_CHECK HELPERS #================================================= +# Start an LXC and execute a command in it +# +# usage: ynh_lxc_pc_exec --name=name --command=command +# | arg: -n, --name= - name of the LXC +# | arg: -c, --command= - command to execute +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_pc_exec () { - # Start the lxc container and execute the given command in it - local cmd=$1 + # Declare an array to define the options of this helper. + local legacy_args=nc + local -A args_array=([n]=name= [c]=command=) + local name + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" - _ynh_lxc_start_and_wait $LXC_NAME + _ynh_lxc_start_and_wait $name start_timer # Execute the command given in argument in the container and log its results. - lxc exec $LXC_NAME --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$cmd" | tee -a "$complete_log" $current_test_log + lxc exec $name --env PACKAGE_CHECK_EXEC=1 -t -- /bin/bash -c "$command" | tee -a "$complete_log" $current_test_log # Store the return code of the command local returncode=${PIPESTATUS[0]} From afe55f2b33f2cc937e07cf595f3fd3f03d693f46 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:37:14 +0200 Subject: [PATCH 17/51] implement ynh_lxc_run_inside --- lib/tests.sh | 4 ++-- lib/ynh_lxd | 17 ++++++++++++++++- lib/ynh_lxd_package_check | 10 +++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index bcb8dec..ea3f03d 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -530,7 +530,7 @@ TEST_BACKUP_RESTORE () { # Remove the previous residual backups rm -rf $TEST_CONTEXT/ynh_backups - ynh_lxc_run_inside rm -rf /home/yunohost.backup/archives + ynh_lxc_run_inside --name=$LXC_NAME --command="rm -rf /home/yunohost.backup/archives" # BACKUP # Made a backup if the installation succeed @@ -573,7 +573,7 @@ TEST_BACKUP_RESTORE () { ynh_lxc_snapshot_load snap0 # Remove the previous residual backups - ynh_lxc_run_inside rm -rf /home/yunohost.backup/archives + ynh_lxc_run_inside --name=$LXC_NAME --command="rm -rf /home/yunohost.backup/archives" # Place the copy of the backup archive in the container. lxc file push -r $TEST_CONTEXT/ynh_backups/archives $LXC_NAME/home/yunohost.backup/ diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 7bbb1f1..91b5783 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -17,8 +17,23 @@ ynh_lxc_stop () { } +# Run a command inside an LXC container +# +# usage: ynh_lxc_run_inside --name=name --command=command +# | arg: -n, --name= - name of the LXC +# | arg: -c, --command= - command to execute +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_run_inside () { - lxc exec $LXC_NAME -- "$@" + # Declare an array to define the options of this helper. + local legacy_args=nc + local -A args_array=([n]=name= [c]=command=) + local name + local command + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + lxc exec $name -- /bin/bash -c "$command" } diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 2445593..7b0e415 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -39,7 +39,7 @@ ynh_lxc_pc_exec () { ynh_lxc_pc_witness_file_create () { [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - ynh_lxc_run_inside $action $1 + ynh_lxc_run_inside --name=$LXC_NAME --command="$action $1" } ynh_lxc_pc_witness_files_set () { @@ -75,12 +75,12 @@ ynh_lxc_pc_witness_files_set () { ynh_lxc_pc_witness_file_create "/etc/systemd/system/witnessfile.service" file # Database - ynh_lxc_run_inside mysqladmin --wait status > /dev/null 2>&1 - echo "CREATE DATABASE witnessdb" | ynh_lxc_run_inside mysql --wait > /dev/null 2>&1 + ynh_lxc_run_inside --name=$LXC_NAME --command="mysqladmin --wait status > /dev/null 2>&1" + echo "CREATE DATABASE witnessdb" | ynh_lxc_run_inside --name=$LXC_NAME --command="mysql --wait > /dev/null 2>&1" } ynh_lxc_pc_witness_file_check () { - if ynh_lxc_run_inside test ! -e "$1" + if ynh_lxc_run_inside --name=$LXC_NAME --command="test ! -e \"$1\"" then log_error "The file $1 is missing ! Something gone wrong !" SET_RESULT "failure" witness @@ -119,7 +119,7 @@ ynh_lxc_pc_witness_files_check () { ynh_lxc_pc_witness_file_check "/etc/systemd/system/witnessfile.service" # Database - if ! ynh_lxc_run_inside mysqlshow witnessdb > /dev/null 2>&1 + if ! ynh_lxc_run_inside --name=$LXC_NAME --command="mysqlshow witnessdb > /dev/null 2>&1" then log_error "The database witnessdb is missing ! Something gone wrong !" SET_RESULT "failure" witness From ce7eddad7af1ace2979fbf079bff7cb3ce2e7143 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:43:12 +0200 Subject: [PATCH 18/51] Implement ynh_lxc_pc_witness_file_create --- lib/ynh_lxd_package_check | 41 +++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 7b0e415..278e622 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -37,9 +37,26 @@ ynh_lxc_pc_exec () { return $returncode } +# Create a witness in an LXC container +# +# usage: ynh_lxc_pc_witness_file_create --name=name --witness=witness --type=type +# | arg: -n, --name= - name of the LXC +# | arg: -w, --witness= - witness to create +# | arg: -t, --type= - type of witness, can be file or directory +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_pc_witness_file_create () { - [ "$2" = "file" ] && local action="touch" || local action="mkdir -p" - ynh_lxc_run_inside --name=$LXC_NAME --command="$action $1" + # Declare an array to define the options of this helper. + local legacy_args=nwt + local -A args_array=([n]=name= [w]=witness= [t]=type=) + local name + local witness + local type + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + [ "$type" = "file" ] && local action="touch" || local action="mkdir -p" + ynh_lxc_run_inside --name=$name --command="$action $witness" } ynh_lxc_pc_witness_files_set () { @@ -47,32 +64,32 @@ ynh_lxc_pc_witness_files_set () { log_debug "Create witness files..." # Nginx conf - ynh_lxc_pc_witness_file_create "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file - ynh_lxc_pc_witness_file_create "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file # /etc - ynh_lxc_pc_witness_file_create "/etc/witnessfile" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/witnessfile" --type=file # /opt directory - ynh_lxc_pc_witness_file_create "/opt/witnessdir" directory + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/opt/witnessdir" --type=directory # /var/www directory - ynh_lxc_pc_witness_file_create "/var/www/witnessdir" directory + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/www/witnessdir" --type=directory # /home/yunohost.app/ - ynh_lxc_pc_witness_file_create "/home/yunohost.app/witnessdir" directory + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/home/yunohost.app/witnessdir" --type=directory # /var/log - ynh_lxc_pc_witness_file_create "/var/log/witnessfile" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/log/witnessfile" --type=file # Config fpm - ynh_lxc_pc_witness_file_create "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file # Config logrotate - ynh_lxc_pc_witness_file_create "/etc/logrotate.d/witnessfile" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/logrotate.d/witnessfile" --type=file # Config systemd - ynh_lxc_pc_witness_file_create "/etc/systemd/system/witnessfile.service" file + ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/systemd/system/witnessfile.service" --type=file # Database ynh_lxc_run_inside --name=$LXC_NAME --command="mysqladmin --wait status > /dev/null 2>&1" From 9b6a68b2cc330863fea024886457e2b29e59efbd Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:45:21 +0200 Subject: [PATCH 19/51] Implement ynh_lxc_pc_witness_files_set --- lib/ynh_lxd_package_check | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 278e622..e7a2677 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -59,41 +59,54 @@ ynh_lxc_pc_witness_file_create () { ynh_lxc_run_inside --name=$name --command="$action $witness" } +# Set witness in an LXC container +# +# usage: ynh_lxc_pc_witness_files_set --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_pc_witness_files_set () { + # 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 "$@" + # Create files to check if the remove script does not remove them accidentally log_debug "Create witness files..." # Nginx conf - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" --type=file # /etc - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/witnessfile" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/witnessfile" --type=file # /opt directory - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/opt/witnessdir" --type=directory + ynh_lxc_pc_witness_file_create --name=$name --witness="/opt/witnessdir" --type=directory # /var/www directory - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/www/witnessdir" --type=directory + ynh_lxc_pc_witness_file_create --name=$name --witness="/var/www/witnessdir" --type=directory # /home/yunohost.app/ - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/home/yunohost.app/witnessdir" --type=directory + ynh_lxc_pc_witness_file_create --name=$name --witness="/home/yunohost.app/witnessdir" --type=directory # /var/log - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/var/log/witnessfile" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/var/log/witnessfile" --type=file # Config fpm - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" --type=file # Config logrotate - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/logrotate.d/witnessfile" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/logrotate.d/witnessfile" --type=file # Config systemd - ynh_lxc_pc_witness_file_create --name=$LXC_NAME --witness="/etc/systemd/system/witnessfile.service" --type=file + ynh_lxc_pc_witness_file_create --name=$name --witness="/etc/systemd/system/witnessfile.service" --type=file # Database - ynh_lxc_run_inside --name=$LXC_NAME --command="mysqladmin --wait status > /dev/null 2>&1" - echo "CREATE DATABASE witnessdb" | ynh_lxc_run_inside --name=$LXC_NAME --command="mysql --wait > /dev/null 2>&1" + ynh_lxc_run_inside --name=$name --command="mysqladmin --wait status > /dev/null 2>&1" + ynh_lxc_run_inside --name=$name --command="echo \"CREATE DATABASE witnessdb\" | mysql --wait > /dev/null 2>&1" } ynh_lxc_pc_witness_file_check () { @@ -173,7 +186,7 @@ ynh_lxc_pc_create () { [[ "$pipestatus" -eq 0 ]] || exit 1 _ynh_lxc_start_and_wait $LXC_NAME - ynh_lxc_pc_witness_files_set + ynh_lxc_pc_witness_files_set --name=$LXC_NAME lxc snapshot $LXC_NAME snap0 } From 7a736218deec797670909b5f2aff5e028c9c28c2 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:47:29 +0200 Subject: [PATCH 20/51] Implement ynh_lxc_pc_witness_file_check --- lib/ynh_lxd_package_check | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index e7a2677..5604fc7 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -109,10 +109,25 @@ ynh_lxc_pc_witness_files_set () { ynh_lxc_run_inside --name=$name --command="echo \"CREATE DATABASE witnessdb\" | mysql --wait > /dev/null 2>&1" } +# Check if a witness exists in an LXC container +# +# usage: ynh_lxc_pc_witness_file_check --name=name --witness=witness +# | arg: -n, --name= - name of the LXC +# | arg: -w, --witness= - witness to create +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_pc_witness_file_check () { - if ynh_lxc_run_inside --name=$LXC_NAME --command="test ! -e \"$1\"" + # Declare an array to define the options of this helper. + local legacy_args=nw + local -A args_array=([n]=name= [w]=witness=) + local name + local witness + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if ynh_lxc_run_inside --name=$name --command="test ! -e \"$witness\"" then - log_error "The file $1 is missing ! Something gone wrong !" + log_error "The file $witness is missing ! Something gone wrong !" SET_RESULT "failure" witness fi } @@ -121,32 +136,32 @@ ynh_lxc_pc_witness_files_check () { # Check all the witness files, to verify if them still here # Nginx conf - ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - ynh_lxc_pc_witness_file_check "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" # /etc - ynh_lxc_pc_witness_file_check "/etc/witnessfile" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/witnessfile" # /opt directory - ynh_lxc_pc_witness_file_check "/opt/witnessdir" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/opt/witnessdir" # /var/www directory - ynh_lxc_pc_witness_file_check "/var/www/witnessdir" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/var/www/witnessdir" # /home/yunohost.app/ - ynh_lxc_pc_witness_file_check "/home/yunohost.app/witnessdir" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/home/yunohost.app/witnessdir" # /var/log - ynh_lxc_pc_witness_file_check "/var/log/witnessfile" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/var/log/witnessfile" # Config fpm - ynh_lxc_pc_witness_file_check "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" # Config logrotate - ynh_lxc_pc_witness_file_check "/etc/logrotate.d/witnessfile" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/logrotate.d/witnessfile" # Config systemd - ynh_lxc_pc_witness_file_check "/etc/systemd/system/witnessfile.service" + ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/systemd/system/witnessfile.service" # Database if ! ynh_lxc_run_inside --name=$LXC_NAME --command="mysqlshow witnessdb > /dev/null 2>&1" From 28c7be085a7113ff310fe478128bdf2135a0f177 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:49:22 +0200 Subject: [PATCH 21/51] Implement ynh_lxc_pc_witness_files_check --- lib/tests.sh | 2 +- lib/ynh_lxd_package_check | 39 ++++++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index ea3f03d..204df29 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -18,7 +18,7 @@ _RUN_YUNOHOST_CMD() { | grep --line-buffered -v 'processing action' returncode=${PIPESTATUS[0]} - ynh_lxc_pc_witness_files_check && return $returncode || return 2 + ynh_lxc_pc_witness_files_check --name=$LXC_NAME && return $returncode || return 2 } _PREINSTALL () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 5604fc7..577201d 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -132,39 +132,52 @@ ynh_lxc_pc_witness_file_check () { fi } -ynh_lxc_pc_witness_files_check () { +# Check witness in an LXC container +# +# usage: ynh_lxc_pc_witness_files_check --name=$LXC_NAME --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_pc_witness_files_check --name=$LXC_NAME () { + # 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 "$@" + # Check all the witness files, to verify if them still here # Nginx conf - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" # /etc - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/witnessfile" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/witnessfile" # /opt directory - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/opt/witnessdir" + ynh_lxc_pc_witness_file_check --name=$name --witness="/opt/witnessdir" # /var/www directory - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/var/www/witnessdir" + ynh_lxc_pc_witness_file_check --name=$name --witness="/var/www/witnessdir" # /home/yunohost.app/ - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/home/yunohost.app/witnessdir" + ynh_lxc_pc_witness_file_check --name=$name --witness="/home/yunohost.app/witnessdir" # /var/log - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/var/log/witnessfile" + ynh_lxc_pc_witness_file_check --name=$name --witness="/var/log/witnessfile" # Config fpm - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" # Config logrotate - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/logrotate.d/witnessfile" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/logrotate.d/witnessfile" # Config systemd - ynh_lxc_pc_witness_file_check --name=$LXC_NAME --witness="/etc/systemd/system/witnessfile.service" + ynh_lxc_pc_witness_file_check --name=$name --witness="/etc/systemd/system/witnessfile.service" # Database - if ! ynh_lxc_run_inside --name=$LXC_NAME --command="mysqlshow witnessdb > /dev/null 2>&1" + if ! ynh_lxc_run_inside --name=$name --command="mysqlshow witnessdb > /dev/null 2>&1" then log_error "The database witnessdb is missing ! Something gone wrong !" SET_RESULT "failure" witness @@ -213,7 +226,7 @@ ynh_lxc_pc_snapshot_create () { start_timer # Check all the witness files, to verify if them still here - ynh_lxc_pc_witness_files_check >&2 + ynh_lxc_pc_witness_files_check --name=$LXC_NAME >&2 # Remove swap files to avoid killing the CI with huge snapshots. ynh_lxc_swapfiles_clean From 3e9652009d792cb0be553de27937d5fdbf1136bb Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:49:38 +0200 Subject: [PATCH 22/51] Update ynh_lxd_package_check --- lib/ynh_lxd_package_check | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 577201d..3c2f681 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -134,11 +134,11 @@ ynh_lxc_pc_witness_file_check () { # Check witness in an LXC container # -# usage: ynh_lxc_pc_witness_files_check --name=$LXC_NAME --name=name +# usage: ynh_lxc_pc_witness_files_check --name=name # | arg: -n, --name= - name of the LXC # # Requires YunoHost version *.*.* or higher. -ynh_lxc_pc_witness_files_check --name=$LXC_NAME () { +ynh_lxc_pc_witness_files_check () { # Declare an array to define the options of this helper. local legacy_args=n local -A args_array=([n]=name=) From 3e0c1a2bacad0f9b86c69b72a77ec0f80b5cd241 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:53:52 +0200 Subject: [PATCH 23/51] Implement ynh_lxc_pc_create --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd_package_check | 35 +++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index b4ea968..3eacd3c 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -255,7 +255,7 @@ run_all_tests() { # Reset and create a fresh container to work with check_lxd_setup ynh_lxc_reset - ynh_lxc_pc_create + 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_package_check b/lib/ynh_lxd_package_check index 3c2f681..533bb7e 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -185,37 +185,52 @@ ynh_lxc_pc_witness_files_check () { fi } +# Create a new LXC from an image +# +# usage: ynh_lxc_pc_create --image=image --name=name +# | arg: -i, --image= - image to create from +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_pc_create () { - log_info "Launching new LXC $LXC_NAME ..." + # Declare an array to define the options of this helper. + local legacy_args=in + local -A args_array=([i]=image= [n]=name=) + local image + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + log_info "Launching new LXC $name ..." # Check if we can launch container from YunoHost remote image - if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then - lxc launch yunohost:$LXC_BASE $LXC_NAME \ + if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then + lxc launch yunohost:$image $name \ -c security.nesting=true \ -c security.privileged=true \ -c limits.memory=80% \ -c limits.cpu.allowance=80% \ >>/proc/self/fd/3 # Check if we can launch container from a local image - elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then - lxc launch $LXC_BASE $LXC_NAME \ + 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 else - log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" + log_critical "Can't find base image $image, run ./package_check.sh --rebuild" fi pipestatus="${PIPESTATUS[0]}" - location=$(lxc list --format json | jq -e --arg LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"') + location=$(lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"') [[ "$location" != "none" ]] && log_info "... on $location" [[ "$pipestatus" -eq 0 ]] || exit 1 - _ynh_lxc_start_and_wait $LXC_NAME - ynh_lxc_pc_witness_files_set --name=$LXC_NAME - lxc snapshot $LXC_NAME snap0 + _ynh_lxc_start_and_wait $name + ynh_lxc_pc_witness_files_set --name=$name + lxc snapshot $name snap0 } ynh_lxc_pc_snapshot_create () { From 0206677353ab413b85105f311990f7f8ac9cec1e Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:56:44 +0200 Subject: [PATCH 24/51] Implement ynh_lxc_pc_snapshot_create --- lib/tests.sh | 4 ++-- lib/ynh_lxd_package_check | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 204df29..6c9c0ca 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -132,7 +132,7 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { ynh_lxc_snapshot_load snap0 \ && _PREINSTALL \ && _INSTALL_APP "path=$check_path" \ - && ynh_lxc_pc_snapshot_create $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 $snapname \ && log_debug "Create a snapshot after app install" \ - && ynh_lxc_pc_snapshot_create $snapname + && ynh_lxc_pc_snapshot_create --name=LXC_NAME --snapname=$snapname # Remove and reinstall the application _REMOVE_APP \ diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 533bb7e..7bdd530 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -233,29 +233,40 @@ ynh_lxc_pc_create () { lxc snapshot $name snap0 } -ynh_lxc_pc_snapshot_create () { - # Create a temporary snapshot - - local snapname=$1 +# Create a snapshot of an LXC container +# +# usage: ynh_lxc_pc_snapshot_create --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_pc_snapshot_create --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 "$@" start_timer # Check all the witness files, to verify if them still here - ynh_lxc_pc_witness_files_check --name=$LXC_NAME >&2 + ynh_lxc_pc_witness_files_check --name=$name >&2 # Remove swap files to avoid killing the CI with huge snapshots. ynh_lxc_swapfiles_clean - ynh_lxc_stop $LXC_NAME + ynh_lxc_stop $name # Check if the snapshot already exist if ! ynh_lxc_snapshot_exists "$snapname" then log_info "(Creating snapshot $snapname ...)" - lxc snapshot $LXC_NAME $snapname + lxc snapshot $name $snapname fi - _ynh_lxc_start_and_wait $LXC_NAME + _ynh_lxc_start_and_wait $name stop_timer 1 } From 6054f9841f7f5df8f27eb53e306e8b97d7a2dd04 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 00:57:43 +0200 Subject: [PATCH 25/51] Update ynh_lxd_package_check --- lib/ynh_lxd_package_check | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 7bdd530..cd35906 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -235,12 +235,12 @@ ynh_lxc_pc_create () { # Create a snapshot of an LXC container # -# usage: ynh_lxc_pc_snapshot_create --name=LXC_NAME --snapname= --name=name --snapname=snapname +# usage: ynh_lxc_pc_snapshot_create --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_pc_snapshot_create --name=LXC_NAME --snapname= () { +ynh_lxc_pc_snapshot_create () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) From dfbf0b2c7aef1aa733b32fc09f8f5254fb2c6233 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:01:02 +0200 Subject: [PATCH 26/51] Implement ynh_lxc_stop --- lib/tests_coordination.sh | 2 +- lib/ynh_lxd | 25 ++++++++++++++++++------- lib/ynh_lxd_package_check | 2 +- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/tests_coordination.sh b/lib/tests_coordination.sh index 3eacd3c..79115ef 100644 --- a/lib/tests_coordination.sh +++ b/lib/tests_coordination.sh @@ -347,7 +347,7 @@ TEST_LAUNCHER () { # End the timer for the test stop_timer 2 - ynh_lxc_stop $LXC_NAME + ynh_lxc_stop --name=$LXC_NAME # Update the lock file with the date of the last finished test. # $$ is the PID of package_check itself. diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 91b5783..2c4233c 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -4,17 +4,28 @@ # LXD HELPERS #================================================= +# Stopping an LXC container +# +# usage: ynh_lxc_stop --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_stop () { - local container_to_stop=$1 + # 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 "$@" + # (We also use timeout 30 in front of the command because sometime lxc # commands can hang forever despite the --timeout >_>...) - timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null + timeout 30 lxc stop --timeout 15 $name 2>/dev/null # If the command times out, then add the option --force if [ $? -eq 124 ]; then - timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null + timeout 30 lxc stop --timeout 15 $name --force 2>/dev/null fi - } # Run a command inside an LXC container @@ -41,7 +52,7 @@ _ynh_lxc_start_and_wait () { restart_container() { - ynh_lxc_stop $1 + ynh_lxc_stop --name=$1 lxc start "$1" } @@ -129,7 +140,7 @@ ynh_lxc_snapshot_load () { # Remove swap files before restoring the snapshot. ynh_lxc_swapfiles_clean - ynh_lxc_stop $LXC_NAME + ynh_lxc_stop --name=$LXC_NAME lxc restore $LXC_NAME $snapname lxc start $LXC_NAME @@ -143,7 +154,7 @@ ynh_lxc_reset () { ynh_lxc_swapfiles_clean fi - ynh_lxc_stop $LXC_NAME + ynh_lxc_stop --name=$LXC_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') diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index cd35906..d5b197b 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -257,7 +257,7 @@ ynh_lxc_pc_snapshot_create () { # Remove swap files to avoid killing the CI with huge snapshots. ynh_lxc_swapfiles_clean - ynh_lxc_stop $name + ynh_lxc_stop --name=$name # Check if the snapshot already exist if ! ynh_lxc_snapshot_exists "$snapname" From 7a23290ae4d69992ba301a38c4222153bc0ee109 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:03:43 +0200 Subject: [PATCH 27/51] Implement _ynh_lxc_restart_container --- lib/ynh_lxd | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 2c4233c..8485369 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -47,15 +47,26 @@ ynh_lxc_run_inside () { lxc exec $name -- /bin/bash -c "$command" } +# Restart a container +# +# usage: _ynh_lxc_restart_container --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +_ynh_lxc_restart_container () { + # 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_lxc_stop --name=$name + lxc start "$name" +} _ynh_lxc_start_and_wait () { - restart_container() - { - ynh_lxc_stop --name=$1 - lxc start "$1" - } - # Try to start the container 3 times. local max_try=3 local i=0 @@ -75,7 +86,7 @@ _ynh_lxc_start_and_wait () { log_debug 'Failed to start the container ... restarting ...' failstart=1 - restart_container "$1" + _ynh_lxc_restart_container --name="$1" fi sleep 1s @@ -91,7 +102,7 @@ _ynh_lxc_start_and_wait () { log_debug 'Failed to access the internet ... restarting' failstart=1 - restart_container "$1" + _ynh_lxc_restart_container --name="$1" fi sleep 1s From ebbbc5b4196ffac02b680596e884ccd194381e77 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:06:21 +0200 Subject: [PATCH 28/51] Implement _ynh_lxc_start_and_wait --- lib/ynh_lxd | 28 ++++++++++++++++++++-------- lib/ynh_lxd_package_check | 6 +++--- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 8485369..b89962b 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -65,7 +65,19 @@ _ynh_lxc_restart_container () { lxc start "$name" } +# Keep sure the LXC is started +# +# usage: _ynh_lxc_start_and_wait --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. _ynh_lxc_start_and_wait () { + # 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 "$@" # Try to start the container 3 times. local max_try=3 @@ -78,7 +90,7 @@ _ynh_lxc_start_and_wait () { # Wait for container to start, we are using systemd to check this, # for the sake of brevity. for j in $(seq 1 10); do - if lxc exec "$1" -- systemctl isolate multi-user.target >/dev/null 2>/dev/null; then + if lxc exec "$name" -- systemctl isolate multi-user.target >/dev/null 2>/dev/null; then break fi @@ -86,7 +98,7 @@ _ynh_lxc_start_and_wait () { log_debug 'Failed to start the container ... restarting ...' failstart=1 - _ynh_lxc_restart_container --name="$1" + _ynh_lxc_restart_container --name="$name" fi sleep 1s @@ -94,7 +106,7 @@ _ynh_lxc_start_and_wait () { # Wait for container to access the internet for j in $(seq 1 10); do - if lxc exec "$1" -- curl -s http://wikipedia.org > /dev/null 2>/dev/null; then + if lxc exec "$name" -- curl -s http://wikipedia.org > /dev/null 2>/dev/null; then break fi @@ -102,7 +114,7 @@ _ynh_lxc_start_and_wait () { log_debug 'Failed to access the internet ... restarting' failstart=1 - _ynh_lxc_restart_container --name="$1" + _ynh_lxc_restart_container --name="$name" fi sleep 1s @@ -118,19 +130,19 @@ _ynh_lxc_start_and_wait () { if [ $i -eq $max_try ] && [ $failstart -eq 1 ] then log_error "The container miserably failed to start or to connect to the internet" - lxc info --show-log $1 + lxc info --show-log $name return 1 fi done - LXC_IP=$(lxc exec $1 -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") + LXC_IP=$(lxc exec $name -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") } ynh_lxc_swapfiles_clean () { # Restart it if needed if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then lxc start $LXC_NAME - _ynh_lxc_start_and_wait $LXC_NAME + _ynh_lxc_start_and_wait --name=$LXC_NAME fi lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' @@ -155,7 +167,7 @@ ynh_lxc_snapshot_load () { lxc restore $LXC_NAME $snapname lxc start $LXC_NAME - _ynh_lxc_start_and_wait $LXC_NAME + _ynh_lxc_start_and_wait --name=$LXC_NAME } ynh_lxc_reset () { diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index d5b197b..c368c06 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -20,7 +20,7 @@ ynh_lxc_pc_exec () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - _ynh_lxc_start_and_wait $name + _ynh_lxc_start_and_wait --name=$name start_timer @@ -228,7 +228,7 @@ ynh_lxc_pc_create () { [[ "$pipestatus" -eq 0 ]] || exit 1 - _ynh_lxc_start_and_wait $name + _ynh_lxc_start_and_wait --name=$name ynh_lxc_pc_witness_files_set --name=$name lxc snapshot $name snap0 } @@ -266,7 +266,7 @@ ynh_lxc_pc_snapshot_create () { lxc snapshot $name $snapname fi - _ynh_lxc_start_and_wait $name + _ynh_lxc_start_and_wait --name=$name stop_timer 1 } From d3883f6b697596a2c000e278aa26a2dc3adc2650 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:13:09 +0200 Subject: [PATCH 29/51] Implement ynh_lxc_swapfiles_clean --- lib/ynh_lxd | 27 ++++++++++++++++++++------- lib/ynh_lxd_package_check | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index b89962b..822b0bd 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -138,14 +138,27 @@ _ynh_lxc_start_and_wait () { LXC_IP=$(lxc exec $name -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") } +# Clean the swapfiles of an LXC container +# +# usage: ynh_lxc_swapfiles_clean --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. ynh_lxc_swapfiles_clean () { + # 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 "$@" + # Restart it if needed - if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then - lxc start $LXC_NAME - _ynh_lxc_start_and_wait --name=$LXC_NAME + if [ "$(lxc info $name | grep Status | awk '{print tolower($2)}')" != "running" ]; then + lxc start $name + _ynh_lxc_start_and_wait --name=$name fi - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' - lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' + lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' + lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' } ynh_lxc_snapshot_exists () { @@ -161,7 +174,7 @@ ynh_lxc_snapshot_load () { log_debug "Loading snapshot $snapname ..." # Remove swap files before restoring the snapshot. - ynh_lxc_swapfiles_clean + ynh_lxc_swapfiles_clean --name=$LXC_NAME ynh_lxc_stop --name=$LXC_NAME @@ -174,7 +187,7 @@ ynh_lxc_reset () { # If the container exists if lxc info $LXC_NAME >/dev/null 2>/dev/null; then # Remove swap files before deletting the continer - ynh_lxc_swapfiles_clean + ynh_lxc_swapfiles_clean --name=$LXC_NAME fi ynh_lxc_stop --name=$LXC_NAME diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index c368c06..eb7a190 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -255,7 +255,7 @@ ynh_lxc_pc_snapshot_create () { ynh_lxc_pc_witness_files_check --name=$name >&2 # Remove swap files to avoid killing the CI with huge snapshots. - ynh_lxc_swapfiles_clean + ynh_lxc_swapfiles_clean --name=$name ynh_lxc_stop --name=$name From 72a3afd02bf5e4ba086198904a1a313aa61094c9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:18:07 +0200 Subject: [PATCH 30/51] 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 From 4f9b7140379e79c7e33a55bfa43b6825c52f9360 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:19:08 +0200 Subject: [PATCH 31/51] Update ynh_lxd --- lib/ynh_lxd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index b1892c7..19e8f6b 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -163,12 +163,12 @@ ynh_lxc_swapfiles_clean () { # Check if a snapshot exist for an LXC container # -# usage: ynh_lxc_snapshot_exists --name=$LXC_NAME --snapname=--name=name --snapname=snapname +# usage: ynh_lxc_snapshot_exists --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=() { +ynh_lxc_snapshot_exists () { # Declare an array to define the options of this helper. local legacy_args=ns local -A args_array=([n]=name= [s]=snapname=) From fc7b071aae0ef726d67df6edceceeb96f0b6275b Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:23:47 +0200 Subject: [PATCH 32/51] Implement ynh_lxc_snapshot_load --- lib/tests.sh | 14 +++++++------- lib/ynh_lxd | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/tests.sh b/lib/tests.sh index 753116b..e167a14 100644 --- a/lib/tests.sh +++ b/lib/tests.sh @@ -129,14 +129,14 @@ _LOAD_SNAPSHOT_OR_INSTALL_APP () { 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 \ + 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 else # Or uses an existing snapshot log_info "(Reusing existing snapshot $snapname)" \ - && ynh_lxc_snapshot_load $snapname + && ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=$snapname fi } @@ -356,7 +356,7 @@ TEST_INSTALL () { [ "$install_type" = "private" ] && { start_test "Installation in private mode"; local is_public="0"; } local snapname=snap_${install_type}install - ynh_lxc_snapshot_load snap0 + ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 _PREINSTALL @@ -392,7 +392,7 @@ _TEST_MULTI_INSTANCE () { local check_path="$(default_install_path)" - ynh_lxc_snapshot_load snap0 + ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 log_small_title "First installation: path=$SUBDOMAIN$check_path" \ && _LOAD_SNAPSHOT_OR_INSTALL_APP "$check_path" \ @@ -436,7 +436,7 @@ TEST_UPGRADE () { cp -a "$package_path" "${package_path}_back" (cd "$package_path"; git checkout --force --quiet "$commit") - ynh_lxc_snapshot_load snap0 + ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 _PREINSTALL @@ -481,7 +481,7 @@ TEST_PORT_ALREADY_USED () { local check_port="$1" local check_path="$(default_install_path)" - ynh_lxc_snapshot_load snap0 + ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 # Build a service with netcat for use this port before the app. echo -e "[Service]\nExecStart=/bin/netcat -l -k -p $check_port\n @@ -570,7 +570,7 @@ TEST_BACKUP_RESTORE () { elif [ $j -eq 1 ] then - ynh_lxc_snapshot_load snap0 + ynh_lxc_snapshot_load --name=$LXC_NAME --snapname=snap0 # Remove the previous residual backups ynh_lxc_run_inside --name=$LXC_NAME --command="rm -rf /home/yunohost.backup/archives" diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 19e8f6b..388a68c 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -180,18 +180,32 @@ ynh_lxc_snapshot_exists () { lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null } +# Load a snapshot of an LXC container +# +# usage: ynh_lxc_snapshot_load --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_load () { - local snapname=$1 + # 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 "$@" + log_debug "Loading snapshot $snapname ..." # Remove swap files before restoring the snapshot. - ynh_lxc_swapfiles_clean --name=$LXC_NAME + ynh_lxc_swapfiles_clean --name=$name - ynh_lxc_stop --name=$LXC_NAME + ynh_lxc_stop --name=$name - lxc restore $LXC_NAME $snapname - lxc start $LXC_NAME - _ynh_lxc_start_and_wait --name=$LXC_NAME + lxc restore $name $snapname + lxc start $name + _ynh_lxc_start_and_wait --name=$name } ynh_lxc_reset () { From 587b240dcc1c978e5ab10863ef41a493a964c4ad Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:26:47 +0200 Subject: [PATCH 33/51] 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" From 89f88521df855a4edfe6094bf0424b4c8ad59e7a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:34:52 +0200 Subject: [PATCH 34/51] Implement lxc_launch --- lib/ynh_lxd | 36 ++++++++++++++++++++++++++++++++++++ lib/ynh_lxd_package_check | 21 ++------------------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 7867cf7..0201e09 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -138,6 +138,42 @@ _ynh_lxc_start_and_wait () { LXC_IP=$(lxc exec $name -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") } +# Launch a new LXC from an image +# +# usage: ynh_lxc_launch --image=image --name=name +# | arg: -i, --image= - image to create from +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_launch (){ + # Declare an array to define the options of this helper. + local legacy_args=in + local -A args_array=([i]=image= [n]=name=) + local image + local name + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then + lxc launch yunohost:$image $name \ + -c security.nesting=true \ + -c security.privileged=true \ + -c limits.memory=80% \ + -c limits.cpu.allowance=80% \ + >>/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 + else + log_critical "Can't find base image $image, run ./package_check.sh --rebuild" + fi +} + # Clean the swapfiles of an LXC container # # usage: ynh_lxc_swapfiles_clean --name=name diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 88e1027..eca5178 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -203,25 +203,8 @@ ynh_lxc_pc_create () { log_info "Launching new LXC $name ..." # Check if we can launch container from YunoHost remote image - if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then - lxc launch yunohost:$image $name \ - -c security.nesting=true \ - -c security.privileged=true \ - -c limits.memory=80% \ - -c limits.cpu.allowance=80% \ - >>/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 - else - log_critical "Can't find base image $image, run ./package_check.sh --rebuild" - fi - + ynh_lxc_launch --image=$image --name=$name + pipestatus="${PIPESTATUS[0]}" location=$(lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"') [[ "$location" != "none" ]] && log_info "... on $location" From 822abcaa9106cc9d836f69a3e54c9388fe061420 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:38:45 +0200 Subject: [PATCH 35/51] Implement ynh_lxc_snapshot_create --- lib/ynh_lxd | 32 ++++++++++++++++++++++++++++++++ lib/ynh_lxd_package_check | 12 +----------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 0201e09..ac6850f 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -216,6 +216,38 @@ ynh_lxc_snapshot_exists () { lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null } +# Create a snapshot of an LXC container +# +# usage: ynh_lxc_snapshot_create --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_create () { + # 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 "$@" + + # Remove swap files to avoid killing the CI with huge snapshots. + ynh_lxc_swapfiles_clean --name=$name + + ynh_lxc_stop --name=$name + + # Check if the snapshot already exist + if ! ynh_lxc_snapshot_exists --name=$name --snapname="$snapname" + then + log_info "(Creating snapshot $snapname ...)" + lxc snapshot $name $snapname + else + log_info "(Recreating snapshot $snapname ...)" + lxc snapshot $name $snapname --reuse + fi +} + # Load a snapshot of an LXC container # # usage: ynh_lxc_snapshot_load --name=name --snapname=snapname diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index eca5178..05aae07 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -237,17 +237,7 @@ ynh_lxc_pc_snapshot_create () { # Check all the witness files, to verify if them still here ynh_lxc_pc_witness_files_check --name=$name >&2 - # Remove swap files to avoid killing the CI with huge snapshots. - ynh_lxc_swapfiles_clean --name=$name - - ynh_lxc_stop --name=$name - - # Check if the snapshot already exist - if ! ynh_lxc_snapshot_exists --name=$name --snapname="$snapname" - then - log_info "(Creating snapshot $snapname ...)" - lxc snapshot $name $snapname - fi + ynh_lxc_snapshot_create --name=$name --snapname=$snapname _ynh_lxc_start_and_wait --name=$name From 3042211d5a9b5b6acbd4d2146e5ae9d7163053bc Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:40:15 +0200 Subject: [PATCH 36/51] Update ynh_lxd_package_check --- lib/ynh_lxd_package_check | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ynh_lxd_package_check b/lib/ynh_lxd_package_check index 05aae07..c21cbca 100644 --- a/lib/ynh_lxd_package_check +++ b/lib/ynh_lxd_package_check @@ -213,7 +213,7 @@ ynh_lxc_pc_create () { _ynh_lxc_start_and_wait --name=$name ynh_lxc_pc_witness_files_set --name=$name - lxc snapshot $name snap0 + ynh_lxc_snapshot_create --name=$name --snapname=snap0 } # Create a snapshot of an LXC container From 3ac9db3cc163377714305345465cae28b52e2e51 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:45:11 +0200 Subject: [PATCH 37/51] Implement ynh_lxc_exists --- lib/ynh_lxd | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index ac6850f..7692e83 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -4,6 +4,28 @@ # LXD HELPERS #================================================= +# Check if a LXC container exists +# +# usage: ynh_lxc_exists --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_exists () { + # 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 ! lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .name' >/dev/null + then + return 1 + else + return 0 + fi +} + # Stopping an LXC container # # usage: ynh_lxc_stop --name=name @@ -213,7 +235,11 @@ ynh_lxc_snapshot_exists () { # 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 + # If the container exists + if ynh_lxc_exists --name=$name + then + lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null + fi } # Create a snapshot of an LXC container @@ -291,14 +317,16 @@ ynh_lxc_reset () { ynh_handle_getopts_args "$@" # If the container exists - if lxc info $name >/dev/null 2>/dev/null; then - # Remove swap files before deletting the continer + if ynh_lxc_exists --name=$name + then + # Remove swap files before deleting the container ynh_lxc_swapfiles_clean --name=$name fi ynh_lxc_stop --name=$name - if lxc info $name >/dev/null 2>/dev/null; then + if ynh_lxc_exists --name=$name + 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 From 3d6c925048eb83d1df9c0627093b89e17668e5c7 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:45:41 +0200 Subject: [PATCH 38/51] Implement ynh_lxc_status --- lib/ynh_lxd | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 7692e83..cd60100 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -26,6 +26,26 @@ ynh_lxc_exists () { fi } +# Return LXC container status +# +# usage: ynh_lxc_status --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_status () { + # 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 ynh_lxc_exists --name=$name + then + lxc list --format json | jq -r --arg name $name '.[] | select(.name==$name) | .state | .status' + fi +} + # Stopping an LXC container # # usage: ynh_lxc_stop --name=name From 9637d647afd3eabf9b8601504ed37eaae8d1c384 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:46:12 +0200 Subject: [PATCH 39/51] Implement ynh_lxc_is_started --- lib/ynh_lxd | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index cd60100..94b190a 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -46,6 +46,28 @@ ynh_lxc_status () { fi } +# Check if an LXC container is running +# +# usage: ynh_lxc_is_started --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_is_started () { + # 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 [ "$(ynh_lxc_status --name=$name)" == Running ] + then + return 0 + else + return 1 + fi +} + # Stopping an LXC container # # usage: ynh_lxc_stop --name=name From 814f41f62be1187cb6bd6dd9845e7d9f71275320 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:46:37 +0200 Subject: [PATCH 40/51] Implement ynh_lxc_is_stopped --- lib/ynh_lxd | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 94b190a..644508d 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -68,6 +68,28 @@ ynh_lxc_is_started () { fi } +# Check if an LXC container is stopped +# +# usage: ynh_lxc_is_stopped --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_is_stopped () { + # 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 [ "$(ynh_lxc_status --name=$name)" == Stopped ] + then + return 0 + else + return 1 + fi +} + # Stopping an LXC container # # usage: ynh_lxc_stop --name=name From 0b5b6422e1bc3a9a9abb7a57c6e5cbd435a0e249 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:49:29 +0200 Subject: [PATCH 41/51] Implement ynh_lxc_start --- lib/ynh_lxd | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 644508d..9d5a883 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -90,6 +90,30 @@ ynh_lxc_is_stopped () { fi } +# Start an LXC container +# +# usage: ynh_lxc_start --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_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 "$@" + + # If the container exists + if ynh_lxc_exists --name=$name + then + if ! ynh_lxc_is_started --name=$name + then + lxc start $name + fi + fi +} + # Stopping an LXC container # # usage: ynh_lxc_stop --name=name @@ -148,7 +172,7 @@ _ynh_lxc_restart_container () { ynh_handle_getopts_args "$@" ynh_lxc_stop --name=$name - lxc start "$name" + ynh_lxc_start --name=$name } # Keep sure the LXC is started @@ -276,7 +300,7 @@ ynh_lxc_swapfiles_clean () { # Restart it if needed if [ "$(lxc info $name | grep Status | awk '{print tolower($2)}')" != "running" ]; then - lxc start $name + ynh_lxc_start --name=$name _ynh_lxc_start_and_wait --name=$name fi lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' @@ -362,7 +386,7 @@ ynh_lxc_snapshot_load () { ynh_lxc_stop --name=$name lxc restore $name $snapname - lxc start $name + ynh_lxc_start --name=$name _ynh_lxc_start_and_wait --name=$name } From f5332971eddf63511d2391e8ab0ad5a028b6a61a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:50:56 +0200 Subject: [PATCH 42/51] Improving ynh_lxc_stop --- lib/ynh_lxd | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 9d5a883..5ffb48e 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -128,13 +128,34 @@ ynh_lxc_stop () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - # (We also use timeout 30 in front of the command because sometime lxc - # commands can hang forever despite the --timeout >_>...) - timeout 30 lxc stop --timeout 15 $name 2>/dev/null + # If the container exists + if ynh_lxc_exists --name=$name + then + ynh_print_info --message="Stopping LXC $name" + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + lxc stop $name + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 30 ];then + break + else + sleep 1 + fi + done - # If the command times out, then add the option --force - if [ $? -eq 124 ]; then - timeout 30 lxc stop --timeout 15 $name --force 2>/dev/null + # If the command times out, then add the option --force + wait_period=0 + while ! ynh_lxc_is_stopped --name=$name + do + lxc stop $name --force + wait_period=$(($wait_period+10)) + if [ $wait_period -gt 30 ];then + break + else + sleep 5 + fi + done fi } From ffa77e8e61bfe51e9a9ace82797884c7b9d1c38f Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:55:07 +0200 Subject: [PATCH 43/51] Improving _ynh_lxc_start_and_wait --- lib/ynh_lxd | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 5ffb48e..f904719 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -211,6 +211,7 @@ _ynh_lxc_start_and_wait () { ynh_handle_getopts_args "$@" # Try to start the container 3 times. + ynh_lxc_start --name=$name local max_try=3 local i=0 while [ $i -lt $max_try ] @@ -221,7 +222,8 @@ _ynh_lxc_start_and_wait () { # Wait for container to start, we are using systemd to check this, # for the sake of brevity. for j in $(seq 1 10); do - if lxc exec "$name" -- systemctl isolate multi-user.target >/dev/null 2>/dev/null; then + if ynh_lxc_run_inside --name=$name --command="systemctl isolate multi-user.target >/dev/null 2>/dev/null" + then break fi @@ -237,7 +239,8 @@ _ynh_lxc_start_and_wait () { # Wait for container to access the internet for j in $(seq 1 10); do - if lxc exec "$name" -- curl -s http://wikipedia.org > /dev/null 2>/dev/null; then + if ynh_lxc_run_inside --name=$name --command="curl -s http://wikipedia.org > /dev/null 2>/dev/null" + then break fi @@ -266,7 +269,7 @@ _ynh_lxc_start_and_wait () { fi done - LXC_IP=$(lxc exec $name -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>") + LXC_IP=$(ynh_lxc_run_inside --name=$name --command="hostname -I | cut -d' ' -f1 | grep -E -o \"\<[0-9.]{8,}\>\"") } # Launch a new LXC from an image @@ -321,7 +324,6 @@ ynh_lxc_swapfiles_clean () { # Restart it if needed if [ "$(lxc info $name | grep Status | awk '{print tolower($2)}')" != "running" ]; then - ynh_lxc_start --name=$name _ynh_lxc_start_and_wait --name=$name fi lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' @@ -407,7 +409,6 @@ ynh_lxc_snapshot_load () { ynh_lxc_stop --name=$name lxc restore $name $snapname - ynh_lxc_start --name=$name _ynh_lxc_start_and_wait --name=$name } From 7af7f02a7489a907dc2df7ceef9079ec46532d93 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:57:15 +0200 Subject: [PATCH 44/51] Implement ynh_lxc_delete --- lib/ynh_lxd | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index f904719..0ac2840 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -308,6 +308,28 @@ ynh_lxc_launch (){ fi } +# Delete a lxc container +# +# usage: ynh_lxc_delete --name=name +# | arg: -n, --name= - name of the LXC +# +# Requires YunoHost version *.*.* or higher. +ynh_lxc_delete () { + # 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 ynh_lxc_exists --name=$name + then + lxc delete $name --force + else + return 1 + fi +} + # Clean the swapfiles of an LXC container # # usage: ynh_lxc_swapfiles_clean --name=name From 8f91c6e28cb2ecf3b6c9ca9ce94aa71180639df9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:58:26 +0200 Subject: [PATCH 45/51] Improving ynh_lxc_swapfiles_clean --- lib/ynh_lxd | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 0ac2840..5e23b9d 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -344,12 +344,10 @@ ynh_lxc_swapfiles_clean () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - # Restart it if needed - if [ "$(lxc info $name | grep Status | awk '{print tolower($2)}')" != "running" ]; then - _ynh_lxc_start_and_wait --name=$name - fi - lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' - lxc exec $name -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' + _ynh_lxc_start_and_wait --name=$name + + ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do swapoff $swapfile; done' + ynh_lxc_run_inside --name=$name --command='for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $swapfile; done' } # Check if a snapshot exist for an LXC container From f35d08db033b686894c97b821205b6f5b825d89e Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 01:59:00 +0200 Subject: [PATCH 46/51] Improving ynh_lxc_snapshot_exists --- lib/ynh_lxd | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 5e23b9d..57e31be 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -369,7 +369,17 @@ ynh_lxc_snapshot_exists () { # If the container exists if ynh_lxc_exists --name=$name then - lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null + if lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots' >/dev/null + then + if lxc list --format json | jq -e --arg name $name --arg snapname $snapname '.[] | select(.name==$name) | .snapshots[] | select(.name==$snapname)' >/dev/null + then + return 0 + else + return 1 + fi + else + return 1 + fi fi } From fc3d11791e60a3e031dfce7766c8aa45eb78a758 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 02:01:01 +0200 Subject: [PATCH 47/51] Implement ynh_lxc_snapshot_delete --- lib/ynh_lxd | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 57e31be..b8b0275 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -415,6 +415,31 @@ ynh_lxc_snapshot_create () { fi } +# Delete a snapshot of an LXC container +# +# usage: ynh_lxc_snapshot_delete --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_delete () { + # 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 "$@" + + if ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + then + lxc delete $name/$snapname + return 0 + else + return 1 + fi +} + # Load a snapshot of an LXC container # # usage: ynh_lxc_snapshot_load --name=name --snapname=snapname From 6c0d35f5b9f998e7d9f7bdb797840b89c279d67b Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 02:05:26 +0200 Subject: [PATCH 48/51] Improving ynh_lxc_snapshot_load --- lib/ynh_lxd | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index b8b0275..5d31a00 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -456,15 +456,20 @@ ynh_lxc_snapshot_load () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - log_debug "Loading snapshot $snapname ..." + if ynh_lxc_snapshot_exists --name="$name" --snapname="$snapname" + then + log_debug "Loading snapshot $snapname ..." - # Remove swap files before restoring the snapshot. - ynh_lxc_swapfiles_clean --name=$name + # Remove swap files before restoring the snapshot. + ynh_lxc_swapfiles_clean --name=$name - ynh_lxc_stop --name=$name + ynh_lxc_stop --name=$name - lxc restore $name $snapname - _ynh_lxc_start_and_wait --name=$name + lxc restore $name $snapname + _ynh_lxc_start_and_wait --name=$name + else + return 1 + fi } # Reset an LXC container From 81b89dc3ce15e65c6a605723c270e73b63d624dc Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 24 Oct 2022 02:06:09 +0200 Subject: [PATCH 49/51] Improving ynh_lxc_reset --- lib/ynh_lxd | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 5d31a00..22b1a09 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -491,15 +491,9 @@ ynh_lxc_reset () { then # Remove swap files before deleting the container ynh_lxc_swapfiles_clean --name=$name - fi - - ynh_lxc_stop --name=$name - - if ynh_lxc_exists --name=$name - then + ynh_lxc_stop --name=$name 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 + ynh_lxc_delete --name=$name fi - - lxc delete $name --force 2>/dev/null } From 41f0704d772320dad5c4dce96e849bcb1a49b864 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 5 Nov 2022 19:48:07 +0100 Subject: [PATCH 50/51] 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 From 2adc1d98e74b204d6d01b5af1947eafd7e2401e2 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 5 Nov 2022 19:51:53 +0100 Subject: [PATCH 51/51] Charset --- lib/ynh_lxd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ynh_lxd b/lib/ynh_lxd index 0025bd1..d8e0bac 100644 --- a/lib/ynh_lxd +++ b/lib/ynh_lxd @@ -193,7 +193,7 @@ ynh_lxc_check_container_start () { ynh_handle_getopts_args "$@" ynh_print_info --message="Test du conteneur $name" - ynh_lxc_start --name=$name # Démarre le conteneur + ynh_lxc_start --name=$name # Démarre le conteneur wait_period=0 while ! ynh_lxc_is_started --name=$name