[SEBA-230]

add chart_version_check.sh verification script
improve failure messages in helmlint.sh
improve helmrepo.sh script to update index properly

Change-Id: I69e31a8671e9962252f2ef9a6b900bfe5df282a2
diff --git a/helmrepo.sh b/helmrepo.sh
index 3c94aa0..fd59ca6 100755
--- a/helmrepo.sh
+++ b/helmrepo.sh
@@ -15,37 +15,91 @@
 # limitations under the License.
 
 # helmrepo.sh
-# creates a helm repo for publishing on guide website
+# creates or updates a helm repo for publishing on the guide website
+# Reference: https://github.com/helm/charts/blob/master/test/repo-sync.sh
 
 set -eu -o pipefail
 
+echo "# helmrepo.sh, using helm: $(helm version -c) #"
+
 # when not running under Jenkins, use current dir as workspace
 WORKSPACE=${WORKSPACE:-.}
 
-REPO_DIR="${REPO_DIR:-chart_repo}"
+# branch to compare against, defaults to master
+GERRIT_BRANCH=${GERRIT_BRANCH:-opencord/master}
+
+# directory to compare against, doesn't need to be present
+OLD_REPO_DIR="${OLD_REPO_DIR:-cord-charts-repo}"
+NEW_REPO_DIR="${NEW_REPO_DIR:-chart_repo}"
 
 GERRIT_BRANCH="${GERRIT_BRANCH:-$(git symbolic-ref --short HEAD)}"
 PUBLISH_URL="${PUBLISH_URL:-charts.opencord.org}"
 
-ORIGINAL_INDEX_YAML="${ORIGINAL_INDEX_YAML:-/var/www/charts/index.yaml}"
+# create and clean NEW_REPO_DIR
+mkdir -p "${NEW_REPO_DIR}"
+rm -f "${NEW_REPO_DIR}"/*
 
-mkdir -p "${REPO_DIR}"
+# if OLD_REPO_DIR doesn't exist, generate packages and index in NEW_REPO_DIR
+if [ ! -d "${OLD_REPO_DIR}" ]
+then
+  echo "Creating new helm repo: ${NEW_REPO_DIR}"
 
-while IFS= read -r -d '' chart
-do
-  chartdir=$(dirname "${chart}")
+  while IFS= read -r -d '' chart
+  do
+    chartdir=$(dirname "${chart#./}")
+    helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
 
-  echo "Adding ${chartdir}"
+  done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
 
-  helm package --dependency-update --destination "${REPO_DIR}" "${chartdir}"
+  helm repo index "${NEW_REPO_DIR}" --url https://"${PUBLISH_URL}"
+  echo "# helmrepo.sh Success! Generated new repo index in ${NEW_REPO_DIR}"
 
-done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
+else
+  # OLD_REPO_DIR exists, check for new charts and update only with changes
+  echo "Found existing helm repo: ${OLD_REPO_DIR}, attempting update"
 
-echo "Generating repo index"
+  # Loop and create chart packages, only if changed
+  while IFS= read -r -d '' chart
+  do
+    chartdir=$(dirname "${chart#./}")
 
-scp -o StrictHostKeyChecking=no "${PUBLISH_URL}":"${ORIGINAL_INDEX_YAML}" .
+    # See if chart version changed from previous HEAD commit
+    chart_yaml_diff=$(git diff -p HEAD^ "${chartdir}/Chart.yaml")
 
-helm repo index "${REPO_DIR}" --url https://"${PUBLISH_URL}" --merge index.yaml
+    if [ -n "$chart_yaml_diff" ]
+    then
+      # assumes that helmlint.sh and chart_version_check.sh have been run
+      # pre-merge, which ensures that all charts are valid and have their
+      # version updated in Chart.yaml
+      new_version_string=$(echo "$chart_yaml_diff" | grep -E '\+version:\s*\d+\.\d+\.\d+$')
+      echo "New version of chart ${chartdir}, creating package: ${new_version_string//+version:/}"
+      helm package --dependency-update --destination "${NEW_REPO_DIR}" "${chartdir}"
+    else
+      echo "Chart unchanged, not packaging: '${chartdir}'"
+    fi
 
-echo "Finished, chart repo generated: ${REPO_DIR}"
+  done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
 
+  # Check for collisions between old/new packages
+  while IFS= read -r -d '' package_path
+  do
+    package=$(basename "${package_path}")
+
+    if [ -f "${OLD_REPO_DIR}/${package}" ]
+    then
+      echo "# helmrepo.sh Failure! Package: ${package} with same version already exists in ${OLD_REPO_DIR}"
+      exit 1
+    fi
+  done < <(find "${NEW_REPO_DIR}" -name '*.tgz' -print0)
+
+  # Create updated index.yaml (new version created in NEW_REPO_DIR)
+  helm repo index --url "https://${PUBLISH_URL}" --merge "${OLD_REPO_DIR}/index.yaml" "${NEW_REPO_DIR}"
+
+  # move over packages and index.yaml
+  mv "${NEW_REPO_DIR}"/*.tgz "${OLD_REPO_DIR}/"
+  mv "${NEW_REPO_DIR}/index.yaml" "${OLD_REPO_DIR}/index.yaml"
+
+  echo "# helmrepo.sh Success! Updated existing repo index in ${OLD_REPO_DIR}"
+fi
+
+exit 0