db085618ac229f361222add2f713865b828e23b5
[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 # Proccess input options
94 if [ $# -lt 1 ]; then
95     usage
96 fi
97
98 while [ $# -gt 0 ];
99 do
100     case "${1}" in
101         -h | --help)
102             usage
103             ;;
104         -p | --helm-port)
105             PORT="${2}"
106             validate_port "${PORT}"
107             shift 2
108             ;;
109         -*)
110             echo "Unknown option ${1}"
111             usage
112             ;;
113         *)
114             # end of options
115             break
116             ;;
117     esac
118 done
119
120 # Configuration
121 PROJECT_DIR="${1}"
122 LIST="${2}"
123 LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
124 HELM_REPO_HOST="127.0.0.1"
125 HELM_REPO_PORT="${PORT:-8879}"
126 HELM_REPO="${HELM_REPO_HOST}:${HELM_REPO_PORT}"
127 HELM_REPO_PATH="dist/packages" # based on PACKAGE_DIR defined in oom/kubernetes/Makefile
128 DOCKER_CONTAINER="generate-certs-${HELM_REPO_PORT}" # oom-cert-service container name override
129 PROJECT="$(basename ${1})"
130
131 if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
132     echo "Wrong path to project directory entered"
133     exit 1
134 elif [ -z "${LIST}" ]; then
135     mkdir -p ${LISTS_DIR}
136     LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
137 else
138     # $2 is not empty - ensure LIST path exists
139     LIST_DIR="$(dirname ${LIST})"
140     mkdir -p "${LIST_DIR}"
141     MSG="${LIST_DIR} didn't exist, created\n"
142 fi
143
144 if [ -e "${LIST}" ]; then
145     mv -f "${LIST}" "${LIST}.bk"
146     MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
147 fi
148
149 # Setup helm
150 HELM_HOME=$(mktemp -p /tmp -d .helm.XXXXXXXX)
151 export HELM_HOME
152 kill_helm # make sure it's not already running
153 mkdir -p "${PROJECT_DIR}/../${HELM_REPO_PATH}"
154 helm init -c --local-repo-url "http://${HELM_REPO}"
155 helm serve --address ${HELM_REPO} --repo-path "${PROJECT_DIR}/../${HELM_REPO_PATH}" &
156 helm repo remove stable 2>/dev/null || true
157
158 # Make all
159 pushd "${PROJECT_DIR}/.."
160 echo "Building project..."
161 export SKIP_LINT=TRUE
162 export DOCKER_CONTAINER
163 make -e all > /dev/null; make -e ${PROJECT} > /dev/null
164 popd
165
166 # Create the list from all enabled subsystems
167 echo "Creating the list..."
168 if [ "${PROJECT}" == "onap" ]; then
169     COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
170     for subsystem in `parse_yaml "${PROJECT_DIR}/resources/overrides/onap-all.yaml"`; do
171         create_list ${subsystem}
172     done | sort -u > ${LIST}
173 else
174     COMMENT="${PROJECT}"
175     create_list ${PROJECT} | sort -u > ${LIST}
176 fi
177
178 # Add comment reffering to the project
179 sed -i "1i# generated from ${COMMENT}" "${LIST}"
180
181 echo -e ${MSG}
182 echo -e 'The list has been created:\n '"${LIST}"
183
184 # Remove temporary helm directory
185 rm -rf ${HELM_HOME}
186 # Kill helm
187 kill_helm
188
189 exit 0