motd.sh: Make docker optional

This commit is contained in:
Alice Gaudon 2023-08-19 19:43:01 +02:00
parent c6192fc397
commit cb8e2fc73f

68
motd.sh
View File

@ -57,44 +57,46 @@ done
### Docker ### ### Docker ###
out+="${bold}[DOCKER]${undim}\n" if command -v docker &> /dev/null; then
containers=($(docker ps --format "{{.Image}}/{{.Names}}")) out+="${bold}[DOCKER]${undim}\n"
container_statuses=($(docker ps --format "{{.State}}")) containers=($(docker ps --format "{{.Image}}/{{.Names}}"))
docker_out=" [root]\n" container_statuses=($(docker ps --format "{{.State}}"))
for i in ${!containers[@]}; do docker_out=" [root]\n"
if [[ "${container_statuses[$i]}" == "running" ]]; then
docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n"
else
docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n"
fi
done
docker_out+=" [rootless]\n"
rootless_users=($(cat "$dir/.motd.users"))
for user in ${rootless_users[@]}; do
if sudo test ! -e /run/user/$(id -u $user)/docker.sock; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! Docker not running !${undim}\n"
continue
fi
containers=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.Image}}/{{.Names}}"))
container_statuses=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.State}}"))
if [ ${#containers[@]} -eq 0 ]; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! No container !${undim}\n"
continue
fi
docker_out+=" - $user\n"
for i in ${!containers[@]}; do for i in ${!containers[@]}; do
if [[ "${container_statuses[$i]}" == "running" ]]; then if [[ "${container_statuses[$i]}" == "running" ]]; then
docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n" docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n"
else else
docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n" docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n"
fi fi
done done
done
out+=$docker_out docker_out+=" [rootless]\n"
rootless_users=($(cat "$dir/.motd.users"))
for user in ${rootless_users[@]}; do
if sudo test ! -e /run/user/$(id -u $user)/docker.sock; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! Docker not running !${undim}\n"
continue
fi
containers=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.Image}}/{{.Names}}"))
container_statuses=($(sudo -n DOCKER_HOST=unix:///run/user/$(id -u $user)/docker.sock docker ps --format "{{.State}}"))
if [ ${#containers[@]} -eq 0 ]; then
docker_out+=" ${redbg}/!\\\\${undim} - $user,${red}! No container !${undim}\n"
continue
fi
docker_out+=" - $user\n"
for i in ${!containers[@]}; do
if [[ "${container_statuses[$i]}" == "running" ]]; then
docker_out+=" - ${containers[$i]},${green}${container_statuses[$i]}${undim}\n"
else
docker_out+=" ${redbg}/!\\\\${undim} - ${containers[$i]},${red}${container_statuses[$i]}${undim}\n"
fi
done
done
out+=$docker_out
fi
printf "$out" | column -ts $',' -o " " printf "$out" | column -ts $',' -o " "
echo -e echo -e