blob: efd0bd14d306a92ec1cae39b6d3de252d292063d [file] [log] [blame]
mayankg2703c3071cc2017-11-22 11:34:59 +00001#!/bin/bash
2
3#function to provide help
4#desc: this function provide help menu
5#argument: -h for help, -p for path, -r for repository
6#calling syntax: options
7
8options() {
9 cat <<EOF
10Usage: $0 [PARAMs]
11-h : help
12-l (Location) : path for searching values.yaml
13 [in case no path is provided then is will scan current directories for values.yml]
14-r (Repository) : name of image repository
15 [format [repository name/url]:(port)]
16 [in case no repository is provided then defualt image repository will be nexus3.onap.org:10001]
17-u (User) : user name for login
18 [in case no user name is provided then default user will be docker]
19-p (Password) : password for login
20 [in case no password is provided then default user will be docker]
21EOF
22}
23
24#function to parse yaml file
25#desc: this function convert yaml file to dotted notion
26#argument: yaml file
27#calling syntax: parse_yaml <yaml_file_name>
28
Guillaume Lambert785bc382021-03-09 21:41:30 +010029parse_yaml () {
mayankg2703c3071cc2017-11-22 11:34:59 +000030 local prefix=$2
31 local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
32 sed -ne "s|^\($s\):|\1|" \
33 -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
34 -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
35 awk -F$fs '{
36 indent = length($1)/2;
37 vname[indent] = $2;
38 for (i in vname) {if (i > indent) {delete vname[i]}}
39 if (length($3) > 0) {
40 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])(".")}
41 printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
42 }
43 }'
44}
45
46#algorithmic steps
47#start
48#scan all values.yaml files
49#parse yaml file into dotted format
50#for each lines check there is image tag in line
51#store image name and check next line for version information
52#if in next line version is not present as a subtag then call docker pull with imageName
53#if version is present in next line then call docker pull with imageName and imageVersion
54#end
55
56
57#start processing for finding images and version
58IMAGE_TEXT="image"
59IMAGE_VERSION_TEXT="Version"
60LOCATION="."
61VALUES_FILE_NAME="values.yaml"
62IMAGE_REPOSITORY="nexus3.onap.org:10001"
63USER_NAME="docker"
64PASSWORD="docker"
65
66#scan for options menu
67while getopts ":h:l:r:u:p:" PARAM; do
68 case $PARAM in
69 h)
70 options
71 exit 1
72 ;;
73 l)
74 LOCATION=${OPTARG}
75 ;;
76 r)
77 IMAGE_REPOSITORY=${OPTARG}
78 ;;
79 u)
80 USER_NAME=${OPTARG}
81 ;;
82 p)
83 PASSWORD=${OPTARG}
84 ;;
85 ?)
86 options
87 exit
88 ;;
89 esac
90done
91
92
93#docker login to nexus repo
94echo docker login -u $USER_NAME -p $PASSWORD $IMAGE_REPOSITORY
95docker login -u $USER_NAME -p $PASSWORD $IMAGE_REPOSITORY
96
97#scan all values.yaml files recursively
98for filename in `find $LOCATION -name $VALUES_FILE_NAME`
99do
100 imageNameWithVersion=" ";
101 #parse yaml files
102 for line in `parse_yaml $filename`
103 do
mayankg2703ce1912d2018-01-31 14:09:38 +0000104 #skiping commented line
Guillaume Lambert42f91fc2021-03-10 14:02:13 +0100105 if echo "$line" | grep -v '^#' >/dev/null; then
mayankg2703ce1912d2018-01-31 14:09:38 +0000106 #find all image subtag inside converted values.yaml file's lines
107 if echo $line | grep -q $IMAGE_TEXT ; then
108 #find imageName inside line
109 imageName=`echo $line | awk -F "=" '{print $2}'`
110 #remove attional prefix and postfix
111 imageNameFinal=`echo "$imageName" | sed -e 's/^"//' -e 's/"$//' `
mayankg2703c3071cc2017-11-22 11:34:59 +0000112
mayankg2703ce1912d2018-01-31 14:09:38 +0000113 #check if line contain Version as a subtag in lines if yes then call docker pull with version
114 if echo $line | grep -q $IMAGE_VERSION_TEXT ; then
115 echo docker pull "$imageNameWithVersion":"$imageNameFinal"
116 docker pull $imageNameWithVersion:$imageNameFinal &
117 imageNameWithVersion=" "
mayankg2703c3071cc2017-11-22 11:34:59 +0000118 else
mayankg2703ce1912d2018-01-31 14:09:38 +0000119 #check Version is not in subtag and old scanned value is present then call docker pull without version
120 if [ "$imageNameWithVersion" != " " ]; then
121 echo docker pull "$imageNameWithVersion"
122 docker pull $imageNameWithVersion &
123 imageNameWithVersion=$imageNameFinal
124 else
125 imageNameWithVersion=$imageNameFinal
126 fi
mayankg2703c3071cc2017-11-22 11:34:59 +0000127 fi
128 fi
mayankg2703c3071cc2017-11-22 11:34:59 +0000129 fi
mayankg2703c3071cc2017-11-22 11:34:59 +0000130 done
131done
132# complete processing
133echo "finished launching pulls"
134#MAX_WAIT_INTERVALS=300
135INTERVAL_COUNT=300
136while [ $(ps -ef | grep docker | grep pull | grep -v $0 | wc -l) -gt 0 ]; do
137 sleep 10
138 INTERVAL_COUNT=$((INTERVAL_COUNT - 1))
139 echo "waiting for last pull"
140 if [ "$INTERVAL_COUNT" -eq 0 ]; then
141 break
142 fi
143done
144