Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 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:" |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 33 | echo " ./$(basename $0) <path to project> [<output list file>]" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 34 | echo " " |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 35 | echo " Example: ./$(basename $0) /root/oom/kubernetes/onap" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 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) |
| 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 | helm template "${PROJECT_DIR}/../${1}" | grep 'image:\ \|tag_version:\ \|h._image' | |
| 59 | sed -e 's/^.*\"h._image\"\ :\ //; s/^.*\"\(.*\)\".*$/\1/' \ |
| 60 | -e 's/\x27\|,//g; s/^.*\(image\|tag_version\):\ //' | tr -d '\r' |
| 61 | } |
| 62 | |
| 63 | # Configuration |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 64 | if [ "${1}" == "-h" ] || [ "${1}" == "--help" ] || [ $# -lt 1 ]; then |
| 65 | usage |
| 66 | fi |
| 67 | |
| 68 | PROJECT_DIR="${1}" |
| 69 | LIST="${2}" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 70 | LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists" |
| 71 | HELM_REPO="local http://127.0.0.1:8879" |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 72 | PROJECT="$(basename ${1})" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 73 | |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 74 | if [ ! -f "${PROJECT_DIR}/../Makefile" ]; then |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 75 | echo "Wrong path to project directory entered" |
| 76 | exit 1 |
| 77 | elif [ -z "${LIST}" ]; then |
| 78 | mkdir -p ${LISTS_DIR} |
Tomáš Levora | e8e9f78 | 2019-05-30 09:31:24 +0200 | [diff] [blame] | 79 | LIST="${LISTS_DIR}/${PROJECT}_docker_images.list" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 80 | fi |
| 81 | |
| 82 | if [ -e "${LIST}" ]; then |
| 83 | mv -f "${LIST}" "${LIST}.bk" |
| 84 | MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n" |
| 85 | fi |
| 86 | |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 87 | # Setup helm |
| 88 | if pgrep -x "helm" > /dev/null; then |
| 89 | echo "helm is already running" |
| 90 | else |
| 91 | helm init -c > /dev/null |
| 92 | helm serve & |
| 93 | fi |
| 94 | |
| 95 | # Create helm repository |
| 96 | if ! helm repo list 2>&1 | awk '{ print $1, $2 }' | grep -q "$HELM_REPO" > /dev/null; then |
| 97 | helm repo add "$HELM_REPO" |
| 98 | fi |
| 99 | |
| 100 | # Make all |
| 101 | pushd "${PROJECT_DIR}/.." |
| 102 | echo "Building project..." |
| 103 | make all > /dev/null; make ${PROJECT} > /dev/null |
| 104 | popd |
| 105 | |
| 106 | # Create the list from all enabled subsystems |
| 107 | echo "Creating the list..." |
| 108 | if [ "${PROJECT}" == "onap" ]; then |
Tomáš Levora | 12836bf | 2019-05-30 17:11:28 +0200 | [diff] [blame^] | 109 | COMMENT="OOM commit $(git --git-dir="${PROJECT_DIR}/../../.git" rev-parse HEAD)" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 110 | for subsystem in `parse_yaml "${PROJECT_DIR}/values.yaml"`; do |
| 111 | create_list ${subsystem} |
Tomáš Levora | 12836bf | 2019-05-30 17:11:28 +0200 | [diff] [blame^] | 112 | done | sort -u > ${LIST} |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 113 | else |
Tomáš Levora | 12836bf | 2019-05-30 17:11:28 +0200 | [diff] [blame^] | 114 | COMMENT="${PROJECT}" |
| 115 | create_list ${PROJECT} | sort -u > ${LIST} |
| 116 | fi |
| 117 | |
| 118 | # Add comment reffering to the project |
| 119 | sed -i "1i# generated from ${COMMENT}" "${LIST}" |
Tomáš Levora | a5a6e7c | 2019-03-04 16:40:09 +0100 | [diff] [blame] | 120 | |
| 121 | echo -e ${MSG} |
| 122 | echo -e 'The list has been created:\n '"${LIST}" |
| 123 | exit 0 |