Vijay Venkatesh Kumar | 8c46517 | 2021-06-03 16:51:33 -0400 | [diff] [blame] | 1 | #!/bin/sh -x |
| 2 | |
| 3 | # Copyright (c) 2021 AT&T. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Pre-requisite |
| 18 | # 1. Chart packages available under local directory provided as input/argument |
| 19 | # 2. helm client installed with push plugin |
| 20 | # 3. ONAP chartmuseum service deployed |
| 21 | |
| 22 | usage() |
| 23 | { |
| 24 | echo "Chart Base directory must be provided as input!!" |
| 25 | echo "Usage: registry-initialize.sh -d chartdirectory \ |
| 26 | <-n namespace override> <-r helmrelease override>" |
| 27 | exit 1 |
| 28 | } |
| 29 | |
| 30 | if [ $# -eq 0 ]; then |
| 31 | usage |
| 32 | fi |
| 33 | |
| 34 | # defaults |
| 35 | NAMESPACE=onap |
| 36 | RLS_NAME=onap |
| 37 | LOGIN="" |
| 38 | PASSWORD="" |
| 39 | |
| 40 | while getopts ":d:n:r:" opt; do |
| 41 | case $opt in |
| 42 | d) BASEDIR="$OPTARG" |
| 43 | ;; |
| 44 | n) NAMESPACE="$OPTARG" |
| 45 | ;; |
| 46 | r) RLS_NAME="$OPTARG" |
| 47 | ;; |
| 48 | \?) echo "Invalid option -$OPTARG" >&2 |
| 49 | usage |
| 50 | ;; |
| 51 | esac |
| 52 | done |
| 53 | |
| 54 | if [ -z "$BASEDIR" ]; then |
| 55 | exit "Chart base directory provided $BASEDIR is empty" |
| 56 | fi |
| 57 | |
| 58 | if [ "$(find $BASEDIR -maxdepth 1 -name '*tgz' -print -quit)" ]; then |
| 59 | echo "$BASEDIR valid" |
| 60 | else |
| 61 | exit "No chart package on $BASEDIR provided" |
| 62 | fi |
| 63 | |
| 64 | LOGIN=$(kubectl -n "$NAMESPACE" get secret \ |
| 65 | "${RLS_NAME}-chartmuseum-registrycred" \ |
| 66 | -o jsonpath='{.data.login}' | base64 -d) |
| 67 | |
| 68 | PASSWORD=$(kubectl -n "$NAMESPACE" get secret \ |
| 69 | "${RLS_NAME}-chartmuseum-registrycred" \ |
| 70 | -o jsonpath='{.data.password}' | base64 -d) |
| 71 | |
| 72 | if [ -z "$LOGIN" ] || [ -z "$PASSWORD" ]; then |
| 73 | echo "Login/Password credential for target registry cannot be retrieved" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
| 77 | # Expose cluster port via port-forwarding |
| 78 | kubectl -n $NAMESPACE port-forward service/chart-museum 27017:80 & |
| 79 | if [ $? -ne 0 ]; then |
| 80 | echo "Error in portforwarding; registry cannot be added!!" |
| 81 | exit 1 |
| 82 | fi |
| 83 | |
| 84 | sleep 5 |
| 85 | |
| 86 | # Add chartmuseum repo as helm repo |
| 87 | # Credentials should match config defined in |
| 88 | # oom\kubernetes\platform\components\chartmuseum\values.yaml |
| 89 | helm repo add k8s-registry http://127.0.0.1:27017 --username "$LOGIN" \ |
| 90 | --password "$PASSWORD" |
| 91 | if [ $? -ne 0 ]; then |
| 92 | echo "registry cannot be added!!" |
| 93 | pkill -f "port-forward service/chart-museum" |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # Initial scope is pushing only dcae charts |
| 98 | # can be expanded to include all onap charts if required |
| 99 | for file in $BASEDIR/dcae*tgz; do |
| 100 | # use helm plugin to push charts |
| 101 | helm push $file k8s-registry |
| 102 | if [ $? -eq 0 ]; then |
| 103 | echo "$file uploaded to registry successfully" |
| 104 | else |
| 105 | echo "registry upload failed!!" |
| 106 | pkill -f "port-forward service/chart-museum" |
| 107 | helm repo remove k8s-registry |
| 108 | exit 1 |
| 109 | fi |
| 110 | done |
| 111 | |
| 112 | echo "All Helm charts successfully uploaded into internal repository" |
| 113 | |
| 114 | # Remove the port-forwarding process |
| 115 | pkill -f "port-forward service/chart-museum" |
| 116 | |
| 117 | # Remove helm registry from local |
| 118 | helm repo remove k8s-registry |