From 2800164883ad19e22a8e8fe35d5f3c46eff9d1d7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 5 Dec 2016 08:48:22 +0100 Subject: Enabling pre and post hooks in container commands. Signed-off-by: Daniel Baumann --- lib/container/auto | 19 +++++++++++++++++++ lib/container/console | 19 +++++++++++++++++++ lib/container/create | 19 +++++++++++++++++++ lib/container/enter | 19 +++++++++++++++++++ lib/container/limit | 19 +++++++++++++++++++ lib/container/list | 19 +++++++++++++++++++ lib/container/move | 19 +++++++++++++++++++ lib/container/remove | 19 +++++++++++++++++++ lib/container/restart | 19 +++++++++++++++++++ lib/container/start | 26 ++++++++++++++++++++++++++ lib/container/status | 19 +++++++++++++++++++ lib/container/stop | 19 +++++++++++++++++++ lib/container/version | 19 +++++++++++++++++++ 13 files changed, 254 insertions(+) (limited to 'lib/container') diff --git a/lib/container/auto b/lib/container/auto index eb23f87..969ce0a 100755 --- a/lib/container/auto +++ b/lib/container/auto @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" Parameters () { @@ -78,6 +79,15 @@ fi HOST="$(hostname -f)" +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run for FILE in "${CONFIG}"/*.conf do @@ -88,3 +98,12 @@ do cnt ${ACTION} -n ${CONTAINER} || true fi done + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/console b/lib/container/console index ab3dfaf..fbf3c2d 100755 --- a/lib/container/console +++ b/lib/container/console @@ -20,6 +20,7 @@ set -e COMMAND="$(basename ${0})" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -89,5 +90,23 @@ case "${STATE}" in ;; esac +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run machinectl login ${NAME} + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/create b/lib/container/create index cd3a455..1e855ef 100755 --- a/lib/container/create +++ b/lib/container/create @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" SCRIPTS="/usr/share/container-tools/scripts" @@ -141,6 +142,15 @@ fi CNT_AUTO="${CNT_AUTO:-$(hostname -f)}" +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Creating bind mounts if [ -n "${BIND}" ] then @@ -194,3 +204,12 @@ sed -e "s|@CNT_AUTO@|${CNT_AUTO}|g" \ # Run "${SCRIPTS}/${SCRIPT}" $(echo "${@}" | sed -e 's| -- | |') + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/enter b/lib/container/enter index 6023d92..4bcd5c6 100755 --- a/lib/container/enter +++ b/lib/container/enter @@ -20,6 +20,7 @@ set -e COMMAND="$(basename ${0})" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -91,6 +92,15 @@ esac LEADER="$(machinectl status ${NAME} | awk '/Leader: / { print $2 }')" +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run if [ -e "${MACHINES}/${NAME}/usr/bin/container-nsenter" ] then @@ -100,3 +110,12 @@ else fi nsenter --target ${LEADER} --mount --uts --ipc --net --pid --root --wd=/root ${OPTIONS} + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/limit b/lib/container/limit index a9882c9..d2a193a 100755 --- a/lib/container/limit +++ b/lib/container/limit @@ -20,6 +20,7 @@ set -e COMMAND="$(basename ${0})" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -182,5 +183,23 @@ then Usage fi +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run systemctl --runtime set-property ${NAME} ${BLOCK_IO_DEVICE_WEIGHT} ${BLOCK_IO_READ_BANDWITH} ${BLOCK_IO_WEIGHT} ${BLOCK_IO_WRITE_BANDWITH} ${CPU_QUOTA} ${CPU_SHARES} ${MEMORY_LIMIT} ${TASKS_MAX} + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/list b/lib/container/list index 3073853..f9f46fb 100755 --- a/lib/container/list +++ b/lib/container/list @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" VERSION="$(container version)" @@ -115,6 +116,15 @@ HOST="${HOST:-$(hostname -f)}" CSV_SEPARATOR="${CSV_SEPARATOR:-,}" +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run List () { @@ -403,3 +413,12 @@ EOF ;; esac + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/move b/lib/container/move index 83e6a20..6e855e4 100755 --- a/lib/container/move +++ b/lib/container/move @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -127,6 +128,15 @@ case "${FORCE}" in ;; esac +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run mv "${CONFIG}/${OLD}.conf" "${CONFIG}/${NEW}.conf" mv "${MACHINES}/${OLD}" "${MACHINES}/${NEW}" @@ -160,3 +170,12 @@ fi # config sed -i -e "s|${OLD}|${NEW}|g" "${CONFIG}/${NEW}.conf" + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/remove b/lib/container/remove index 3670ca4..eec3b02 100755 --- a/lib/container/remove +++ b/lib/container/remove @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -128,6 +129,15 @@ case "${VERBOSE}" in ;; esac +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # data if [ -e "${CONFIG}/${NAME}.conf" ] then @@ -150,3 +160,12 @@ fi # Run rm --preserve-root --one-file-system -rf ${RM_OPTIONS} "${MACHINES}/${NAME}" rm -f ${RM_OPTIONS} "${CONFIG}/${NAME}.conf" + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/restart b/lib/container/restart index 468b297..1c4c591 100755 --- a/lib/container/restart +++ b/lib/container/restart @@ -20,6 +20,7 @@ set -e COMMAND="$(basename ${0})" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -77,5 +78,23 @@ then exit 1 fi +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run machinectl reboot ${NAME} + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/start b/lib/container/start index 20d738e..40ee195 100755 --- a/lib/container/start +++ b/lib/container/start @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" START="false" @@ -155,6 +156,22 @@ case "${HOST_ARCHITECTURE}" in ;; esac +case "${START}" in + start) + ;; + + *) + # Pre hooks + for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" + do + if [ -x "${FILE}" ] + then + "${FILE}" + fi + done + ;; +esac + # config if [ -e "${CONFIG}/${NAME}.conf" ] then @@ -421,5 +438,14 @@ case "${START}" in *) # Run ${SETARCH} systemd-nspawn --keep-unit ${BIND} ${BOOT} ${CAPABILITY} ${DIRECTORY} ${DROP_CAPABILITY} ${MACHINE} ${NETWORK_VETH_EXTRA} ${LINK_JOURNAL} ${REGISTER} + + # Post hooks + for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" + do + if [ -x "${FILE}" ] + then + "${FILE}" + fi + done ;; esac diff --git a/lib/container/status b/lib/container/status index 65a7210..7429c78 100755 --- a/lib/container/status +++ b/lib/container/status @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" Parameters () @@ -78,5 +79,23 @@ then exit 1 fi +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + # Run systemctl status container@${NAME}.service --full + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/stop b/lib/container/stop index 3327025..e91d0c6 100755 --- a/lib/container/stop +++ b/lib/container/stop @@ -21,6 +21,7 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" CLEAN="false" @@ -90,6 +91,15 @@ then exit 1 fi +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + STATE="$(machinectl show ${NAME} 2>&1 | awk -F= '/^State=/ { print $2 }')" case "${CLEAN}" in @@ -182,3 +192,12 @@ esac # Run machinectl ${MODE} ${NAME} + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done diff --git a/lib/container/version b/lib/container/version index b57c17d..f72355e 100755 --- a/lib/container/version +++ b/lib/container/version @@ -18,6 +18,25 @@ set -e +HOOKS="/etc/container-tools/hooks" SHARE="/usr/share/container-tools" +# Pre hooks +for FILE in "${HOOKS}/pre-${COMMAND}".* "${HOOKS}/${NAME}.pre-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done + cat "${SHARE}/VERSION.txt" + +# Post hooks +for FILE in "${HOOKS}/post-${COMMAND}".* "${HOOKS}/${NAME}.post-${COMMAND}" +do + if [ -x "${FILE}" ] + then + "${FILE}" + fi +done -- cgit v1.2.3