Guillaume Lambert | 85b1492 | 2021-03-12 13:53:18 +0100 | [diff] [blame] | 1 | #!/bin/sh |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 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 | |
| 8 | options() { |
| 9 | cat <<EOF |
| 10 | Usage: $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] |
| 21 | EOF |
| 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 Lambert | 785bc38 | 2021-03-09 21:41:30 +0100 | [diff] [blame] | 29 | parse_yaml () { |
guillaume.lambert | f657a81 | 2021-12-07 20:21:17 +0100 | [diff] [blame] | 30 | local prefix |
| 31 | prefix=$2 |
| 32 | local s |
| 33 | s='[[:space:]]*' |
| 34 | local w |
| 35 | w='[a-zA-Z0-9_]*' |
| 36 | local fs |
| 37 | fs=$(echo @|tr @ '\034') |
| 38 | |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 39 | sed -ne "s|^\($s\):|\1|" \ |
| 40 | -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \ |
| 41 | -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 | |
| 42 | awk -F$fs '{ |
| 43 | indent = length($1)/2; |
| 44 | vname[indent] = $2; |
| 45 | for (i in vname) {if (i > indent) {delete vname[i]}} |
| 46 | if (length($3) > 0) { |
| 47 | vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])(".")} |
| 48 | printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3); |
| 49 | } |
| 50 | }' |
| 51 | } |
| 52 | |
| 53 | #algorithmic steps |
| 54 | #start |
| 55 | #scan all values.yaml files |
| 56 | #parse yaml file into dotted format |
| 57 | #for each lines check there is image tag in line |
| 58 | #store image name and check next line for version information |
| 59 | #if in next line version is not present as a subtag then call docker pull with imageName |
| 60 | #if version is present in next line then call docker pull with imageName and imageVersion |
| 61 | #end |
| 62 | |
| 63 | |
| 64 | #start processing for finding images and version |
| 65 | IMAGE_TEXT="image" |
| 66 | IMAGE_VERSION_TEXT="Version" |
| 67 | LOCATION="." |
| 68 | VALUES_FILE_NAME="values.yaml" |
| 69 | IMAGE_REPOSITORY="nexus3.onap.org:10001" |
| 70 | USER_NAME="docker" |
| 71 | PASSWORD="docker" |
| 72 | |
| 73 | #scan for options menu |
| 74 | while getopts ":h:l:r:u:p:" PARAM; do |
| 75 | case $PARAM in |
| 76 | h) |
| 77 | options |
| 78 | exit 1 |
| 79 | ;; |
| 80 | l) |
| 81 | LOCATION=${OPTARG} |
| 82 | ;; |
| 83 | r) |
| 84 | IMAGE_REPOSITORY=${OPTARG} |
| 85 | ;; |
| 86 | u) |
| 87 | USER_NAME=${OPTARG} |
| 88 | ;; |
| 89 | p) |
| 90 | PASSWORD=${OPTARG} |
| 91 | ;; |
| 92 | ?) |
| 93 | options |
| 94 | exit |
| 95 | ;; |
| 96 | esac |
| 97 | done |
| 98 | |
| 99 | |
| 100 | #docker login to nexus repo |
| 101 | echo docker login -u $USER_NAME -p $PASSWORD $IMAGE_REPOSITORY |
| 102 | docker login -u $USER_NAME -p $PASSWORD $IMAGE_REPOSITORY |
| 103 | |
| 104 | #scan all values.yaml files recursively |
| 105 | for filename in `find $LOCATION -name $VALUES_FILE_NAME` |
| 106 | do |
| 107 | imageNameWithVersion=" "; |
| 108 | #parse yaml files |
| 109 | for line in `parse_yaml $filename` |
| 110 | do |
mayankg2703 | ce1912d | 2018-01-31 14:09:38 +0000 | [diff] [blame] | 111 | #skiping commented line |
Guillaume Lambert | 42f91fc | 2021-03-10 14:02:13 +0100 | [diff] [blame] | 112 | if echo "$line" | grep -v '^#' >/dev/null; then |
mayankg2703 | ce1912d | 2018-01-31 14:09:38 +0000 | [diff] [blame] | 113 | #find all image subtag inside converted values.yaml file's lines |
| 114 | if echo $line | grep -q $IMAGE_TEXT ; then |
| 115 | #find imageName inside line |
| 116 | imageName=`echo $line | awk -F "=" '{print $2}'` |
| 117 | #remove attional prefix and postfix |
| 118 | imageNameFinal=`echo "$imageName" | sed -e 's/^"//' -e 's/"$//' ` |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 119 | |
guillaume.lambert | 7bf306e | 2021-09-08 12:03:22 +0200 | [diff] [blame] | 120 | #check if line contain Version as a subtag in lines if yes then call docker pull with version |
mayankg2703 | ce1912d | 2018-01-31 14:09:38 +0000 | [diff] [blame] | 121 | if echo $line | grep -q $IMAGE_VERSION_TEXT ; then |
| 122 | echo docker pull "$imageNameWithVersion":"$imageNameFinal" |
| 123 | docker pull $imageNameWithVersion:$imageNameFinal & |
| 124 | imageNameWithVersion=" " |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 125 | else |
mayankg2703 | ce1912d | 2018-01-31 14:09:38 +0000 | [diff] [blame] | 126 | #check Version is not in subtag and old scanned value is present then call docker pull without version |
| 127 | if [ "$imageNameWithVersion" != " " ]; then |
| 128 | echo docker pull "$imageNameWithVersion" |
| 129 | docker pull $imageNameWithVersion & |
| 130 | imageNameWithVersion=$imageNameFinal |
| 131 | else |
| 132 | imageNameWithVersion=$imageNameFinal |
| 133 | fi |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 134 | fi |
| 135 | fi |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 136 | fi |
mayankg2703 | c3071cc | 2017-11-22 11:34:59 +0000 | [diff] [blame] | 137 | done |
| 138 | done |
| 139 | # complete processing |
| 140 | echo "finished launching pulls" |
| 141 | #MAX_WAIT_INTERVALS=300 |
| 142 | INTERVAL_COUNT=300 |
| 143 | while [ $(ps -ef | grep docker | grep pull | grep -v $0 | wc -l) -gt 0 ]; do |
| 144 | sleep 10 |
| 145 | INTERVAL_COUNT=$((INTERVAL_COUNT - 1)) |
| 146 | echo "waiting for last pull" |
| 147 | if [ "$INTERVAL_COUNT" -eq 0 ]; then |
| 148 | break |
| 149 | fi |
| 150 | done |
| 151 | |