From 6d29b0542a82f6cf12429291aec488c649bf1323 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Sun, 12 Jul 2020 20:37:59 +0200 Subject: [PATCH] [__lxc_container] better support config modifications at creation time When a container is created, the configuration is unknown to the explorer. To do the job without the knowleage, following is added: If the container will be created, it adds all absent configuration options to be deleted. Because it do not know which configuration options exist, it tries to remove all if there exists. This behaviour is extended for containers that will be cloned: Because the container who will be cloned is known and it will copy the config nearly one to one, so it will read this configuration. The removal of configuration is generally improved by generating a pattern to ignore spaces at the beginning, near the equal sign and at the end. This deletes the correct configuration lines even they have malformed whitespaces. --- .../type/__lxc_container/diff-conf-changes.sh | 15 +++++++++++++- .../conf/type/__lxc_container/explorer/config | 20 +++++++++++++++++-- .../conf/type/__lxc_container/gencode-remote | 8 ++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/cdist/conf/type/__lxc_container/diff-conf-changes.sh b/cdist/conf/type/__lxc_container/diff-conf-changes.sh index 37d9f195..d860a8b8 100755 --- a/cdist/conf/type/__lxc_container/diff-conf-changes.sh +++ b/cdist/conf/type/__lxc_container/diff-conf-changes.sh @@ -46,9 +46,13 @@ trimm_whitespaces() { } +# save states +state_should="$(cat "$__object/parameter/state")" +state_is="$(cat "$__object/explorer/state")" + # Only required if container will not be absent # prepare the wanted configuration lines -if [ "$(cat "$__object/parameter/state")" != "absent" ]; then +if [ "$state_should" != "absent" ]; then # create the files directory mkdir "$__object/files/" @@ -69,4 +73,13 @@ if [ "$(cat "$__object/parameter/state")" != "absent" ]; then # config.del grep -f "$__object/explorer/config" \ "$__object/files/config-absent" > "$__object/files/config.del" || true + + + # fallback: if template parameter exists and container currently absent; try to remove all configs + # If the container is currently absent, nothing will be removed, even it's in the default config + # In this case, it will try to delete every configuration option to be sure + # only the cloning configuration is known, but not if the container will be created in an other way + if [ "$state_is" = "absent" ] && ! [ -f "$__object/parameter/clone" ]; then + cp "$__object/files/config-absent" "$__object/files/config.del" + fi fi diff --git a/cdist/conf/type/__lxc_container/explorer/config b/cdist/conf/type/__lxc_container/explorer/config index 287a5063..16fcf14a 100755 --- a/cdist/conf/type/__lxc_container/explorer/config +++ b/cdist/conf/type/__lxc_container/explorer/config @@ -22,8 +22,24 @@ else fi -# assemble the container configuration file -config="$lxcpath/$name/config" +# if the container will be cloned, use the configuration of the container to clone from +# else, just use the normal container configuration +clone="$__object/parameter/clone" +if [ -f "$clone" ] && [ "$("$__type_explorer/state")" = "absent" ]; then + clone="$(cat "$clone")" + clonepath="$__object/parameter/clonepath" + if [ -f "$clonepath" ]; then + clonepath="$(cat "$clonepath")" + else + clonepath="$lxcpath" + fi + + # set the config path of the container to clone from + config="$clonepath/$clone/config" +else + # assemble the configuration file of the container + config="$lxcpath/$name/config" +fi # check if the file exist, else the container is absent if [ -r "$config" ]; then diff --git a/cdist/conf/type/__lxc_container/gencode-remote b/cdist/conf/type/__lxc_container/gencode-remote index ca2f2827..bd3f49b6 100755 --- a/cdist/conf/type/__lxc_container/gencode-remote +++ b/cdist/conf/type/__lxc_container/gencode-remote @@ -205,10 +205,14 @@ DONE # check if smth. to be deleted # must be before adding, because it takes the content of the original file # because 'cat $file > $file' will not work (> opens before command reads it) + # will create extended regex pattern for grep with malformed spaces if [ -s "$__object/files/config.del" ]; then cat < "\$tmpconfig" -$(cat "$__object/files/config.del") +grep -v -E -f - <<'ABSENT' "$container_config" > "\$tmpconfig" +$(awk -v FS=' = ' -v OFS=' = ' -v blank='[[:blank:]]*' ' +function ntostring(n) { ret=""; for(i=n; i<=NF; i++) ret=ret $i (i