yunohost-ci/utils.sh

285 lines
11 KiB
Bash
Raw Normal View History

2020-03-26 17:50:12 +01:00
#!/usr/bin/env bash
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
2020-05-26 19:32:46 +02:00
source $current_dir/prints.sh
2020-05-26 19:30:45 +02:00
source $current_dir/variables.sh # Get variables from variables.
2020-05-26 19:21:35 +02:00
2020-03-26 17:50:12 +01:00
wait_container()
{
2020-04-30 19:40:43 +02:00
restart_container()
{
2024-03-10 20:48:39 +01:00
incus stop "$1"
incus start "$1"
2020-04-30 19:40:43 +02:00
}
2020-03-26 17:50:12 +01:00
2020-04-30 19:40:43 +02:00
# 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
2020-03-26 17:50:12 +01:00
2020-04-30 19:40:43 +02:00
# Wait for container to start, we are using systemd to check this,
# for the sake of brevity.
for j in $(seq 1 10); do
2024-03-10 20:48:39 +01:00
if incus exec "$1" -- /bin/bash -c "systemctl isolate multi-user.target" >/dev/null 2>/dev/null; then
2020-04-30 19:40:43 +02:00
break
fi
2020-04-30 19:30:39 +02:00
2020-04-30 19:40:43 +02:00
if [ "$j" == "10" ]; then
2020-05-26 19:21:35 +02:00
error 'Failed to start the container'
2020-04-30 19:40:43 +02:00
failstart=1
restart_container "$1"
fi
2020-04-30 19:30:39 +02:00
2020-04-30 19:40:43 +02:00
sleep 1s
done
2020-04-30 19:30:39 +02:00
2020-04-30 19:40:43 +02:00
# Wait for container to access the internet
for j in $(seq 1 10); do
2024-03-10 20:48:39 +01:00
if incus exec "$1" -- /bin/bash -c "! which wget > /dev/null 2>&1 || wget -q --spider http://github.com"; then
2020-04-30 19:40:43 +02:00
break
fi
if [ "$j" == "10" ]; then
2020-05-26 19:21:35 +02:00
error 'Failed to access the internet'
2020-05-26 19:09:17 +02:00
failstart=1
2024-03-10 20:48:39 +01:00
incus exec "$1" -- /bin/bash -c "echo 'resolv-file=/etc/resolv.dnsmasq.conf' > /etc/dnsmasq.d/resolvconf"
incus exec "$1" -- /bin/bash -c "echo 'nameserver 8.8.8.8' > /etc/resolv.dnsmasq.conf"
incus exec "$1" -- /bin/bash -c "sed -i 's/#IGNORE/IGNORE/g' /etc/default/dnsmasq"
incus exec "$1" -- /bin/bash -c "systemctl restart dnsmasq"
incus exec "$1" -- /bin/bash -c "journalctl -u dnsmasq -n 100 --no-pager"
2020-05-26 19:09:17 +02:00
restart_container "$1"
fi
sleep 1s
done
# Wait dpkg
for j in $(seq 1 10); do
2024-03-10 20:48:39 +01:00
if ! incus exec "$1" -- /bin/bash -c "fuser /var/lib/dpkg/lock > /dev/null 2>&1" &&
! incus exec "$1" -- /bin/bash -c "fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1" &&
! incus exec "$1" -- /bin/bash -c "fuser /var/cache/apt/archives/lock > /dev/null 2>&1"; then
2020-05-26 19:09:17 +02:00
break
fi
if [ "$j" == "10" ]; then
2020-05-26 19:21:35 +02:00
error 'Waiting too long for lock release'
2020-04-30 19:40:43 +02:00
failstart=1
restart_container "$1"
fi
sleep 1s
done
# Has started and has access to the internet
if [ $failstart -eq 0 ]
then
break
2020-04-30 19:30:39 +02:00
fi
2020-04-30 19:40:43 +02:00
# Fail if the container failed to start
if [ $i -eq $max_try ] && [ $failstart -eq 1 ]
then
# Inform GitLab Runner that this is a system failure, so it
# should be retried.
exit "$SYSTEM_FAILURE_EXIT_CODE"
fi
2020-04-30 19:30:39 +02:00
done
2020-03-26 17:50:12 +01:00
}
create_snapshot()
2020-03-28 22:02:10 +01:00
{
local instance_to_publish=$1
2023-05-17 15:10:29 +02:00
local ynh_version=$2
local snapshot=$3
2023-05-17 12:13:08 +02:00
# Create snapshot
2024-03-10 20:48:39 +01:00
incus snapshot "$instance_to_publish" "$ynh_version-$snapshot" --reuse
2023-05-17 15:10:29 +02:00
}
restore_snapshot()
{
2024-03-10 20:48:39 +01:00
local incus_name=$1
2023-05-17 15:10:29 +02:00
local ynh_version=$2
local snapshot=$3
2024-03-10 20:48:39 +01:00
local retry_incus=0
2024-03-10 20:48:39 +01:00
while [[ ${retry_incus} -lt 10 ]]
do
2024-03-10 20:48:39 +01:00
incus restore "$incus_name" "$ynh_version-$snapshot" && break || retry_incus=$(($retry_incus+1))
info "Failed to restore snapshot? Retrying in 10 sec ..."
2024-03-10 20:48:39 +01:00
if [[ ${retry_incus} -ge 3 ]]
then
warn "If this keeps happening, restarting the LXD daemon might help :| ..."
fi
sleep 10
done
2024-03-10 20:48:39 +01:00
if [[ ${retry_incus} -ge 10 ]]
then
error "Failed to properly restore the snapshot zrgmblg"
return 1
fi
2020-03-28 22:02:10 +01:00
}
2020-05-20 14:35:41 +02:00
# These lines are used to extract the dependencies/recommendations from the debian/control file.
# /!\ There's a high risk of lamentable failure if we change the format of this file
get_dependencies()
{
2023-05-17 12:48:04 +02:00
local debian_version=$1
if [[ "$debian_version" == "bullseye" ]]
then
local branch="dev"
else
local branch="$debian_version"
fi
2023-05-16 12:14:15 +02:00
2023-05-17 12:48:04 +02:00
# To extract the dependencies, we want to retrieve the lines between "^Dependencies:" and the new line that doesn't start with a space (exclusively) . Then, we remove ",", then we remove the version specifiers "(>= X.Y)", then we add simple quotes to packages when there is a pipe (or) 'php-mysql|php-mysqlnd'.
YUNOHOST_DEPENDENCIES=$(curl https://raw.githubusercontent.com/YunoHost/yunohost/$branch/debian/control 2> /dev/null | sed -n '/^Depends:/,/^\w/{//!p}' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | grep -v moulinette | grep -v ssowat | tr "\n" " ")
YUNOHOST_RECOMMENDS=$(curl https://raw.githubusercontent.com/YunoHost/yunohost/$branch/debian/control 2> /dev/null | sed -n '/^Recommends:/,/^\w/{//!p}' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | tr "\n" " ")
MOULINETTE_DEPENDENCIES=$(curl https://raw.githubusercontent.com/YunoHost/moulinette/$branch/debian/control 2> /dev/null | sed -n '/^Depends:/,/^\w/{//!p}' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | tr "\n" " ")
# Same as above, except that all dependencies are in the same line
SSOWAT_DEPENDENCIES=$(curl https://raw.githubusercontent.com/YunoHost/ssowat/$branch/debian/control 2> /dev/null | grep '^Depends:' | sed 's/Depends://' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | tr "\n" " ")
BUILD_DEPENDENCIES="git-buildpackage postfix python3-setuptools python3-pip devscripts"
2023-05-22 11:44:19 +02:00
TESTS_DEPENDENCIES="git hub"
2023-05-17 12:48:04 +02:00
PIP3_PKG='mock pip pyOpenSSL pytest pytest-cov pytest-mock pytest-sugar requests-mock tox ansi2html black jinja2 types-ipaddress types-enum34 types-cryptography types-toml types-requests types-PyYAML types-pyOpenSSL types-mock "packaging<22"'
2023-05-17 15:10:29 +02:00
2023-05-17 12:48:04 +02:00
if [[ "$debian_version" == "bookworm" ]]
then
2023-05-22 11:44:19 +02:00
# We add php8.2-cli, mariadb-client and mariadb-server to the dependencies for test_app_resources
TESTS_DEPENDENCIES="$TESTS_DEPENDENCIES php8.2-cli mariadb-client mariadb-server"
2023-05-17 12:48:04 +02:00
PIP3_PKG="$PIP3_PKG --break-system-packages"
fi
2020-05-20 14:35:41 +02:00
}
2020-03-28 22:02:10 +01:00
2020-03-26 17:50:12 +01:00
rebuild_base_containers()
{
2023-05-17 16:48:45 +02:00
local image_to_rebuild=$1
local debian_version=$2
local ynh_version=$3
local arch=$4
2023-05-17 15:10:29 +02:00
2024-03-10 20:48:39 +01:00
if incus info "$image_to_rebuild" &>/dev/null
2023-05-17 15:10:29 +02:00
then
2024-03-10 20:48:39 +01:00
incus delete -f "$image_to_rebuild"
2023-05-17 15:10:29 +02:00
fi
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus launch images:debian/$debian_version/$arch "$image_to_rebuild" -c security.nesting=true
2020-03-26 17:50:12 +01:00
2023-05-17 16:48:45 +02:00
wait_container "$image_to_rebuild"
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "apt-get update"
incus exec "$image_to_rebuild" -- /bin/bash -c "apt-get install --assume-yes wget curl"
2020-03-26 17:50:12 +01:00
# Install Git LFS, git comes pre installed with ubuntu image.
2021-03-09 21:47:42 +01:00
# Disable this line because we don't need to add a new repo to have git-lfs
2024-03-10 20:48:39 +01:00
#incus exec "$image_to_rebuild" -- /bin/bash -c "curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash"
incus exec "$image_to_rebuild" -- /bin/bash -c "apt-get install --assume-yes git-lfs"
2020-03-26 17:50:12 +01:00
# Install gitlab-runner binary since we need for cache/artifacts.
if [[ $debian_version == "bullseye" ]]
then
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "wget https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_amd64.deb"
incus exec "$image_to_rebuild" -- /bin/bash -c "dpkg -i gitlab-runner_amd64.deb"
else
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "curl -s https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | os=debian dist=$debian_version bash"
incus exec "$image_to_rebuild" -- /bin/bash -c "apt-get install --assume-yes gitlab-runner"
fi
2020-05-18 16:52:26 +02:00
2022-01-20 19:09:20 +01:00
INSTALL_SCRIPT="https://raw.githubusercontent.com/YunoHost/install_script/main/$debian_version"
# Download the YunoHost install script
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "curl $INSTALL_SCRIPT > install.sh"
# Patch the YunoHost install script
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "sed -i -E 's/(step\s+install_yunohost_packages)/#\1/' install.sh"
incus exec "$image_to_rebuild" -- /bin/bash -c "sed -i -E 's/(step\s+restart_services)/echo skip restart service #\1/' install.sh"
2020-03-26 17:50:12 +01:00
# Run the YunoHost install script patched
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "cat install.sh | bash -s -- -a -d $ynh_version"
2020-05-19 11:44:57 +02:00
2023-05-16 12:14:15 +02:00
get_dependencies $debian_version
2020-05-19 17:48:31 +02:00
2020-05-18 16:52:26 +02:00
# Pre install dependencies
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "DEBIAN_FRONTEND=noninteractive SUDO_FORCE_REMOVE=yes apt-get --assume-yes install --assume-yes $YUNOHOST_DEPENDENCIES $YUNOHOST_RECOMMENDS $MOULINETTE_DEPENDENCIES $SSOWAT_DEPENDENCIES $BUILD_DEPENDENCIES $TESTS_DEPENDENCIES"
incus exec "$image_to_rebuild" -- /bin/bash -c "python3 -m pip install -U $PIP3_PKG"
2020-05-18 17:41:33 +02:00
2020-08-15 11:04:26 +02:00
# Disable apt-daily
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "systemctl -q disable apt-daily.timer --now"
incus exec "$image_to_rebuild" -- /bin/bash -c "systemctl -q disable apt-daily-upgrade.timer --now"
incus exec "$image_to_rebuild" -- /bin/bash -c "systemctl -q disable apt-daily.service --now"
incus exec "$image_to_rebuild" -- /bin/bash -c "systemctl -q disable apt-daily-upgrade.service --now"
2020-08-15 11:04:26 +02:00
2023-05-17 15:10:29 +02:00
mkdir -p $current_dir/cache
chmod 777 $current_dir/cache
2024-03-10 20:48:39 +01:00
incus config device add "$image_to_rebuild" cache-folder disk path=/cache source="$current_dir/cache"
2023-05-17 13:03:46 +02:00
2023-05-17 16:48:45 +02:00
create_snapshot "$image_to_rebuild" "$ynh_version" "before-install"
echo "Created snapshot base 'before-install' for $image_to_rebuild"
2020-08-17 18:39:56 +02:00
# Install YunoHost
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "curl $INSTALL_SCRIPT | bash -s -- -a -d $ynh_version"
2020-08-17 18:39:56 +02:00
# Run postinstall
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- /bin/bash -c "yunohost tools postinstall -d domain.tld -u syssa -F 'Syssa Mine' -p the_password --ignore-dyndns --force-diskspace"
2020-08-17 18:39:56 +02:00
2024-03-10 20:48:39 +01:00
# Disable services which are not really mandatory, to reduce the incus memory footprint and hopefully speed things up a bit
if [[ "$debian_version" == "bookworm" ]]
then
2024-03-10 20:48:39 +01:00
incus exec "$image_to_rebuild" -- systemctl -q disable metronome --now
incus exec "$image_to_rebuild" -- systemctl -q disable rspamd --now
incus exec "$image_to_rebuild" -- systemctl -q disable postsrsd --now
incus exec "$image_to_rebuild" -- systemctl -q disable yunohost-api --now
incus exec "$image_to_rebuild" -- systemctl -q disable fake-hwclock.service --now
incus exec "$image_to_rebuild" -- systemctl -q disable yunoprompt --now
incus exec "$image_to_rebuild" -- systemctl -q disable haveged.service --now
incus exec "$image_to_rebuild" -- systemctl -q disable unattended-upgrades.service --now
incus exec "$image_to_rebuild" -- systemctl -q disable e2scrub_all.timer --now
incus exec "$image_to_rebuild" -- systemctl -q disable logrotate.timer --now
incus exec "$image_to_rebuild" -- systemctl -q disable phpsessionclean.timer --now
incus exec "$image_to_rebuild" -- systemctl -q disable systemd-tmpfiles-clean.timer --now
fi
2023-05-17 16:48:45 +02:00
create_snapshot "$image_to_rebuild" "$ynh_version" "after-install"
echo "Created snapshot base 'after-install' for $image_to_rebuild"
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus stop "$image_to_rebuild"
2020-03-26 17:50:12 +01:00
}
2023-05-16 17:31:19 +02:00
update_container() {
2023-05-17 16:48:45 +02:00
local image_to_update=$1
local debian_version=$2
local ynh_version=$3
local snapshot=$4
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
if ! incus info "$image_to_update" &>/dev/null
2020-05-18 16:52:26 +02:00
then
2020-05-26 19:21:35 +02:00
error "Unable to upgrade image $image_to_update"
2020-05-18 16:52:26 +02:00
return
fi
2020-03-26 17:50:12 +01:00
# Start and run upgrade
2023-05-17 15:10:29 +02:00
restore_snapshot "$image_to_update" "$ynh_version" "$snapshot"
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus start "$image_to_update" 2>&1 || true
2023-05-17 15:10:29 +02:00
wait_container "$image_to_update"
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus exec "$image_to_update" -- /bin/bash -c "apt-get update"
incus exec "$image_to_update" -- /bin/bash -c "apt-get upgrade --assume-yes"
2020-05-19 20:57:20 +02:00
2023-05-16 12:14:15 +02:00
get_dependencies $debian_version
2020-05-19 20:57:20 +02:00
2024-03-10 20:48:39 +01:00
incus exec "$image_to_update" -- /bin/bash -c "DEBIAN_FRONTEND=noninteractive SUDO_FORCE_REMOVE=yes apt-get --assume-yes -o Dpkg::Options::=\"--force-confold\" install --assume-yes $YUNOHOST_DEPENDENCIES $YUNOHOST_RECOMMENDS $MOULINETTE_DEPENDENCIES $SSOWAT_DEPENDENCIES $BUILD_DEPENDENCIES $TESTS_DEPENDENCIES"
incus exec "$image_to_update" -- /bin/bash -c "python3 -m pip install -U $PIP3_PKG"
2020-03-26 17:50:12 +01:00
2023-05-17 15:10:29 +02:00
create_snapshot "$image_to_update" "$ynh_version" "$snapshot"
2020-03-26 17:50:12 +01:00
2024-03-10 20:48:39 +01:00
incus stop "$image_to_update"
2020-05-03 23:44:49 +02:00
}