Improve anti-staging checker.
[oom.git] / kubernetes / contrib / tools / check-for-staging-images.sh
1 #!/bin/bash
2
3 # Copyright © 2020 Samsung Electronics
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 BASE_URL="https://nexus3.onap.org/repository/docker.release"
18
19 if [ "$GERRIT_BRANCH" == "staging" ]; then
20     exit 0
21 fi
22
23 USED_IMAGES=$(grep -r -E -o -h ':\s*onap/.*:.*' | sed -e 's/^: //' -e 's/^ //' | sort | uniq)
24 REPO_IMAGES=$(curl -s $BASE_URL/v2/_catalog | jq -r '.repositories[]')
25 NOT_AVAILABLE_IMAGES=$(echo "$USED_IMAGES" | grep -vE  "$(echo "$REPO_IMAGES" | tr "\n" "|" | sed 's/|$//')")
26 USED_IMAGES=$(echo "$USED_IMAGES" | grep -E "$(echo "$REPO_IMAGES" | tr "\n" "|" | sed 's/|$//')")
27 for i in $USED_IMAGES; do
28     TMP_IMG=$(echo "$i" | cut -d ":" -f1)
29     TMP_TAG=$(echo "$i" | cut -d ":" -f2)
30     if [ "$LAST_IMG" != "$TMP_IMG" ]; then
31         AVAILABLE_TAGS=$(curl -s $BASE_URL/v2/$TMP_IMG/tags/list | jq -r '.tags[]')
32     fi
33     if ! echo "$AVAILABLE_TAGS" | grep "$TMP_TAG" > /dev/null; then
34         NOT_AVAILABLE_IMAGES="$NOT_AVAILABLE_IMAGES\n$i"
35     fi
36     LAST_IMG="$TMP_IMG"
37     printf "."
38 done
39 printf "\n"
40 if [ -n "$NOT_AVAILABLE_IMAGES" ]; then
41     echo "[ERROR] Only release images are allowed in helm charts."
42     echo "[ERROR] Images not found in release repo:"
43     echo -e "$NOT_AVAILABLE_IMAGES"
44     exit 1
45 fi
46 exit 0