978ab61f4060fdd018ef76e930e49c5ccea7d472
[infra/cicd.git] / jjb / cloud-infra / cloud-infra-scenario-deploy.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 set -o errexit
22 set -o nounset
23 set -o pipefail
24
25 # this function processes commit message to determine the impacted scenario
26 # using the input provided by the committer.
27 #
28 # installer type and scenario should be placed at the beginning of lines
29 # separately and the format of the entry in commit message is as below
30 #
31 # installer-type: <installer type>
32 # deploy-scenario: <scenario name>
33 #
34 # example is
35 #
36 # installer-type: kubespray
37 # deploy-scenario: k8-calico-nofeature
38 function override_installer_scenario() {
39   echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC to determine the installer and the scenario"
40   cd $WORKSPACE
41   COMMIT_MESSAGE=$(git show -s --format=%B)
42
43   if [[ "$COMMIT_MESSAGE" =~ "deploy-scenario:" && "$COMMIT_MESSAGE" =~ "installer-type:" ]]; then
44     export INSTALLER_TYPE=$(echo "$COMMIT_MESSAGE" | grep '^installer-type:' | cut -d":" -f2 | sed 's/\s*//g')
45     export DEPLOY_SCENARIO=$(echo "$COMMIT_MESSAGE" | grep '^deploy-scenario:' | cut -d":" -f2 | sed 's/\s*//g')
46     generate_temporary_sdf
47   else
48     echo "Installer type or deploy scenario is not specified."
49     echo "Falling back to programmatically determining them."
50     determine_scenario
51   fi
52 }
53
54 # this function will process the changed files to determine the impacted scenario
55 # if it is no specified by the committer. only one scenario per change should be
56 # touched.
57 # TODO: this is not implented yet.
58 function determine_scenario() {
59   echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
60   cd $WORKSPACE
61   COMMIT_MESSAGE=$(git show -s --format=%B)
62   echo "Not implemented!"
63   exit 1
64 }
65
66 # this function generates temporary sdf file with the details of the impacted
67 # scenario.
68 function generate_temporary_sdf() {
69 echo "Generating temporary SDF to use for verification"
70   cat << EOF > $WORKSPACE/sdf.yml
71 scenario:
72   $DEPLOY_SCENARIO:
73     scm: git
74     src: $PROJECT_GIT_URL
75     version: $GERRIT_PATCHSET_REVISION
76     refspec: $GERRIT_REFSPEC
77     installers:
78       $INSTALLER_TYPE:
79         role: scenarios/$DEPLOY_SCENARIO/$INSTALLER_TYPE/role/$DEPLOY_SCENARIO
80         distros:
81           - ubuntu
82 EOF
83 }
84
85 echo "Determining the impacted scenario"
86
87 declare -a DEPLOY_SCENARIO
88
89 override_installer_scenario
90 generate_temporary_sdf
91
92 # determine PDF
93 if [[ $DEPLOY_TYPE == "baremetal" ]]; then
94   PDF="https://gerrit.nordix.org/gitweb?p=infra/hwconfig.git;a=blob_plain;f=pods/${NODE_NAME}-pdf.yml"
95   IDF="https://gerrit.nordix.org/gitweb?p=infra/hwconfig.git;a=blob_plain;f=pods/${NODE_NAME}-idf.yml"
96 else
97   PDF="https://gerrit.nordix.org/gitweb?p=infra/hwconfig.git;a=blob_plain;f=pods/nordix-vpod1-pdf.yml"
98   IDF="https://gerrit.nordix.org/gitweb?p=infra/hwconfig.git;a=blob_plain;f=pods/nordix-vpod1-idf.yml"
99 fi
100
101 # clone infra/engine repository
102 echo "Deploying scenario $DEPLOY_SCENARIO using $INSTALLER_TYPE"
103 cd $WORKSPACE && git clone -q $ENGINE_REPO_URL
104 cd engine
105 ./engine/deploy.sh -d kubespray -s $DEPLOY_SCENARIO -p $PDF -i $IDF -b $WORKSPACE/sdf.yml -c
106
107 # vim: set ts=2 sw=2 expandtab: