mirror of https://git.sr.ht/~skiqqy/bin
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
3.1 KiB
185 lines
3.1 KiB
#!/usr/bin/env bash
|
|
# Author: Skiqqy
|
|
# Podman MANager
|
|
|
|
declare -A coms
|
|
|
|
# Available commands
|
|
coms[spin]='up|down [CNAME]\nStart/Stop all/specific container(s).'
|
|
coms[help]='[COMMAND]\nPrint helpful information about a command.'
|
|
coms[cron]='add|remove [CNAME]\nAdd or remove a boot crontjob to start the container.'
|
|
|
|
sname=$(basename "$0")
|
|
|
|
usage()
|
|
{
|
|
cat << EOF
|
|
|
|
$sname ~ Podman MANager
|
|
|
|
Usage: $sname [options] COMMAND [ARGS...]
|
|
|
|
Options
|
|
-h --help Shows this message.
|
|
--commands See available commands
|
|
|
|
EOF
|
|
exit "${1:-0}"
|
|
}
|
|
|
|
# Use a menu to get container name, think dmenu for tty
|
|
select_contd()
|
|
{
|
|
select contd in $(podman ps -a --format '{{.Names}}' | tr '\n' ' ')
|
|
do
|
|
[ -z "$contd" ] && return 1
|
|
printf '%s' "$contd" && return
|
|
done
|
|
}
|
|
|
|
spin()
|
|
{
|
|
local com
|
|
case "$1" in
|
|
up|u)
|
|
com=start
|
|
;;
|
|
down|d)
|
|
com=stop
|
|
;;
|
|
*)
|
|
help spin
|
|
return 1
|
|
;;
|
|
esac
|
|
|
|
if [ -z "$2" ]
|
|
then
|
|
for contd in $(podman ps -a --format '{{.Names}}')
|
|
do
|
|
printf '%s...\n' "$contd"
|
|
podman "$com" "$contd"
|
|
done
|
|
else
|
|
podman "$com" "$2"
|
|
fi
|
|
}
|
|
|
|
# add/remove user cronjobs for starting containers on boot
|
|
cron()
|
|
{
|
|
local contd
|
|
local job
|
|
[ -z "$1" ] && help cron && return 1
|
|
|
|
cronfile=/tmp/pman.crontab.tmp # tmp file name
|
|
contd=${2:-$(select_contd)}
|
|
[ -z "$contd" ] && return 1
|
|
job="@reboot sleep 150 && ${BASH_SOURCE[0]} s u $contd" # The job to be runk
|
|
crontab -l > "$cronfile"
|
|
|
|
case "$1" in
|
|
add|a)
|
|
if ! grep "$job" "$cronfile" > /dev/null
|
|
then
|
|
echo "$job" >> "$cronfile" # Add if its not in the file
|
|
fi
|
|
;;
|
|
remove|rem|r)
|
|
grep -v "$job" "$cronfile" > "$cronfile.tmp" # remove, using invert matching
|
|
mv "$cronfile.tmp" "$cronfile" # Rename
|
|
;;
|
|
esac
|
|
|
|
crontab "$cronfile"
|
|
}
|
|
|
|
help()
|
|
{
|
|
[ -z "$1" ] && usage
|
|
if [ -n "${coms[$1]}" ]
|
|
then
|
|
printf 'Usage: $ %s %s %b\n' "$sname" "$1" "${coms[$1]}"
|
|
else
|
|
printf 'Unknown command <%s>\n' "$1"
|
|
fi
|
|
}
|
|
|
|
commands()
|
|
{
|
|
printf '\n Commands:\n\n'
|
|
for key in "${!coms[@]}"
|
|
do
|
|
printf ' %-15s%s\n' "$key" "$(cut -d '\' -f 2- <<< "${coms[$key]}" | cut -b2-)"
|
|
done
|
|
printf '\n See $ %s help COMMAND\n\n' "$sname"
|
|
}
|
|
|
|
main()
|
|
{
|
|
# Parse long args
|
|
for opt in "$@"
|
|
do
|
|
shift
|
|
case "$opt" in
|
|
--commands)
|
|
commands
|
|
return
|
|
;;
|
|
--help)
|
|
set -- "$@" '-h'
|
|
;;
|
|
--*)
|
|
printf 'ERR: unknown long arg %s\n' "$opt"
|
|
usage 1
|
|
;;
|
|
*)
|
|
set -- "$@" "$opt" # Nothing to do with this arg
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Parse short opts
|
|
OPTIND=1
|
|
while getopts 'h' opt
|
|
do
|
|
case "$opt" in
|
|
h)
|
|
usage
|
|
;;
|
|
*)
|
|
#usage 1
|
|
;;
|
|
esac
|
|
done
|
|
shift $((OPTIND-1))
|
|
|
|
# By default print container info
|
|
if [ -z "$1" ]
|
|
then
|
|
# podman --format does wierd things on other platforms, this forces the format i want
|
|
printf "%b\n\n" "$(podman ps -a --format \
|
|
"\\n - {{.Names}}\\n\\tPorts: {{.Ports}}\\n\\tStatus: {{.Status}}")"
|
|
return
|
|
fi
|
|
|
|
local arg
|
|
arg="$1"
|
|
|
|
# map short names to actual names
|
|
case "$arg" in
|
|
s)
|
|
arg=spin
|
|
;;
|
|
esac
|
|
|
|
if [ -n "$1" ] && [ -n "${coms[$arg]}" ]
|
|
then
|
|
shift 1
|
|
$arg "$@" # run the command
|
|
else
|
|
printf 'ERR: Unknown command <%s>\nSee $ %s --commands\n' "$1" "$sname"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|
|
|