[BUILD] Improve helm template output
[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-2021 © 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 OOM project
22
23 ### NOTE: helm v3.x.x, helm push plugin and docker need to be installed; those are
24 ### required for correct processing 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 OOM project"
32     echo "      Usage:"
33     echo "        ./$(basename $0) [OPTION]... <path to project> [<output list file>]"
34     echo "      "
35     echo "      Options:"
36     echo "          -h | --help            Show script usage synopsis"
37     echo "          -p | --helm-port       Chart repository server port"
38     echo "          -b | --helm-bin        Path to Helm executable"
39     echo "      "
40     echo "      Example: ./$(basename $0) /root/oom/kubernetes/onap"
41     echo "      "
42     echo "      Dependencies: helm v3.x.x, helm push plugin, python-yaml, make"
43     echo "      "
44     exit 1
45 }
46
47 parse_yaml() {
48 python3 - <<PYP
49 #!/usr/bin/python3
50 import yaml
51 import sys
52
53 with open("${1}", 'r') as f:
54     values = yaml.load(f, Loader=yaml.SafeLoader)
55     enabled = filter(lambda x: values[x].get('enabled', False) == True, values)
56     print(' '.join(enabled))
57 PYP
58 }
59
60 create_list() {
61     if [ -d "${PROJECT_DIR}/../${1}" ]; then
62         SUBSYS_DIR="${PROJECT_DIR}/../${1}"
63     elif [ -d "${PROJECT_DIR}/../common/${1}" ]; then
64         SUBSYS_DIR="${PROJECT_DIR}/../common/${1}"
65     else
66         >&2 echo -e \n"    !!! ${1} sybsystem does not exist !!!"\n
67     fi
68     ${HELM_BIN} template --set global.masterPassword=TemplatePassword,global.offlineDeploymentBuild=true,global.importCustomCertsEnabled=true -f ${PROJECT_DIR}/values.yaml "${SUBSYS_DIR}" | grep 'image:\ \|tag_version:\ \|h._image' | grep -v "image: TBD" |
69         sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \
70             -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r'
71 }
72
73 # Kill helm repository if already running
74 kill_chart_repo() {
75     if [ $(docker ps -aq -f name="^chartmuseum-${HELM_REPO_PORT}$") ];
76     then
77         docker kill "chartmuseum-${HELM_REPO_PORT}"
78     fi
79 }
80
81 validate_port() {
82     if [ -z $1 ];
83     then
84         echo "Error: No valid port number provided"
85         exit 1
86     fi
87     if ! [[ "$1" =~ ^[0-9]*$ ]];
88     then
89         echo "Error: "${1}" is not a valid port number"
90         exit 1
91     fi
92 }
93
94 validate_bin() {
95     if [ -z $1 ];
96     then
97         echo "Error: No path to executable provided"
98         exit 1
99     else
100         if ! [[ -x ${1} && -f ${1} ]];
101         then
102             echo "Error: ${1} is not an executable"
103             exit 1
104         fi
105     fi
106 }
107
108 check_chart_repo() {
109     sleep 2 # let the helm repository process settle
110     if [ ! $(docker ps -aq -f name="^chartmuseum-${HELM_REPO_PORT}$") ];
111     then
112         echo "Fatal: Helm chart repository docker container failed to start"
113         exit 1
114     fi
115 }
116
117 # Proccess input options
118 if [ $# -lt 1 ]; then
119     usage
120 fi
121
122 while [ $# -gt 0 ];
123 do
124     case "${1}" in
125         -h | --help)
126             usage
127             ;;
128         -p | --helm-port)
129             PORT="${2}"
130             validate_port "${PORT}"
131             shift 2
132             ;;
133         -b | --helm-bin)
134             BIN="${2}"
135             validate_bin "${BIN}"
136             shift 2
137             ;;
138         -*)
139             echo "Unknown option ${1}"
140             usage
141             ;;
142         *)
143             # end of options
144             break
145             ;;
146     esac
147 done
148
149 # Configuration
150 PROJECT_DIR="${1}"
151 LIST="${2}"
152 LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
153 HELM_BIN=${BIN:-helm}
154 HELM_REPO_HOST="127.0.0.1"
155 HELM_REPO_PORT="${PORT:-8879}"
156 HELM_REPO="${HELM_REPO_HOST}:${HELM_REPO_PORT}"
157 HELM_REPO_PATH="dist/packages" # based on PACKAGE_DIR defined in oom/kubernetes/Makefile
158 DOCKER_CONTAINER="generate-certs-${HELM_REPO_PORT}" # oom-cert-service container name override
159 PROJECT="$(basename ${1})"
160
161 if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
162     echo "Wrong path to project directory entered"
163     exit 1
164 elif [ -z "${LIST}" ]; then
165     mkdir -p ${LISTS_DIR}
166     LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
167 else
168     # $2 is not empty - ensure LIST path exists
169     LIST_DIR="$(dirname ${LIST})"
170     mkdir -p "${LIST_DIR}"
171     MSG="${LIST_DIR} didn't exist, created\n"
172 fi
173
174 if [ -e "${LIST}" ]; then
175     mv -f "${LIST}" "${LIST}.bk"
176     MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
177 fi
178
179 HELM_HOME=$(mktemp -p /tmp -d .helm.XXXXXXXX)
180 export HELM_HOME
181
182 kill_chart_repo # make sure it's not already running
183
184 export HELM_CONFIG_HOME="${HELM_HOME}/.config"
185 export HELM_CACHE_HOME="${HELM_HOME}/.cache"
186 mkdir --mode=777 ${HELM_HOME}/chartmuseum
187 docker run --rm -it -d -p ${HELM_REPO_PORT}:8080 \
188   --name "chartmuseum-${HELM_REPO_PORT}" \
189   -e STORAGE=local -e STORAGE_LOCAL_ROOTDIR=/charts \
190   -v ${HELM_HOME}/chartmuseum:/charts chartmuseum/chartmuseum
191 sleep 2 # let the chartmuseum process settle
192 ${HELM_BIN} repo add local "http://${HELM_REPO}"
193
194 check_chart_repo
195
196 # Make all
197 pushd "${PROJECT_DIR}/.."
198 echo "Building project..."
199 export SKIP_LINT=TRUE
200 export DOCKER_CONTAINER
201 export HELM_BIN
202 make -e all > /dev/null
203 popd
204
205 # Create the list from all enabled subsystems
206 echo "Creating the list..."
207 if [ "${PROJECT}" == "onap" ]; then
208     COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
209     for subsystem in `parse_yaml "${PROJECT_DIR}/resources/overrides/onap-all.yaml"`; do
210         create_list ${subsystem}
211     done | sort -u > ${LIST}
212 else
213     COMMENT="${PROJECT}"
214     create_list ${PROJECT} | sort -u > ${LIST}
215 fi
216
217 # Add comment reffering to the project
218 sed -i "1i# generated from ${COMMENT}" "${LIST}"
219
220 echo -e ${MSG}
221 echo -e 'The list has been created:\n '"${LIST}"
222
223 # Kill helm
224 kill_chart_repo
225 # Remove temporary helm directory
226 rm -rf ${HELM_HOME}
227
228 exit 0