11#! /bin/bash
22
3+ helm_update () {
4+ if [ " $HELM " == " helm_v3" ]; then
5+ LINE=" $( $HELM ls --all -f " ^$NAME \$ " --output json | jq -r " $JQ_CMD " | tr ' [:upper:]' ' [:lower:]' ) "
6+ else
7+ LINE=" $( $HELM ls --all " ^$NAME \$ " --output json | jq -r " $JQ_CMD " | tr ' [:upper:]' ' [:lower:]' ) "
8+ fi
9+ INSTALLED_VERSION=$( echo $LINE | cut -f1 -d,)
10+ STATUS=$( echo $LINE | cut -f2 -d,)
11+
12+ if [ -e /config/values.yaml ]; then
13+ VALUES=" --values /config/values.yaml"
14+ fi
15+
16+ if [ " $1 " = " delete" ]; then
17+ if [ -z " $INSTALLED_VERSION " ]; then
18+ exit
19+ fi
20+ if [ " $HELM " == " helm_v3" ]; then
21+ $HELM uninstall $NAME || true
22+ else
23+ $HELM delete $NAME || true
24+ $HELM " $@ " --purge $NAME
25+ fi
26+ exit
27+ fi
28+
29+ if [ -z " $INSTALLED_VERSION " ] && [ -z " $STATUS " ]; then
30+ $HELM " $@ " $NAME $CHART $VALUES
31+ exit
32+ fi
33+ if [ -z " $VERSION " ] || [ " $INSTALLED_VERSION " = " $VERSION " ]; then
34+ if [ " $STATUS " = " deployed" ]; then
35+ echo Already installed $NAME , upgrading
36+ $HELM upgrade $NAME $CHART $VALUES
37+ exit
38+ fi
39+ fi
40+
41+ if [ " $STATUS " = " failed" ] || [ " $STATUS " = " deleted" ]; then
42+ if [ " $HELM " == " helm_v3" ]; then
43+ $HELM uninstall $NAME
44+ else
45+ $HELM " $@ " --purge $NAME
46+ fi
47+ echo Deleted
48+ $HELM " $@ " $NAME_ARG $NAME $CHART $VALUES
49+ exit
50+ fi
51+
52+ $HELM " $@ " $NAME_ARG $NAME $CHART $VALUES
53+ }
54+
55+ helm_repo_init () {
56+ # if the chart is url skip repo update
57+ if grep -q -e " https\?://" <<< " $CHART" ; then
58+ echo " chart path is a url, skipping repo update"
59+ return
60+ fi
61+
62+ if [ " $HELM " == " helm_v3" ]; then
63+ if [[ $CHART == stable/* ]]; then
64+ $HELM repo add stable https://kubernetes-charts.storage.googleapis.com/
65+ $HELM repo update
66+ fi
67+ else
68+ $HELM repo update --strict || $HELM repo remove stable
69+ fi
70+
71+ if [ -n " $REPO " ]; then
72+ $HELM repo add ${NAME%%/* } $REPO
73+ $HELM repo update
74+ fi
75+ }
76+
77+ HELM=" helm_v3"
78+ NAME_ARG=" "
79+ JQ_CMD=' "\(.[0].app_version),\(.[0].status)"'
80+
381set -e -v
482eval set -- $( sed -e " s/%{KUBERNETES_API}%/${KUBERNETES_SERVICE_HOST} :${KUBERNETES_SERVICE_PORT} /g" <<< " ${@@Q}" )
583set +v -x
@@ -10,53 +88,15 @@ update-ca-certificates
1088tiller --listen=127.0.0.1:44134 --storage=secret &
1189export HELM_HOST=127.0.0.1:44134
1290
13- helm init --skip-refresh --client-only
14- helm repo update --strict || helm repo remove stable
15- if [ -n " $REPO " ]; then
16- helm repo add ${NAME%%/* } $REPO
17- fi
18- helm repo update
19-
20-
21- JQ_CMD=' "\(.Releases[0].AppVersion),\(.Releases[0].Status)"'
22- LINE=" $( helm ls --all " ^$NAME \$ " --output json | jq -r " $JQ_CMD " ) "
23- INSTALLED_VERSION=$( echo $LINE | cut -f1 -d,)
24- STATUS=$( echo $LINE | cut -f2 -d,)
25-
26- if [ -e /config/values.yaml ]; then
27- VALUES=" --values /config/values.yaml"
28- fi
29-
30- if [ " $1 " = " delete" ]; then
31- if [ -z " $INSTALLED_VERSION " ]; then
32- exit
33- fi
34- helm delete $NAME || true
35- helm " $@ "
36- exit
91+ helm_v2 init --skip-refresh --client-only
92+ EXIST=$( helm_v2 ls --all " ^$NAME \$ " --output json | jq -r ' .Releases | length' )
93+ if [ " $EXIST " == " 1" ]; then
94+ HELM=" helm_v2"
95+ NAME_ARG=" --name"
96+ JQ_CMD=' "\(.Releases[0].AppVersion),\(.Releases[0].Status)"'
97+ LINE=" $( $HELM ls --all " ^$NAME \$ " --output json | jq -r " $JQ_CMD " ) "
3798fi
3899
39- if [ -z " $INSTALLED_VERSION " ] && [ -z " $STATUS " ]; then
40- helm " $@ " $VALUES
41- exit
42- fi
43- if [ -z " $VERSION " ] || [ " $INSTALLED_VERSION " = " $VERSION " ]; then
44- if [ " $STATUS " = " DEPLOYED" ]; then
45- echo Already installed $NAME , upgrading
46- # We assume the args are always install --name foo CHART
47- shift 2
48- helm upgrade " $@ " $VALUES
49- exit
50- fi
51- fi
52-
53- if [ " $STATUS " = " FAILED" ] || [ " $STATUS " = " DELETED" ]; then
54- helm delete --purge $NAME
55- echo Deleted
56- helm " $@ " $VALUES
57- exit
58- fi
100+ helm_repo_init
101+ helm_update $@
59102
60- # We assume the args are always install --name foo CHART
61- shift 2
62- helm upgrade " $@ " $VALUES
0 commit comments