From f64e4e06f0d06bde27e4dc8f030a7c92d5203c1e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 11 Dec 2016 12:34:55 +0100 Subject: Adding nwdiag export format to container list command. Signed-off-by: Daniel Baumann --- lib/container/list | 87 ++++++++++++++++++++++++++++++++++++++++- share/bash-completion/container | 2 +- share/man/container-list.1.txt | 14 ++++++- 3 files changed, 99 insertions(+), 4 deletions(-) diff --git a/lib/container/list b/lib/container/list index a07b85b..45430a4 100755 --- a/lib/container/list +++ b/lib/container/list @@ -27,7 +27,7 @@ VERSION="$(container version)" Parameters () { - GETOPT_LONGOPTIONS="all,csv-separator:,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 -- ${@})" @@ -63,6 +63,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 @@ -93,7 +103,7 @@ Parameters () Usage () { - echo "Usage: container ${COMMAND} [-a|--all] [--csv-separator SEPARATOR] [--format FORMAT] [-h|--host HOSTNAME] [-o|--other] [-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 } @@ -129,12 +139,49 @@ 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}" ;; 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 cli) RED="$(tput setaf 1)$(tput bold)" @@ -174,6 +221,42 @@ cat << EOF EOF ;; + + 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 + + ;; 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)" diff --git a/share/bash-completion/container b/share/bash-completion/container index 3b0e033..abdd89a 100644 --- a/share/bash-completion/container +++ b/share/bash-completion/container @@ -139,7 +139,7 @@ _container() ;; -f|--format) - opts="cli csv json shell" + opts="cli csv json nwdiag shell" COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 ;; diff --git a/share/man/container-list.1.txt b/share/man/container-list.1.txt index 488b718..f19d2ef 100644 --- a/share/man/container-list.1.txt +++ b/share/man/container-list.1.txt @@ -50,11 +50,17 @@ The following container options are available, defaults to *--started --stopped* Specify custom CSV separator, defaults to ','. *-f, --format='FORMAT'*:: - Use format to list container. Currently available formats are 'cli' (default), 'csv', 'json', or 'shell'. + Use format to list container. Currently available formats are 'cli' (default), 'csv', 'json', 'nwdiag', or 'shell'. *-h, --host='HOSTNAME'*:: List only container that are enabled for automatic start on the specified hostname. Defaults to list containers of the local system only. Using 'all' shows all container regardless of any automatic start configuration. +*--nwdiag-color='COLOR'*:: + Specify custom nwdiag color for the host box, defaults to '#3465a4'. + +*--nwdiag-label='LABEL'*:: + Specify custom nwdiag label for the diagram, defaults to empty. + *-o, --other*:: List only container that are not enable for automatic start on the current system. @@ -78,6 +84,12 @@ EXAMPLES *Create a JSON export of all started and stopped containers:*:: sudo container list -f json +*Create a nwdiag export of all started and stopped containers:*:: + sudo container list -f nwdiag + +*Create a SVG image via nwdiag of all started and stopped containers:*:: + sudo container list -f nwdiag | nwdiag -T svg -o cnt-list.svg - + *Create a shell export of all started and stopped containers:*:: sudo container list -f shell -- cgit v1.2.3