Set SKIP_LINT=true prior to building helm repository
[oom/offline-installer.git] / build / creating_data / docker-images-collector.sh
1 #! /usr/bin/env bash
2
3 #   COPYRIGHT NOTICE STARTS HERE
4 #
5 #   Copyright 2019 © Samsung Electronics Co., Ltd.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #
19 #   COPYRIGHT NOTICE ENDS HERE
20
21 ### This script is preparing docker images list based on kubernetes project
22
23 ### NOTE: helm needs to be installed and working, it is required for correct processing
24 ### of helm charts in oom directory
25
26 # Fail fast settings
27 set -e
28
29 usage () {
30     echo "      "
31     echo "  This script is preparing docker images list based on kubernetes project"
32     echo "      Usage:"
33     echo "        ./$(basename $0) <path to project> [<output list file>]"
34     echo "      "
35     echo "      Example: ./$(basename $0) /root/oom/kubernetes/onap"
36     echo "      "
37     echo "      Dependencies: helm, python-yaml, make"
38     echo "      "
39     exit 1
40 }
41
42 parse_yaml() {
43 python3 - <<PYP
44 #!/usr/bin/python3
45 import yaml
46 import sys
47
48 with open("${1}", 'r') as f:
49     values = yaml.load(f, Loader=yaml.SafeLoader)
50     enabled = filter(lambda x: values[x].get('enabled', False) == True, values)
51     print(' '.join(enabled))
52 PYP
53 }
54
55 create_list() {
56     if [ -d "${PROJECT_DIR}/../${1}" ]; then
57         SUBSYS_DIR="${PROJECT_DIR}/../${1}"
58     elif [ -d "${PROJECT_DIR}/../common/${1}" ]; then
59         SUBSYS_DIR="${PROJECT_DIR}/../common/${1}"
60     else
61         >&2 echo -e \n"    !!! ${1} sybsystem does not exist !!!"\n
62     fi
63     helm template --set global.masterPassword=TemplatePassword -f ${PROJECT_DIR}/values.yaml "${SUBSYS_DIR}" | grep 'image:\ \|tag_version:\ \|h._image' |
64         sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \
65             -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r'
66 }
67
68 # Configuration
69 if [ "${1}" == "-h" ] || [ "${1}" == "--help" ] || [ $# -lt 1 ]; then
70     usage
71 fi
72
73 PROJECT_DIR="${1}"
74 LIST="${2}"
75 LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
76 HELM_REPO="local http://127.0.0.1:8879"
77 PROJECT="$(basename ${1})"
78
79 if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
80     echo "Wrong path to project directory entered"
81     exit 1
82 elif [ -z "${LIST}" ]; then
83     mkdir -p ${LISTS_DIR}
84     LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
85 else
86     # $2 is not empty - ensure LIST path exists
87     LIST_DIR="$(dirname ${LIST})"
88     mkdir -p "${LIST_DIR}"
89     MSG="${LIST_DIR} didn't exist, created\n"
90 fi
91
92 if [ -e "${LIST}" ]; then
93     mv -f "${LIST}" "${LIST}.bk"
94     MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
95 fi
96
97 # Setup helm
98 if ps -eaf | grep -v "grep" | grep "helm" > /dev/null; then
99     echo "helm is already running"
100 else
101     helm init -c > /dev/null
102     helm serve &
103     helm repo remove stable 2>/dev/null || true
104 fi
105
106 # Create helm repository
107 if ! helm repo list 2>&1 | awk '{ print $1, $2 }' | grep -q "$HELM_REPO" > /dev/null; then
108     helm repo add $HELM_REPO
109 fi
110
111 # Make all
112 pushd "${PROJECT_DIR}/.."
113 echo "Building project..."
114 export SKIP_LINT=TRUE
115 make all > /dev/null; make ${PROJECT} > /dev/null
116 popd
117
118 # Create the list from all enabled subsystems
119 echo "Creating the list..."
120 if [ "${PROJECT}" == "onap" ]; then
121     COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
122     for subsystem in `parse_yaml "${PROJECT_DIR}/resources/overrides/onap-all.yaml"`; do
123         create_list ${subsystem}
124     done | sort -u > ${LIST}
125 else
126     COMMENT="${PROJECT}"
127     create_list ${PROJECT} | sort -u > ${LIST}
128 fi
129
130 # Add comment reffering to the project
131 sed -i "1i# generated from ${COMMENT}" "${LIST}"
132
133 echo -e ${MSG}
134 echo -e 'The list has been created:\n '"${LIST}"
135 exit 0