Implement _ynh_lxc_start_and_wait

This commit is contained in:
yalh76 2022-10-24 01:06:21 +02:00
parent 7a23290ae4
commit ebbbc5b419
2 changed files with 23 additions and 11 deletions

View file

@ -65,7 +65,19 @@ _ynh_lxc_restart_container () {
lxc start "$name" 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 () { _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. # Try to start the container 3 times.
local max_try=3 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, # Wait for container to start, we are using systemd to check this,
# for the sake of brevity. # for the sake of brevity.
for j in $(seq 1 10); do 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 break
fi fi
@ -86,7 +98,7 @@ _ynh_lxc_start_and_wait () {
log_debug 'Failed to start the container ... restarting ...' log_debug 'Failed to start the container ... restarting ...'
failstart=1 failstart=1
_ynh_lxc_restart_container --name="$1" _ynh_lxc_restart_container --name="$name"
fi fi
sleep 1s sleep 1s
@ -94,7 +106,7 @@ _ynh_lxc_start_and_wait () {
# Wait for container to access the internet # Wait for container to access the internet
for j in $(seq 1 10); do 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 break
fi fi
@ -102,7 +114,7 @@ _ynh_lxc_start_and_wait () {
log_debug 'Failed to access the internet ... restarting' log_debug 'Failed to access the internet ... restarting'
failstart=1 failstart=1
_ynh_lxc_restart_container --name="$1" _ynh_lxc_restart_container --name="$name"
fi fi
sleep 1s sleep 1s
@ -118,19 +130,19 @@ _ynh_lxc_start_and_wait () {
if [ $i -eq $max_try ] && [ $failstart -eq 1 ] if [ $i -eq $max_try ] && [ $failstart -eq 1 ]
then then
log_error "The container miserably failed to start or to connect to the internet" 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 return 1
fi fi
done 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 () { ynh_lxc_swapfiles_clean () {
# Restart it if needed # Restart it if needed
if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then if [ "$(lxc info $LXC_NAME | grep Status | awk '{print tolower($2)}')" != "running" ]; then
lxc start $LXC_NAME lxc start $LXC_NAME
_ynh_lxc_start_and_wait $LXC_NAME _ynh_lxc_start_and_wait --name=$LXC_NAME
fi 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 swapoff $swapfile; done'
lxc exec $LXC_NAME -- bash -c 'for swapfile in $(ls /swap_* 2>/dev/null); do rm -f $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 restore $LXC_NAME $snapname
lxc start $LXC_NAME lxc start $LXC_NAME
_ynh_lxc_start_and_wait $LXC_NAME _ynh_lxc_start_and_wait --name=$LXC_NAME
} }
ynh_lxc_reset () { ynh_lxc_reset () {

View file

@ -20,7 +20,7 @@ ynh_lxc_pc_exec () {
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
_ynh_lxc_start_and_wait $name _ynh_lxc_start_and_wait --name=$name
start_timer start_timer
@ -228,7 +228,7 @@ ynh_lxc_pc_create () {
[[ "$pipestatus" -eq 0 ]] || exit 1 [[ "$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 ynh_lxc_pc_witness_files_set --name=$name
lxc snapshot $name snap0 lxc snapshot $name snap0
} }
@ -266,7 +266,7 @@ ynh_lxc_pc_snapshot_create () {
lxc snapshot $name $snapname lxc snapshot $name $snapname
fi fi
_ynh_lxc_start_and_wait $name _ynh_lxc_start_and_wait --name=$name
stop_timer 1 stop_timer 1
} }