Implement lxc_launch

This commit is contained in:
yalh76 2022-10-24 01:34:52 +02:00
parent 587b240dcc
commit 89f88521df
2 changed files with 38 additions and 19 deletions

View file

@ -138,6 +138,42 @@ _ynh_lxc_start_and_wait () {
LXC_IP=$(lxc exec $name -- hostname -I | cut -d' ' -f1 | grep -E -o "\<[0-9.]{8,}\>")
}
# Launch a new LXC from an image
#
# usage: ynh_lxc_launch --image=image --name=name
# | arg: -i, --image= - image to create from
# | arg: -n, --name= - name of the LXC
#
# Requires YunoHost version *.*.* or higher.
ynh_lxc_launch (){
# Declare an array to define the options of this helper.
local legacy_args=in
local -A args_array=([i]=image= [n]=name=)
local image
local name
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then
lxc launch yunohost:$image $name \
-c security.nesting=true \
-c security.privileged=true \
-c limits.memory=80% \
-c limits.cpu.allowance=80% \
>>/proc/self/fd/3
# Check if we can launch container from a local image
elif lxc image list $image | grep -q -w $image; then
lxc launch $image $name \
-c security.nesting=true \
-c security.privileged=true \
-c limits.memory=80% \
-c limits.cpu.allowance=80% \
>>/proc/self/fd/3
else
log_critical "Can't find base image $image, run ./package_check.sh --rebuild"
fi
}
# Clean the swapfiles of an LXC container
#
# usage: ynh_lxc_swapfiles_clean --name=name

View file

@ -203,25 +203,8 @@ ynh_lxc_pc_create () {
log_info "Launching new LXC $name ..."
# Check if we can launch container from YunoHost remote image
if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then
lxc launch yunohost:$image $name \
-c security.nesting=true \
-c security.privileged=true \
-c limits.memory=80% \
-c limits.cpu.allowance=80% \
>>/proc/self/fd/3
# Check if we can launch container from a local image
elif lxc image list $image | grep -q -w $image; then
lxc launch $image $name \
-c security.nesting=true \
-c security.privileged=true \
-c limits.memory=80% \
-c limits.cpu.allowance=80% \
>>/proc/self/fd/3
else
log_critical "Can't find base image $image, run ./package_check.sh --rebuild"
fi
ynh_lxc_launch --image=$image --name=$name
pipestatus="${PIPESTATUS[0]}"
location=$(lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"')
[[ "$location" != "none" ]] && log_info "... on $location"