2021-01-27 00:35:18 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
set -eu
|
|
|
|
|
2024-08-13 18:34:42 +02:00
|
|
|
source recipes
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
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"
|
2024-06-09 18:49:58 +02:00
|
|
|
|
|
|
|
function help()
|
2021-01-27 00:35:18 +01:00
|
|
|
{
|
2024-06-09 18:49:58 +02:00
|
|
|
cat << EOF
|
|
|
|
|
2024-06-09 18:53:11 +02:00
|
|
|
Usage: ./image_builder [command] [additional args...]
|
2024-06-09 18:49:58 +02:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
- 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
|
2024-06-09 18:49:58 +02:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
- RELEASE stable, testing or unstable
|
|
|
|
- DEBIAN_VERSION bullseye, or bookworm
|
2024-06-09 18:49:58 +02:00
|
|
|
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2024-06-09 18:53:11 +02:00
|
|
|
function main()
|
|
|
|
{
|
2024-08-07 23:06:23 +02:00
|
|
|
|
2024-06-09 18:49:58 +02:00
|
|
|
KNOWN_COMMANDS=$(declare -F | awk '{print $3}')
|
2024-08-07 23:06:23 +02:00
|
|
|
if [ "$CMD" == "help" ] || [ "$CMD" == "--help" ]
|
2024-06-09 18:49:58 +02:00
|
|
|
then
|
|
|
|
help
|
|
|
|
elif grep -q -w "$1" <<< "$KNOWN_COMMANDS"
|
|
|
|
then
|
|
|
|
cmd="$1"
|
2024-08-07 23:06:23 +02:00
|
|
|
set -x
|
|
|
|
$cmd
|
2021-01-27 00:35:18 +01:00
|
|
|
else
|
2024-06-09 18:49:58 +02:00
|
|
|
echo "Unknown command '$1', check --help to list available commands"
|
|
|
|
exit 1
|
2021-01-27 00:35:18 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-13 18:34:42 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
function _publish_as()
|
2021-02-03 16:48:22 +01:00
|
|
|
{
|
2024-08-13 18:34:42 +02:00
|
|
|
_run_function_in_container slimify
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
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
|
2024-08-07 23:06:23 +02:00
|
|
|
#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
|
2024-08-13 18:34:42 +02:00
|
|
|
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
|
2024-08-07 23:06:23 +02:00
|
|
|
incus stop "$CONTAINER"
|
2021-02-03 16:48:22 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create image before install
|
2024-08-07 23:06:23 +02:00
|
|
|
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'))"
|
2024-06-09 18:49:58 +02:00
|
|
|
|
2021-02-03 16:48:22 +01:00
|
|
|
# Remove old image
|
2024-08-07 23:06:23 +02:00
|
|
|
#incus image delete "$finger_print_to_delete"
|
2021-02-03 16:48:22 +01:00
|
|
|
|
|
|
|
if [ $should_restart = 1 ]
|
|
|
|
then
|
2024-08-07 23:06:23 +02:00
|
|
|
incus start "$CONTAINER"
|
2021-02-03 16:48:22 +01:00
|
|
|
sleep 5
|
2024-08-07 23:06:23 +02:00
|
|
|
# 240501: fix because the container was not getting an IP
|
|
|
|
$IN_CONTAINER dhclient eth0
|
2021-02-03 16:48:22 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
function rebuild_build_and_lint()
|
2021-01-27 00:35:18 +01:00
|
|
|
{
|
2024-08-13 18:34:42 +02:00
|
|
|
incus info $CONTAINER >/dev/null 2>/dev/null && incus delete $CONTAINER --force
|
2024-08-07 23:06:23 +02:00
|
|
|
incus launch images:debian/$DEBIAN_VERSION/$ARCH $CONTAINER
|
|
|
|
sleep 5
|
|
|
|
$IN_CONTAINER dhclient eth0
|
2024-08-07 23:53:30 +02:00
|
|
|
incus file push ./gitlab-runner-light.deb $CONTAINER/root/
|
2024-08-08 20:48:10 +02:00
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
###########################################################################
|
2024-08-13 18:34:42 +02:00
|
|
|
|
|
|
|
_run_function_in_container build_and_lint && _publish_as "build-and-lint"
|
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
###########################################################################
|
|
|
|
|
|
|
|
incus stop $CONTAINER
|
|
|
|
incus delete $CONTAINER
|
|
|
|
}
|
2021-01-27 00:35:18 +01:00
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
function rebuild()
|
|
|
|
{
|
2024-08-13 18:34:42 +02:00
|
|
|
incus info $CONTAINER >/dev/null 2>/dev/null && incus delete $CONTAINER --force
|
2024-08-07 23:06:23 +02:00
|
|
|
incus launch images:debian/$DEBIAN_VERSION/$ARCH $CONTAINER -c security.privileged=true -c security.nesting=true
|
|
|
|
sleep 5
|
|
|
|
$IN_CONTAINER dhclient eth0
|
2024-08-07 23:53:30 +02:00
|
|
|
incus file push ./gitlab-runner-light.deb $CONTAINER/root/
|
2024-02-19 23:43:02 +01:00
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
###########################################################################
|
|
|
|
|
2024-08-13 18:34:42 +02:00
|
|
|
_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"
|
2024-08-07 23:06:23 +02:00
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
|
|
|
|
incus stop $CONTAINER
|
|
|
|
incus delete $CONTAINER
|
2024-02-19 23:43:02 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 18:49:58 +02:00
|
|
|
function update_appci()
|
2024-02-19 23:43:02 +01:00
|
|
|
{
|
2024-08-07 23:06:23 +02:00
|
|
|
local BASE="ynh-dev-$DEBIAN_VERSION-$ARCH-$RELEASE-base"
|
|
|
|
incus launch $BASE $CONTAINER -c security.privileged=true -c security.nesting=true
|
2024-06-09 18:49:58 +02:00
|
|
|
|
2024-08-13 18:34:42 +02:00
|
|
|
$IN_CONTAINER apt update
|
|
|
|
$IN_CONTAINER apt full-upgrade --assume-yes
|
2024-02-19 23:43:02 +01:00
|
|
|
|
2024-08-13 18:34:42 +02:00
|
|
|
_run_function_in_container appci && _publish_as appci
|
2021-01-27 00:35:18 +01:00
|
|
|
|
2024-08-07 23:06:23 +02:00
|
|
|
incus stop $CONTAINER
|
|
|
|
incus delete $CONTAINER
|
2021-01-27 00:35:18 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 18:49:58 +02:00
|
|
|
main $@
|