Spaces to tabs

This commit is contained in:
yalh76 2022-10-23 23:54:40 +02:00
parent 775d3540e1
commit 26530dd3de

View file

@ -5,138 +5,138 @@
#================================================= #=================================================
LXC_CREATE () { LXC_CREATE () {
log_info "Launching new LXC $LXC_NAME ..." log_info "Launching new LXC $LXC_NAME ..."
# Check if we can launch container from YunoHost remote image # 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 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 \ lxc launch yunohost:$LXC_BASE $LXC_NAME \
-c security.nesting=true \ -c security.nesting=true \
-c security.privileged=true \ -c security.privileged=true \
-c limits.memory=80% \ -c limits.memory=80% \
-c limits.cpu.allowance=80% \ -c limits.cpu.allowance=80% \
>>/proc/self/fd/3 >>/proc/self/fd/3
# Check if we can launch container from a local image # Check if we can launch container from a local image
elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then
lxc launch $LXC_BASE $LXC_NAME \ lxc launch $LXC_BASE $LXC_NAME \
-c security.nesting=true \ -c security.nesting=true \
-c security.privileged=true \ -c security.privileged=true \
-c limits.memory=80% \ -c limits.memory=80% \
-c limits.cpu.allowance=80% \ -c limits.cpu.allowance=80% \
>>/proc/self/fd/3 >>/proc/self/fd/3
else else
log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild"
fi fi
pipestatus="${PIPESTATUS[0]}" 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 LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"')
[[ "$location" != "none" ]] && log_info "... on $location" [[ "$location" != "none" ]] && log_info "... on $location"
[[ "$pipestatus" -eq 0 ]] || exit 1 [[ "$pipestatus" -eq 0 ]] || exit 1
_LXC_START_AND_WAIT $LXC_NAME _LXC_START_AND_WAIT $LXC_NAME
set_witness_files set_witness_files
lxc snapshot $LXC_NAME snap0 lxc snapshot $LXC_NAME snap0
} }
LXC_SNAPSHOT_EXISTS() { LXC_SNAPSHOT_EXISTS() {
local snapname=$1 local snapname=$1
lxc list --format json \ lxc list --format json \
| jq -e --arg LXC_NAME $LXC_NAME --arg snapname $snapname \ | jq -e --arg LXC_NAME $LXC_NAME --arg snapname $snapname \
'.[] | select(.name==$LXC_NAME) | .snapshots[] | select(.name==$snapname)' \ '.[] | select(.name==$LXC_NAME) | .snapshots[] | select(.name==$snapname)' \
>/dev/null >/dev/null
} }
CREATE_LXC_SNAPSHOT () { 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 all the witness files, to verify if them still here
check_witness_files >&2 check_witness_files >&2
# Remove swap files to avoid killing the CI with huge snapshots. # Remove swap files to avoid killing the CI with huge snapshots.
CLEAN_SWAPFILES CLEAN_SWAPFILES
LXC_STOP $LXC_NAME LXC_STOP $LXC_NAME
# Check if the snapshot already exist # Check if the snapshot already exist
if ! LXC_SNAPSHOT_EXISTS "$snapname" if ! LXC_SNAPSHOT_EXISTS "$snapname"
then then
log_info "(Creating snapshot $snapname ...)" log_info "(Creating snapshot $snapname ...)"
lxc snapshot $LXC_NAME $snapname lxc snapshot $LXC_NAME $snapname
fi fi
_LXC_START_AND_WAIT $LXC_NAME _LXC_START_AND_WAIT $LXC_NAME
stop_timer 1 stop_timer 1
} }
LOAD_LXC_SNAPSHOT () { LOAD_LXC_SNAPSHOT () {
local snapname=$1 local snapname=$1
log_debug "Loading snapshot $snapname ..." log_debug "Loading snapshot $snapname ..."
# Remove swap files before restoring the snapshot. # Remove swap files before restoring the snapshot.
CLEAN_SWAPFILES CLEAN_SWAPFILES
LXC_STOP $LXC_NAME LXC_STOP $LXC_NAME
lxc restore $LXC_NAME $snapname lxc restore $LXC_NAME $snapname
lxc start $LXC_NAME lxc start $LXC_NAME
_LXC_START_AND_WAIT $LXC_NAME _LXC_START_AND_WAIT $LXC_NAME
} }
#================================================= #=================================================
LXC_EXEC () { LXC_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 _LXC_START_AND_WAIT $LXC_NAME
start_timer start_timer
# Execute the command given in argument in the container and log its results. # 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 $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 # Store the return code of the command
local returncode=${PIPESTATUS[0]} local returncode=${PIPESTATUS[0]}
log_debug "Return code: $returncode" log_debug "Return code: $returncode"
stop_timer 1 stop_timer 1
# Return the exit code of the ssh command # Return the exit code of the ssh command
return $returncode return $returncode
} }
LXC_STOP () { LXC_STOP () {
local container_to_stop=$1 local container_to_stop=$1
# (We also use timeout 30 in front of the command because sometime lxc # (We also use timeout 30 in front of the command because sometime lxc
# commands can hang forever despite the --timeout >_>...) # 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 $container_to_stop 2>/dev/null
# If the command times out, then add the option --force # If the command times out, then add the option --force
if [ $? -eq 124 ]; then if [ $? -eq 124 ]; then
timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null
fi fi
} }
LXC_RESET () { LXC_RESET () {
# If the container exists # If the container exists
if lxc info $LXC_NAME >/dev/null 2>/dev/null; then if lxc info $LXC_NAME >/dev/null 2>/dev/null; then
# Remove swap files before deletting the continer # Remove swap files before deletting the continer
CLEAN_SWAPFILES CLEAN_SWAPFILES
fi fi
LXC_STOP $LXC_NAME LXC_STOP $LXC_NAME
if lxc info $LXC_NAME >/dev/null 2>/dev/null; then 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') 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 swapoff "$(lxc storage get $current_storage source)/containers/$LXC_NAME/rootfs/swap" 2>/dev/null
fi 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() restart_container()
{ {
LXC_STOP $1 LXC_STOP $1
lxc start "$1" lxc start "$1"
} }
@ -198,115 +198,115 @@ _LXC_START_AND_WAIT() {
# Fail if the container failed to start # Fail if the container failed to start
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 $1
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 $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 _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'
} }
RUN_INSIDE_LXC() { RUN_INSIDE_LXC() {
lxc exec $LXC_NAME -- "$@" lxc exec $LXC_NAME -- "$@"
} }
set_witness_files () { set_witness_files () {
# Create files to check if the remove script does not remove them accidentally # Create files to check if the remove script does not remove them accidentally
log_debug "Create witness files..." log_debug "Create witness files..."
create_witness_file () { create_witness_file () {
[ "$2" = "file" ] && local action="touch" || local action="mkdir -p" [ "$2" = "file" ] && local action="touch" || local action="mkdir -p"
RUN_INSIDE_LXC $action $1 RUN_INSIDE_LXC $action $1
} }
# Nginx conf # Nginx conf
create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file create_witness_file "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" file
create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file create_witness_file "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" file
# /etc # /etc
create_witness_file "/etc/witnessfile" file create_witness_file "/etc/witnessfile" file
# /opt directory # /opt directory
create_witness_file "/opt/witnessdir" directory create_witness_file "/opt/witnessdir" directory
# /var/www directory # /var/www directory
create_witness_file "/var/www/witnessdir" directory create_witness_file "/var/www/witnessdir" directory
# /home/yunohost.app/ # /home/yunohost.app/
create_witness_file "/home/yunohost.app/witnessdir" directory create_witness_file "/home/yunohost.app/witnessdir" directory
# /var/log # /var/log
create_witness_file "/var/log/witnessfile" file create_witness_file "/var/log/witnessfile" file
# Config fpm # Config fpm
create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file create_witness_file "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" file
# Config logrotate # Config logrotate
create_witness_file "/etc/logrotate.d/witnessfile" file create_witness_file "/etc/logrotate.d/witnessfile" file
# Config systemd # Config systemd
create_witness_file "/etc/systemd/system/witnessfile.service" file create_witness_file "/etc/systemd/system/witnessfile.service" file
# Database # Database
RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1 RUN_INSIDE_LXC mysqladmin --wait status > /dev/null 2>&1
echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1 echo "CREATE DATABASE witnessdb" | RUN_INSIDE_LXC mysql --wait > /dev/null 2>&1
} }
check_witness_files () { 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 () { check_file_exist () {
if RUN_INSIDE_LXC test ! -e "$1" if RUN_INSIDE_LXC test ! -e "$1"
then then
log_error "The file $1 is missing ! Something gone wrong !" log_error "The file $1 is missing ! Something gone wrong !"
SET_RESULT "failure" witness SET_RESULT "failure" witness
fi fi
} }
# Nginx conf # Nginx conf
check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf" check_file_exist "/etc/nginx/conf.d/$DOMAIN.d/witnessfile.conf"
check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf" check_file_exist "/etc/nginx/conf.d/$SUBDOMAIN.d/witnessfile.conf"
# /etc # /etc
check_file_exist "/etc/witnessfile" check_file_exist "/etc/witnessfile"
# /opt directory # /opt directory
check_file_exist "/opt/witnessdir" check_file_exist "/opt/witnessdir"
# /var/www directory # /var/www directory
check_file_exist "/var/www/witnessdir" check_file_exist "/var/www/witnessdir"
# /home/yunohost.app/ # /home/yunohost.app/
check_file_exist "/home/yunohost.app/witnessdir" check_file_exist "/home/yunohost.app/witnessdir"
# /var/log # /var/log
check_file_exist "/var/log/witnessfile" check_file_exist "/var/log/witnessfile"
# Config fpm # Config fpm
check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf" check_file_exist "/etc/php/$DEFAULT_PHP_VERSION/fpm/pool.d/witnessfile.conf"
# Config logrotate # Config logrotate
check_file_exist "/etc/logrotate.d/witnessfile" check_file_exist "/etc/logrotate.d/witnessfile"
# Config systemd # Config systemd
check_file_exist "/etc/systemd/system/witnessfile.service" check_file_exist "/etc/systemd/system/witnessfile.service"
# Database # Database
if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1 if ! RUN_INSIDE_LXC mysqlshow witnessdb > /dev/null 2>&1
then then
log_error "The database witnessdb is missing ! Something gone wrong !" log_error "The database witnessdb is missing ! Something gone wrong !"
SET_RESULT "failure" witness SET_RESULT "failure" witness
return 1 return 1
fi fi
} }