[__lxc_container] refactored gencode-remote processing

litte changes to `gencode-local` and more messages, too.
This commit is contained in:
matze 2020-07-04 14:25:13 +02:00
commit c92d562934
3 changed files with 101 additions and 47 deletions

View file

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

View file

@ -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 <<LXC
lxc-create $LXC_PARAM -n "$name" -t "$template" $create_opts -- $template_opts
LXC
# remove empty tempfile
if [ "$empty_config" ]; then
cat <<RM
rm -rf "$empty_config"
RM
fi
fi
# end of su here-document
echo "SU"
# write to the message system
echo "create" >> "$__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 <<USER
su "$user" - <<SU
USER
# 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 <<LXC
lxc-stop $LXC_PARAM -n "$name"
lxc-destroy $LXC_PARAM --snapshots -n "$name"
LXC
# end of su here-document
echo "SU"
# write to the message system
echo "destroy" >> "$__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 <<USER
su "$user" - <<SU
USER
# unfreeze if nessecary to change state
if [ "$state_is" = "frozen" ]; then
cat <<LXC
lxc-unfreeze $LXC_PARAM -n "$name"
LXC
echo "melt" >> "$__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 <<LXC
lxc-unfreeze $LXC_PARAM -n "$name"
LXC
fi
cat <<LXC
lxc-start $LXC_PARAM -n "$name"
LXC
echo "start" >> "$__messages_out"
fi
;;
# stop the container if it should be removed
stopped)
if [ "$state_is" = "frozen" ]; then
cat <<LXC
lxc-unfreeze $LXC_PARAM -n "$name"
LXC
fi
cat <<LXC
lxc-stop $LXC_PARAM -n "$name"
LXC
echo "stop" >> "$__messages_out"
;;
# container should be stopped if not already done
absent)
if [ "$state_is" != "stopped" ]; then
cat <<LXC
lxc-stop $LXC_PARAM -n "$name"
LXC
fi
;;
frozen)
# stopped containers can't be frozen; they must be started first
if [ "$state_is" = "stopped" ]; then
if [ "$state_is" = "stopped" ] || [ "$state_is" = "absent" ]; then
cat <<LXC
lxc-start $LXC_PARAM -n "$name"
LXC
echo "start" >> "$__messages_out"
fi
cat <<LXC
lxc-freeze $LXC_PARAM -n "$name"
LXC
echo "freeze" >> "$__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 <<USER
su "$user" - <<SU
lxc-destroy $LXC_PARAM --snapshots -n "$name"
SU
USER
# write to the message system
echo "destroy" >> "$__messages_out"
fi

View file

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