Update script to generate spec per branch
[oom/utils.git] / external-schema-repo-generator / generator / install.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 spec_configmap_filename=$1
24 k8s_configmap_name=$2
25 specs_directory=$3
26 generation_directory=$4
27
28 # Constants
29 MAX_CONFIG_MAP_UPDATE_SIZE=262144 # If ConfigMaps is bigger then this value (in Bytes) it can not be updated
30 MAX_SPEC_SIZE=1048576 # 1MB
31
32 # Alias
33 alias kubectl_onap="kubectl -n onap"
34
35 # Checks whether ConfigMap exists
36 # When file does not exist exits with return code 1
37 # $1 - name of spec_configmap_filename
38 check_if_spec_exists() {
39   check_arguments $# $EXPECTED_1_ARG
40   spec_filename="$1"
41   if [ ! -f "$spec_filename" ]; then
42     echo "Spec file $spec_filename does not exist."
43     # todo add location of spec with filename
44     exit 1
45   fi
46 }
47
48 # If spec file is to big to be apply it needs to be created
49 # If ConfigMap with same name exists, iot needs do be destroyed
50 # $1 - name of spec file
51 create_config_map() {
52   echo "ConfigMap spec file is too long for 'kubectl apply'. Actual spec length: $spec_size, max spec length: $MAX_CONFIG_MAP_UPDATE_SIZE"
53   echo "Creating new ConfigMap $k8s_configmap_name"
54   kubectl_onap replace --force -f "$spec_filename"
55 }
56
57 # Install ConfigMap from spec
58 # $1 - name of spec file
59 # $2 - size of spec file
60 install_config_map() {
61   check_arguments $# $EXPECTED_2_ARGS
62   spec_filename="$1"
63   spec_size="$2"
64   if [ "$spec_size" -le $MAX_CONFIG_MAP_UPDATE_SIZE ]; then
65     echo "Applying ConfigMap $k8s_configmap_name"
66     kubectl_onap apply -f "$spec_filename"
67   else
68     create_config_map
69   fi
70 }
71
72 # Uploads ConfigMap spec to Kubernetes
73 # $1 - name of spec_configmap_filename
74 upload_config_map() {
75   check_arguments $# $EXPECTED_1_ARG
76   spec_filename="$1"
77   spec_size=$(stat --printf="%s" "$spec_filename")
78   if [ "$spec_size" -le "$MAX_SPEC_SIZE" ]; then
79     install_config_map $spec_filename $spec_size
80   else
81       echo "WARNING!!!!"
82       echo "  Config file is to big to be installed"
83       echo "  Config file size is: $spec_size Bytes"
84       echo "  Max size is: $MAX_SPEC_SIZE Bytes"
85   fi
86 }
87
88 # install all specs located in generated specs directory
89 # $1 - branch name
90 install_all_spec_in_directory() {
91   FILES="./*"
92   for f in $FILES
93   do
94     echo "installing $f"
95     check_if_spec_exists $f
96     upload_config_map $f
97   done
98 }
99
100 # Moving to directory containing specs
101 move_to_specs_directory() {
102   target_directory="$generation_directory/$specs_directory"
103   echo "Moving to directory containing specs: $target_directory"
104   cd ./"$target_directory"
105 }
106
107 main() {
108   check_arguments $arguments_number $EXPECTED_4_ARGS
109   move_to_specs_directory
110   install_all_spec_in_directory
111   move_to_starting_directory
112 }
113
114 main