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