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
clean_containers( )
{
2020-05-18 16:52:26 +02:00
local base_image_to_clean = $1
2020-03-26 17:50:12 +01:00
for image_to_delete in " $base_image_to_clean " { ,"-tmp" }
do
if lxc info $image_to_delete & >/dev/null
then
2020-03-26 18:12:20 +01:00
lxc delete $image_to_delete --force
2020-03-26 17:50:12 +01:00
fi
done
2020-05-19 16:19:08 +02:00
for image_to_delete in " $base_image_to_clean - " { "before-install" ,"after-install" }
do
if lxc image info $image_to_delete & >/dev/null
then
lxc image delete $image_to_delete
fi
done
2020-03-26 17:50:12 +01:00
}
wait_container( )
{
2020-04-30 19:40:43 +02:00
restart_container( )
{
lxc stop " $1 "
lxc start " $1 "
}
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
if lxc exec " $1 " -- /bin/bash -c "systemctl isolate multi-user.target" >/dev/null 2>/dev/null; then
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
2020-08-15 11:12:48 +02:00
if lxc 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
restart_container " $1 "
fi
sleep 1s
done
# Wait dpkg
for j in $( seq 1 10) ; do
2020-08-15 10:58:35 +02:00
if ! lxc exec " $1 " -- /bin/bash -c "fuser /var/lib/dpkg/lock > /dev/null 2>&1" &&
! lxc exec " $1 " -- /bin/bash -c "fuser /var/lib/dpkg/lock-frontend > /dev/null 2>&1" &&
! lxc 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
}
2020-03-28 22:02:10 +01:00
rotate_image( )
{
local instance_to_publish = $1
local alias_image = $2
# Save the finger print to delete the old image later
2020-05-18 16:52:26 +02:00
local finger_print_to_delete = $( lxc image info " $alias_image " | grep Fingerprint | awk '{print $2}' )
2020-03-28 22:02:10 +01:00
local should_restart = 0
# If the container is running, stop it
if [ $( lxc info $instance_to_publish | grep Status | awk '{print $2}' ) = "Running" ]
then
should_restart = 1
lxc stop " $instance_to_publish "
fi
# Create image before install
lxc publish " $instance_to_publish " --alias " $alias_image "
# Remove old image
lxc image delete " $finger_print_to_delete "
if [ $should_restart = 1 ]
then
lxc start " $instance_to_publish "
2020-03-28 22:57:00 +01:00
wait_container " $instance_to_publish "
2020-03-28 22:02:10 +01:00
fi
}
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( )
{
# 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'.
2021-03-09 19:39:29 +01:00
YUNOHOST_DEPENDENCIES = $( curl https://raw.githubusercontent.com/YunoHost/yunohost/dev/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/dev/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/dev/debian/control 2> /dev/null | sed -n '/^Depends:/,/^\w/{//!p}' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | tr "\n" " " )
2020-05-20 14:35:41 +02:00
# Same as above, except that all dependencies are in the same line
2021-03-09 19:39:29 +01:00
SSOWAT_DEPENDENCIES = $( curl https://raw.githubusercontent.com/YunoHost/ssowat/dev/debian/control 2> /dev/null | grep '^Depends:' | sed 's/Depends://' | sed -e "s/,//g" -e "s/[(][^)]*[)]//g" -e "s/ | \S\+//g" | tr "\n" " " )
2021-03-09 21:38:11 +01:00
BUILD_DEPENDENCIES = "git-buildpackage postfix python-setuptools python3-pip devscripts"
2020-08-10 10:24:52 +02:00
PIP_PKG = "mock pip pytest pytest-cov pytest-mock pytest-sugar requests-mock tox ansi2html"
2021-03-09 21:38:11 +01:00
PIP3_PKG = "mock pip pytest pytest-cov pytest-mock pytest-sugar requests-mock tox ansi2html black"
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( )
{
2020-05-18 16:52:26 +02:00
local debian_version = $1
local ynh_version = $2
local arch = $3
local base_image_to_rebuild = " yunohost- $debian_version - $ynh_version "
2020-03-26 17:50:12 +01:00
2020-03-26 18:12:20 +01:00
lxc launch images:debian/$debian_version /$arch " $base_image_to_rebuild -tmp "
2020-03-26 17:50:12 +01:00
wait_container " $base_image_to_rebuild -tmp "
2020-08-01 21:17:14 +02:00
lxc config set " $base_image_to_rebuild -tmp " security.nesting true # Need this for buster because it is using apparmor
2020-04-08 14:37:04 +02:00
2020-03-26 17:50:12 +01:00
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "apt-get update"
2020-05-18 16:52:26 +02:00
lxc exec " $base_image_to_rebuild -tmp " -- /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.
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | bash"
2020-05-18 16:52:26 +02:00
lxc exec " $base_image_to_rebuild -tmp " -- /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.
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "curl -s https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | bash"
2020-05-18 16:52:26 +02:00
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "apt-get install --assume-yes gitlab-runner"
2021-03-09 20:29:26 +01:00
INSTALL_SCRIPT = " https://install.yunohost.org/ $debian_version "
2020-03-26 17:50:12 +01:00
2020-05-19 16:19:08 +02:00
# Download the YunoHost install script
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c " curl $INSTALL_SCRIPT > install.sh "
# Patch the YunoHost install script
2020-05-19 16:56:57 +02:00
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "sed -i -E 's/(step\s+install_yunohost_packages)/#\1/' install.sh"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "sed -i -E 's/(step\s+restart_services)/#\1/' install.sh"
2020-03-26 17:50:12 +01:00
2020-05-19 16:19:08 +02:00
# Run the YunoHost install script patched
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c " cat install.sh | bash -s -- -a -d $ynh_version "
2020-05-19 11:44:57 +02:00
2021-03-09 19:39:29 +01:00
get_dependencies
2020-05-19 17:48:31 +02:00
2020-05-18 16:52:26 +02:00
# Pre install dependencies
2020-05-20 14:35:41 +02:00
lxc exec " $base_image_to_rebuild -tmp " -- /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 "
2021-03-09 21:38:11 +01:00
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c " python -m pip install -U $PIP_PKG "
lxc exec " $base_image_to_rebuild -tmp " -- /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
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q stop apt-daily.timer"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q stop apt-daily-upgrade.timer"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q stop apt-daily.service"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q stop apt-daily-upgrade.service"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q disable apt-daily.timer"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q disable apt-daily-upgrade.timer"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q disable apt-daily.service"
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "systemctl -q disable apt-daily-upgrade.service"
2020-08-17 18:39:56 +02:00
rotate_image " $base_image_to_rebuild -tmp " " $base_image_to_rebuild -before-install "
# Install YunoHost
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c " curl $INSTALL_SCRIPT | bash -s -- -a -d $ynh_version "
# Run postinstall
lxc exec " $base_image_to_rebuild -tmp " -- /bin/bash -c "yunohost tools postinstall -d domain.tld -p the_password --ignore-dyndns"
2020-05-19 16:19:08 +02:00
rotate_image " $base_image_to_rebuild -tmp " " $base_image_to_rebuild -after-install "
2020-03-26 17:50:12 +01:00
2020-03-28 22:02:10 +01:00
lxc stop " $base_image_to_rebuild -tmp "
2020-03-28 22:57:00 +01:00
2020-03-26 17:50:12 +01:00
lxc delete " $base_image_to_rebuild -tmp "
}
update_image( ) {
2020-05-19 20:57:20 +02:00
local debian_version = $1
local ynh_version = $2
local snapshot = $3
local image_to_update = " yunohost- $debian_version - $ynh_version - $snapshot "
2020-03-26 17:50:12 +01:00
2020-05-18 16:52:26 +02:00
if ! lxc image info " $image_to_update " & >/dev/null
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
lxc launch " $image_to_update " " $image_to_update -tmp "
wait_container " $image_to_update -tmp "
lxc exec " $image_to_update -tmp " -- /bin/bash -c "apt-get update"
2020-05-19 00:54:46 +02:00
lxc exec " $image_to_update -tmp " -- /bin/bash -c "apt-get upgrade --assume-yes"
2020-05-19 20:57:20 +02:00
2021-03-09 19:39:29 +01:00
get_dependencies
2020-05-19 20:57:20 +02:00
2020-05-20 14:35:41 +02:00
lxc exec " $image_to_update -tmp " -- /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 "
2021-03-09 21:38:11 +01:00
lxc exec " $image_to_update -tmp " -- /bin/bash -c " python -m pip install -U $PIP_PKG "
lxc exec " $image_to_update -tmp " -- /bin/bash -c " python3 -m pip install -U $PIP3_PKG "
2020-05-18 17:41:33 +02:00
2020-03-28 22:02:10 +01:00
rotate_image " $image_to_update -tmp " " $image_to_update "
2020-03-26 17:50:12 +01:00
2020-03-28 22:02:10 +01:00
lxc stop " $image_to_update -tmp "
2020-03-26 17:50:12 +01:00
lxc delete " $image_to_update -tmp "
2020-05-03 23:44:49 +02:00
}