Merge "Add scripts to prepull and preload the docker images"
[it/dep.git] / bin / prepull-images
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) OVERRIDEYAML=$2
29         shift
30         ;; 
31
32
33     -d) IMAGE_DIRECTORY_PATH=$2
34         shift
35         ;;
36
37
38     *) 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
39
40     esac
41
42     shift
43
44 done
45
46 if [ -z $OVERRIDEYAML ]; then
47    echo "Deploy recipe is missing. Please use -f to specify the recipe path."
48    exit 1
49 fi
50
51 if [ -z $IMAGE_DIRECTORY_PATH ]; then
52     IMAGE_DIRECTORY_PATH=/tmp/ric_image
53     rm -rf $IMAGE_DIRECTORY_PATH
54     mkdir -p $IMAGE_DIRECTORY_PATH
55 fi
56
57
58 ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
59 TEMP_DIR=/tmp
60 rm -rf $TEMP_DIR/imagelist
61 touch $TEMP_DIR/imagelist
62 CHART_ARRAY=()
63 while IFS= read -r -d $'\0'; do
64     CHART_ARRAY+=("$REPLY")
65 done < <(find $ROOT_DIR/../ -name Chart.yaml -printf '%h\0')
66
67 rm -fr $TEMP_DIR/ric-common*.tgz
68
69 helm package -d $TEMP_DIR $ROOT_DIR/../ric-common/Common-Template/helm/ric-common
70 echo "***************************************"
71
72
73
74 for dir in "${CHART_ARRAY[@]}"
75 do
76   
77     #rm -rf $dir/charts
78     rm -rf $dir/tmpcharts
79     echo "Analyzing Chart $(echo $dir | awk '{n=split($0, a, "/"); print a[n]}')"    
80     echo $dir 
81     mkdir -p $dir/charts
82     cp $TEMP_DIR/ric-common*.tgz $dir/charts/
83
84     helm dep up $dir > /dev/null 2>&1
85     
86     
87     if [ -z $OVERRIDEYAML ]; then
88         IMAGE_ARRAY=$(helm template $dir | grep "image:" | awk '{ gsub(/.*image: /, "", $0); gsub(/"/, "", $0); print $0}')
89     else
90         IMAGE_ARRAY=$(helm template -f $OVERRIDEYAML $dir | grep "image:" | awk '{ gsub(/.*image: /, "", $0); gsub(/"/, "", $0); print $0}' )
91     fi
92
93     for f in $IMAGE_ARRAY; do
94         if [ ! -z $f ]; then
95             FOUND=$(grep $f $TEMP_DIR/imagelist)
96             if [ -z $FOUND ]; then
97                 echo "Found unique docker image $f."
98                 echo $f >> $TEMP_DIR/imagelist
99             fi
100         fi
101     done
102
103     echo "***************************************"
104 done
105
106
107 while IFS= read -r image
108 do
109     IMAGENAME=$(echo $image | awk '{ n=split($0, a, "/"); print a[n] }')
110
111     echo "Pulling image $image"
112     RESULT=$(docker pull $image |& grep "no basic auth credentials" )
113     if [ ! -z "$RESULT" ]; then
114         echo "You are not logined to docker registry. Please login by running \"docker login DOCKER_REGISTRY\""
115         exit 1
116     fi
117
118     echo "Saving image $image"
119     docker save $image -o $IMAGE_DIRECTORY_PATH/$IMAGENAME
120
121     echo "************************************************************"
122
123
124 done < "$TEMP_DIR/imagelist"
125
126
127