feat: add scripts/manage.sh

This commit is contained in:
cătălin 2024-07-14 19:37:37 +02:00
commit a4b6a8c186
No known key found for this signature in database
3 changed files with 165 additions and 30 deletions

View file

@ -1,29 +1,35 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-executables-have-shebangs
- id: check-json
- id: pretty-format-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
args:
- --allow-multiple-documents
- id: detect-private-key
- id: trailing-whitespace
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-executables-have-shebangs
- id: check-json
- id: pretty-format-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-yaml
args:
- --allow-multiple-documents
- id: detect-private-key
- id: trailing-whitespace
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.86.0
hooks:
- id: terraform_fmt
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.86.0
hooks:
- id: terraform_fmt
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
args: [--format, parsable, --strict]
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
args: [--format, parsable, --strict]
- repo: https://github.com/shellcheck-py/shellcheck-py
rev: v0.10.0.1
hooks:
- id: shellcheck
files: \.sh

View file

@ -10,13 +10,8 @@ lint--kubescore:
lint--tflint:
tflint --recursive
lint--scripts:
shellcheck scripts/**.sh
lint:
make lint--pre-commit
make lint--kubeconform
make lint--tflint
make lint--scripts

134
scripts/manage.sh Executable file
View file

@ -0,0 +1,134 @@
#!/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