scripts/mhssh_lxc

69 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
red=$(tput setaf 1)
grn=$(tput setaf 2)
raz=$(tput sgr0)
usage() {
echo "Run a single command on multiple lxc containers"
echo "Usage : $0 [-h|--help] [-n|--no-color] [-o|--os OS] CMD ..."
echo -e "\n\t-h --help\tprint this help\n\t-n --no-color\tdisable colors\n\n"
echo "Host list configured to be found in '$hosts'"
}
nocmd() {
echo -e "at least one command expected...\n"
usage
exit 3
}
# arg "parsing"
[ $# -eq 0 ] && nocmd
case "$1" in
-h|--help)
usage
exit 1
;;
-n|--no-color)
[ $# -eq 1 ] && nocmd
shift
red='[ERR] '
grn=''
raz=''
;;
-o|--os)
if [[ "$#" -gt 0 ]];
then
shift;
OS=$1;
hosts="$HOME/.config/mhssh_lxc.$OS.hosts";
shift;
fi;
;;
esac
if [ -z $hosts ];
then
hosts="$HOME/.config/mhssh_lxc.hosts";
fi
if [ ! -f "$hosts" ]
then
echo -e "File '$hosts' not found" >&2
echo ""
usage
exit 2
fi
while read -r host;
do
echo -e "\n$grn=====================\n$grn$host\n$grn=====================$raz\n"
stdout=$(echo -ne "$grn$(printf "%20s" "$host") : $raz")
stderr=$(echo -ne "$red$(printf "%20s" "$host") : $raz")
{
lxc-attach -n "$host" -- "$@" 2>&3 | sed -e "s/^/$stdout/" 1>&2
} 3>&1 | sed -e "s/^/$stderr/"
done <<< $(cat $hosts)