3 # ============LICENSE_START=======================================================
5 # ================================================================================
6 # Copyright (C) 2020-2021 Nokia. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
31 generation_directory=$9
34 SCHEMA_MAP_FILENAME="schema-map.json"
35 SCHEMA_MAP_NAME="schema-map"
45 tmp_location=$(mktemp -d)
48 # Create and move to directory for storing generated files
49 move_to_generation_directory() {
50 mkdir "$generation_directory"
51 cd ./"$generation_directory"
54 # Indents each line of string by adding indent_size*indent_string spaces on the beginning
55 # Optional argument is indent_string level, default: 1
56 # correct usage example:
57 # echo "Sample Text" | indent_string 2
61 if [ -n "$1" ]; then indent_string=$1; fi
62 pr -to $(expr "$indent_string" \* "$indent_size")
65 # Clones all branches selected in $BRANCH from $repo_url
67 for actual_branch in $branches; do
68 clone_branch "$actual_branch"
72 # Clones single branch $1 from $repo_url.
75 check_arguments $# $EXPECTED_1_ARG
76 if [ -d $tmp_location/"$1" ]; then
77 echo "Skipping cloning repository."
78 echo "Branch $1 has already been cloned in the directory ./$tmp_location/$1"
79 echo "To redownload branch remove ./$tmp_location/$1."
81 echo "Cloning repository from branch $1"
82 git clone --quiet --single-branch --branch "$1" "$repo_url" "$tmp_location/$1" 2>/dev/null
84 if [ $result -ne $SUCCESS_CODE ] ; then
85 echo "Problem with cloning branch $1."
86 echo "Branch $1 will not be added to spec."
88 valid_branches="${valid_branches} $1"
93 # Creates file with name $configmap_filename
94 # Inserts ConfigMap metadata and sets name as $configmap_name
96 add_config_map_metadata() {
97 branch_configmap_filename="${configmap_filename}-$1.yaml"
98 branch_configmap_name=$( echo "$configmap_name-$1" | tr '[:upper:]' '[:lower:]' )
99 echo "Creating ConfigMap spec file: $branch_configmap_filename"
100 cat << EOF > "$branch_configmap_filename"
104 name: $branch_configmap_name
106 name: $branch_configmap_name
112 # For each selected branch:
113 # clones the branch from repository,
114 # adds schemas from branch to ConfigMap spec
117 echo "Adding schemas from branch $1 to spec"
118 add_schemas_from_branch "$1"
121 # Adds schemas from single branch to spec
123 add_schemas_from_branch() {
124 branch_configmap_filename="${configmap_filename}-$1.yaml"
125 check_arguments $# $EXPECTED_1_ARG
126 schemas=$(ls -g $tmp_location/$1/$schemas_location/*.yaml | awk '{print $NF}')
127 for schema in $schemas; do
128 echo "$1-$(basename $schema): |-" | indent_string $INDENTATION_LEVEL_1
129 cat "$schema" | indent_string $INDENTATION_LEVEL_2
131 } >> "$branch_configmap_filename"
133 move_to_spec_directory() {
134 mkdir "$specs_directory"
135 cd ./"$specs_directory"
138 # Generates mapping file for collected schemas directly in spec
139 # $1 - schema map name
140 generate_mapping_file() {
141 schema_map_filename="${configmap_filename}-$1.yaml"
142 echo "Generating mapping file in spec"
143 echo "$SCHEMA_MAP_FILENAME"": |-" | indent_string $INDENTATION_LEVEL_1 >> "$schema_map_filename"
144 echo "[" | indent_string $INDENTATION_LEVEL_2 >> "$schema_map_filename"
146 for actual_branch in $valid_branches; do
147 echo "Adding mappings from branch: $actual_branch"
148 add_mappings_from_branch $actual_branch $schema_map_filename
151 truncate -s-2 "$schema_map_filename"
152 echo "" >> "$schema_map_filename"
153 echo "]" | indent_string $INDENTATION_LEVEL_2 >> "$schema_map_filename"
156 # Adds mappings from single branch directly to spec
158 # $2 - schema map file name
159 add_mappings_from_branch() {
160 check_arguments $# $EXPECTED_2_ARGS
161 schema_map_filename="$2"
162 schemas=$(ls -g $tmp_location/$1/$schemas_location/*.yaml | awk '{print $NF}' )
164 for schema in $schemas; do
165 repo_endpoint=$(echo "$repo_url" | cut -d/ -f4- | rev | cut -d. -f2- | rev)
166 schema_repo_path=$(echo "$schema" | cut -d/ -f4-)
167 public_url_schemas_location=${repo_url%.*}
168 public_url=$public_url_schemas_location/$TREE/$schema_repo_path
169 local_url=$vendor/$repo_endpoint/$TREE/$schema_repo_path
171 echo "{" | indent_string $INDENTATION_LEVEL_3 >> "$schema_map_filename"
172 echo "\"publicURL\": \"$public_url\"," | indent_string $INDENTATION_LEVEL_4 >> "$schema_map_filename"
173 echo "\"localURL\": \"$local_url\"" | indent_string $INDENTATION_LEVEL_4 >> "$schema_map_filename"
174 echo "}," | indent_string $INDENTATION_LEVEL_3 >> "$schema_map_filename"
178 # Create snippet file to describe how to connect mount ConfigMaps in VES
180 echo "Generating snippets in file: $snippet_filename"
182 base_mounts_path="/opt/app/VESCollector/etc/externalRepo"
183 base_mounts_name="external-repo-$vendor-schemas"
185 - mountPath: $base_mounts_path/schema-map
186 name: $base_mounts_name"
191 name: $configmap_name-$SCHEMA_MAP_NAME
192 name: $base_mounts_name"
194 for actual_branch in $valid_branches; do
196 actual_branch_name=$( echo "$actual_branch" | tr '[:upper:]' '[:lower:]' )
198 repo_endpoint=$(echo "$repo_url" | cut -d/ -f4- | rev | cut -d. -f2- | rev)
199 local_url=$vendor/$repo_endpoint/$TREE/$actual_branch/$schemas_location
200 mounts_paths="$mounts_paths
201 - mountPath: $base_mounts_path/$local_url
202 name: $base_mounts_name-$actual_branch_name"
204 config_maps="$config_maps
207 name: $configmap_name-$actual_branch_name
208 name: $base_mounts_name-$actual_branch_name"
212 cat << EOF > "$snippet_filename"
213 Snippets for mounting ConfigMap in DCAE VESCollector Deployment
214 =========================================================================
217 These snippets will override existing in VESCollector schemas and mapping file.
219 No extra configuration in VESCollector is needed with these snippets.
222 #### spec.template.spec.containers[0].volumeMounts
226 #### spec.template.spec.volumes
233 for actual_branch in $valid_branches; do
234 schemas=$(ls -g $tmp_location/$actual_branch/$schemas_location/*.yaml | awk '{print $NF}')
235 for schema in $schemas; do
236 repo_endpoint=$(echo "$repo_url" | cut -d/ -f4- | rev | cut -d. -f2- | rev)
237 schema_repo_path=$(echo "$schema" | cut -d/ -f4-)
239 key="$actual_branch-$(basename "$schema")"
240 path=$vendor/$repo_endpoint/$TREE/$schema_repo_path
241 schemas_entries="$schemas_entries- key: $key\n path: $path\n"
244 schemas_entries=$(echo "$schemas_entries" | indent_string $INDENTATION_LEVEL_5)
247 # Generate specs for branch
249 generate_specs_for_branch() {
250 check_arguments $# $EXPECTED_1_ARG
251 add_config_map_metadata $1
255 # Generate specs for schema map
256 generate_specs_for_schema_map() {
257 add_config_map_metadata "$SCHEMA_MAP_NAME"
258 generate_mapping_file "$SCHEMA_MAP_NAME"
261 # Generate specs for all releases and for schema map
263 move_to_spec_directory
264 for actual_branch in $valid_branches; do
265 generate_specs_for_branch $actual_branch
267 generate_specs_for_schema_map
272 # todo add check of global env whether script should be ran
274 check_arguments $arguments_number $EXPECTED_9_ARGS
275 move_to_generation_directory
279 move_to_starting_directory