Skip to content

Commit

Permalink
helm-toolbox: improve upsert code (#69)
Browse files Browse the repository at this point in the history
* feat(helm-toolbox): improve upsert code

* add replace wait function by OTB helm --wait flag
* remove failure on empty tiller version
* add retries

* refactor(helm-toolbox): fix retry

* refactor(helm-toolbox): improve retry code

* refactor(helm-toolbox): bring back old command exec

* fix formating
  • Loading branch information
alebabai authored Jun 26, 2018
1 parent 7b05d7c commit c0dec60
Showing 1 changed file with 42 additions and 47 deletions.
89 changes: 42 additions & 47 deletions bin/helm_toolbox.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,64 @@
export TIMEOUT=3
export STDOUT=${STDOUT:-/dev/null}

RETRIES=${RETRIES:-5}

# helper functions
function info() {
echo -e "\e[32mINFO:\e[0m $1";
function info() {
echo -e "\e[32mINFO:\e[0m $1";
}

function err() {
echo -e "\e[31mERROR:\e[0m $1" ;
exit 1;
echo -e "\e[31mERROR:\e[0m $1" ;
exit 1;
}

function check() {
command -v "$1" >/dev/null 2>&1 || err "$1 not installed!";
function check() {
command -v "$1" >/dev/null 2>&1 || err "$1 not installed!";
}

# wait for helm to become operational
wait_for_helm() {
info "Waiting for helm tiller..."
while true; do
status=$(echo $(kubectl get pods -l app=helm -l name=tiller --show-all=false -o=custom-columns=STATUS:.status.phase --no-headers=true -nkube-system))
info "Helm status: $status"
if [ "$status" = "Running" ]; then
break;
fi
sleep $TIMEOUT
done
function set_context() {
if [ -n "$KUBE_CONTEXT" ]; then
info "Using ${KUBE_CONTEXT} kube context"
kubectl config use-context ${KUBE_CONTEXT}
fi
}

function set_context() {
if [ -n "$KUBE_CONTEXT" ]; then
info "Using ${KUBE_CONTEXT} kube context"
kubectl config use-context ${KUBE_CONTEXT}
fi
function retry() {
local attempt=0
local limit=$1
local command=${@:2}
until [[ $attempt -ge $limit ]]
do
info "Perform attempt - $[$attempt+1]"
$command && break
attempt=$[$attempt+1]
sleep $TIMEOUT
done
}

function upsert() {
helm_version=$(helm version --client --short | grep -Eo "v[0-9]\.[0-9]\.[0-9]")
tiller_version=$(timeout $TIMEOUT helm version --server --short | grep -Eo "v[0-9]\.[0-9]\.[0-9]")

if [ -z "$tiller_version" ]; then
err "Unable to connect to helm server"
helm_version=$(helm version --client --short | grep -Eo "v[0-9]\.[0-9]\.[0-9]")
tiller_version=$(timeout $TIMEOUT helm version --server --short | grep -Eo "v[0-9]\.[0-9]\.[0-9]")
if [ "$helm_version" != "$tiller_version" ]; then
info "Helm version: $helm_version, differs with tiller version: ${tiller_version:-'not installed'}"
info "Upgrarding tiller to $helm_version"
helm init --upgrade --force-upgrade --wait > $STDOUT
info "Helm version"
helm version --short | sed 's/^/ - /'
else
info "Helm version: $helm_version matches tiller version: $tiller_version."
info "Initializing helm client..."
helm init --client-only > $STDOUT
fi

if [ "$helm_version" != "$tiller_version" ]; then
info "Helm version: $helm_version, differs with tiller version: $tiller_version"
info "Upgrarding tiller to $helm_version"
helm init --upgrade --force-upgrade > $STDOUT
wait_for_helm
sleep 3
info "Helm version"
helm version --short | sed 's/^/ - /'
else
info "Helm version: $helm_version matches tiller version: $tiller_version."
info "Initializing helm client..."
helm init --client-only > $STDOUT
fi
}

if [ "$1" == "upsert" ]; then
check kubectl
check helm
check timeout
set_context
upsert
check kubectl
check helm
# check timeout
set_context
retry $RETRIES upsert
else
err "Unknown commmand"
err "Unknown commmand"
fi

0 comments on commit c0dec60

Please sign in to comment.