.dots/scripts/podman.sh

101 lines
2 KiB
Bash
Raw Normal View History

2024-11-28 14:02:31 +01:00
#!/usr/bin/env sh
# author: Daniel Sommer <daniel.sommer@velvettear.de>
# license: MIT
[[ "$(id -u)" != "0" ]] && printf ">> error: permission denied\n" >&2 && exit 1
[[ -z "$1" ]] && printf ">> error: action unspecified\n" >&2 && exit 1
printf ">> podman control script started!\n\n"
current="$(pwd)"
what="podman-compose.yml"
path="$2"
[[ -z "$path" ]] && path="$(dirname $0)"
path="$(realpath $path)"
printf "> looking for '"$what"' files in '"$path"'...\n"
stacks="$(find "$path" -name 'podman-compose.yml')"
[[ -z "$stacks" ]] && printf ">> error: could not find any files\n" >&2 && exit 1
case "$1" in
up|down|restart|upgrade|update)
;;
*)
printf "error: action '"$1"' unknown\n" >&2 && exit 1
;;
esac
for stack in $stacks; do
dir="$(dirname "$stack")"
name="$(basename "$dir")"
cd "$dir"
case "$1" in
up)
printf "\n>> starting '"$name"'..."
podman-compose up -d &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
fi
;;
down)
printf "\n>> stopping '"$name"'..."
podman-compose down &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
fi
;;
restart)
printf "\n>> restarting '"$name"'..."
podman-compose restart &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
fi
;;
upgrade|update)
printf "\n>> upgrading '"$name"'...\n"
printf "> stopping '"$name"'..."
podman-compose down &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
break
fi
printf "> removing image(s)..."
podman image prune -a -f &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
break
fi
printf "> pulling new image(s) and restarting '"$name"'..."
podman-compose up -d &> /dev/null
if [[ "$?" == "0" ]]; then
printf " done!\n"
else
printf " error!\n"
fi
;;
esac
done
cd "$current"
msg="\n>> podman control script finished"
[[ "$SECONDS" ]] && msg="$msg after "$SECONDS" seconds"
printf "$msg!\n"