From c92d56293488eebf8df96f55153a0ca1fb16edc7 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Sat, 4 Jul 2020 14:25:13 +0200 Subject: [PATCH] [__lxc_container] refactored gencode-remote processing litte changes to `gencode-local` and more messages, too. --- cdist/conf/type/__lxc_container/gencode-local | 3 +- .../conf/type/__lxc_container/gencode-remote | 133 ++++++++++++------ cdist/conf/type/__lxc_container/man.rst | 12 ++ 3 files changed, 101 insertions(+), 47 deletions(-) diff --git a/cdist/conf/type/__lxc_container/gencode-local b/cdist/conf/type/__lxc_container/gencode-local index dbfca7e8..04935070 100755 --- a/cdist/conf/type/__lxc_container/gencode-local +++ b/cdist/conf/type/__lxc_container/gencode-local @@ -4,8 +4,7 @@ # This uploads the diff of the configuration file, which is applied by gencode-remote -# check both states -state_is="$(cat "$__object/explorer/state")" +# Check desired state state_should="$(cat "$__object/parameter/state")" # Check the configurations only if the container exists diff --git a/cdist/conf/type/__lxc_container/gencode-remote b/cdist/conf/type/__lxc_container/gencode-remote index b03f13d2..d1e06b6e 100755 --- a/cdist/conf/type/__lxc_container/gencode-remote +++ b/cdist/conf/type/__lxc_container/gencode-remote @@ -2,6 +2,16 @@ # gencode-remote # Does all common stuff for the container on the host +# +# Following stages need to be observed when executing this type +# 1. Container creation IF $state_is = absent +# 2. patching container config IF NOT $state_should = absent +# 3. look after runstates (running,frozen,stopped) +# 4. Container destruction IF $state_should = absent +# +# It is only required if: +# - $state_is != $state_should +# - config changed (detects if changes are uploaded) # Parameter gathering @@ -18,7 +28,7 @@ state_is="$(cat "$__object/explorer/state")" state_should="$(cat "$__object/parameter/state")" # general lxc things -lxcpath="$(cat "$__object/parameter/lxcpath" || true)" +lxcpath="$(cat "$__object/parameter/lxcpath" 2>/dev/null || true)" # Summerize common parameter arguments, listed in manual as "COMMON OPTIONS" @@ -31,9 +41,25 @@ if [ -n "$lxcpath" ]; then fi +# shortcut +tmppath="$__object/files/remote-tmpfile" +# shortcut function +config_changes() { + if ( [ -f "${tmppath}_add" ] || [ -f "${tmppath}_del" ] ); then + return 0 + else + return 1 + fi +} + + +# Short curcit if nothing changed +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 @@ -98,35 +124,25 @@ LXC cat <> "$__messages_out" fi - - # end of su here-document - echo "SU" - - # write to the message system - echo "create" >> "$__messages_out" ;; + + # list other states to get a correct error message in the wildcard case absent) - # change privileges (too complex to make exceptions) - cat <> "$__messages_out" ;; # error handling: invalid state @@ -137,13 +153,8 @@ LXC esac -# shortcut -tmppath="$__object/files/remote-tmpfile" - # Check the configurations only if the container exists and there are changes -if [ "$state_should" != "absent" ] && \ - ( [ -f "${tmppath}_add" ] || [ -f "${tmppath}_del" ] ); then - +if config_changes; then # generate config path container_config="$(cat "$__object/explorer/lxcpath")/$name/config" # create remote tmpfile for config @@ -180,54 +191,86 @@ DONE fi -# TODO user context again # Check if there is a difference within the states -if [ "$state_is" != "$state_should" ] && [ "$state_should" != "absent" ]; then +if [ "$state_is" != "$state_should" ] && [ "$state_should" != "present" ]; then # change privileges (too complex to make exceptions) cat <> "$__messages_out" + fi + # handle if the container should exist or not case "$state_should" in running) - if [ "$state_is" = "frozen" ]; then + # already running if unfreezed + if [ "$state_is" != "frozen" ]; then cat <> "$__messages_out" + fi ;; + # stop the container if it should be removed stopped) - if [ "$state_is" = "frozen" ]; then - cat <> "$__messages_out" + ;; + + # container should be stopped if not already done + absent) + if [ "$state_is" != "stopped" ]; then + cat <> "$__messages_out" fi cat <> "$__messages_out" ;; *) - ;; # must be checked by previous case + # must be checked by previous case + ;; esac # 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" +fi diff --git a/cdist/conf/type/__lxc_container/man.rst b/cdist/conf/type/__lxc_container/man.rst index f84b0619..ddf08daa 100644 --- a/cdist/conf/type/__lxc_container/man.rst +++ b/cdist/conf/type/__lxc_container/man.rst @@ -123,3 +123,15 @@ destroy config The container configuration changed. + +start + Started the container. + +stop + Stopped the container. + +freeze + Freezed all container processes. The container will be started if not yet done to be able to freeze. + +melt + Unfreezed all container processes. Will be done if any other state (except `present`) should be reached.