b38699f84d33c2931a8f7f7759cc2bca3b3421d4
[it/dev.git] / bin / onboard-xapps
1 #!/bin/bash
2 ##############################################################################
3 #
4 #   Copyright (c) 2019 AT&T Intellectual Property.
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
20 # Installs well-known RIC charts then verifies specified helm chart
21 # Requires chart tgz archives in /tmp
22
23
24 while [ -n "$1" ]; do # while loop starts
25
26     case "$1" in
27
28     -d) DOCKER_REGISTRY=$2
29         shift
30         ;;
31
32     -p) CHART_DIRECTORY_PATH=$2
33         shift
34         ;;
35
36     -h) HELM_REPO=$2
37         shift
38         ;;
39
40     *) 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."
41         ;; # In case you typed a different option other than -d or -p
42
43     esac
44
45     shift
46
47 done
48
49 if [ -z "$DOCKER_REGISTRY" ]; then
50    echo "Please specify the docker registry path with the -d option."
51    exit 1
52 fi
53
54 if [ -z "$CHART_DIRECTORY_PATH" ]; then
55    echo "Please specify the xApp directory path with the -p option."
56    exit 1
57 fi
58
59 if [ -z "$HELM_REPO" ]; then
60    echo "Please specify the helm repo uploading URL with the -h option."
61    exit 1
62 fi
63
64
65 echo "************************************************************"
66
67 for image in $CHART_DIRECTORY_PATH/docker_images/*/*; do
68         echo "Loading image $image"
69         OUTPUT=$(docker load -i $image)
70         IMAGE_PATH_ORIGINAL=$(echo $OUTPUT | grep image: | awk '{print $3}' )
71         IMAGENAME=$(echo $IMAGE_PATH_ORIGINAL | awk '{ n=split($0, a, "/"); print a[n] }')
72         echo "Pushing image $DOCKER_REGISTRY/$IMAGENAME"
73         docker tag $IMAGE_PATH_ORIGINAL $DOCKER_REGISTRY/$IMAGENAME
74         docker push $DOCKER_REGISTRY/$IMAGENAME || { echo "Failed to push the docker image." ; exit 1; }
75
76 done
77
78 rm -rf $CHART_DIRECTORY_PATH/chart_packages
79 mkdir -p $CHART_DIRECTORY_PATH/chart_packages
80
81 echo "************************************************************"
82 for chart in $CHART_DIRECTORY_PATH/helm_charts/*; do
83         echo "Onboard Helm Charts"
84
85         sed -i "s/^    repository: .*/    repository: $DOCKER_REGISTRY/" $chart/values.yaml
86         helm package $chart -d $CHART_DIRECTORY_PATH/chart_packages
87
88 done
89
90
91
92 echo "************************************************************"
93 for tarball in $CHART_DIRECTORY_PATH/chart_packages/*; do
94         TARBALL_FILE_NAME=$(echo $tarball | awk -F '/' '{print $NF}')
95         CHART_NAME=$(echo $TARBALL_FILE_NAME |  awk -F '-' '{gsub($NF, ""); print substr($0,1,length($0)-1)}')
96         CHART_VERSION=$(echo $TARBALL_FILE_NAME |  awk -F '-' '{print substr ($NF,1,length($NF)-4)}')
97
98         DELETE_MESSAGE=$(curl -X DELETE --connect-timeout 2 $HELM_REPO/api/charts/$CHART_NAME/$CHART_VERSION 2>/dev/null)
99         LOCATE_CHART=$(curl --connect-timeout 2 $HELM_REPO/index.yaml |& grep $TARBALL_FILE_NAME)
100
101         if [ ! -z "$LOCATE_CHART" ]; then
102             echo "ERROR: Helm chart delete fail."
103             echo $DELETE_MESSAGE
104             exit 1
105         fi
106
107         UPLOAD_MESSAGE=$(curl --data-binary "@$tarball" --connect-timeout 2 $HELM_REPO/api/charts  2>/dev/null)
108         LOCATE_CHART=$(curl --connect-timeout 2 $HELM_REPO/index.yaml |& grep $TARBALL_FILE_NAME )
109
110         if [ -z "$LOCATE_CHART" ]; then
111             echo "ERROR: Helm chart upload failed."
112             echo $UPLOAD_MESSAGE
113             exit 1
114         fi
115
116         echo "Helm chart $CHART_NAME version $CHART_VERSION uploaded successfully."
117
118 done