forked from catalin/fukuops
134 lines
3.4 KiB
Bash
Executable file
134 lines
3.4 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
print_usage() {
|
|
echo "Usage:"
|
|
echo " $0 <up|down> <service_name|all> [-d|--daemon]"
|
|
echo " $0 sh <service_name> <subservice_name> [--shell <shell_name>]"
|
|
echo " $0 logs <service_name> [<subservice_name>] [-f|--follow]"
|
|
exit 1
|
|
}
|
|
|
|
# Check if correct number of arguments is provided
|
|
if [ $# -lt 2 ]; then
|
|
print_usage
|
|
fi
|
|
|
|
action=$1
|
|
service=$2
|
|
daemon_flag=""
|
|
shell="sh"
|
|
follow_flag=""
|
|
|
|
case "$action" in
|
|
up|down)
|
|
if [ $# -gt 3 ]; then
|
|
print_usage
|
|
fi
|
|
if [ $# -eq 3 ]; then
|
|
if [ "$3" = "-d" ] || [ "$3" = "--daemon" ]; then
|
|
daemon_flag="-d"
|
|
else
|
|
echo "Invalid flag. Use '-d' or '--daemon' for detached mode."
|
|
exit 1
|
|
fi
|
|
fi
|
|
;;
|
|
sh)
|
|
if [ $# -lt 3 ]; then
|
|
print_usage
|
|
fi
|
|
subservice=$3
|
|
shift 3
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--shell)
|
|
if [ -z "$2" ]; then
|
|
echo "Shell name is missing after --shell flag."
|
|
exit 1
|
|
fi
|
|
shell="$2"
|
|
shift 2
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1"
|
|
print_usage
|
|
;;
|
|
esac
|
|
done
|
|
;;
|
|
logs)
|
|
subservice=""
|
|
shift 2
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
-f|--follow)
|
|
follow_flag="--follow"
|
|
shift
|
|
;;
|
|
*)
|
|
if [ -z "$subservice" ]; then
|
|
subservice="$1"
|
|
else
|
|
echo "Unknown option: $1"
|
|
print_usage
|
|
fi
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
;;
|
|
*)
|
|
echo "Invalid action. Use 'up', 'down', 'sh', or 'logs'."
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
execute_docker_compose() {
|
|
dir=$1
|
|
cmd=$2
|
|
echo "Executing in $dir: docker compose $cmd $daemon_flag"
|
|
cd "$dir" && docker compose "$cmd" $daemon_flag
|
|
cd - > /dev/null || exit
|
|
}
|
|
|
|
execute_docker_shell() {
|
|
dir=$1
|
|
subservice=$2
|
|
echo "Executing in $dir: docker compose exec $subservice $shell"
|
|
cd "$dir" && docker compose exec "$subservice" "$shell"
|
|
cd - > /dev/null || exit
|
|
}
|
|
|
|
execute_docker_logs() {
|
|
dir=$1
|
|
subservice=$2
|
|
echo "Executing in $dir: docker compose logs $follow_flag $subservice"
|
|
cd "$dir" && docker compose logs $follow_flag "$subservice"
|
|
cd - > /dev/null || exit
|
|
}
|
|
|
|
if [ "$action" = "sh" ]; then
|
|
if [ ! -d "$service" ]; then
|
|
echo "Service directory '$service' not found."
|
|
exit 1
|
|
fi
|
|
execute_docker_shell "$service" "$subservice"
|
|
elif [ "$action" = "logs" ]; then
|
|
if [ ! -d "$service" ]; then
|
|
echo "Service directory '$service' not found."
|
|
exit 1
|
|
fi
|
|
execute_docker_logs "$service" "$subservice"
|
|
elif [ "$service" = "all" ]; then
|
|
for dir in */; do
|
|
if [ -f "${dir}docker-compose.yml" ] || [ -f "${dir}docker-compose.yaml" ]; then
|
|
execute_docker_compose "$dir" "$action"
|
|
fi
|
|
done
|
|
else
|
|
if [ ! -d "$service" ]; then
|
|
echo "Service directory '$service' not found."
|
|
exit 1
|
|
fi
|
|
execute_docker_compose "$service" "$action"
|
|
fi
|