Update script to generate spec per branch
[oom/utils.git] / external-schema-repo-generator / generator / generate.sh
1 #!/bin/sh
2
3 # ============LICENSE_START=======================================================
4 # OOM
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=========================================================
18
19 . ./utils.sh
20
21 # Arguments renaming
22 arguments_number=$#
23 repo_url=$1
24 branches=$2
25 schemas_location=$3
26 vendor=$4
27 configmap_filename=$5
28 configmap_name=$6
29 snippet_filename=$7
30 specs_directory=$8
31 generation_directory=$9
32
33 # Constants
34 SCHEMA_MAP_FILENAME="schema-map.json"
35 SCHEMA_MAP_NAME="schema-map"
36 SUCCESS_CODE=0
37 TREE=tree
38 INDENTATION_LEVEL_1=1
39 INDENTATION_LEVEL_2=2
40 INDENTATION_LEVEL_3=3
41 INDENTATION_LEVEL_4=4
42 INDENTATION_LEVEL_5=5
43
44 # Variables
45 tmp_location=$(mktemp -d)
46 valid_branches=""
47
48 # Create and move to directory for storing generated files
49 move_to_generation_directory() {
50   mkdir "$generation_directory"
51   cd ./"$generation_directory"
52 }
53
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
58 indent_string() {
59   indent_size=2
60   indent_string=1
61   if [ -n "$1" ]; then indent_string=$1; fi
62   pr -to $(expr "$indent_string" \* "$indent_size")
63 }
64
65 # Clones all branches selected in $BRANCH from $repo_url
66 clone_repo() {
67   for actual_branch in $branches; do
68     clone_branch "$actual_branch"
69   done
70 }
71
72 # Clones single branch $1 from $repo_url.
73 # $1 - branch name
74 clone_branch() {
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."
80   else
81     echo "Cloning repository from branch $1"
82     git clone --quiet --single-branch --branch "$1" "$repo_url" "$tmp_location/$1" 2>/dev/null
83     result=$?
84     if [ $result -ne $SUCCESS_CODE ] ; then
85       echo "Problem with cloning branch $1."
86       echo "Branch $1 will not be added to spec."
87     else
88       valid_branches="${valid_branches} $1"
89     fi
90   fi
91 }
92
93 # Creates file with name $configmap_filename
94 # Inserts ConfigMap metadata and sets name as $configmap_name
95 # $1 - branch 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"
101 apiVersion: v1
102 kind: ConfigMap
103 metadata:
104   name: $branch_configmap_name
105   labels:
106     name: $branch_configmap_name
107   namespace: onap
108 data:
109 EOF
110 }
111
112 # For each selected branch:
113 #   clones the branch from repository,
114 #   adds schemas from branch to ConfigMap spec
115 # $1 - branch name
116 add_schemas() {
117   echo "Adding schemas from branch $1 to spec"
118   add_schemas_from_branch "$1"
119 }
120
121 # Adds schemas from single branch to spec
122 # $1 - branch name
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
130   done
131 } >> "$branch_configmap_filename"
132
133 move_to_spec_directory() {
134   mkdir "$specs_directory"
135   cd ./"$specs_directory"
136 }
137
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"
145
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
149   done
150
151   truncate -s-2 "$schema_map_filename"
152   echo "" >> "$schema_map_filename"
153   echo "]" | indent_string $INDENTATION_LEVEL_2 >> "$schema_map_filename"
154 }
155
156 # Adds mappings from single branch directly to spec
157 # $1 - branch name
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}' )
163
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
170
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"
175   done
176 }
177
178 # Create snippet file to describe how to connect mount ConfigMaps in VES
179 create_snippet() {
180   echo "Generating snippets in file: $snippet_filename"
181   generate_entries
182   base_mounts_path="/opt/app/VESCollector/etc/externalRepo"
183   base_mounts_name="external-repo-$vendor-schemas"
184   mounts_paths="
185         - mountPath: $base_mounts_path/schema-map
186           name: $base_mounts_name"
187
188   config_maps="
189       - configMap:
190           defaultMode: 420
191           name: $configmap_name-$SCHEMA_MAP_NAME
192         name: $base_mounts_name"
193
194   for actual_branch in $valid_branches; do
195
196     actual_branch_name=$( echo "$actual_branch" | tr '[:upper:]' '[:lower:]' )
197
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"
203
204     config_maps="$config_maps
205       - configMap:
206           defaultMode: 420
207           name: $configmap_name-$actual_branch_name
208         name: $base_mounts_name-$actual_branch_name"
209   done
210
211
212   cat << EOF > "$snippet_filename"
213 Snippets for mounting ConfigMap in DCAE VESCollector Deployment
214 =========================================================================
215
216 ## Description
217 These snippets will override existing in VESCollector schemas and mapping file.
218
219 No extra configuration in VESCollector is needed with these snippets.
220
221 ## Snippets
222 #### spec.template.spec.containers[0].volumeMounts
223 \`\`\`$mounts_paths
224 \`\`\`
225
226 #### spec.template.spec.volumes
227 \`\`\`$config_maps
228 \`\`\`
229 EOF
230 }
231
232 generate_entries() {
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-)
238
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"
242     done
243   done
244   schemas_entries=$(echo "$schemas_entries" | indent_string $INDENTATION_LEVEL_5)
245 }
246
247 # Generate specs for branch
248 # $1 - branch name
249 generate_specs_for_branch() {
250   check_arguments $# $EXPECTED_1_ARG
251   add_config_map_metadata $1
252   add_schemas $1
253 }
254
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"
259 }
260
261 # Generate specs for all releases and for schema map
262 generate_specs() {
263   move_to_spec_directory
264   for actual_branch in $valid_branches; do
265     generate_specs_for_branch $actual_branch
266   done
267   generate_specs_for_schema_map
268   cd ..
269 }
270
271
272 # todo add check of global env whether script should be ran
273 main() {
274   check_arguments $arguments_number $EXPECTED_9_ARGS
275   move_to_generation_directory
276   clone_repo
277   generate_specs
278   create_snippet
279   move_to_starting_directory
280 }
281
282 main