homelab/fighter/scripts/docker-stacks.sh

96 lines
2.9 KiB
Bash
Raw Normal View History

2023-10-06 15:56:19 -07:00
# takes a docker-compose.yml file path and returns a boolean to represent
# whether that stack depends on an smb share under the `/mnt/nas` path
2023-10-10 14:21:48 -07:00
function check_nas {
2023-10-10 14:36:07 -07:00
docker-compose config | grep -q /mnt/nas
2023-10-06 15:56:19 -07:00
MATCH=$?
2023-10-10 14:38:47 -07:00
if [ $MATCH != 0]; then
2023-10-06 15:56:19 -07:00
return false
fi
}
# takes a docker-compose.yml file path and returns a boolean to represent
# whether that stack passes a docker-compose config lint
2023-10-10 14:29:53 -07:00
function lint {
2023-10-10 14:36:07 -07:00
docker-compose config > /dev/null 2>&1
return $?
}
2023-10-10 14:29:53 -07:00
function compose_config {
2023-10-10 14:36:07 -07:00
docker-compose config
2023-10-10 14:29:53 -07:00
}
# takes a docker-compose.yml file path and shuts it down
function compose_down {
2023-10-10 14:36:07 -07:00
docker-compose down
}
# takes a docker-compose.yml file path and brings it up
function compose_up {
2023-10-10 14:16:24 -07:00
if [ "$FORCERECREATE" = true ]; then
2023-10-10 14:36:07 -07:00
docker-compose up --force-recreate -d
2023-10-10 14:16:24 -07:00
elif [ "$FORCERECREATE" = false ]; then
2023-10-10 14:36:07 -07:00
docker-compose up -d
2023-10-10 14:16:24 -07:00
else
echo "Bad variable value: \$FORCERECREATE=$FORCERECREATE"
fi
}
function main {
2023-10-10 14:21:48 -07:00
#echo "\$ARGS is $ARGS"
2023-10-10 14:16:24 -07:00
STACKS_DIRECTORY="/home/admin/homelab/fighter/config"
while [[ $# -gt 0 ]]; do
#echo "case is $1"
case $1 in
2023-10-10 14:16:24 -07:00
# global flags are parsed first
2023-10-10 14:21:48 -07:00
-n|--nas-only) NAS_ONLY=true; shift ;;
2023-10-10 14:16:24 -07:00
-l|--lint) LINT=true; shift ;;
-p|--path) STACKS_DIRECTORY="$2"; shift; shift ;;
-v|--verbose) VERBOSE=true; shift ;;
# commands are parsed with nested parsing for subcommand flags
up*) COMMAND="up"; shift;
while [[ $# -gt 0 ]]; do
case $1 in
2023-10-10 14:21:48 -07:00
-f|--force-recreate) FORCE_RECREATE=true; shift;;
2023-10-10 14:29:53 -07:00
*) echo "Unrecognized option '$1' for '$COMMAND'"; exit 1;;
2023-10-10 14:16:24 -07:00
esac
done
2023-10-09 11:21:23 -07:00
;;
2023-10-10 14:16:24 -07:00
down*) COMMAND="down"; shift;
while [[ $# -gt 0 ]]; do
case $1 in
2023-10-10 14:21:48 -07:00
-o|--remove-orphans) REMOVE_ORPHANS=true; shift;;
2023-10-10 14:29:53 -07:00
*) echo "Unrecognized option '$1' for '$COMMAND'"; exit 1;;
esac
done
;;
config*) COMMAND="config"; shift;
while [[ $# -gt 0 ]]; do
case $1 in
-n|--no-interpolate) NO_INTERPOLATE=true; shift;;
*) echo "Unrecognized option '$1' for '$COMMAND'"; exit 1;;
2023-10-10 14:16:24 -07:00
esac
done
2023-10-09 11:21:23 -07:00
;;
2023-10-10 14:16:24 -07:00
*) echo "Unrecognized option $1"; exit 1 ;;
esac
done
2023-10-10 14:16:24 -07:00
for stack in "$STACKS_DIRECTORY"/* ; do
2023-10-10 14:36:07 -07:00
cd $stack
2023-10-10 14:38:47 -07:00
if [ $NAS_ONLY ]; then
if
2023-10-10 14:21:48 -07:00
case $COMMAND in
2023-10-10 14:38:47 -07:00
up) echo "$COMMAND at $PWD"
;;
down) echo "$COMMAND at $PWD"
;;
config) echo "$COMMAND at $PWD"
;;
2023-10-10 14:21:48 -07:00
*) echo "Unrecognized command '$COMMAND'" ;;
esac
2023-10-10 14:16:24 -07:00
done
}
2023-10-10 14:16:24 -07:00
ARGS="$@"
main $ARGS