From 304d974f7bbb488ba80a522cb0f01fc3b79aa1b2 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Sat, 11 Jul 2020 18:18:55 +0200 Subject: [PATCH] [__lxc_container] moved config diff incl. gencode-local to gencode-remote Rather than the config handling is split up over gencode-{local,remote} and the manifest, it is now completly moved to the gencode-remote script. This avoids complexity, file uploading and direct ssh interactions as it was used before. The diff creation was extracted into an extra script, and the gencode-local was removed. Now, the gencode-remote script executes the config diff script before the short curcuit and uses here-documents and pipes if the diff is required on the remote host. With this, the script size grows, but should be no difference if the file is downloaded temporarily. With this, the remote handling of an empty configuration file was enhanced by handling it completely into the code-remote script (hope escaping works correctly). --- .../type/__lxc_container/diff-conf-changes.sh | 72 +++++++++++++++++++ cdist/conf/type/__lxc_container/gencode-local | 69 ------------------ .../conf/type/__lxc_container/gencode-remote | 30 +++++--- cdist/conf/type/__lxc_container/manifest | 59 --------------- 4 files changed, 91 insertions(+), 139 deletions(-) create mode 100755 cdist/conf/type/__lxc_container/diff-conf-changes.sh delete mode 100755 cdist/conf/type/__lxc_container/gencode-local diff --git a/cdist/conf/type/__lxc_container/diff-conf-changes.sh b/cdist/conf/type/__lxc_container/diff-conf-changes.sh new file mode 100755 index 00000000..37d9f195 --- /dev/null +++ b/cdist/conf/type/__lxc_container/diff-conf-changes.sh @@ -0,0 +1,72 @@ +#!/bin/sh -e +# diff-conf-changes.sh + +# This script handles the configuration given to the type. It will be diffed against the existing +# container options and write the "to be done"-options to a separate file. +# +# Output files in "$__object/files/" : +# - config-{present,absent} +# - config.add +# - config.del + + +# Function to read multiple parameter input for configuration and print it to a single file. +# It will handle file paths as well as the stdin. Instead of the file, the content will be printed. +# +# Parameters: +# 1: parameter name +# 2: file to print +write_multi_param() { + if ! [ -f "$__object/parameter/$1" ]; then return 0; fi + + while read -r line; do + if [ "$line" = "-" ]; then + # append stdin + cat "$__object/stdin" + elif [ -f "$line" ]; then + # append file content + cat "$line" + else + # print out content + printf "%s\n" "$line" + fi + done < "$__object/parameter/$1" +} + +# Function to get rid of whitespaces inside of key-value pairs. It will clear all of these. +# +# Parameters: +# 1: the file which should be handled +trimm_whitespaces() { + mv "$1" "$1"~ # backup file to read write back original file + grep -E -v "^([[:blank:]]*)(#|$)" "$1"~ \ + | sed 's/^[[:blank:]]*//; s/[[:blank:]]*=[[:blank:]]*/ = /; s/[[:blank:]]*$//' \ + > "$1" + rm -f "$1"~ +} + + +# Only required if container will not be absent +# prepare the wanted configuration lines +if [ "$(cat "$__object/parameter/state")" != "absent" ]; then + # create the files directory + mkdir "$__object/files/" + + # Summary of the locally given configuration options + # config-present + conffile="$__object/files/config-present" + write_multi_param config > "$conffile" + trimm_whitespaces "$conffile" + # config-absent + absentfile="$__object/files/config-absent" + write_multi_param config-absent > "$absentfile" + trimm_whitespaces "$absentfile" + + # Diff the configuration options by removing duplicate config options + # config.add + grep -v -f "$__object/explorer/config" -f "$__object/files/config-absent" \ + "$__object/files/config-present" > "$__object/files/config.add" || true + # config.del + grep -f "$__object/explorer/config" \ + "$__object/files/config-absent" > "$__object/files/config.del" || true +fi diff --git a/cdist/conf/type/__lxc_container/gencode-local b/cdist/conf/type/__lxc_container/gencode-local deleted file mode 100755 index e0572bb4..00000000 --- a/cdist/conf/type/__lxc_container/gencode-local +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/sh -e -# gencode-local - -# This uploads the diff of the configuration file, which is applied by gencode-remote - - -# Get required parameters -name="$__object/parameter/name" -if [ -f "$name" ]; then - name="$(cat "$name")" -else - name="$__object_id" -fi - -state_is="$(cat "$__object/explorer/state")" -state_should="$(cat "$__object/parameter/state")" - - -# Check the configurations only if the container exists -if [ "$state_should" != "absent" ]; then - # do changes - - # predict remote lxc config file - container_config="$(cat "$__object/explorer/lxcpath")/$name/config" - - # IPv6 fix - if echo "${__target_host}" | grep -q -E '^[0-9a-fA-F:]+$' - then - my_target_host="[${__target_host}]" - else - my_target_host="${__target_host}" - fi - - # create remote container directory if container is not yet available - if [ "$state_is" = "absent" ]; then - # this must be done because the container will be created after the configuration is uploaded - # would also be possible to do everything in gencode-remote to keep the order - $__remote_exec $__target_host "mkdir $(dirname "$container_config")" - fi - - - # config-present - # remove all config lines from config-present that are already set or absent anyway - if grep -v -f "$__object/explorer/config" -f "$__object/files/config-absent" \ - "$__object/files/config-present" > "$__object/files/config.add"; then - - # get temp file name - add_dest="$($__remote_exec $__target_host "mktemp $container_config-add.XXXXXXXXXX")" - printf "%s" "$add_dest" > "$__object/files/remote-tmpfile_add" - # upload delta - cat < "$__object/files/config.del"; then - - # get temp file name - del_dest="$($__remote_exec $__target_host "mktemp $container_config-del.XXXXXXXXXX")" - printf "%s" "$del_dest" > "$__object/files/remote-tmpfile_del" - # upload delta - cat < $file' will not work (> opens before command reads it) + if [ -s "$__object/files/config.del" ]; then cat < "\$tmpconfig" -rm -rf "$(cat "${tmppath}_del")" +grep -v -f - <<'ABSENT' "$container_config" > "\$tmpconfig" +$(cat "$__object/files/config.del") +ABSENT DONE fi # check if smth. to be added - if [ -f "${tmppath}_add" ]; then + if [ -s "$__object/files/config.add" ]; then cat <> "\$tmpconfig" -rm -rf "$(cat "${tmppath}_add")" +cat <<'PRESENT' >> "\$tmpconfig" +$(cat "$__object/files/config.add") +PRESENT DONE fi diff --git a/cdist/conf/type/__lxc_container/manifest b/cdist/conf/type/__lxc_container/manifest index 4a74ea18..99cfa1c0 100755 --- a/cdist/conf/type/__lxc_container/manifest +++ b/cdist/conf/type/__lxc_container/manifest @@ -76,62 +76,3 @@ if [ -f "$param/backingstore" ]; then fi fi fi - - - - -# ============================== # -# == CONFIGURATION HANDLING == # -# ============================== # - -# Function to read multiple parameter input for configuration and print it to a single file. -# It will handle file paths as well as the stdin. Instead of the file, the content will be printed. -# -# Parameters: -# 1: parameter name -# 2: file to print -write_multi_param() { - if ! [ -f "$__object/parameter/$1" ]; then return 0; fi - - while read -r line; do - if [ "$line" = "-" ]; then - # append stdin - cat "$__object/stdin" - elif [ -f "$line" ]; then - # append file content - cat "$line" - else - # print out content - printf "%s\n" "$line" - fi - done < "$__object/parameter/$1" -} - -# Function to get rid of whitespaces inside of key-value pairs. It will clear all of these. -# -# Parameters: -# 1: the file which should be handled -trimm_whitespaces() { - mv "$1" "$1"~ # backup file to see original content - grep -E -v "^([[:blank:]]*)(#|$)" "$1"~ \ - | sed 's/^[[:blank:]]*//; s/[[:blank:]]*=[[:blank:]]*/ = /; s/[[:blank:]]*$//' \ - > "$1" -} - - -# Only required if container will not be absent -# prepare the wanted configuration lines -if [ "$(cat "$__object/parameter/state")" != "absent" ]; then - # Create the files directory - mkdir "$__object/files/" - - # Create the file of the locally configuration - conffile="$__object/files/config-present" - write_multi_param config > "$conffile" - trimm_whitespaces "$conffile" - - # Create the file of the absent configuration - absentfile="$__object/files/config-absent" - write_multi_param config-absent > "$absentfile" - trimm_whitespaces "$absentfile" -fi