Created
January 16, 2020 07:09
-
-
Save karancode/dff8732b52d60d10f42da1955cb9255e to your computer and use it in GitHub Desktop.
Rollout restart all the deployments in k8s cluster - supporting multiple environments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| ### | |
| # This is a general-purpose function to ask Yes/No questions in Bash, either | |
| # with or without a default answer. It keeps repeating the question until it | |
| # gets a valid answer. | |
| ask() { | |
| # https://gist.github.com/davejamesmiller/1965569 | |
| local prompt default reply | |
| if [ "${2:-}" = "Y" ]; then | |
| prompt="Y/n" | |
| default=Y | |
| elif [ "${2:-}" = "N" ]; then | |
| prompt="y/N" | |
| default=N | |
| else | |
| prompt="y/n" | |
| default= | |
| fi | |
| while true; do | |
| # Ask the question (not using "read -p" as it uses stderr not stdout) | |
| echo -n "$1 [$prompt] " | |
| # Read the answer (use /dev/tty in case stdin is redirected from somewhere else) | |
| read reply </dev/tty | |
| # Default? | |
| if [ -z "$reply" ]; then | |
| reply=$default | |
| fi | |
| # Check if the reply is valid | |
| case "$reply" in | |
| Y*|y*) return 0 ;; | |
| N*|n*) return 1 ;; | |
| esac | |
| done | |
| } | |
| set -e | |
| source ./config | |
| rollout_restart_all() | |
| { | |
| while read -r deployment; do | |
| echo "rollout restart deployment : ${deployment} " | |
| echo "-------------------------------------------" | |
| kubectl --namespace default rollout restart deployment/${deployment} | |
| echo "sleeping 5 second..." | |
| sleep 5 | |
| done < ./all_deployments_$ENV.txt | |
| } | |
| echo "setting context based on ./config file ENV" | |
| while : | |
| do | |
| case $ENV in | |
| dev) | |
| kubectx $DEV_CTX | |
| echo "context set to $DEV_CTX " | |
| break | |
| ;; | |
| stg) | |
| kubectx $STG_CTX | |
| echo "context set to $STG_CTX" | |
| break | |
| ;; | |
| perf) | |
| kubectx $PERF_CTX | |
| echo "context set to $PERF_CTX" | |
| break | |
| ;; | |
| prod) | |
| if ask "!!!PROD!!!"; then | |
| kubectx $PROD_CTX | |
| echo "context set to $PROD_CTX" | |
| else | |
| echo "NO! Exiting..." | |
| exit 1 | |
| fi | |
| esac | |
| done | |
| if ask "generating list of deployments!"; then | |
| kubectl get deployment --namespace default -o json | jq -r .items[].metadata.name > ./all_deployments_$ENV.txt | |
| echo "check all deployments list: " | |
| ls -al ./all_deployments_$ENV.txt | |
| else | |
| echo "NO! exiting...." | |
| exit 1 | |
| fi | |
| if ask "Rollout restart deployments ?"; then | |
| if [ "$ENV" = "prod" ]; then | |
| if ask "!!!PROD!!!"; then | |
| rollout_restart_all | |
| else | |
| echo "Be careful... Exiting..." | |
| exit 1 | |
| fi | |
| else | |
| rollout_restart_all | |
| fi | |
| else | |
| echo "NO! Exiting..." | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment