lxd_img_builder/image_builder

152 lines
4.6 KiB
Text
Raw Normal View History

2021-01-27 00:35:18 +01:00
#!/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()
2021-01-27 00:35:18 +01:00
{
cat << EOF
2024-06-09 18:53:11 +02:00
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
}
2024-06-09 18:53:11 +02:00
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
2021-01-27 00:35:18 +01:00
else
echo "Unknown command '$1', check --help to list available commands"
exit 1
2021-01-27 00:35:18 +01:00
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()
2021-02-03 16:48:22 +01:00
{
_run_function_in_container slimify
local shortname="$1"
local alias="ynh-$shortname-$DEBIAN_VERSION-$ARCH-$RELEASE-base"
2021-02-03 16:48:22 +01:00
# Save the finger print to delete the old image later
#local finger_print_to_delete=$(incus image info "$alias" | grep Fingerprint | awk '{print $2}')
2021-02-03 16:48:22 +01:00
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" ]
2021-02-03 16:48:22 +01:00
then
should_restart=1
incus stop "$CONTAINER"
2021-02-03 16:48:22 +01:00
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'))"
2021-02-03 16:48:22 +01:00
# Remove old image
#incus image delete "$finger_print_to_delete"
2021-02-03 16:48:22 +01:00
if [ $should_restart = 1 ]
then
incus start "$CONTAINER"
2021-02-03 16:48:22 +01:00
sleep 5
# 240501: fix because the container was not getting an IP
$IN_CONTAINER dhclient eth0
2021-02-03 16:48:22 +01:00
fi
}
function rebuild_build_and_lint()
2021-01-27 00:35:18 +01:00
{
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
}
2021-01-27 00:35:18 +01:00
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
2021-01-27 00:35:18 +01:00
incus stop $CONTAINER
incus delete $CONTAINER
2021-01-27 00:35:18 +01:00
}
main $@