From d956e3e653b61075ef0a49ea3645993ad6805058 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Sat, 10 Oct 2020 20:26:24 +0100 Subject: [PATCH] Add deployment scripts --- helmcharts/TestApp/build_and_deploy.sh | 18 +++++ helmcharts/TestApp/deploy_and_wait.sh | 97 ++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 helmcharts/TestApp/build_and_deploy.sh create mode 100644 helmcharts/TestApp/deploy_and_wait.sh diff --git a/helmcharts/TestApp/build_and_deploy.sh b/helmcharts/TestApp/build_and_deploy.sh new file mode 100644 index 0000000..119d7d9 --- /dev/null +++ b/helmcharts/TestApp/build_and_deploy.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -euo pipefail + +# Change to this directory if you're running outside the directory +cd "$(dirname "$0")" + +# Build the current docker images +./docker_build.sh $* + +# Deploy and wait for success +CHART="./charts/test-app" \ +RELEASE_NAME="my-test-app-release" \ +NAMESPACE="local" \ +HELM_ARGS="--set test-app-cli.image.tag=$1 \ + --set test-app-api.image.tag=$1 \ + --set test-app-service.image.tag=$1 \ +" \ +./deploy_and_wait.sh \ No newline at end of file diff --git a/helmcharts/TestApp/deploy_and_wait.sh b/helmcharts/TestApp/deploy_and_wait.sh new file mode 100644 index 0000000..e770fe9 --- /dev/null +++ b/helmcharts/TestApp/deploy_and_wait.sh @@ -0,0 +1,97 @@ +#!/bin/bash +set -euo pipefail + +# Required Variables: +[ -z "$CHART" ] && echo "Need to set CHART" && exit 1; +[ -z "$RELEASE_NAME" ] && echo "Need to set RELEASE_NAME" && exit 1; +[ -z "$NAMESPACE" ] && echo "Need to set NAMESPACE" && exit 1; + +# Set the helm context to the same as the kubectl context +KUBE_CONTEXT=$(kubectl config current-context) + +# Install/upgrade the chart +helm upgrade --install \ + $RELEASE_NAME \ + $CHART \ + --kube-context "${KUBE_CONTEXT}" \ + --namespace="$NAMESPACE" \ + $HELM_ARGS + +echo 'LOG: Watching for successful release...' + +# Timeout after 6 repeats = 60 seconds +release_timeout=6 +counter=0 + +# Loop while $counter < $release_timeout +while [ $counter -lt $release_timeout ]; do + # Fetch a list of release names + releases="$(helm ls -q --kube-context "${KUBE_CONTEXT}")" + + # Check if $releases contains RELEASE_NAME + if ! echo "${releases}" | grep -qF "${RELEASE_NAME}"; then + + echo "${releases}" + echo "LOG: ${RELEASE_NAME} not found. ${counter}/${release_timeout} checks completed; retrying." + + # NOTE: The pre-increment usage. This makes the arithmatic expression + # always exit 0. The post-increment form exits non-zero when counter + # is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes + ((++counter)) + sleep 10 + else + # Our release is there, we can stop checking + break + fi +done + +if [ $counter -eq $release_timeout ]; then + echo "LOG: ${RELEASE_NAME} failed to appear." 1>&2 + exit 1 +fi + + +# Timeout after 20 mins (to leave time for migrations) +timeout=120 +counter=0 + +# While $counter < $timeout +while [ $counter -lt $timeout ]; do + + # Fetch all pods tagged with the release + release_pods="$(kubectl get pods \ + -l "app.kubernetes.io/instance=${RELEASE_NAME}" \ + -o 'custom-columns=NAME:.metadata.name,STATUS:.status.phase' \ + -n "${NAMESPACE}" \ + --context "${KUBE_CONTEXT}" \ + --no-headers \ + )" + + # If we have any failures, then the release failed + if echo "${release_pods}" | grep -qE 'Failed'; then + echo "LOG: ${RELEASE_NAME} failed. Check the pod logs." + exit 1 + fi + + # Are any of the pods _not_ in the Running/Succeeded status? + if echo "${release_pods}" | grep -qvE 'Running|Succeeded'; then + + echo "${release_pods}" | grep -vE 'Running|Succeeded' + echo "${RELEASE_NAME} pods not ready. ${counter}/${timeout} checks completed; retrying." + + # NOTE: The pre-increment usage. This makes the arithmatic expression + # always exit 0. The post-increment form exits non-zero when counter + # is zero. More information here: http://wiki.bash-hackers.org/syntax/arith_expr#arithmetic_expressions_and_return_codes + ((++counter)) + sleep 10 + else + #All succeeded, we're done! + echo "${release_pods}" + echo "LOG: All ${RELEASE_NAME} pods running. Done!" + exit 0 + fi +done + +# We timed out +echo "LOG: Release ${RELEASE_NAME} did not complete in time" 1>&2 +exit 1 \ No newline at end of file