Merge "Updated inventory template in install guide"
[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 © 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
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) <path to project> [<output list file>]"
34     echo "      "
35     echo "      Example: ./$(basename $0) /root/oom/kubernetes/onap"
36     echo "      "
37     echo "      Dependencies: helm, python-yaml, make"
38     echo "      "
39     exit 1
40 }
41
42 parse_yaml() {
43 python - <<PYP
44 #!/usr/bin/python
45 from __future__ import print_function
46 import yaml
47 import sys
48
49 with open("${1}", 'r') as f:
50     values = yaml.load(f, Loader=yaml.SafeLoader)
51
52     enabled = filter(lambda x: values[x].get('enabled', False) == True, values)
53     print(' '.join(enabled))
54 PYP
55 }
56
57 create_list() {
58     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' |
66         sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \
67             -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r'
68 }
69
70 # Configuration
71 if [ "${1}" == "-h" ] || [ "${1}" == "--help" ] || [ $# -lt 1 ]; then
72     usage
73 fi
74
75 PROJECT_DIR="${1}"
76 LIST="${2}"
77 LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists"
78 HELM_REPO="local http://127.0.0.1:8879"
79 PROJECT="$(basename ${1})"
80
81 if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then
82     echo "Wrong path to project directory entered"
83     exit 1
84 elif [ -z "${LIST}" ]; then
85     mkdir -p ${LISTS_DIR}
86     LIST="${LISTS_DIR}/${PROJECT}_docker_images.list"
87 fi
88
89 if [ -e "${LIST}" ]; then
90     mv -f "${LIST}" "${LIST}.bk"
91     MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n"
92 fi
93
94 # Setup helm
95 if pgrep -x "helm" > /dev/null; then
96     echo "helm is already running"
97 else
98     helm init -c > /dev/null
99     helm serve &
100 fi
101
102 # Create helm repository
103 if ! helm repo list 2>&1 | awk '{ print $1, $2 }' | grep -q "$HELM_REPO" > /dev/null; then
104     helm repo add "$HELM_REPO"
105 fi
106
107 # Make all
108 pushd "${PROJECT_DIR}/.."
109 echo "Building project..."
110 make all > /dev/null; make ${PROJECT} > /dev/null
111 popd
112
113 # Create the list from all enabled subsystems
114 echo "Creating the list..."
115 if [ "${PROJECT}" == "onap" ]; then
116     COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)"
117     for subsystem in `parse_yaml "${PROJECT_DIR}/values.yaml"`; do
118         create_list ${subsystem}
119     done | sort -u > ${LIST}
120 else
121     COMMENT="${PROJECT}"
122     create_list ${PROJECT} | sort -u > ${LIST}
123 fi
124
125 # Add comment reffering to the project
126 sed -i "1i# generated from ${COMMENT}" "${LIST}"
127
128 echo -e ${MSG}
129 echo -e 'The list has been created:\n '"${LIST}"
130 exit 0