diff options
author | Daniel Baumann <mail@daniel-baumann.ch> | 2016-12-11 12:34:28 +0000 |
---|---|---|
committer | Daniel Baumann <mail@daniel-baumann.ch> | 2016-12-11 12:34:51 +0000 |
commit | c57f861235add6b95e81c9835e5078452d342e7c (patch) | |
tree | 4ed55298f238922ef4edb1ce0304a559fc9c6a41 /lib/container/list | |
parent | Releasing debian version 20161201-1. (diff) | |
download | open-infrastructure-compute-tools-c57f861235add6b95e81c9835e5078452d342e7c.tar.xz open-infrastructure-compute-tools-c57f861235add6b95e81c9835e5078452d342e7c.zip |
Merging upstream version 20161210.
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'lib/container/list')
-rwxr-xr-x | lib/container/list | 282 |
1 files changed, 246 insertions, 36 deletions
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 |