summaryrefslogtreecommitdiffstats
path: root/lib/container/list
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@open-infrastructure.net>2016-05-06 11:58:18 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2016-05-06 12:46:43 +0000
commit462ad1d08895d79c60f0a9aa08837858ab2cb683 (patch)
treec82ac09ae56b78b025b89f9e7374bc8b294c661a /lib/container/list
parentAdding upstream version 20160415. (diff)
downloadopen-infrastructure-compute-tools-462ad1d08895d79c60f0a9aa08837858ab2cb683.tar.xz
open-infrastructure-compute-tools-462ad1d08895d79c60f0a9aa08837858ab2cb683.zip
Adding upstream version 20160501.upstream/20160501
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'lib/container/list')
-rwxr-xr-xlib/container/list154
1 files changed, 136 insertions, 18 deletions
diff --git a/lib/container/list b/lib/container/list
index 110e6ea..5ab5755 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' -and -not -name '.snap' -and -not -name '.snapshot' -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