From f04aa87e434b4fa248a6cd1f35e5af623189df4c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 19 Mar 2016 12:10:04 +0100 Subject: Adding short and full list format to container list command. Signed-off-by: Daniel Baumann --- lib/container/list | 154 ++++++++++++++++++++++++++++++++++++----- share/man/container-list.1.txt | 17 ++++- 2 files changed, 150 insertions(+), 21 deletions(-) diff --git a/lib/container/list b/lib/container/list index 110e6ea..e9e3305 100755 --- a/lib/container/list +++ b/lib/container/list @@ -22,46 +22,164 @@ COMMAND="$(basename ${0})" MACHINES="/var/lib/machines" +Parameters () +{ + LONG_OPTIONS="all,format:,started,stopped" + OPTIONS="a,f:,s,t" + + PARAMETERS="$(getopt --longoptions ${LONG_OPTIONS} --name=${COMMAND} --options ${OPTIONS} --shell sh -- ${@})" + + if [ "${?}" != "0" ] + then + echo "'${COMMAND}': getopt exit" >&2 + exit 1 + fi + + eval set -- "${PARAMETERS}" + + while true + do + case "${1}" in + -a|--all) + LIST="all" + shift 1 + ;; + + -f|--format) + FORMAT="${2}" + shift 2 + ;; + + -s|--started) + LIST="started" + shift 1 + ;; + + -t|--stopped) + LIST="stopped" + shift 1 + ;; + + --) + shift 1 + break + ;; + + *) + echo "'${COMMAND}': getopt error" >&2 + exit 1 + ;; + esac + done +} + Usage () { - echo "Usage: container ${COMMAND}" >&2 + echo "Usage: container ${COMMAND} -a|--all --format FORMAT -s|--started -t|--stopped" >&2 exit 1 } -if [ -n "${1}" ] -then - Usage -fi +Parameters "${@}" + +LIST="${LIST:-all}" +FORMAT="${FORMAT:-full}" # Run -CONTAINERS="$(cd "${MACHINES}" && find -maxdepth 1 -type d -and -not -name 'lost+found' -printf '%P\n' | sort)" +case "${FORMAT}" in + full) + RED="$(tput setaf 1)$(tput bold)" + GREEN="$(tput setaf 2)$(tput bold)" + YELLOW="$(tput setaf 3)$(tput bold)" + BLUE="$(tput setaf 4)$(tput bold)" + NORMAL="$(tput sgr0)" -echo RUNNING +cat << EOF +Container IPv4 Address Status +-------------------------------------------------------------------------------- +EOF + + ;; + + short) + ;; +esac + +CONTAINERS="$(cd "${MACHINES}" && find -maxdepth 1 -type d -and -not -name 'lost+found' -printf '%P\n' | sort)" for CONTAINER in ${CONTAINERS} do + # FIXME: ignore lxc container for now + if [ -e "${MACHINES}/${CONTAINER}/rootfs" ] + then + continue + fi + STATE="$(machinectl show ${CONTAINER} 2>&1 | awk -F= '/^State=/ { print $2 }')" + if [ -e "${MACHINES}/${CONTAINER}/etc/network/interfaces" ] + then + ADDRESS="$(awk '/address/ { print $2 }' ${MACHINES}/${CONTAINER}/etc/network/interfaces)" + else + ADDRESS="n/a" + fi + case "${STATE}" in running) - echo " ${CONTAINER}" + STATUS="${GREEN}started${NORMAL}" + + ;; + + *) + STATUS="${RED}stopped${NORMAL}" ;; esac -done -echo -echo STOPPED + case "${LIST}" in + all) + case "${FORMAT}" in + short) + printf "${CONTAINER}\n" + ;; -for CONTAINER in ${CONTAINERS} -do - STATE="$(machinectl show ${CONTAINER} 2>&1 | awk -F= '/^State=/ { print $2 }')" + full) + printf "%-72s %-29s %-7s\n" "${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" "${STATUS}" + ;; + esac + ;; - case "${STATE}" in - running) + started) + case "${STATE}" in + running) + case "${FORMAT}" in + short) + printf "${CONTAINER}\n" + ;; + + full) + printf "%-72s %-29s %-7s\n" "${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" "${STATUS}" + ;; + esac + ;; + esac ;; - *) - echo " ${CONTAINER}" + stopped) + case "${STATE}" in + running) + ;; + + *) + case "${FORMAT}" in + short) + printf "${CONTAINER}\n" + ;; + + full) + printf "%-72s %-29s %-7s\n" "${BLUE}${CONTAINER}${NORMAL}" "${YELLOW}${ADDRESS}${NORMAL}" "${STATUS}" + ;; + esac + ;; + esac ;; esac done diff --git a/share/man/container-list.1.txt b/share/man/container-list.1.txt index 4dcbd78..e7015b7 100644 --- a/share/man/container-list.1.txt +++ b/share/man/container-list.1.txt @@ -39,13 +39,24 @@ The container list command lists container on the system. OPTIONS ------- -This command has no options. +The following container options are available: +*-a, --all*:: + List all available container. + +*-f, --format='FORMAT'*:: + Use format to list container. Currently available formats are 'short' or 'full' (default). + +*-s, --started*:: + List only started container. + +*-t, --stopped*:: + List only stopped container. EXAMPLES -------- -*List container on the the system:*:: - sudo container list +*List all started container on the the system as a machine-readable list:*:: + sudo container list --all --format=short SEE ALSO -- cgit v1.2.3