[__lxc_container] got type to fly with heavier changes
Helper scripts do not work with explorers, so there are useless; and some other problems detected at runtime. `gencode-remote` *will be refactored - be aware*
This commit is contained in:
parent
1b735fb150
commit
939abf6d45
8 changed files with 128 additions and 160 deletions
cdist/conf/type/__lxc_container
|
@ -25,13 +25,10 @@ fi
|
|||
# assemble the container configuration file
|
||||
config="$lxcpath/$name/config"
|
||||
|
||||
# check if the file exist
|
||||
# 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:]]*$//'
|
||||
else
|
||||
>&2 printf "config file \"%s\" does not exist or is not readable\n" "$config"
|
||||
exit 1
|
||||
fi
|
||||
|
|
|
@ -3,14 +3,21 @@
|
|||
|
||||
# Echos the lxcpath variable for all container instances
|
||||
|
||||
# Load important functions and parameter handling
|
||||
. "$__type/helper-param"
|
||||
. "$__type/helper-exec"
|
||||
# get parameter if exist
|
||||
lxcpath="$(cat "$__object/parameter/lxcpath" 2>/dev/null || true)"
|
||||
|
||||
|
||||
# lxcpath parameter
|
||||
if [ -z "$lxcpath" ]; then
|
||||
lxc_g config "lxc.lxcpath"
|
||||
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
|
||||
else
|
||||
# output the parameter
|
||||
echo "$lxcpath"
|
||||
fi
|
||||
|
|
|
@ -4,15 +4,40 @@
|
|||
# Outputs if the container exist
|
||||
# possible values: present || absent || running || stopped || frozen
|
||||
|
||||
# source param lib
|
||||
. "$__type/helper-param"
|
||||
. "$__type/helper-exec"
|
||||
# general parameters
|
||||
name="$__object/parameter/name"
|
||||
if [ -f "$name" ]; then
|
||||
name="$(cat "$name")"
|
||||
else
|
||||
name="$__object_id"
|
||||
fi
|
||||
user="$(cat "$__object/parameter/user")"
|
||||
|
||||
# lxcpath
|
||||
lxcpath="$(cat "$__object/parameter/lxcpath" 2>/dev/null || true)"
|
||||
|
||||
# assemble optional parameters
|
||||
LXC_PARAM=""
|
||||
if [ "$lxcpath" ]; then
|
||||
LXC_PARAM="$LXC_PARAM -P \"$lxcpath\""
|
||||
fi
|
||||
|
||||
|
||||
# function to get information
|
||||
lxc_info() {
|
||||
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_c info -H -S >/dev/null 2>&1; then
|
||||
if ! lxc_info -S >/dev/null 2>&1; then
|
||||
# not exist
|
||||
echo "absent"
|
||||
else
|
||||
# print state (command output matches to type states)
|
||||
lxc_c info -H -s | tr '[:upper:]' '[:lower:]'
|
||||
lxc_info -s | tr '[:upper:]' '[:lower:]'
|
||||
fi
|
||||
|
|
|
@ -48,7 +48,7 @@ OUT
|
|||
printf "%s" "$del_dest" > "$__object/files/remote-tmpfile_del"
|
||||
# upload diff
|
||||
cat <<OUT
|
||||
$__remote_copy "$__object/files/config.add" "$my_target_host:$del_dest"
|
||||
$__remote_copy "$__object/files/config.del" "$my_target_host:$del_dest"
|
||||
OUT
|
||||
fi
|
||||
fi
|
||||
|
|
|
@ -4,24 +4,44 @@
|
|||
# Does all common stuff for the container on the host
|
||||
|
||||
|
||||
# Source to the common parameters
|
||||
. "$__type/helper-param"
|
||||
# Parameter gathering
|
||||
name="$__object/parameter/name"
|
||||
if [ -f "$name" ]; then
|
||||
name="$(cat "$name")"
|
||||
else
|
||||
name="$__object_id"
|
||||
fi
|
||||
user="$(cat "$__object/parameter/user")"
|
||||
|
||||
# check both states
|
||||
state_is="$(cat "$__object/explorer/state")"
|
||||
state_should="$(cat "$__object/parameter/state")"
|
||||
|
||||
# general lxc things
|
||||
lxcpath="$(cat "$__object/parameter/lxcpath" || true)"
|
||||
|
||||
# Check if there is a difference within the states
|
||||
if [ "$state_is" != "$state_should" ]; then
|
||||
# change privileges (too complex to make exceptions)
|
||||
cat <<USER
|
||||
|
||||
# Summerize common parameter arguments, listed in manual as "COMMON OPTIONS"
|
||||
# will passed raw; must qouted manually
|
||||
LXC_PARAM="-q"
|
||||
|
||||
# if lxcpath given
|
||||
if [ -n "$lxcpath" ]; then
|
||||
LXC_PARAM="$LXC_PARAM -P \"$lxcpath\""
|
||||
fi
|
||||
|
||||
|
||||
|
||||
# handle if the container should exist or not
|
||||
case "$state_should" in
|
||||
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 <<USER
|
||||
su "$user" - <<SU
|
||||
USER
|
||||
|
||||
# handle if the container should exist or not
|
||||
case "$state_should" in
|
||||
present|running|stopped|frozen)
|
||||
# check, if the container should be created or cloned
|
||||
if [ -f "$__object/parameter/clone" ]; then
|
||||
copy_from="$(cat "$__object/parameter/clone")"
|
||||
|
@ -79,41 +99,50 @@ LXC
|
|||
lxc-create $LXC_PARAM -n "$name" -t "$template" $create_opts -- $template_opts
|
||||
LXC
|
||||
fi
|
||||
fi
|
||||
|
||||
# write to the message system
|
||||
echo "create" >> "$__messages_out"
|
||||
;;
|
||||
# end of su here-document
|
||||
echo "SU"
|
||||
|
||||
absent)
|
||||
# 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
|
||||
# write to the message system
|
||||
echo "create" >> "$__messages_out"
|
||||
;;
|
||||
|
||||
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
|
||||
|
||||
# write to the message system
|
||||
echo "destroy" >> "$__messages_out"
|
||||
;;
|
||||
# end of su here-document
|
||||
echo "SU"
|
||||
|
||||
# error handling: invalid state
|
||||
*)
|
||||
printf "Container '%s' in unknown state %s\n" "$name" "$state_should" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
# write to the message system
|
||||
echo "destroy" >> "$__messages_out"
|
||||
;;
|
||||
|
||||
# end of su here-document
|
||||
echo "SU"
|
||||
fi
|
||||
# error handling: invalid state
|
||||
*)
|
||||
printf "Container '%s' in unknown state %s\n" "$name" "$state_should" >&2
|
||||
exit 2
|
||||
;;
|
||||
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
|
||||
# shortcut
|
||||
tmppath="$__object/files/remote-tmpfile"
|
||||
|
||||
# generate config path
|
||||
container_config="$(cat "$__object/explorer/lxcpath")/$name/config"
|
||||
|
@ -123,6 +152,15 @@ tmpconfig="\$(mktemp "$container_config.XXXXXXXXXX")"
|
|||
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
|
||||
cat <<DONE
|
||||
grep -v -f "$(cat "${tmppath}_del")" "$container_config" > "\$tmpconfig"
|
||||
rm -rf "$(cat "${tmppath}_del")"
|
||||
DONE
|
||||
fi
|
||||
|
||||
# check if smth. to be added
|
||||
if [ -f "${tmppath}_add" ]; then
|
||||
cat <<DONE
|
||||
|
@ -131,14 +169,6 @@ rm -rf "$(cat "${tmppath}_add")"
|
|||
DONE
|
||||
fi
|
||||
|
||||
# check if smth. to be deleted
|
||||
if [ -f "${tmppath}_del" ]; then
|
||||
cat <<DONE
|
||||
grep -v -f "$(cat "${tmppath}_del")" "\$tmpconfig" > "\$tmpconfig"
|
||||
rm -rf "$(cat "${tmppath}_del")"
|
||||
DONE
|
||||
fi
|
||||
|
||||
|
||||
# apply all changes
|
||||
cat <<DONE
|
||||
|
|
|
@ -1,68 +0,0 @@
|
|||
# vi: syntax=sh
|
||||
# helper-exec
|
||||
|
||||
# Adds functions to easily handle the execution of lxc commands
|
||||
# requires to source the helper-param before .. ;)
|
||||
|
||||
# Following global variables required (from helper-param):
|
||||
# $user: the unix user to execute lxc commands with
|
||||
# $name: the lxc container name
|
||||
|
||||
|
||||
# Execute a command with a special user.
|
||||
#
|
||||
# Parameters:
|
||||
# 1: the execution user
|
||||
# 2: the execution command
|
||||
# 3: all further parameters for the command
|
||||
exec_user() {
|
||||
# save important arguments
|
||||
_user="$1"
|
||||
_cmd="$(which -- "$2")"
|
||||
shift 2
|
||||
|
||||
# check if the user must be switched
|
||||
if [ "$_user" != "$USER" ]; then
|
||||
# execute it wit su
|
||||
# here, compatibility to non-sudo systems is provided and the "shell parameter thing" is misused
|
||||
# to provide more parameter safety.
|
||||
su -s "$_cmd" -- "$_user" "$@"
|
||||
|
||||
# no switch of the user - keep the same
|
||||
else
|
||||
# execute the command
|
||||
"$_cmd" "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
# Function to execute lxc commands in the correct execution. Only works for container-specific commands.
|
||||
# Default things such as default parameters (see $LXC_PARAM), execution user and the container name are
|
||||
# applied within the function.
|
||||
#
|
||||
# Parameters:
|
||||
# 1: the lxc command without the preceding "lxc"
|
||||
# 2-n: all other lxc arguments
|
||||
lxc_c() {
|
||||
# get exec name
|
||||
lxccmd="lxc-$1"
|
||||
shift
|
||||
|
||||
# execute the command
|
||||
exec_user "$user" "$lxccmd" $LXC_PARAM -n "$name" "$@"
|
||||
}
|
||||
|
||||
# Function to execute global lxc commands within the correct execution environment. Container-specific
|
||||
# commands require to pass the -n flag with the container name, rather lxc_c is recommended.
|
||||
# Default things such as default parameters (see $LXC_PARAM) and execution are applied to the command.
|
||||
#
|
||||
# Parameters:
|
||||
# 1: the lxc command without the preceding "lxc"
|
||||
# 2-n: all other lxc arguments
|
||||
lxc_g() {
|
||||
# get exec name
|
||||
lxccmd="lxc-$1"
|
||||
shift
|
||||
|
||||
# execute the command
|
||||
exec_user "$user" "$lxccmd" $LXC_PARAM "$@"
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
# vi: syntax=sh
|
||||
# helper-param
|
||||
|
||||
# Shortcut to optain all basic required parameters for working.
|
||||
|
||||
|
||||
# Parameter gathering
|
||||
name="$__object/parameter/name"
|
||||
if [ -f "$name" ]; then
|
||||
name="$(cat "$name")"
|
||||
else
|
||||
name="$__object_id"
|
||||
fi
|
||||
user="$(cat "$__object/parameter/user")"
|
||||
|
||||
# general lxc things
|
||||
lxcpath="$(cat "$__object/parameter/lxcpath" || true)"
|
||||
|
||||
|
||||
# Summerize common parameter arguments, listed in manual as "COMMON OPTIONS"
|
||||
# will passed raw; must qouted manually
|
||||
LXC_PARAM="-q"
|
||||
|
||||
# if lxcpath given
|
||||
if [ -n "$lxcpath" ]; then
|
||||
LXC_PARAM="$LXC_PARAM -P \"$lxcpath\""
|
||||
fi
|
|
@ -10,6 +10,7 @@
|
|||
# basepath of the parameter folder (shortcut)
|
||||
param="$__object/parameter/"
|
||||
|
||||
|
||||
# ====================== #
|
||||
# == WARNING CHECKS == #
|
||||
# ====================== #
|
||||
|
@ -63,17 +64,18 @@ fi
|
|||
# 1: parameter name
|
||||
# 2: file to print
|
||||
write_multi_param() {
|
||||
touch "$2"
|
||||
if ! [ -f "$__object/parameter/$1" ]; then return 0; fi
|
||||
|
||||
while read line; do
|
||||
if [ "$line" = "-" ]; then
|
||||
# append stdin
|
||||
cat "$__object/stdin" >> "$2"
|
||||
cat "$__object/stdin"
|
||||
elif [ -f "$line" ]; then
|
||||
# append file content
|
||||
cat "$line" >> "$2"
|
||||
cat "$line"
|
||||
else
|
||||
# print out content
|
||||
printf "%s\n" "$line" >> "$2"
|
||||
printf "%s\n" "$line"
|
||||
fi
|
||||
done < "$__object/parameter/$1"
|
||||
}
|
||||
|
@ -83,24 +85,26 @@ write_multi_param() {
|
|||
# Parameters:
|
||||
# 1: the file which should be handled
|
||||
trimm_whitespaces() {
|
||||
cp "$1" "$1"~ # backup file to see original content
|
||||
grep -E -v "^([[:blank:]]*)(#|$)" "$1" \
|
||||
| sed 's/^[[:blank:]]*//; s/[[:blank:]]*=[[:blank:]]*/ = /; s/[[:blank:]]*$//'
|
||||
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
|
||||
# get the wanted configuration lines
|
||||
# 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" > "$conffile"
|
||||
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" > "$absentfile"
|
||||
write_multi_param config-absent > "$absentfile"
|
||||
trimm_whitespaces "$absentfile"
|
||||
fi
|
||||
|
|
Loading…
Add table
Reference in a new issue