diff --git a/cdist/conf/type/__lxc_container/diff-conf-changes.sh b/cdist/conf/type/__lxc_container/diff-conf-changes.sh index d860a8b8..b18b9fe4 100755 --- a/cdist/conf/type/__lxc_container/diff-conf-changes.sh +++ b/cdist/conf/type/__lxc_container/diff-conf-changes.sh @@ -17,20 +17,20 @@ # 1: parameter name # 2: file to print write_multi_param() { - if ! [ -f "$__object/parameter/$1" ]; then return 0; fi + 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" + 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. @@ -38,11 +38,11 @@ write_multi_param() { # 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"~ + 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"~ } @@ -53,33 +53,33 @@ state_is="$(cat "$__object/explorer/state")" # Only required if container will not be absent # prepare the wanted configuration lines if [ "$state_should" != "absent" ]; then - # create the files directory - mkdir "$__object/files/" + # 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" + # 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 + # 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 - # 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 + # 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 16fcf14a..5aa020e4 100755 --- a/cdist/conf/type/__lxc_container/explorer/config +++ b/cdist/conf/type/__lxc_container/explorer/config @@ -6,7 +6,7 @@ # abort if container will be absent - no config required if [ "$(cat "$__object/parameter/state")" = "absent" ]; then - exit 0 + exit 0 fi @@ -26,25 +26,25 @@ fi # 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 + 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" + # 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" + # 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 - # print configuration and strip files to just values - # grep will hide all comments and empty lines - all others must be key-values - # sed will uniform patterns, that no spaces will be problematic (before, in the middle and at end) - grep -E -v "^([[:blank:]]*)(#|$)" "$config" | sed 's/^[[:blank:]]*//; s/[[:blank:]]*=[[:blank:]]*/ = /; s/[[:blank:]]*$//' + # print configuration and strip files to just values + # grep will hide all comments and empty lines - all others must be key-values + # sed will uniform patterns, that no spaces will be problematic (before, in the middle and at end) + grep -E -v "^([[:blank:]]*)(#|$)" "$config" | sed 's/^[[:blank:]]*//; s/[[:blank:]]*=[[:blank:]]*/ = /; s/[[:blank:]]*$//' fi diff --git a/cdist/conf/type/__lxc_container/explorer/lxcpath b/cdist/conf/type/__lxc_container/explorer/lxcpath index a4116b9b..e633cff6 100755 --- a/cdist/conf/type/__lxc_container/explorer/lxcpath +++ b/cdist/conf/type/__lxc_container/explorer/lxcpath @@ -9,15 +9,15 @@ lxcpath="$(cat "$__object/parameter/lxcpath" 2>/dev/null || true)" # lxcpath parameter if [ -z "$lxcpath" ]; then - user="$(cat "$__object/parameter/user")" + user="$(cat "$__object/parameter/user")" - # gather default value from config - if [ "$user" != "$USER" ]; then - su -s "$(which -- lxc-config)" -- "$user" lxc.lxcpath - else - lxc-config lxc.lxcpath - fi + # gather default value from config + if [ "$user" != "$USER" ]; then + su -s "$(which -- lxc-config)" -- "$user" lxc.lxcpath + else + lxc-config lxc.lxcpath + fi else - # output the parameter - echo "$lxcpath" + # output the parameter + echo "$lxcpath" fi diff --git a/cdist/conf/type/__lxc_container/explorer/state b/cdist/conf/type/__lxc_container/explorer/state index c3c7f30f..86831ce2 100755 --- a/cdist/conf/type/__lxc_container/explorer/state +++ b/cdist/conf/type/__lxc_container/explorer/state @@ -19,27 +19,27 @@ lxcpath="$(cat "$__object/parameter/lxcpath" 2>/dev/null || true)" # assemble optional parameters LXC_PARAM="" if [ "$lxcpath" ]; then - # shellcheck disable=SC2089 - LXC_PARAM="$LXC_PARAM -P \"$lxcpath\"" + # shellcheck disable=SC2089 + LXC_PARAM="$LXC_PARAM -P \"$lxcpath\"" fi # function to get information lxc_info() { - # shellcheck disable=SC2090 disable=SC2086 - if [ "$user" != "$USER" ]; then - su -s "$(which -- lxc-info)" -- "$user" $LXC_PARAM -n "$name" -H "$@" - else - lxc-info $LXC_PARAM -n "$name" -H "$@" - fi + # shellcheck disable=SC2090 disable=SC2086 + if [ "$user" != "$USER" ]; then + su -s "$(which -- lxc-info)" -- "$user" $LXC_PARAM -n "$name" -H "$@" + else + lxc-info $LXC_PARAM -n "$name" -H "$@" + fi } # the lxc-info command will exit non-zero if container not exist if ! lxc_info -S >/dev/null 2>&1; then - # not exist - echo "absent" + # not exist + echo "absent" else - # print state (command output matches to type states) - lxc_info -s | tr '[:upper:]' '[:lower:]' + # print state (command output matches to type states) + lxc_info -s | tr '[:upper:]' '[:lower:]' fi diff --git a/cdist/conf/type/__lxc_container/gencode-remote b/cdist/conf/type/__lxc_container/gencode-remote index 480d7d41..f9e4dceb 100755 --- a/cdist/conf/type/__lxc_container/gencode-remote +++ b/cdist/conf/type/__lxc_container/gencode-remote @@ -44,31 +44,31 @@ fi # shortcut for checking config changes # returns success (0) if there are changes config_changes() { - if [ -s "$__object/files/config.add" ] || [ -s "$__object/files/config.del" ]; then - return 0 - else - return 1 - fi + if [ -s "$__object/files/config.add" ] || [ -s "$__object/files/config.del" ]; then + return 0 + else + return 1 + fi } # shortcut to add bdev parameters if available # $1: $__object parameter name without "bdev-" # $2: target parameter flag (like --foo), else "--$1" will be used bdev_add_if_available() { - _param_path="$__object/parameter/bdev-$1" - if [ -f "$_param_path" ]; then - backingstore_opts="$backingstore_opts ${2:---$1} '$(cat "$_param_path")'" - fi + _param_path="$__object/parameter/bdev-$1" + if [ -f "$_param_path" ]; then + backingstore_opts="$backingstore_opts ${2:---$1} '$(cat "$_param_path")'" + fi } # shortcut to add template parameters if available # $1: $__object parameter # $2: target parameter flag (like --foo), else "--$1" will be used template_add_if_available() { - _param_path="$__object/parameter/$1" - if [ -f "$_param_path" ]; then - template_opts="$template_opts ${2:---$1} '$(cat "$_param_path")'" - fi + _param_path="$__object/parameter/$1" + if [ -f "$_param_path" ]; then + template_opts="$template_opts ${2:---$1} '$(cat "$_param_path")'" + fi } @@ -81,291 +81,291 @@ if [ "$state_is" = "$state_should" ] && ( ! config_changes ); then exit; fi # handle if the container should exist or not case "$state_should" in - # all states where the container should exist - present|running|stopped|frozen) - # only relevant if the container does not exist before - if [ "$state_is" = "absent" ]; then - # change privileges (too complex to make exceptions) - cat </dev/null || true)" + # check, if the container should be created or cloned + if [ -f "$__object/parameter/clone" ]; then + copy_from="$(cat "$__object/parameter/clone")" + copypath="$(cat "$__object/parameter/clonepath" 2>/dev/null || true)" - # assemble own optional lxc options - lxc_opts="" - if [ -n "$copypath" ]; then - # this is set as the default $lxcpath, because it is the origin - lxc_opts="$lxc_opts -P \"$copypath\"" - fi - if [ -n "$lxcpath" ]; then - # this is set as the newpath, because it is the destination - lxc_opts="$lxc_opts -p \"$lxcpath\"" - fi + # assemble own optional lxc options + lxc_opts="" + if [ -n "$copypath" ]; then + # this is set as the default $lxcpath, because it is the origin + lxc_opts="$lxc_opts -P \"$copypath\"" + fi + if [ -n "$lxcpath" ]; then + # this is set as the newpath, because it is the destination + lxc_opts="$lxc_opts -p \"$lxcpath\"" + fi - # print lxc-copy code (because of the lxcpath thing, $LXC_PARAM conflicts) - cat <> "$__messages_out" - fi - ;; + # write to the message system + echo "create" >> "$__messages_out" + fi + ;; - # list other states to get a correct error message in the wildcard case - absent) - ;; + # list other states to get a correct error message in the wildcard case + absent) + ;; - # error handling: invalid state - *) - printf "Container '%s' in unknown state %s\n" "$name" "$state_should" >&2 - exit 2 - ;; + # error handling: invalid state + *) + printf "Container '%s' in unknown state %s\n" "$name" "$state_should" >&2 + exit 2 + ;; esac # Check the configurations only if the container exists and there are changes if config_changes; then - # generate config path - container_config="$(cat "$__object/explorer/lxcpath")/$name/config" - # create remote tmpfile for config - cat < $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 < $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" $(awk -v FS=' = ' -v OFS=' = ' -v blank='[[:blank:]]*' ' function ntostring(n) { ret=""; for(i=n; i<=NF; i++) ret=ret $i (i> "\$tmpconfig" $(cat "$__object/files/config.add") PRESENT DONE - fi + fi - # apply all changes - cat <> "$__messages_out" + echo "config" >> "$__messages_out" - # restart container only if it is running - # do not echo messages start/stop or melt/start/stop/freeze; only restart to avoid junk - case "$state_is" in - running|frozen) - # only do it if the container will run again - case "$state_should" in - present|running|frozen) - # su stuff - cat <> "$__messages_out" - fi - fi + else + # correct current state + state_is="running" + echo "melt" >> "$__messages_out" + fi + fi - # ending - echo "restart" >> "$__messages_out" - echo "SU" - ;; - esac - ;; - esac + # ending + echo "restart" >> "$__messages_out" + echo "SU" + ;; + esac + ;; + esac fi # Check if there is a difference within the states if [ "$state_is" != "$state_should" ] && [ "$state_should" != "present" ]; then - # change privileges (too complex to make exceptions) - cat <> "$__messages_out" - fi + echo "melt" >> "$__messages_out" + fi - # handle if the container should exist or not - case "$state_should" in - running) - # already running if unfreezed - if [ "$state_is" != "frozen" ]; then - cat <> "$__messages_out" - fi - ;; + echo "start" >> "$__messages_out" + fi + ;; - # stop the container if it should be removed - stopped) - cat <> "$__messages_out" - ;; + echo "stop" >> "$__messages_out" + ;; - # container should be stopped if not already done - absent) - if [ "$state_is" != "stopped" ]; then - cat <> "$__messages_out" - fi - cat <> "$__messages_out" + fi + cat <> "$__messages_out" - ;; + echo "freeze" >> "$__messages_out" + ;; - *) - # must be checked by previous case - ;; - esac + *) + # must be checked by previous case + ;; + esac - # end of su here-document - echo "SU" + # end of su here-document + echo "SU" fi # Check if the container needs to be removed if [ "$state_should" = "absent" ]; then - # change privileges (too complex to make exceptions) - # then, shutdown and delete the container - # we could force-delete, but better be polite - # snapshots are deleted, too - to keep everything clean. May someone does not want it? - cat <> "$__messages_out" + # write to the message system + echo "destroy" >> "$__messages_out" fi diff --git a/cdist/conf/type/__lxc_container/issue.txt b/cdist/conf/type/__lxc_container/issue.txt deleted file mode 100644 index f65f9780..00000000 --- a/cdist/conf/type/__lxc_container/issue.txt +++ /dev/null @@ -1 +0,0 @@ -$__object_id == name ? sperate parameter required?? multiple same names possible due to --lxcpath! diff --git a/cdist/conf/type/__lxc_container/manifest b/cdist/conf/type/__lxc_container/manifest index 99cfa1c0..0071c4fb 100755 --- a/cdist/conf/type/__lxc_container/manifest +++ b/cdist/conf/type/__lxc_container/manifest @@ -17,62 +17,62 @@ param="$__object/parameter/" # valid check if only --template or --clone given if [ -f "$param/template" ] && [ -f "$param/clone" ]; then - # error and exit - >&2 echo "error: can not create and clone a container at once!" - >&2 echo "error: --template and --clone can not work together" - exit 2 + # error and exit + >&2 echo "error: can not create and clone a container at once!" + >&2 echo "error: --template and --clone can not work together" + exit 2 fi # no clone option if --template given if [ -f "$param/template" ]; then - if [ -f "$param/clonepath" ] - then - >&2 echo "error: container will created by template, no clone options required!" - exit 2 - fi + if [ -f "$param/clonepath" ] + then + >&2 echo "error: container will created by template, no clone options required!" + exit 2 + fi fi # no template options if --clone given if [ -f "$param/clone" ]; then - if [ -f "$param/template-opts" ] \ - || [ -f "$param/default-config" ] \ - || [ -f "$param/no-default-config" ] \ - || [ -f "$param/release" ] \ - || [ -f "$param/arch" ] - then - >&2 echo "error: container will created by clone, no template options required!" - exit 2 - fi + if [ -f "$param/template-opts" ] \ + || [ -f "$param/default-config" ] \ + || [ -f "$param/no-default-config" ] \ + || [ -f "$param/release" ] \ + || [ -f "$param/arch" ] + then + >&2 echo "error: container will created by clone, no template options required!" + exit 2 + fi fi # check backingstore values if [ -f "$param/backingstore" ]; then - backingstore="$(cat "$param/backingstore")" + backingstore="$(cat "$param/backingstore")" - if [ "$backingstore" != "dir" ] && [ "$backingstore" != "none" ]; then - if [ -f "$param/bdev-dir" ] - then - >&2 echo "error: --bdev-dir is only possible for backingstore 'dir' or 'none'" - exit 2 - fi - fi + if [ "$backingstore" != "dir" ] && [ "$backingstore" != "none" ]; then + if [ -f "$param/bdev-dir" ] + then + >&2 echo "error: --bdev-dir is only possible for backingstore 'dir' or 'none'" + exit 2 + fi + fi - if [ "$backingstore" != "lvm" ] && [ "$backingstore" != "loop" ]; then - if [ -f "$param/bdev-fstype" ] \ - || [ -f "$param/bdev-fssize" ] - then - >&2 echo "error: --bdev-fstype and --bdev-fssize only available for backingstore 'lvm' or 'loop'" - exit 2 - fi - fi + if [ "$backingstore" != "lvm" ] && [ "$backingstore" != "loop" ]; then + if [ -f "$param/bdev-fstype" ] \ + || [ -f "$param/bdev-fssize" ] + then + >&2 echo "error: --bdev-fstype and --bdev-fssize only available for backingstore 'lvm' or 'loop'" + exit 2 + fi + fi - if [ "$backingstore" != "lvm" ]; then - if [ -f "$param/bdev-lvname" ] \ - || [ -f "$param/bdev-vgname" ] \ - || [ -f "$param/bdev-thinpool" ] - then - >&2 echo "error: --bdev-{lv,vg}name and --bdev-thinpool only available for backingstore 'lvm'" - exit 2 - fi - fi + if [ "$backingstore" != "lvm" ]; then + if [ -f "$param/bdev-lvname" ] \ + || [ -f "$param/bdev-vgname" ] \ + || [ -f "$param/bdev-thinpool" ] + then + >&2 echo "error: --bdev-{lv,vg}name and --bdev-thinpool only available for backingstore 'lvm'" + exit 2 + fi + fi fi diff --git a/cdist/conf/type/__lxc_container/todo.txt b/cdist/conf/type/__lxc_container/todo.txt index e5f8f471..41822842 100644 --- a/cdist/conf/type/__lxc_container/todo.txt +++ b/cdist/conf/type/__lxc_container/todo.txt @@ -8,6 +8,10 @@ auto.. (lxc autostart): - group - start -If config already created, it's considered the container is already created - -> first create; then setup - detection if template changed?? +config: + - remove whole sections + - keep prettier config files? + + +if multi-valued object ids come to be a thing, following pattern is also used internal at lxc: + $lxcpath:$name