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:" |
| 33 | echo " ./$(basename $0) <project version> <path to project> [<output list file>]" |
| 34 | echo " " |
| 35 | echo " Example: ./$(basename $0) onap_3.0.2 /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) |
| 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 |
| 64 | TAG="${1}" |
| 65 | PROJECT_DIR="${2}" |
| 66 | LIST="${3}" |
| 67 | LISTS_DIR="$(readlink -f $(dirname ${0}))/../data_lists" |
| 68 | HELM_REPO="local http://127.0.0.1:8879" |
| 69 | |
| 70 | if [ "${1}" == "-h" ] || [ "${1}" == "--help" ] || [ $# -lt 2 ]; then |
| 71 | usage |
| 72 | elif [ ! -f "${PROJECT_DIR}/../Makefile" ]; then |
| 73 | echo "Wrong path to project directory entered" |
| 74 | exit 1 |
| 75 | elif [ -z "${LIST}" ]; then |
| 76 | mkdir -p ${LISTS_DIR} |
| 77 | LIST="${LISTS_DIR}/${TAG}-docker_images.list" |
| 78 | fi |
| 79 | |
| 80 | if [ -e "${LIST}" ]; then |
| 81 | mv -f "${LIST}" "${LIST}.bk" |
| 82 | MSG="$(realpath ${LIST}) already existed\nCreated backup $(realpath ${LIST}).bk\n" |
| 83 | fi |
| 84 | |
| 85 | PROJECT="$(basename ${2})" |
| 86 | |
| 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 |
| 109 | for subsystem in `parse_yaml "${PROJECT_DIR}/values.yaml"`; do |
| 110 | create_list ${subsystem} |
| 111 | done |
| 112 | else |
| 113 | create_list ${PROJECT} |
| 114 | fi | sort -u > ${LIST} |
| 115 | |
| 116 | echo -e ${MSG} |
| 117 | echo -e 'The list has been created:\n '"${LIST}" |
| 118 | exit 0 |