Add warning message when the default storage class is not set
[it/dep.git] / bin / deploy-nonrtric
index 887c04e..6ce07a5 100755 (executable)
@@ -1,6 +1,7 @@
 #!/bin/bash
 ################################################################################
-#   Copyright (c) 2020 Nordix Foundation.                                      #
+#   Copyright (c) 2023 Nordix Foundation.                                      #
+#   Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.  #
 #                                                                              #
 #   Licensed under the Apache License, Version 2.0 (the "License");            #
 #   you may not use this file except in compliance with the License.           #
@@ -29,7 +30,7 @@ else
 
         -f) OVERRIDEYAML=$2
             shift
-            ;; 
+            ;;
         *) echo "Option $1 not recognized" ;; # In case you typed a different option other than a,b,c
 
         esac
@@ -49,80 +50,160 @@ if [ -z "$OVERRIDEYAML" ];then
     exit 1
 fi
 
+if ! command -v yq > /dev/null 2>&1; then
+    echo "yq is not installed. Installing yq..."
+    sudo snap install yq --channel=v4/stable
+fi
 
-ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
-
-echo "** $ROOT_DIR"
-rm $ROOT_DIR/../nonrtric/helm/*/charts/*.tgz
-
-
-#Check for helm3
-IS_HELM3=$(helm version -c --short|grep -e "^v3")
-
-if [ $IS_HELM3 ]
-then
-  # Check for servcm plugin
-  helm plugin list | grep -q "^servecm"
-  if [ $? -eq "1" ]
-  then
-    helm plugin install https://github.com/jdolitsky/helm-servecm
-  fi
+INSTALL_KONG=$(cat $OVERRIDEYAML | yq e '.nonrtric.installKong' -)
+if [ $? -ne 0 ] || [ -z "$INSTALL_KONG"  ]; then
+    echo "Error: failed to parse installKong from YAML with yq. Aborting install."
+    exit 1
 fi
 
-# Start Helm local repo if there isn't one
-HELM_REPO_PID=$(ps -x | grep  "helm serve" | grep -v "grep" | awk '{print $1}')
-if [ -z "$HELM_REPO_PID" ]; then
-    if [ -z $IS_HELM3 ]
-    then
-      nohup helm serve >& /dev/null &
-    else
-      echo EUID:$EUID
-      if [ $EUID -ne "0" ]
-         then
-           echo "Error: Please run the command with sudo as helm servecm needs to copy a file in location needing privilege"
-           exit;
-         fi
-      eval $(helm env |grep HELM_REPOSITORY_CACHE)
-      echo yes > /tmp/ric-yes
-      nohup sudo sh -c "helm servecm --port=8879 --context-path=/charts  --storage local --storage-local-rootdir $HELM_REPOSITORY_CACHE/local/  < /tmp/ric-yes " &
-    fi
+INSTALL_CAPIF=$(cat $OVERRIDEYAML | yq e '.nonrtric.installCapifcore' -)
+if [ $? -ne 0 ] || [ -z "$INSTALL_CAPIF"  ]; then
+    echo "Error: failed to parse installCapifcore from YAML with yq. Aborting install."
+    exit 1
 fi
 
+INSTALL_SERVICEMANAGER=$(cat $OVERRIDEYAML | yq e '.nonrtric.installServicemanager' -)
+if [ $? -ne 0 ] || [ -z "$INSTALL_SERVICEMANAGER"  ]; then
+    echo "Error: failed to parse installServicemanager from YAML with yq. Aborting install."
+    exit 1
+fi
 
-# Package common templates and serve it using Helm local repo
-HELM_LOCAL_REPO=""
-if [ $IS_HELM3 ]
-then
-  eval $(helm env |grep HELM_REPOSITORY_CACHE)
-  HELM_LOCAL_REPO="${HELM_REPOSITORY_CACHE}/local/"
-else
-  HELM_HOME=$(helm home)
-  HELM_LOCAL_REPO="${HELM_HOME}/repository/local/"
+if [ "$INSTALL_SERVICEMANAGER" == "true" ]; then
+    if [ "$INSTALL_KONG" == "false" ]; then
+        echo "Error: INSTALL_KONG must be true if INSTALL_SERVICEMANAGER is true. Aborting install."
+        exit 1
+    fi
+    if [ "$INSTALL_CAPIF" == "false" ]; then
+        echo "Error: INSTALL_CAPIF must be true if INSTALL_SERVICEMANAGER is true. Aborting install."
+        exit 1
+    fi
 fi
 
+ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
 
-rm $HELM_LOCAL_REPO/*
+echo "** $ROOT_DIR"
+rm $ROOT_DIR/../nonrtric/helm/*/charts/*.tgz
 
-helm repo remove local
-$ROOT_DIR/prepare-common-templates
+source $ROOT_DIR/build-nonrtric-base
+
+charts_already_exists=()
+
+COMPONENTS="
+a1controller \
+a1simulator \
+capifcore \
+controlpanel \
+dmaapadapterservice \
+dmaapmediatorservice \
+dmeparticipant \
+helmmanager \
+informationservice \
+nonrtricgateway \
+orufhrecovery \
+policymanagementservice \
+ransliceassurance \
+rappcatalogueenhancedservice \
+rappcatalogueservice \
+rappmanager \
+servicemanager \
+"
 
-COMPONENTS="controlpanel a1controller a1simulator policymanagementservice enrichmentservice rappcatalogueservice nonrtricgateway"
 for component in $COMPONENTS; do
     echo "Packaging NONRTRIC component [$component]"
     helm dep up $ROOT_DIR/../nonrtric/helm/$component
     VERSION=$(cat $ROOT_DIR/../nonrtric/helm/$component/Chart.yaml | grep version | awk '{print $2}')
     helm package -d /tmp $ROOT_DIR/../nonrtric/helm/$component
-    cp /tmp/$component-$VERSION.tgz ${HELM_LOCAL_REPO}
+    resp_code=$(curl -s -o /dev/null -w "%{http_code}" --data-binary @/tmp/$component-$VERSION.tgz $CM_CHART_POST_URL)
+    echo "Chart upload status of $component is $resp_code"
+    if [ "$resp_code" -eq 409 ]; then
+      charts_already_exists+=("$component")
+    fi
 done
 
+if [ ${#charts_already_exists[@]} -gt 0 ]; then
+  echo "----------------------------------- WARNING!!! -------------------------------------------"
+  echo "The following charts already exists in ChartMuseum '${charts_already_exists[@]}'."
+  echo "The current build of the charts hasn't been updated because the charts already exist."
+  echo "It is recommended to delete the charts from ChartMuseum before the build."
+  echo "------------------------------------------------------------------------------------------"
+fi
+
 helm dep up $ROOT_DIR/../nonrtric/helm/nonrtric
 
 helm repo index ${HELM_LOCAL_REPO}
 
 # Make sure that helm local repo is added
-helm repo add local http://127.0.0.1:8879/charts --force-update
+helm repo add local $CM_CHART_GET_URL --force-update
 
 echo "Finished Packaging NONRTRIC components [$COMPONENTS]"
 
 
-$ROOT_DIR/../nonrtric/bin/install -f $OVERRIDEYAML
+COMMON_BLOCK=$(cat $OVERRIDEYAML | awk '/^common:/{getline; while ($0 ~ /^ +.*|^ *$/) {print $0; if (getline == 0) {break}}}')
+NAMESPACE_BLOCK=$(cat $OVERRIDEYAML | awk '/^  namespace:/{getline; while ($0 ~ /^    .*|^ *$/) {print $0; if (getline == 0) {break}}}')
+NONRTRIC_NAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *nonrtric:/{print $2}')
+RELEASE_PREFIX=$(echo "$COMMON_BLOCK" | awk '/^ *releasePrefix:/{print $2}')
+INSTALL_RANPM=$(cat $OVERRIDEYAML | awk '/^  installRanpm:/{print $2}')
+
+if ! kubectl get ns ${NONRTRIC_NAMESPACE:-nonrtric}> /dev/null 2>&1; then
+    kubectl create ns ${NONRTRIC_NAMESPACE:-nonrtric}
+fi
+
+if ! kubectl get ns onap > /dev/null 2>&1; then
+    kubectl create ns onap
+fi
+
+HELM_NAME_OPT=""
+if [ -z $IS_HELM3 ];then
+   HELM_NAME_OPT="--name"
+fi
+
+echo "Install Kong- $INSTALL_KONG"
+
+if [ "$INSTALL_KONG" = true ];then
+  DEFAULT_SC=$(kubectl get sc -o jsonpath='{.items[?(@.metadata.annotations.storageclass\.kubernetes\.io/is-default-class=="true")].metadata.name}')
+  if [ -z "$DEFAULT_SC" ];then
+    echo "----------------------------------- WARNING!!! -------------------------------------------------------"
+    echo "Default storage class is not set."
+    echo "Kong PV and PVC creation may fail if it unable to create/use a storage class."
+    echo "Storage class for the kong installation can be specified in nonrtric/helm/kongstorage/kongvalues.yaml"
+    echo "------------------------------------------------------------------------------------------------------"
+  else
+    echo "Default storage class is set to $DEFAULT_SC"
+  fi
+
+  echo "Install kongstorage through helm"
+  helm install kongstorage -n "${NONRTRIC_NAMESPACE:-nonrtric}" ${HELM_NAME_OPT}  "$ROOT_DIR/../nonrtric/helm/kongstorage"
+
+  echo "Installing Kong"
+  helm repo add kong https://charts.konghq.com --force-update
+  helm repo update
+  helm install oran-nonrtric kong/kong -n ${NONRTRIC_NAMESPACE:-nonrtric} -f "$ROOT_DIR/../nonrtric/helm/kongstorage/kongvalues.yaml"
+fi
+
+kubectl create configmap -n ${NONRTRIC_NAMESPACE:-nonrtric} nonrtric-recipe --from-file=recipe=$OVERRIDEYAML
+
+echo "Deploying NONRTRIC"
+
+echo "Install Ranpm- $INSTALL_RANPM"
+
+if [ "$INSTALL_RANPM" = true ];then
+   echo "Running install-ranpm.sh"
+   chmod +x ${ROOT_DIR}/../ranpm/install/install-ranpm.sh
+   ${ROOT_DIR}/../ranpm/install/install-ranpm.sh
+   echo "install-ranpm.sh completed"
+fi
+
+echo "helm install -f $OVERRIDEYAML --namespace ${NONRTRIC_NAMESPACE:-nonrtric} ${HELM_NAME_OPT} ${RELEASE_PREFIX} $ROOT_DIR/../nonrtric/helm/nonrtric"
+helm install -f $OVERRIDEYAML -n "${NONRTRIC_NAMESPACE:-nonrtric}" ${HELM_NAME_OPT} "${RELEASE_PREFIX}" "$ROOT_DIR/../nonrtric/helm/nonrtric"
+
+if [ "$INSTALL_SERVICEMANAGER" == "true" ]; then
+    pushd "$ROOT_DIR/../nonrtric/servicemanager-preload"
+    # Send stderr to null to turn off chatty logging
+    ./servicemanager-preload.sh config-nonrtric.yaml 2>/dev/null
+    popd
+fi