Initial commit of code
[it/dev.git] / bin / prepull-xapp-charts
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     -f) XAPPLISTFILE=$2
29         shift
30         ;; 
31
32     -d) CHART_DIRECTORY_PATH=$2
33         shift
34         ;;
35
36
37     *) echo "Option $1 not recognized. Please use -f to specify the recipe path." ;; # In case you typed a different option other than a,b,c
38
39     esac
40
41     shift
42
43 done
44
45 if [ -z "$XAPPLISTFILE" ]; then
46    echo "xApp list file is missing. Please use -f to specify the path."
47    exit 1
48 fi
49
50 if [ -z "$CHART_DIRECTORY_PATH" ]; then
51     CHART_DIRECTORY_PATH=/tmp/xapp_charts
52 fi
53
54
55 rm -rf $CHART_DIRECTORY_PATH
56 mkdir -p $CHART_DIRECTORY_PATH/helm_charts
57 mkdir -p $CHART_DIRECTORY_PATH/docker_images
58
59
60 while IFS= read -r chart
61 do
62     CHARTNAME=$(echo $chart | awk '{ n=split($0, a, "/"); split(a[n],b,":"); print b[1] }')
63     CHARTVERSION=$(echo $chart | awk '{ n=split($0, a, "/"); split(a[n],b,":"); print b[2] }')
64     HELM_REPO=$(echo $chart | awk -F'/' '{gsub($NF,""); print substr($0,1,length($0)-1)}')
65      
66     echo "Fetching helm charts $CHARTNAME version $CHARTVERSION from repo $HELM_REPO"
67     helm repo add temp $HELM_REPO > /dev/null
68     helm fetch temp/$CHARTNAME --version $CHARTVERSION -d $CHART_DIRECTORY_PATH/helm_charts --untar
69     helm repo remove temp > /dev/null
70
71
72 done < "$XAPPLISTFILE"
73
74 echo "************************************************************"
75 for chart in $CHART_DIRECTORY_PATH/helm_charts/*; do
76
77
78     CHART_NAME=$(echo $chart | awk -F '/' '{print $NF}')
79     mkdir -p $CHART_DIRECTORY_PATH/docker_images/$CHART_NAME
80
81     IMAGE_ARRAY=$(helm template $chart | grep "image:" | awk '{ gsub(/.*image: /, "", $0); gsub(/"/, "", $0); print $0}' )
82
83     while read -r image; do
84
85         IMAGENAME=$(echo $image | awk '{ n=split($0, a, "/"); print a[n] }')
86         echo "Pulling image $image"
87         RESULT=$(docker pull $image |& grep "no basic auth credentials" )
88         if [ ! -z "$RESULT" ]; then
89             echo "You are not logined to docker registry. Please login by running \"docker login DOCKER_REGISTRY\""
90             exit 1
91         fi
92     
93         echo "Saving image $image"
94         docker save $image -o $CHART_DIRECTORY_PATH/docker_images/$CHART_NAME/$IMAGENAME
95
96
97
98
99     done <<< "$IMAGE_ARRAY"
100
101
102 done
103
104
105
106
107 echo "************************************************************"
108 echo "xApp helm charts are downloaded to: $CHART_DIRECTORY_PATH"