From a4b6a8c186d3149e9d2dccdb7119647b440a41fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=C4=83t=C4=83lin?= Date: Sun, 14 Jul 2024 19:37:37 +0200 Subject: [PATCH] feat: add scripts/manage.sh --- .pre-commit-config.yaml | 56 +++++++++-------- Makefile | 5 -- scripts/manage.sh | 134 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 30 deletions(-) create mode 100755 scripts/manage.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cad2fc6..9669872 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/Makefile b/Makefile index 40e2258..6e09254 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/scripts/manage.sh b/scripts/manage.sh new file mode 100755 index 0000000..49842db --- /dev/null +++ b/scripts/manage.sh @@ -0,0 +1,134 @@ +#!/bin/sh + +print_usage() { + echo "Usage:" + echo " $0 [-d|--daemon]" + echo " $0 sh [--shell ]" + echo " $0 logs [] [-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