diff options
Diffstat (limited to '')
-rwxr-xr-x | lib/container/auto | 19 | ||||
-rwxr-xr-x | lib/container/console | 19 | ||||
-rwxr-xr-x | lib/container/create | 19 | ||||
-rwxr-xr-x | lib/container/enter | 19 | ||||
-rwxr-xr-x | lib/container/limit | 19 | ||||
-rwxr-xr-x | lib/container/list | 282 | ||||
-rwxr-xr-x | lib/container/move (renamed from lib/container/rename) | 25 | ||||
-rwxr-xr-x | lib/container/remove | 19 | ||||
-rwxr-xr-x | lib/container/restart | 19 | ||||
-rwxr-xr-x | lib/container/start | 26 | ||||
-rwxr-xr-x | lib/container/status | 19 | ||||
-rwxr-xr-x | lib/container/stop | 19 | ||||
-rwxr-xr-x | lib/container/version | 19 |
13 files changed, 484 insertions, 39 deletions
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> + <name>${CONTAINER}</name> + <ipv4Address>${ADDRESS}</ipv4Address> + </container> +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 +<container-tools> + <version>${VERSION}</version> +</container-tools> +<host> + <name>${HOST}</name> +</host> +<containers> +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 +</containers> +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/rename b/lib/container/move index 467d687..6e855e4 100755 --- a/lib/container/rename +++ 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 () @@ -110,7 +111,7 @@ case "${FORCE}" in *) if container list --other | grep -qs "^${OLD}$" then - echo -n "'${OLD}': rename remote container to '${NEW}' [y|N]? " + echo -n "'${OLD}': rename container to '${NEW}' [y|N]? " read FORCE FORCE="$(echo ${FORCE} | tr [A-Z] [a-z])" @@ -127,11 +128,20 @@ 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}" -# Renaming bind mounts +# bind mounts BIND="$(awk -F= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" if [ -n "${BIND}" ] @@ -158,5 +168,14 @@ then done fi -# Updating configuration file +# 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 |