#!/bin/bash ############################################################################## # # Copyright (c) 2019 AT&T Intellectual Property. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ############################################################################## # Installs well-known RIC charts then verifies specified helm chart # Requires chart tgz archives in /tmp while [ -n "$1" ]; do # while loop starts case "$1" in -d) DOCKER_REGISTRY=$2 shift ;; -p) CHART_DIRECTORY_PATH=$2 shift ;; -h) HELM_REPO=$2 shift ;; *) echo "Option $1 not recognized. Please specify the docker registry path with the -d option and specify the xApp directory path with the -p option." ;; # In case you typed a different option other than -d or -p esac shift done if [ -z "$DOCKER_REGISTRY" ]; then echo "Please specify the docker registry path with the -d option." exit 1 fi if [ -z "$CHART_DIRECTORY_PATH" ]; then echo "Please specify the xApp directory path with the -p option." exit 1 fi if [ -z "$HELM_REPO" ]; then echo "Please specify the helm repo uploading URL with the -h option." exit 1 fi echo "************************************************************" for image in $CHART_DIRECTORY_PATH/docker_images/*/*; do echo "Loading image $image" OUTPUT=$(docker load -i $image) IMAGE_PATH_ORIGINAL=$(echo $OUTPUT | grep image: | awk '{print $3}' ) IMAGENAME=$(echo $IMAGE_PATH_ORIGINAL | awk '{ n=split($0, a, "/"); print a[n] }') echo "Pushing image $DOCKER_REGISTRY/$IMAGENAME" docker tag $IMAGE_PATH_ORIGINAL $DOCKER_REGISTRY/$IMAGENAME docker push $DOCKER_REGISTRY/$IMAGENAME || { echo "Failed to push the docker image." ; exit 1; } done rm -rf $CHART_DIRECTORY_PATH/chart_packages mkdir -p $CHART_DIRECTORY_PATH/chart_packages echo "************************************************************" for chart in $CHART_DIRECTORY_PATH/helm_charts/*; do echo "Onboard Helm Charts" sed -i "s/^ repository: .*/ repository: $DOCKER_REGISTRY/" $chart/values.yaml helm package $chart -d $CHART_DIRECTORY_PATH/chart_packages done echo "************************************************************" for tarball in $CHART_DIRECTORY_PATH/chart_packages/*; do TARBALL_FILE_NAME=$(echo $tarball | awk -F '/' '{print $NF}') CHART_NAME=$(echo $TARBALL_FILE_NAME | awk -F '-' '{gsub($NF, ""); print substr($0,1,length($0)-1)}') CHART_VERSION=$(echo $TARBALL_FILE_NAME | awk -F '-' '{print substr ($NF,1,length($NF)-4)}') DELETE_MESSAGE=$(curl -X DELETE --connect-timeout 2 $HELM_REPO/api/charts/$CHART_NAME/$CHART_VERSION 2>/dev/null) LOCATE_CHART=$(curl --connect-timeout 2 $HELM_REPO/index.yaml |& grep $TARBALL_FILE_NAME) if [ ! -z "$LOCATE_CHART" ]; then echo "ERROR: Helm chart delete fail." echo $DELETE_MESSAGE exit 1 fi UPLOAD_MESSAGE=$(curl --data-binary "@$tarball" --connect-timeout 2 $HELM_REPO/api/charts 2>/dev/null) LOCATE_CHART=$(curl --connect-timeout 2 $HELM_REPO/index.yaml |& grep $TARBALL_FILE_NAME ) if [ -z "$LOCATE_CHART" ]; then echo "ERROR: Helm chart upload failed." echo $UPLOAD_MESSAGE exit 1 fi echo "Helm chart $CHART_NAME version $CHART_VERSION uploaded successfully." done