73c19bc4e61a4feda3f1aeb3645aad2e331ce5a5
[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
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) [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 "      "
39     echo "      Example: ./$(basename $0) /root/oom/kubernetes/onap"
40     echo "      "
41     echo "      Dependencies: helm, python-yaml, make"
42     echo "      "
43     exit 1
44 }
45
46 parse_yaml() {
47 python3 - <<PYP
48 #!/usr/bin/python3
49 import yaml
50 import sys
51
52 with open("${1}", 'r') as f:
53     values = yaml.load(f, Loader=yaml.SafeLoader)
54     enabled = filter(lambda x: values[x].get('enabled', False) == True, values)
55     print(' '.join(enabled))
56 PYP
57 }
58
59 create_list() {
60     if [ -d "${PROJECT_DIR}/../${1}" ]; then
61         SUBSYS_DIR="${PROJECT_DIR}/../${1}"
62     elif [ -d "${PROJECT_DIR}/../common/${1}" ]; then
63         SUBSYS_DIR="${PROJECT_DIR}/../common/${1}"
64     else
65         >&2 echo -e \n"    !!! ${1} sybsystem does not exist !!!"\n
66     fi
67     helm template --set global.masterPassword=TemplatePassword -f ${PROJECT_DIR}/values.yaml "${SUBSYS_DIR}" | grep 'image:\ \|tag_version:\ \|h._image' |
68         sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \
69             -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r'
70 }
71
72 # Kill helm if already running
73 kill_helm() {
74     for pid in $(pgrep -f "helm serve --address ${HELM_REPO}");
75     do
76         kill $pid
77     done
78 }
79
80 validate_port() {
81     if [ -z $1 ];
82     then
83         echo "Error: No valid port number provided"
84         exit 1
85     fi
86     if ! [[ "$1" =~ ^[0-9]*$ ]];
87     then
88         echo "Error: "${1}" is not a valid port number"
89         exit 1
90     fi
91 }
92
93 check_helm() {
94     sleep 2 # let the helm process settle
95     if [ $(pgrep -f "helm serve --address ${HELM_REPO}" -c) -eq 0 ];
96     then
97         echo "Fatal: Helm chart repository server failed to start"
98         exit 1
99     fi
100 }
101
102 # Proccess input options
103 if [ $# -lt 1 ]; then
104     usage
105 fi
106
107 while [ $# -gt 0 ];
108 do
109     case "${1}" in
110         -h | --help)
111             usage
112             ;;
113         -p | --helm-port)
114             PORT="${2}"
115             validate_port "${PORT}"
116             shift 2
117             ;;
118         -*)
119             echo "Unknown option ${1}"
120             usage
121             ;;
122         *)
123             # end of options
124             break
125             ;;
126     esac
127 done
128
129 # Configuration
130 PROJECT_DIR="${1}"
131 LIST="${2}"
132 LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
133 HELM_REPO_HOST="127.0.0.1"
134 HELM_REPO_PORT="${PORT:-8879}"
135 HELM_REPO="${HELM_REPO_HOST}:${HELM_REPO_PORT}"
136 HELM_REPO_PATH="dist/packages" # based on PACKAGE_DIR defined in oom/kubernetes/Makefile
137 DOCKER_CONTAINER="generate-certs-${HELM_REPO_PORT}" # oom-cert-service container name override
138 PROJECT="$(basename ${1})"
139
140 if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
141     echo "Wrong path to project directory entered"
142     exit 1
143 elif [ -z "${LIST}" ]; then
144     mkdir -p ${LISTS_DIR}
145     LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
146 else
147     # $2 is not empty - ensure LIST path exists
148     LIST_DIR="$(dirname ${LIST})"
149     mkdir -p "${LIST_DIR}"
150     MSG="${LIST_DIR} didn't exist, created\n"
151 fi
152
153 if [ -e "${LIST}" ]; then
154     mv -f "${LIST}" "${LIST}.bk"
155     MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
156 fi
157
158 # Setup helm
159 HELM_HOME=$(mktemp -p /tmp -d .helm.XXXXXXXX)
160 export HELM_HOME
161 kill_helm # make sure it's not already running
162 mkdir -p "${PROJECT_DIR}/../${HELM_REPO_PATH}"
163 helm init -c --local-repo-url "http://${HELM_REPO}"
164 helm serve --address ${HELM_REPO} --repo-path "${PROJECT_DIR}/../${HELM_REPO_PATH}" &
165 helm repo remove stable 2>/dev/null || true
166 check_helm
167
168 # Make all
169 pushd "${PROJECT_DIR}/.."
170 echo "Building project..."
171 export SKIP_LINT=TRUE
172 export DOCKER_CONTAINER
173 make -e all > /dev/null; make -e ${PROJECT} > /dev/null
174 popd
175
176 # Create the list from all enabled subsystems
177 echo "Creating the list..."
178 if [ "${PROJECT}" == "onap" ]; then
179     COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
180     for subsystem in `parse_yaml "${PROJECT_DIR}/resources/overrides/onap-all.yaml"`; do
181         create_list ${subsystem}
182     done | sort -u > ${LIST}
183 else
184     COMMENT="${PROJECT}"
185     create_list ${PROJECT} | sort -u > ${LIST}
186 fi
187
188 # Add comment reffering to the project
189 sed -i "1i# generated from ${COMMENT}" "${LIST}"
190
191 echo -e ${MSG}
192 echo -e 'The list has been created:\n '"${LIST}"
193
194 # Remove temporary helm directory
195 rm -rf ${HELM_HOME}
196 # Kill helm
197 kill_helm
198
199 exit 0