Fix NONRTRIC installation to use own ChartMuseum
[it/dep.git] / bin / deploy-nonrtric
1 #!/bin/bash
2 ################################################################################
3 #   Copyright (c) 2023 Nordix Foundation.                                      #
4 #   Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved.  #
5 #                                                                              #
6 #   Licensed under the Apache License, Version 2.0 (the "License");            #
7 #   you may not use this file except in compliance with the License.           #
8 #   You may obtain a copy of the License at                                    #
9 #                                                                              #
10 #       http://www.apache.org/licenses/LICENSE-2.0                             #
11 #                                                                              #
12 #   Unless required by applicable law or agreed to in writing, software        #
13 #   distributed under the License is distributed on an "AS IS" BASIS,          #
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15 #   See the License for the specific language governing permissions and        #
16 #   limitations under the License.                                             #
17 ################################################################################
18
19 # This script deploys NonRtRic components automatically
20
21
22
23 if [ "$#" -eq 1 ]; then
24     OVERRIDEYAML=$1
25 else
26
27     while [ -n "$1" ]; do # while loop starts
28
29         case "$1" in
30
31         -f) OVERRIDEYAML=$2
32             shift
33             ;;
34         *) echo "Option $1 not recognized" ;; # In case you typed a different option other than a,b,c
35
36         esac
37
38         shift
39
40     done
41 fi
42
43
44 if [ -z "$OVERRIDEYAML" ];then
45     echo "****************************************************************************************************************"
46     echo "                                                     ERROR                                                      "
47     echo "****************************************************************************************************************"
48     echo "RIC deployment without deployment recipe is currently disabled. Please specify an recipe with the -f option."
49     echo "****************************************************************************************************************"
50     exit 1
51 fi
52
53
54 ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
55
56 echo "** $ROOT_DIR"
57 rm $ROOT_DIR/../nonrtric/helm/*/charts/*.tgz
58
59 #ChartMuseum configuration
60 CM_PORT="8879"
61 CM_BASE_URL="http://127.0.0.1:$CM_PORT"
62 CM_CHART_GET_URL="$CM_BASE_URL/charts"
63 CM_CHART_POST_URL="$CM_BASE_URL/charts/api/charts"
64
65 #Check for helm3
66 IS_HELM3=$(helm version -c --short|grep -e "^v3")
67
68 if ! command -v chartmuseum &> /dev/null
69 then
70   pushd /tmp
71   echo "Installing ChartMuseum binary..."
72   wget https://get.helm.sh/chartmuseum-v0.13.1-linux-amd64.tar.gz
73   tar xvfz chartmuseum-v0.13.1-linux-amd64.tar.gz
74   sudo mv /tmp/linux-amd64/chartmuseum /usr/local/bin/chartmuseum
75   popd
76 else
77   echo "ChartMuseum is already installed."
78 fi
79
80 # Package common templates and serve it using Helm local repo
81 HELM_LOCAL_REPO="./chartstorage"
82 rm $HELM_LOCAL_REPO/*
83
84 #Start Chart Museum server if there isn't one
85 CHART_MUSEUM_PID=$(lsof -i :"$CM_PORT" | grep "chartmus" | grep -v "grep" | awk '{print $2}')
86 if [ -z "$CHART_MUSEUM_PID" ]; then
87   echo "Starting ChartMuseum  on port $CM_PORT..."
88   nohup chartmuseum --port=$CM_PORT --storage="local" --context-path=/charts --storage-local-rootdir=$HELM_LOCAL_REPO >/dev/null 2>&1 &
89 else
90   echo "ChartMuseum is already running..."
91 fi
92
93 # Check if ChartMuseum  is ready to serve request
94 command="curl --silent --output /dev/null  $CM_BASE_URL"
95 for i in $(seq 1 5)
96 do $command && s=0 && break || s=$? && echo "Failed to establish a connection with the ChartMuseum server. Retrying after 5s" && sleep 5;
97 done
98
99 if [ $s -gt 0 ]
100 then
101         echo "Cmd to test ChartMuseum failed with ($s): $command"
102         exit $s
103 fi
104
105 helm repo remove local
106 helm repo add local $CM_CHART_GET_URL
107
108 echo -e "\nPackaging NONRTRIC common [nonrtric-common]"
109 NONRTRIC_COMMON_CHART_VERSION=$(cat $ROOT_DIR/../ric-common/Common-Template/helm/nonrtric-common/Chart.yaml | grep version | awk '{print $2}')
110 helm package -d /tmp $ROOT_DIR/../ric-common/Common-Template/helm/nonrtric-common
111 curl --data-binary @/tmp/nonrtric-common-$NONRTRIC_COMMON_CHART_VERSION.tgz $CM_CHART_POST_URL
112
113 COMPONENTS="controlpanel a1controller a1simulator policymanagementservice informationservice rappcatalogueservice rappcatalogueenhancedservice nonrtricgateway dmaapadapterservice dmaapmediatorservice helmmanager orufhrecovery ransliceassurance capifcore rappmanager dmeparticipant"
114 for component in $COMPONENTS; do
115     echo "Packaging NONRTRIC component [$component]"
116     helm dep up $ROOT_DIR/../nonrtric/helm/$component
117     VERSION=$(cat $ROOT_DIR/../nonrtric/helm/$component/Chart.yaml | grep version | awk '{print $2}')
118     helm package -d /tmp $ROOT_DIR/../nonrtric/helm/$component
119     curl --data-binary @/tmp/$component-$VERSION.tgz $CM_CHART_POST_URL
120 done
121
122 helm dep up $ROOT_DIR/../nonrtric/helm/nonrtric
123
124 helm repo index ${HELM_LOCAL_REPO}
125
126 # Make sure that helm local repo is added
127 helm repo add local $CM_CHART_GET_URL --force-update
128
129 echo "Finished Packaging NONRTRIC components [$COMPONENTS]"
130
131
132 COMMON_BLOCK=$(cat $OVERRIDEYAML | awk '/^common:/{getline; while ($0 ~ /^ +.*|^ *$/) {print $0; if (getline == 0) {break}}}')
133 NAMESPACE_BLOCK=$(cat $OVERRIDEYAML | awk '/^  namespace:/{getline; while ($0 ~ /^    .*|^ *$/) {print $0; if (getline == 0) {break}}}')
134 NONRTRIC_NAMESPACE=$(echo "$NAMESPACE_BLOCK" | awk '/^ *nonrtric:/{print $2}')
135 RELEASE_PREFIX=$(echo "$COMMON_BLOCK" | awk '/^ *releasePrefix:/{print $2}')
136 INSTALL_KONG=$(cat $OVERRIDEYAML | awk '/^  installKong:/{print $2}')
137 INSTALL_RANPM=$(cat $OVERRIDEYAML | awk '/^  installRanpm:/{print $2}')
138
139 if ! kubectl get ns ${NONRTRIC_NAMESPACE:-nonrtric}> /dev/null 2>&1; then
140     kubectl create ns ${NONRTRIC_NAMESPACE:-nonrtric}
141 fi
142 if ! kubectl get ns onap > /dev/null 2>&1; then
143     kubectl create ns onap
144 fi
145
146 echo "Install Kong- $INSTALL_KONG"
147
148 if [ "$INSTALL_KONG" = true ];then
149    echo "Installing Kong"
150    helm repo add kong https://charts.konghq.com --force-update
151    helm repo update
152    helm install kong-nonrtric --namespace ${NONRTRIC_NAMESPACE:-nonrtric} kong/kong --set ingressController.installCRDs=false --set admin.enabled=true
153 fi
154
155 kubectl create configmap -n ${NONRTRIC_NAMESPACE:-nonrtric} nonrtric-recipe --from-file=recipe=$OVERRIDEYAML
156
157 echo "Deploying NONRTRIC"
158
159 echo "Install Ranpm- $INSTALL_RANPM"
160
161 if [ "$INSTALL_RANPM" = true ];then
162    echo "Running install-ranpm.sh"
163    chmod +x ${ROOT_DIR}/../ranpm/install/install-ranpm.sh
164    ${ROOT_DIR}/../ranpm/install/install-ranpm.sh
165    echo "install-ranpm.sh completed"
166 fi
167
168 HELM_NAME_OPT=""
169 if [ -z $IS_HELM3 ];then
170    HELM_NAME_OPT="--name"
171 fi
172
173 echo "helm install -f $OVERRIDEYAML --namespace ${NONRTRIC_NAMESPACE:-nonrtric} ${HELM_NAME_OPT} ${RELEASE_PREFIX} $ROOT_DIR/../nonrtric/helm/nonrtric"
174 helm install -f $OVERRIDEYAML --namespace "${NONRTRIC_NAMESPACE:-nonrtric}" ${HELM_NAME_OPT} "${RELEASE_PREFIX}" "$ROOT_DIR/../nonrtric/helm/nonrtric"