From c57f861235add6b95e81c9835e5078452d342e7c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 11 Dec 2016 13:34:28 +0100 Subject: Merging upstream version 20161210. 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 | 282 +++++++++++++++++++++++++++++++++++++++++++------- lib/container/move | 181 ++++++++++++++++++++++++++++++++ lib/container/remove | 19 ++++ lib/container/rename | 162 ----------------------------- lib/container/restart | 19 ++++ lib/container/start | 26 +++++ lib/container/status | 19 ++++ lib/container/stop | 19 ++++ lib/container/version | 19 ++++ 14 files changed, 643 insertions(+), 198 deletions(-) create mode 100755 lib/container/move delete mode 100755 lib/container/rename (limited to 'lib') 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 3c23c24..f9f46fb 100755 --- a/lib/container/list +++ b/lib/container/list @@ -21,11 +21,14 @@ set -e COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" MACHINES="/var/lib/machines" +VERSION="$(container version)" + Parameters () { - GETOPT_LONGOPTIONS="all,format:,host:,other,started,stopped," + GETOPT_LONGOPTIONS="all,csv-separator:,format:,host:,nwdiag-color:,nwdiag-label:,other,started,stopped," GETOPT_OPTIONS="a,f:,h:,o,s,t," PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" @@ -46,6 +49,11 @@ Parameters () shift 1 ;; + --csv-separator) + CSV_SEPARATOR="${2}" + shift 2 + ;; + -f|--format) FORMAT="${2}" shift 2 @@ -56,6 +64,16 @@ Parameters () shift 2 ;; + --nwdiag-color) + NWDIAG_COLOR="${2}" + shift 2 + ;; + + --nwdiag-label) + NWDIAG_LABEL="${2}" + shift 2 + ;; + -o|--other) LIST="${LIST} other" shift 1 @@ -86,19 +104,116 @@ Parameters () Usage () { - echo "Usage: container ${COMMAND} -a|--all --format FORMAT -s|--started -t|--stopped" >&2 + echo "Usage: container ${COMMAND} [-a|--all] [--csv-separator SEPARATOR] [--format FORMAT] [-h|--host HOSTNAME] [--nwdiag-color COLOR] [--nwdiag-label LABEL] [-o|--other] [-s|--started] [-t|--stopped]" >&2 exit 1 } Parameters "${@}" LIST="${LIST:-started stopped}" -FORMAT="${FORMAT:-full}" +FORMAT="${FORMAT:-cli}" 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 () +{ + case "${FORMAT}" in + cli) + printf "%-80s %-29s\n" "${STATUS} ${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" + ;; + + csv) + echo "${HOST}${CSV_SEPARATOR}${CONTAINER}${CSV_SEPARATOR}${STATE}${CSV_SEPARATOR}${ADDRESS}" + ;; + + json) + +cat << EOF + { + "name": "${CONTAINER}", + "status": "${STATE}", + "ipv4Address": "${ADDRESS}", + }, +EOF + + ;; + + nwdiag) + Nwdiag_width "${CONTAINER}" + + case "${STATE}" in + started) + COLOR="#73d216" + ;; + + stopped) + COLOR="#cc0000" + ;; + + other) + COLOR="#d3d7cf" + ;; + esac + + echo " ${CONTAINER} [address = \"${ADDRESS}\", color = \"${COLOR}\", shape = \"flowchart.terminator\", width = \"${WIDTH}\"];" + ;; + + shell) + echo "${CONTAINER}" + ;; + + yaml) + +cat << EOF + - name: ${CONTAINER} + ipv4_address: ${ADDRESS} +EOF + + ;; + + xml) + +cat << EOF + + ${CONTAINER} + ${ADDRESS} + +EOF + + ;; + esac +} + +Nwdiag_width () +{ + NAME="${1}" + + CHARACTERS="$(echo "${NAME}" | wc -c)" + + if [ "${CHARACTERS}" -gt 13 ] + then + # default width is 128 fitting 13 characters + WIDTH="$(( ${CHARACTERS} - 13 ))" + WIDTH="$(( ${WIDTH} * 5 ))" + WIDTH="$(( ${WIDTH} + 128 ))" + else + WIDTH="128" + fi +} + case "${FORMAT}" in - full) + cli) RED="$(tput setaf 1)$(tput bold)" GREEN="$(tput setaf 2)$(tput bold)" YELLOW="$(tput setaf 3)$(tput bold)" @@ -113,36 +228,95 @@ EOF ;; - short) + csv) + +cat << EOF +# container-tools version ${VERSION} +Host${CSV_SEPARATOR}Container${CSV_SEPARATOR}Status${CSV_SEPARATOR}IPv4-Address +EOF + ;; -esac -Show () + json) + +cat << EOF { - case "${FORMAT}" in - short) - printf "${CONTAINER}\n" - ;; + "container-tools": { + "version": "${VERSION}", + }, + "host": { + "name": "${HOST}", + }, + "container": [ +EOF - full) - FIRST_LINE="true" - - for ADDRESS in ${ADDRESSES} - do - case "${FIRST_LINE}" in - true) - FIRST_LINE="false" - printf "%-80s %-29s\n" "${STATUS} ${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" - ;; - - *) - printf "%-80s %-29s\n" "" "${ADDRESS}" - ;; - esac - done - ;; - esac -} + ;; + + nwdiag) + NETWORK="$(echo ${HOST} | sed -e 's|\.|_|g')" + +cat << EOF +# container-tools ${VERSION} +nwdiag { + external_connector = none; + network ${NETWORK} { +EOF + + if [ -n "${NWDIAG_LABEL}" ] + then + echo " label = \"${NWDIAG_LABEL}\"" + else + echo " label = \"\"" + fi + + NUMBER="$(${0} --format=short | wc -l)" + + if [ -n "${NWDIAG_COLOR}" ] + then + COLOR="${NWDIAG_COLOR}" + else + COLOR="#3465a4" + fi + + Nwdiag_width "${HOST}" + +cat << EOF + # host + ${HOST} [color = "${COLOR}", shape = "box", numbered = "${NUMBER}", width = "${WIDTH}"]; + # container +EOF + + ;; + + yaml) + +cat << EOF +--- +container_tools: + version: ${VERSION} + +host: + name: ${HOST} + +container: +EOF + + ;; + + xml) + +cat << EOF + + ${VERSION} + + + ${HOST} + + +EOF + + ;; +esac CONTAINERS="$(cd "${MACHINES}" 2>/dev/null && find -maxdepth 1 -type d -and -not -name '.container-tools' -and -not -name 'container-tools' -and -not -name 'lost+found' -and -not -name '.snap' -and -not -name '.snapshot' -printf '%P\n' | sort)" @@ -183,21 +357,21 @@ do ;; esac - ADDRESSES="" + ADDRESS="" if ls "${MACHINES}/${CONTAINER}/etc/systemd/network"/*.network > /dev/null 2>&1 then - ADDRESSES="$(awk -F= '/Address/ { print $2 }' ${MACHINES}/${CONTAINER}/etc/systemd/network/*.network)" + ADDRESS="$(awk -F= '/Address/ { print $2 }' ${MACHINES}/${CONTAINER}/etc/systemd/network/*.network | head -n1)" elif [ -e "${MACHINES}/${CONTAINER}/etc/network/interfaces" ] then - ADDRESSES="$(awk '/address/ { print $2 }' ${MACHINES}/${CONTAINER}/etc/network/interfaces)" + ADDRESS="$(awk '/address/ { print $2 }' ${MACHINES}/${CONTAINER}/etc/network/interfaces | head -n1)" fi - ADDRESSES="${ADDRESSES:-n/a}" + ADDRESS="${ADDRESS:-n/a}" if echo ${LIST} | grep -qs all then - Show + List fi for ITEM in other started stopped @@ -206,9 +380,45 @@ do then case "${STATE}" in ${ITEM}) - Show + List ;; esac fi done done + +case "${FORMAT}" in + cli) + +cat << EOF +-------------------------------------------------------------------------------- +EOF + + ;; + + json) + +cat << EOF + ], +} +EOF + + ;; + + xml) + +cat << EOF + +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 new file mode 100755 index 0000000..6e855e4 --- /dev/null +++ b/lib/container/move @@ -0,0 +1,181 @@ +#!/bin/sh + +# container-tools - Manage systemd-nspawn containers +# Copyright (C) 2014-2016 Daniel Baumann +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +COMMAND="$(basename ${0})" + +CONFIG="/etc/container-tools/config" +HOOKS="/etc/container-tools/hooks" +MACHINES="/var/lib/machines" + +Parameters () +{ + GETOPT_LONGOPTIONS="force,new:,old:," + GETOPT_OPTIONS="f,n:,o:," + + PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" + + if [ "${?}" != "0" ] + then + echo "'${COMMAND}': getopt exit" >&2 + exit 1 + fi + + eval set -- "${PARAMETERS}" + + while true + do + case "${1}" in + -f|--force) + FORCE="true" + shift 1 + ;; + + -n|--new) + NEW="${2}" + shift 2 + ;; + + -o|--old) + OLD="${2}" + shift 2 + ;; + + --) + shift 1 + break + ;; + + *) + echo "'${COMMAND}': getopt error" >&2 + exit 1 + ;; + esac + done +} + +Usage () +{ + echo "Usage: container ${COMMAND} [-f|--force] -n|--new NAME -o|--old NAME" >&2 + exit 1 +} + +Parameters "${@}" + +if [ -z "${OLD}" ] || [ -z "${NEW}" ] +then + Usage +fi + +if [ ! -e "${MACHINES}/${OLD}" ] +then + echo "'${OLD}': no such container" >&2 + exit 1 +fi + +if [ ! -e "${MACHINES}/${NEW}" ] +then + echo "'${NEW}': container already exists" >&2 + exit 1 +fi + +STATE="$(machinectl show ${OLD} 2>&1 | awk -F= '/^State=/ { print $2 }')" + +case "${STATE}" in + running) + echo "'${OLD}': container is started" >&2 + exit 1 + ;; +esac + +case "${FORCE}" in + true) + ;; + + *) + if container list --other | grep -qs "^${OLD}$" + then + echo -n "'${OLD}': rename container to '${NEW}' [y|N]? " + read FORCE + + FORCE="$(echo ${FORCE} | tr [A-Z] [a-z])" + + case "${FORCE}" in + y|yes) + ;; + + *) + exit 1 + ;; + esac + fi + ;; +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}" + +# bind mounts +BIND="$(awk -F= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" + +if [ -n "${BIND}" ] +then + BINDS="$(echo ${BIND} | sed -e 's|;| |g')" + + for BIND in ${BINDS} + do + SOURCE_OLD="$(echo ${BIND} | awk -F: '{ print $1 }')" + SOURCE_NEW="$(echo ${SOURCE_OLD} | sed -e "s|${OLD}|${NEW}|g")" + + if [ "${SOURCE_OLD}" != "${SOURCE_NEW}" ] + then + mv "${SOURCE_OLD}" "${SOURCE_NEW}" + fi + + TARGET_OLD="$(echo ${BIND} | awk -F: '{ print $2 }')" + TARGET_NEW="$(echo ${TARGET_OLD} | sed -e "s|${OLD}|${NEW}|g")" + + if [ "${TARGET_OLD}" != "${TARGET_NEW}" ] + then + mv "${MACHINES}/${NEW}/${TARGET_OLD}" "${MACHINES}/${NEW}/${TARGET_NEW}" + fi + done +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/rename b/lib/container/rename deleted file mode 100755 index 467d687..0000000 --- a/lib/container/rename +++ /dev/null @@ -1,162 +0,0 @@ -#!/bin/sh - -# container-tools - Manage systemd-nspawn containers -# Copyright (C) 2014-2016 Daniel Baumann -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -set -e - -COMMAND="$(basename ${0})" - -CONFIG="/etc/container-tools/config" -MACHINES="/var/lib/machines" - -Parameters () -{ - GETOPT_LONGOPTIONS="force,new:,old:," - GETOPT_OPTIONS="f,n:,o:," - - PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})" - - if [ "${?}" != "0" ] - then - echo "'${COMMAND}': getopt exit" >&2 - exit 1 - fi - - eval set -- "${PARAMETERS}" - - while true - do - case "${1}" in - -f|--force) - FORCE="true" - shift 1 - ;; - - -n|--new) - NEW="${2}" - shift 2 - ;; - - -o|--old) - OLD="${2}" - shift 2 - ;; - - --) - shift 1 - break - ;; - - *) - echo "'${COMMAND}': getopt error" >&2 - exit 1 - ;; - esac - done -} - -Usage () -{ - echo "Usage: container ${COMMAND} [-f|--force] -n|--new NAME -o|--old NAME" >&2 - exit 1 -} - -Parameters "${@}" - -if [ -z "${OLD}" ] || [ -z "${NEW}" ] -then - Usage -fi - -if [ ! -e "${MACHINES}/${OLD}" ] -then - echo "'${OLD}': no such container" >&2 - exit 1 -fi - -if [ ! -e "${MACHINES}/${NEW}" ] -then - echo "'${NEW}': container already exists" >&2 - exit 1 -fi - -STATE="$(machinectl show ${OLD} 2>&1 | awk -F= '/^State=/ { print $2 }')" - -case "${STATE}" in - running) - echo "'${OLD}': container is started" >&2 - exit 1 - ;; -esac - -case "${FORCE}" in - true) - ;; - - *) - if container list --other | grep -qs "^${OLD}$" - then - echo -n "'${OLD}': rename remote container to '${NEW}' [y|N]? " - read FORCE - - FORCE="$(echo ${FORCE} | tr [A-Z] [a-z])" - - case "${FORCE}" in - y|yes) - ;; - - *) - exit 1 - ;; - esac - fi - ;; -esac - -# Run -mv "${CONFIG}/${OLD}.conf" "${CONFIG}/${NEW}.conf" -mv "${MACHINES}/${OLD}" "${MACHINES}/${NEW}" - -# Renaming bind mounts -BIND="$(awk -F= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" - -if [ -n "${BIND}" ] -then - BINDS="$(echo ${BIND} | sed -e 's|;| |g')" - - for BIND in ${BINDS} - do - SOURCE_OLD="$(echo ${BIND} | awk -F: '{ print $1 }')" - SOURCE_NEW="$(echo ${SOURCE_OLD} | sed -e "s|${OLD}|${NEW}|g")" - - if [ "${SOURCE_OLD}" != "${SOURCE_NEW}" ] - then - mv "${SOURCE_OLD}" "${SOURCE_NEW}" - fi - - TARGET_OLD="$(echo ${BIND} | awk -F: '{ print $2 }')" - TARGET_NEW="$(echo ${TARGET_OLD} | sed -e "s|${OLD}|${NEW}|g")" - - if [ "${TARGET_OLD}" != "${TARGET_NEW}" ] - then - mv "${MACHINES}/${NEW}/${TARGET_OLD}" "${MACHINES}/${NEW}/${TARGET_NEW}" - fi - done -fi - -# Updating configuration file -sed -i -e "s|${OLD}|${NEW}|g" "${CONFIG}/${NEW}.conf" 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