lxd_img_builder/image_builder

151 lines
4.6 KiB
Bash
Executable file

#!/bin/bash
set -eu
source recipes
readonly CMD=${1:-help}
readonly RELEASE=${2:-stable}
readonly DEBIAN_VERSION=${3:-bullseye}
readonly ARCH=${4:-$(dpkg --print-architecture)}
readonly CONTAINER=$RELEASE-$DEBIAN_VERSION-$ARCH
readonly IN_CONTAINER="incus exec $CONTAINER --"
[[ "$DEBIAN_VERSION" == "bullseye" ]] && gitbranch="dev" || gitbranch="$DEBIAN_VERSION"
function help()
{
cat << EOF
Usage: ./image_builder [command] [additional args...]
The 'base' image is after yunohost install script, but before postinstall. It is fetched by ynh-dev for local development.
The 'appci' image is after postinstall, with a first user named 'package_checker' as expected by the CI (package_check)
Commands:
- rebuild [RELEASE] [DEBIAN_VERSION] Rebuild the base and appci image for RELEASE on DEBIAN_VERSION
- update_appci [RELEASE] [DEBIAN_VERSION] Update the appci image for RELEASE on DEBIAN_VERSION
- rebuild_build_and_lint [RELEASE] [DEBIAN_VERSION] Rebuild the special 'build-and-lint' image for core CI
Arguments:
- RELEASE stable, testing or unstable
- DEBIAN_VERSION bullseye, or bookworm
EOF
}
function main()
{
KNOWN_COMMANDS=$(declare -F | awk '{print $3}')
if [ "$CMD" == "help" ] || [ "$CMD" == "--help" ]
then
help
elif grep -q -w "$1" <<< "$KNOWN_COMMANDS"
then
cmd="$1"
set -x
$cmd
else
echo "Unknown command '$1', check --help to list available commands"
exit 1
fi
}
function _run_function_in_container()
{
CODE=$(type $1 | sed '1,3d;$d' | sed -e "s/\$RELEASE/$RELEASE/g" -e "s/\$DEBIAN_VERSION/$DEBIAN_VERSION/g" -e "s/\$gitbranch/$gitbranch/g")
cat << EOF | incus exec $CONTAINER
set -eux
$CODE
EOF
}
function _publish_as()
{
_run_function_in_container slimify
local shortname="$1"
local alias="ynh-$shortname-$DEBIAN_VERSION-$ARCH-$RELEASE-base"
# Save the finger print to delete the old image later
#local finger_print_to_delete=$(incus image info "$alias" | grep Fingerprint | awk '{print $2}')
local should_restart=0
# If the container is running, stop it
if [ "$(incus info $CONTAINER >/dev/null 2>/dev/null | grep Status | awk '{print tolower($2)}')" = "running" ]
then
should_restart=1
incus stop "$CONTAINER"
fi
# Create image before install
incus publish "$CONTAINER" --alias "$alias" --reuse --public "os=YunoHost" "ynh-release=$RELEASE" "release=${DEBIAN_VERSION^}" "architecture=$ARCH" "stage=ynh-$shortname" "description=YunoHost $DEBIAN_VERSION $RELEASE ynh-$shortname $ARCH ($(date '+%Y%m%d'))"
# Remove old image
#incus image delete "$finger_print_to_delete"
if [ $should_restart = 1 ]
then
incus start "$CONTAINER"
sleep 5
# 240501: fix because the container was not getting an IP
$IN_CONTAINER dhclient eth0
fi
}
function rebuild_build_and_lint()
{
incus info $CONTAINER >/dev/null 2>/dev/null && incus delete $CONTAINER --force
incus launch images:debian/$DEBIAN_VERSION/$ARCH $CONTAINER
sleep 5
$IN_CONTAINER dhclient eth0
incus file push ./gitlab-runner-light.deb $CONTAINER/root/
###########################################################################
_run_function_in_container build_and_lint && _publish_as "build-and-lint"
###########################################################################
incus stop $CONTAINER
incus delete $CONTAINER
}
function rebuild()
{
incus info $CONTAINER >/dev/null 2>/dev/null && incus delete $CONTAINER --force
incus launch images:debian/$DEBIAN_VERSION/$ARCH $CONTAINER -c security.privileged=true -c security.nesting=true
sleep 5
$IN_CONTAINER dhclient eth0
incus file push ./gitlab-runner-light.deb $CONTAINER/root/
###########################################################################
_run_function_in_container before_install && _publish_as "before-install"
_run_function_in_container dev && _publish_as "dev"
_run_function_in_container appci && _publish_as "appci"
_run_function_in_container core_tests && _publish_as "core-tests"
###########################################################################
incus stop $CONTAINER
incus delete $CONTAINER
}
function update_appci()
{
local BASE="ynh-dev-$DEBIAN_VERSION-$ARCH-$RELEASE-base"
incus launch $BASE $CONTAINER -c security.privileged=true -c security.nesting=true
$IN_CONTAINER apt update
$IN_CONTAINER apt full-upgrade --assume-yes
_run_function_in_container appci && _publish_as appci
incus stop $CONTAINER
incus delete $CONTAINER
}
main $@