[__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.
This commit is contained in:
matze 2020-07-12 20:37:59 +02:00
commit 6d29b0542a
3 changed files with 38 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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 <<DONE
grep -v -f - <<'ABSENT' "$container_config" > "\$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<NF ? OFS : ""); return ret }
{ printf "^%s%s%s=%s%s%s$%s", blank, $1, blank, blank, ntostring(2), blank, ORS }
' "$__object/files/config.del" )
ABSENT
DONE
fi