blob: 6761c3285b9457dd4310e3b1fb1374424008e14c [file] [log] [blame]
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +01001#! /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
27set -e
28
29usage () {
30 echo " "
31 echo " This script is preparing docker images list based on kubernetes project"
32 echo " Usage:"
Tomáš Levorae8e9f782019-05-30 09:31:24 +020033 echo " ./$(basename $0) <path to project> [<output list file>]"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010034 echo " "
Tomáš Levorae8e9f782019-05-30 09:31:24 +020035 echo " Example: ./$(basename $0) /root/oom/kubernetes/onap"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010036 echo " "
37 echo " Dependencies: helm, python-yaml, make"
38 echo " "
39 exit 1
40}
41
42parse_yaml() {
43python - <<PYP
44#!/usr/bin/python
45from __future__ import print_function
46import yaml
47import sys
48
49with open("${1}", 'r') as f:
Tomáš Levorafd005222019-06-05 12:53:05 +020050 values = yaml.load(f, Loader=yaml.SafeLoader)
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010051
52 enabled = filter(lambda x: values[x].get('enabled', False) == True, values)
53 print(' '.join(enabled))
54PYP
55}
56
57create_list() {
Tomáš Levora0830f7a2019-05-31 14:10:15 +020058 if [ -d "${PROJECT_DIR}/../${1}" ]; then
59 SUBSYS_DIR="${PROJECT_DIR}/../${1}"
60 elif [ -d "${PROJECT_DIR}/../common/${1}" ]; then
61 SUBSYS_DIR="${PROJECT_DIR}/../common/${1}"
62 else
63 >&2 echo -e \n" !!! ${1} sybsystem does not exist !!!"\n
64 fi
65 helm template "${SUBSYS_DIR}" | grep 'image:\ \|tag_version:\ \|h._image' |
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010066 sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \
67 -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r'
68}
69
70# Configuration
Tomáš Levorae8e9f782019-05-30 09:31:24 +020071if [ "${1}" == "-h" ] || [ "${1}" == "--help" ] || [ $# -lt 1 ]; then
72 usage
73fi
74
75PROJECT_DIR="${1}"
76LIST="${2}"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010077LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
78HELM_REPO="local http://127.0.0.1:8879"
Tomáš Levorae8e9f782019-05-30 09:31:24 +020079PROJECT="$(basename ${1})"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010080
Tomáš Levorae8e9f782019-05-30 09:31:24 +020081if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010082 echo "Wrong path to project directory entered"
83 exit 1
84elif [ -z "${LIST}" ]; then
85 mkdir -p ${LISTS_DIR}
Tomáš Levorae8e9f782019-05-30 09:31:24 +020086 LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010087fi
88
89if [ -e "${LIST}" ]; then
90 mv -f "${LIST}" "${LIST}.bk"
91 MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
92fi
93
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +010094# Setup helm
95if pgrep -x "helm" > /dev/null; then
96 echo "helm is already running"
97else
98 helm init -c > /dev/null
99 helm serve &
100fi
101
102# Create helm repository
103if ! helm repo list 2>&1 | awk '{ print $1, $2 }' | grep -q "$HELM_REPO" > /dev/null; then
104 helm repo add "$HELM_REPO"
105fi
106
107# Make all
108pushd "${PROJECT_DIR}/.."
109echo "Building project..."
110make all > /dev/null; make ${PROJECT} > /dev/null
111popd
112
113# Create the list from all enabled subsystems
114echo "Creating the list..."
115if [ "${PROJECT}" == "onap" ]; then
Tomáš Levora12836bf2019-05-30 17:11:28 +0200116 COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +0100117 for subsystem in `parse_yaml "${PROJECT_DIR}/values.yaml"`; do
118 create_list ${subsystem}
Tomáš Levora12836bf2019-05-30 17:11:28 +0200119 done | sort -u > ${LIST}
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +0100120else
Tomáš Levora12836bf2019-05-30 17:11:28 +0200121 COMMENT="${PROJECT}"
122 create_list ${PROJECT} | sort -u > ${LIST}
123fi
124
125# Add comment reffering to the project
126sed -i "1i# generated from ${COMMENT}" "${LIST}"
Tomáš Levoraa5a6e7c2019-03-04 16:40:09 +0100127
128echo -e ${MSG}
129echo -e 'The list has been created:\n '"${LIST}"
130exit 0