Allow the use of incus instead of LXD. In the future, we could remove LXD support, making the README way shorter (no snap, etc)

This commit is contained in:
Salamandar 2024-02-23 10:57:04 +01:00
parent 5c8e3e773b
commit be80111c61

92
ynh-dev
View file

@ -3,14 +3,16 @@
function show_usage() {
cat <<EOF
${BLUE}On the host, to manage the LXC${NORMAL}
${BLUE}==============================${NORMAL}
${BLUE}On the host, to manage boxes${NORMAL}
${BLUE}============================${NORMAL}
start [DIST] [NAME] [YNH_BRANCH] (Create and) starts a LXC (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
attach [DIST] [NAME] [YNH_BRANCH] Attach an already started LXC (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
destroy [DIST] [NAME] [YNH_BRANCH] Destroy the ynh-dev LXC (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
start [DIST] [NAME] [YNH_BRANCH] (Create and) starts a box (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
attach [DIST] [NAME] [YNH_BRANCH] Attach an already started box (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
destroy [DIST] [NAME] [YNH_BRANCH] Destroy the ynh-dev box (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
rebuild [DIST] [NAME] [YNH_BRANCH] Rebuild a fresh, up-to-date box (DIST=bookworm, NAME=ynh-dev and YNH_BRANCH=unstable by default)
Pass YNHDEV_BACKEND=incus to use incus instead of lxd.
${BLUE}Inside the dev instance${NORMAL}
${BLUE}=======================${NORMAL}
@ -168,25 +170,67 @@ function check_lxd_setup()
|| critical "There is no 'lxcbr0' or 'lxdbr0' interface... Did you ran 'lxd init' ?"
}
function check_incus_setup()
{
# Check incus is installed somehow
if ! which incus &>/dev/null; then
critical "You need to have Incus installed for ynh-dev to be usable from the host machine. Refer to the README to know how to install it."
fi
if ! id -nG "$(whoami)" | grep -qw "incus-admin"; then
critical "You need to be in the incus-admin group!"
fi
ip a | grep -q incusbr0 \
|| warn "There is no 'incusbr0' interface... Did you ran 'incus admin init' ?"
set_incus_remote
}
function set_incus_remote()
{
configured=$(incus remote list -f json | jq 'has("yunohost")')
if [[ "$configured" != "true" ]]; then
incus remote add yunohost https://devbaseimgs.yunohost.org --public
fi
}
function check_setup()
{
if [[ "${YNHDEV_BACKEND:-}" == "incus" ]]; then
check_incus_setup
else
check_lxd_setup
fi
}
function backend()
{
if [[ "${YNHDEV_BACKEND:-}" == "incus" ]]; then
incus "$@"
else
sudo lxc "$@"
fi
}
function start_ynhdev()
{
check_lxd_setup
check_setup
local DIST=${1:-bookworm}
local YNH_BRANCH=${3:-unstable}
local BOX=${2:-ynh-dev}-${DIST}-${YNH_BRANCH}
if ! sudo lxc info $BOX &>/dev/null
if ! backend info $BOX &>/dev/null
then
if ! sudo lxc image info $BOX-base &>/dev/null
if ! backend image info $BOX-base &>/dev/null
then
LXC_BASE="ynh-dev-$DIST-amd64-$YNH_BRANCH-base"
sudo lxc launch yunohost:$LXC_BASE $BOX -c security.nesting=true -c security.privileged=true \
backend launch yunohost:$LXC_BASE $BOX -c security.nesting=true -c security.privileged=true \
|| critical "Failed to launch the container ?"
else
sudo lxc launch $BOX-base $BOX -c security.nesting=true -c security.privileged=true
backend launch $BOX-base $BOX -c security.nesting=true -c security.privileged=true
fi
sudo lxc config device add $BOX ynhdev-shared-folder disk path=/ynh-dev source="$(readlink -f $(pwd))"
backend config device add $BOX ynhdev-shared-folder disk path=/ynh-dev source="$(readlink -f $(pwd))"
info "Now attaching to the container"
else
info "Attaching to existing container"
@ -197,41 +241,41 @@ function start_ynhdev()
function attach_ynhdev()
{
check_lxd_setup
check_setup
local DIST=${1:-bookworm}
local YNH_BRANCH=${3:-unstable}
local BOX=${2:-ynh-dev}-${DIST}-${YNH_BRANCH}
sudo lxc start $BOX 2>/dev/null || true
sudo lxc exec $BOX --cwd /ynh-dev -- /bin/bash
backend start $BOX 2>/dev/null || true
backend exec $BOX --cwd /ynh-dev -- /bin/bash
}
function destroy_ynhdev()
{
check_lxd_setup
check_setup
local DIST=${1:-bookworm}
local YNH_BRANCH=${3:-unstable}
local BOX=${2:-ynh-dev}-${DIST}-${YNH_BRANCH}
sudo lxc stop $BOX
sudo lxc delete $BOX
backend stop $BOX
backend delete $BOX
}
function rebuild_ynhdev()
{
check_lxd_setup
check_setup
local DIST=${1:-bookworm}
local YNH_BRANCH=${3:-unstable}
local BOX=${2:-ynh-dev}-${DIST}-${YNH_BRANCH}
set -x
sudo lxc info $BOX-rebuild >/dev/null && sudo lxc delete $BOX-rebuild --force
sudo lxc launch images:debian/$DIST/amd64 $BOX-rebuild -c security.nesting=true -c security.privileged=true
backend info $BOX-rebuild >/dev/null && backend delete $BOX-rebuild --force
backend launch images:debian/$DIST/amd64 $BOX-rebuild -c security.nesting=true -c security.privileged=true
sleep 5
sudo lxc exec $BOX-rebuild -- apt install curl -y
backend exec $BOX-rebuild -- apt install curl -y
INSTALL_SCRIPT="https://install.yunohost.org/$DIST"
sudo lxc exec $BOX-rebuild -- /bin/bash -c "curl $INSTALL_SCRIPT | bash -s -- -a -d $YNH_BRANCH"
sudo lxc stop $BOX-rebuild
sudo lxc publish $BOX-rebuild --alias $BOX-base
backend exec $BOX-rebuild -- /bin/bash -c "curl $INSTALL_SCRIPT | bash -s -- -a -d $YNH_BRANCH"
backend stop $BOX-rebuild
backend publish $BOX-rebuild --alias $BOX-base
set +x
}