blob: 45ee44f3c580103e902585ba0d78c1857fc534b6 [file] [log] [blame]
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -04001#!/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
22usage()
23{
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020024 echo "Chart Base directory or helm chart from local repo must be provided as input!!"
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040025 echo "Usage: registry-initialize.sh -d chartdirectory \
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020026<-n namespace override> <-r helmrelease override> <-p chart name prefix> | <-h helm charts from local repo>"
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040027 exit 1
28}
29
30if [ $# -eq 0 ]; then
31 usage
32fi
33
34# defaults
35NAMESPACE=onap
36RLS_NAME=onap
37LOGIN=""
38PASSWORD=""
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020039PREF=""
40HELM_REPO=local
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040041
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020042while getopts ":d:n:r:p:h:c:" opt; do
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040043 case $opt in
44 d) BASEDIR="$OPTARG"
45 ;;
46 n) NAMESPACE="$OPTARG"
47 ;;
48 r) RLS_NAME="$OPTARG"
49 ;;
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020050 p) PREF="$OPTARG"
51 ;;
52 h) HELM_CHART="$OPTARG"
53 ;;
54 c) HELM_REPO="$OPTARG"
55 ;;
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040056 \?) echo "Invalid option -$OPTARG" >&2
57 usage
58 ;;
59 esac
60done
61
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020062
63if [ -z "$BASEDIR" ] && [ -z "$HELM_CHART" ] ; then
64 echo "Chart base directory provided $BASEDIR and helm chart from local repo is empty"
65 exit
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040066fi
67
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +020068if [ -n "$BASEDIR" ] && [ -n "$HELM_CHART" ] ; then
69 echo "Both chart base directory $BASEDIR and helm chart from local repo $HELM_CHART cannot be used at the same time "
70 exit
71fi
72
73if [ -n "$BASEDIR" ]; then
74 if [ "$(find $BASEDIR -maxdepth 1 -name '*tgz' -print -quit)" ]; then
75 echo "$BASEDIR valid"
76 else
77 echo "No chart package on $BASEDIR provided"
78 exit
79 fi
80fi
81
82if [ -n "$HELM_CHART" ]; then
83 tmp_location=$(mktemp -d)
84 helm pull $HELM_REPO/$HELM_CHART -d $tmp_location
85 if [ $? -eq 0 ]; then
86 echo "Helm chart $HELM_CHART has been pulled out from in $HELM_REPO repo"
87 BASEDIR=$tmp_location
88 else
89 echo "No chart package $HELM_CHART on $HELM_REPO repo"
90 exit
91 fi
92fi
93
94if [ -z "$PREF" ] && [ -z "$HELM_CHART" ] ; then
95 PREF=dcae
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -040096fi
97
98LOGIN=$(kubectl -n "$NAMESPACE" get secret \
99 "${RLS_NAME}-chartmuseum-registrycred" \
100 -o jsonpath='{.data.login}' | base64 -d)
101
102PASSWORD=$(kubectl -n "$NAMESPACE" get secret \
103 "${RLS_NAME}-chartmuseum-registrycred" \
104 -o jsonpath='{.data.password}' | base64 -d)
105
106if [ -z "$LOGIN" ] || [ -z "$PASSWORD" ]; then
107 echo "Login/Password credential for target registry cannot be retrieved"
108 exit 1
109fi
110
111# Expose cluster port via port-forwarding
112kubectl -n $NAMESPACE port-forward service/chart-museum 27017:80 &
113if [ $? -ne 0 ]; then
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +0200114 echo "Error in port forwarding; registry cannot be added!!"
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -0400115 exit 1
116fi
117
118sleep 5
119
120# Add chartmuseum repo as helm repo
121# Credentials should match config defined in
122# oom\kubernetes\platform\components\chartmuseum\values.yaml
123helm repo add k8s-registry http://127.0.0.1:27017 --username "$LOGIN" \
124 --password "$PASSWORD"
125if [ $? -ne 0 ]; then
126 echo "registry cannot be added!!"
127 pkill -f "port-forward service/chart-museum"
128 exit 1
129fi
130
131# Initial scope is pushing only dcae charts
132# can be expanded to include all onap charts if required
Krzysztof Kuzmicki0b06cdd2021-09-30 14:13:39 +0200133for file in $BASEDIR/$PREF*tgz; do
Vijay Venkatesh Kumar8c465172021-06-03 16:51:33 -0400134 # use helm plugin to push charts
135 helm push $file k8s-registry
136 if [ $? -eq 0 ]; then
137 echo "$file uploaded to registry successfully"
138 else
139 echo "registry upload failed!!"
140 pkill -f "port-forward service/chart-museum"
141 helm repo remove k8s-registry
142 exit 1
143 fi
144done
145
146echo "All Helm charts successfully uploaded into internal repository"
147
148# Remove the port-forwarding process
149pkill -f "port-forward service/chart-museum"
150
151# Remove helm registry from local
152helm repo remove k8s-registry