VOL-3276 - verify and add helm repos

always check if required helm repos have been
added and if not, add them

Change-Id: I87d5b5a795ae0780fb880e12ade8bde501e72c28
diff --git a/voltha b/voltha
index eb02a2a..67c539b 100755
--- a/voltha
+++ b/voltha
@@ -1542,9 +1542,6 @@
         espin - "$VERIFIED"
         wait_for_pods - "kube-system" 1 "includes" "Waiting for Tiller POD to start" "$NO_LABEL" "tiller-deploy-.*"
     fi
-    bspin - "Add Google Incubator repository to Helm"
-    (set -x; helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
 
     # HACK (sort-of) - the config for tiller is about to be patched, which will
     # cause the tiller pod to be recreated. This can sometimes cause a timing
@@ -1554,31 +1551,7 @@
     # scale up of the pod so the script controlls when it should be expecting
     # things
     (set -x; kubectl -n kube-system scale deploy tiller-deploy --replicas=0 >>"$LOG" 2>&1) >>"$LOG" 2>&1
-
-    bspin - "Add Google Stable repository to Helm"
-    (set -x; helm repo add stable https://kubernetes-charts.storage.googleapis.com >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add ONF VOLTHA repository to Helm"
-    (set -x; helm repo add onf https://charts.opencord.org >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add ONF ONOS repository to Helm"
-    (set -x; helm repo add onos https://charts.onosproject.org >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add ONF Atomix repository to Helm"
-    (set -x; helm repo add atomix https://charts.atomix.io >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add Custom BBSIM SADIS Server repository to Helm"
-    (set -x; helm repo add bbsim-sadis https://ciena.github.io/bbsim-sadis-server/charts >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add  Elastic repository to Helm"
-    (set -x; helm repo add elastic https://helm.elastic.co  >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Add Fluentd-ElasticSearch repository to Helm"
-    (set -x; helm repo add kiwigrid https://kiwigrid.github.io >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
-    bspin - "Update Helm repository cache"
-    (set -x; helm repo update >>"$LOG" 2>&1) >>"$LOG" 2>&1
-    espin - "$VERIFIED"
+    wait_for_pods - "kube-system" 0 "not" "Waiting for Tiller POD to shutdown" "$NO_LABEL" "tiller-deploy-.*"
 
     # Create and k8s service account so that Helm can create pods
     bspin - "Create Tiller ServiceAccount"
@@ -1603,13 +1576,48 @@
         espin "$VERIFIED"
     fi
 
-    if [ "$UPDATE_HELM_REPOS" == "yes" ]; then
-        bspin - "Update Helm repository cache"
-        (set -x; helm repo update >>"$LOG" 2>&1) >>"$LOG" 2>&1
-        espin - "$VERIFIED"
-    fi
 fi
 wait_for_pods - "kube-system" 1 "includes" "Waiting for Tiller POD to start" "$NO_LABEL" "tiller-deploy-.*"
+
+REPOS="onf|https://charts.opencord.org|ONF_VOLTHA \
+    stable|https://kubernetes-charts.storage.googleapis.com|Google_Stable \
+    incubator|https://kubernetes-charts-incubator.storage.googleapis.com|Google_Incubator"
+
+if is_in "$WITH_ONOS" "yes,legacy,classic"; then
+    REPOS+=" onos|https://charts.onosproject.org|ONF_ONOS \
+        atomix|https://charts.atomix.io|ONF_Atomi"
+fi
+if [ "$CONFIG_SADIS" == "external" ]; then
+    REPOS+=" bbsim-sadis|https://ciena.github.io/bbsim-sadis-server/charts|Custom_BBSIM_SADIS_Server"
+fi
+if [ "$WITH_EFK" == "yes" ]; then
+    REPOS+=" elastic|https://helm.elastic.co|Elastic \
+    kiwigrid|https://kiwigrid.github.io|Fluentd-ElasticSearch"
+fi
+REPO_UPDATE_REQ="no"
+
+bspin - "Verify Helm Repository"
+for REPO in $REPOS; do
+    REPO_NAME="$(echo "$REPO" | cut -d\| -f1)"
+    REPO_URL="$(echo "$REPO" | cut -d\| -f2)"
+    REPO_LABEL="$(echo "$REPO" | cut -d\| -f3 | sed -e 's/_/ /g')"
+
+    sspin - "Verify Helm Repository: $REPO_LABEL$CEOL"
+    if [ "$(helm repo list 2>/dev/null | grep -c "$REPO_NAME" 2>/dev/null)" -eq 0 ]; then
+        sspin - "Add Helm Repository: $REPO_LABEL$CEOL"
+        (set -x; helm repo add "$REPO_NAME" "$REPO_URL" >>"$LOG" 2>&1) >>"$LOG" 2>&1
+        REPO_UPDATE_REQ="yes"
+    fi
+done
+sspin - "Verify Helm Repositories$CEOL"
+espin - "$VERIFIED"
+
+if [ "$REPO_UPDATE_REQ" == "yes" ] || [ "$UPDATE_HELM_REPOS" == "yes" ]; then
+    bspin - "Update Helm repository cache"
+    (set -x; helm repo update >>"$LOG" 2>&1) >>"$LOG" 2>&1
+    espin - "$VERIFIED"
+fi
+
 if [ "$WITH_TIMINGS" == "yes" ]; then
     NOW="$(date +%s)"
     printtime $((NOW - STIME))