engine: Generate SDF for change
[infra/cicd.git] / jjb / engine / scripts / generate-sdf-for-change.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
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 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 # NOTE (fdegir): we do not need to do anything for the changes coming to infra/engine repo
22 if [[ "$GERRIT_PROJECT" == "infra/engine" ]]; then
23   exit 0
24 fi
25
26 echo "Info : Cloning infra/engine repository"
27 cd "$HOME" && /bin/rm -rf "$WORKSPACE"
28 git clone -q "$ENGINE_REPO_URL" "$WORKSPACE"
29
30 export SDF_ORIGINAL="$WORKSPACE/engine/inventory/group_vars/all/sdf.yaml"
31 export SDF_PATCHSET="/tmp/sdf.yaml.$GERRIT_CHANGE_NUMBER"
32
33 /bin/rm -rf "$SDF_PATCHSET"
34
35 export TOOL_TYPE="${GERRIT_PROJECT//*\//}"
36 export TOOL_MATCH="false"
37 echo "Info  : Tool type is $TOOL_TYPE"
38 echo "Info  : Updating SDF with change SHA and refspec"
39 while IFS= read -r line; do
40   if [[ "$TOOL_MATCH" == "true" && "$line" =~ "version" ]]; then
41     echo "$line" | sed "s#version:.*#version: $GERRIT_PATCHSET_REVISION\n    refspec: $GERRIT_REFSPEC#g" >> "$SDF_PATCHSET"
42     export TOOL_MATCH="false"
43     continue
44   fi
45   if [[ "$line" =~ "$TOOL_TYPE:" && "$TOOL_MATCH" == "false" ]]; then
46     export TOOL_MATCH="true"
47   fi
48   echo "$line" >> "$SDF_PATCHSET"
49 done < "$SDF_ORIGINAL"
50 # NOTE (fdegir): log the diff for possible troubleshooting
51 echo
52 echo "Info  : Diff between original SDF and SDF generated for the change"
53 echo "-------------------------------------------------------------------------"
54 diff "$SDF_ORIGINAL" "$SDF_PATCHSET"
55 echo "-------------------------------------------------------------------------"
56 echo
57
58 # NOTE (fdegir): move SDF generated for the change into the original SDF
59 /bin/mv -f "$SDF_PATCHSET" "$SDF_ORIGINAL"
60
61 # NOTE (fdegir): log the final SDF
62 echo
63 echo "Info  : Generated SDF content"
64 echo "-------------------------------------------------------------------------"
65 cat "$SDF_ORIGINAL"
66 echo "-------------------------------------------------------------------------"
67 echo