Implement ynh_lxc_pc_create

This commit is contained in:
yalh76 2022-10-24 00:53:52 +02:00
parent 3e9652009d
commit 3e0c1a2bac
2 changed files with 26 additions and 11 deletions

View file

@ -255,7 +255,7 @@ run_all_tests() {
# Reset and create a fresh container to work with # Reset and create a fresh container to work with
check_lxd_setup check_lxd_setup
ynh_lxc_reset ynh_lxc_reset
ynh_lxc_pc_create ynh_lxc_pc_create --image=$LXC_BASE --name=$LXC_NAME
# Be sure that the container is running # Be sure that the container is running
ynh_lxc_pc_exec --name=$LXC_NAME --command="true" ynh_lxc_pc_exec --name=$LXC_NAME --command="true"

View file

@ -185,37 +185,52 @@ ynh_lxc_pc_witness_files_check () {
fi fi
} }
# Create a new LXC from an image
#
# usage: ynh_lxc_pc_create --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_pc_create () { ynh_lxc_pc_create () {
log_info "Launching new LXC $LXC_NAME ..." # 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 "$@"
log_info "Launching new LXC $name ..."
# Check if we can launch container from YunoHost remote image # Check if we can launch container from YunoHost remote image
if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$LXC_BASE | grep -q -w $LXC_BASE; then if lxc remote list | grep -q "yunohost" && lxc image list yunohost:$image | grep -q -w $image; then
lxc launch yunohost:$LXC_BASE $LXC_NAME \ lxc launch yunohost:$image $name \
-c security.nesting=true \ -c security.nesting=true \
-c security.privileged=true \ -c security.privileged=true \
-c limits.memory=80% \ -c limits.memory=80% \
-c limits.cpu.allowance=80% \ -c limits.cpu.allowance=80% \
>>/proc/self/fd/3 >>/proc/self/fd/3
# Check if we can launch container from a local image # Check if we can launch container from a local image
elif lxc image list $LXC_BASE | grep -q -w $LXC_BASE; then elif lxc image list $image | grep -q -w $image; then
lxc launch $LXC_BASE $LXC_NAME \ lxc launch $image $name \
-c security.nesting=true \ -c security.nesting=true \
-c security.privileged=true \ -c security.privileged=true \
-c limits.memory=80% \ -c limits.memory=80% \
-c limits.cpu.allowance=80% \ -c limits.cpu.allowance=80% \
>>/proc/self/fd/3 >>/proc/self/fd/3
else else
log_critical "Can't find base image $LXC_BASE, run ./package_check.sh --rebuild" log_critical "Can't find base image $image, run ./package_check.sh --rebuild"
fi fi
pipestatus="${PIPESTATUS[0]}" pipestatus="${PIPESTATUS[0]}"
location=$(lxc list --format json | jq -e --arg LXC_NAME $LXC_NAME '.[] | select(.name==$LXC_NAME) | .location' | tr -d '"') location=$(lxc list --format json | jq -e --arg name $name '.[] | select(.name==$name) | .location' | tr -d '"')
[[ "$location" != "none" ]] && log_info "... on $location" [[ "$location" != "none" ]] && log_info "... on $location"
[[ "$pipestatus" -eq 0 ]] || exit 1 [[ "$pipestatus" -eq 0 ]] || exit 1
_ynh_lxc_start_and_wait $LXC_NAME _ynh_lxc_start_and_wait $name
ynh_lxc_pc_witness_files_set --name=$LXC_NAME ynh_lxc_pc_witness_files_set --name=$name
lxc snapshot $LXC_NAME snap0 lxc snapshot $name snap0
} }
ynh_lxc_pc_snapshot_create () { ynh_lxc_pc_snapshot_create () {