_LXC_START_AND_WAIT to _ynh_lxc_start_and_wait

This commit is contained in:
yalh76 2022-10-24 00:18:41 +02:00
parent b134ed4f86
commit 17a2403c9d
2 changed files with 73 additions and 73 deletions

View file

@ -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 () { ynh_lxc_snapshot_exists () {
local snapname=$1 local snapname=$1
lxc list --format json \ lxc list --format json \
@ -36,7 +104,7 @@ ynh_lxc_snapshot_load () {
lxc restore $LXC_NAME $snapname lxc restore $LXC_NAME $snapname
lxc start $LXC_NAME lxc start $LXC_NAME
_LXC_START_AND_WAIT $LXC_NAME _ynh_lxc_start_and_wait $LXC_NAME
} }
ynh_lxc_reset () { ynh_lxc_reset () {

View file

@ -8,7 +8,7 @@ ynh_lxc_pc_exec () {
# Start the lxc container and execute the given command in it # Start the lxc container and execute the given command in it
local cmd=$1 local cmd=$1
_LXC_START_AND_WAIT $LXC_NAME _ynh_lxc_start_and_wait $LXC_NAME
start_timer start_timer
@ -143,7 +143,7 @@ ynh_lxc_pc_create () {
[[ "$pipestatus" -eq 0 ]] || exit 1 [[ "$pipestatus" -eq 0 ]] || exit 1
_LXC_START_AND_WAIT $LXC_NAME _ynh_lxc_start_and_wait $LXC_NAME
ynh_lxc_pc_witness_files_set ynh_lxc_pc_witness_files_set
lxc snapshot $LXC_NAME snap0 lxc snapshot $LXC_NAME snap0
} }
@ -170,84 +170,16 @@ ynh_lxc_pc_snapshot_create () {
lxc snapshot $LXC_NAME $snapname lxc snapshot $LXC_NAME $snapname
fi fi
_LXC_START_AND_WAIT $LXC_NAME _ynh_lxc_start_and_wait $LXC_NAME
stop_timer 1 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() { CLEAN_SWAPFILES() {
# 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
_LXC_START_AND_WAIT $LXC_NAME _ynh_lxc_start_and_wait $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'