[__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).
This commit is contained in:
parent
1b69d037ac
commit
304d974f7b
4 changed files with 91 additions and 139 deletions
72
cdist/conf/type/__lxc_container/diff-conf-changes.sh
Executable file
72
cdist/conf/type/__lxc_container/diff-conf-changes.sh
Executable file
|
|
@ -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
|
||||
|
|
@ -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 <<OUT
|
||||
$__remote_copy "$__object/files/config.add" "$my_target_host:$add_dest"
|
||||
OUT
|
||||
fi
|
||||
|
||||
# config-absent
|
||||
# only find those lines which should be absent in the config
|
||||
if grep -f "$__object/explorer/config" "$__object/files/config-absent" \
|
||||
> "$__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 <<OUT
|
||||
$__remote_copy "$__object/files/config.del" "$my_target_host:$del_dest"
|
||||
OUT
|
||||
fi
|
||||
fi
|
||||
|
|
@ -42,9 +42,9 @@ fi
|
|||
|
||||
|
||||
# shortcut for checking config changes
|
||||
tmppath="$__object/files/remote-tmpfile"
|
||||
# returns success (0) if there are changes
|
||||
config_changes() {
|
||||
if [ -f "${tmppath}_add" ] || [ -f "${tmppath}_del" ]; then
|
||||
if [ -s "$__object/files/config.add" ] || [ -s "$__object/files/config.del" ]; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
|
|
@ -72,6 +72,9 @@ template_add_if_available() {
|
|||
}
|
||||
|
||||
|
||||
# Generate configuration diff files
|
||||
"$__type"/diff-conf-changes.sh
|
||||
|
||||
# Short curcit if nothing changed
|
||||
if [ "$state_is" = "$state_should" ] && ( ! config_changes ); then exit; fi
|
||||
|
||||
|
|
@ -129,8 +132,10 @@ LXC
|
|||
create_opts=""
|
||||
if [ -f "$__object/parameter/no-default-config" ]; then
|
||||
# generate a random empty file and append
|
||||
empty_config="$($__remote_exec $__target_host "mktemp \${TMPDIR:-/tmp}/cdist_lxc-empty-conf.XXXXXXXXXX")"
|
||||
create_opts="$create_opts -f \"$empty_config\""
|
||||
cat <<TMPFILE
|
||||
empty_conf="\$(mktemp "\${TMPDIR:-/tmp}/cdist__lxc_container-empty-conf.XXXXXXXXXX")"
|
||||
TMPFILE
|
||||
create_opts="$create_opts -f \"\$empty_config\""
|
||||
|
||||
elif [ -f "$__object/parameter/default-config" ]; then
|
||||
# append the configuration
|
||||
|
|
@ -198,19 +203,22 @@ cp -p "$container_config" "\$tmpconfig"
|
|||
DONE
|
||||
|
||||
# check if smth. to be deleted
|
||||
# must be before adding, because it takes the content of the original file
|
||||
if [ -f "${tmppath}_del" ]; then
|
||||
# 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)
|
||||
if [ -s "$__object/files/config.del" ]; then
|
||||
cat <<DONE
|
||||
grep -v -f "$(cat "${tmppath}_del")" "$container_config" > "\$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 <<DONE
|
||||
cat "$(cat "${tmppath}_add")" >> "\$tmpconfig"
|
||||
rm -rf "$(cat "${tmppath}_add")"
|
||||
cat <<'PRESENT' >> "\$tmpconfig"
|
||||
$(cat "$__object/files/config.add")
|
||||
PRESENT
|
||||
DONE
|
||||
fi
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue