Merge "fuel, armband: basic healthcheck in verify jobs"
[releng.git] / jjb / xci / xci-set-scenario.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 SUSE and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 set -o pipefail
11
12 #----------------------------------------------------------------------
13 # This script is used by CI and executed by Jenkins jobs.
14 # You are not supposed to use this script manually if you don't know
15 # what you are doing.
16 #----------------------------------------------------------------------
17
18 # This function allows developers to specify the impacted scenario by adding
19 # the info about installer and scenario into the commit message or using
20 # the topic branch names. This results in either skipping the real verification
21 # totally or skipping the determining the installer and scenario programmatically.
22 # It is important to note that this feature is only available to generic scenarios
23 # and only single installer/scenario pair is allowed.
24 # The input in commit message should be placed at the end of the commit message body,
25 # before the signed-off and change-id lines.
26 #
27 # Pattern to be searched in Commit Message
28 #   deploy-scenario:<scenario-name>
29 #   installer-type:<installer-type>
30 # Examples:
31 #   deploy-scenario:os-odl-nofeature
32 #   installer-type:osa
33 #
34 #   deploy-scenario:k8-nosdn-nofeature
35 #   installer-type:kubespray
36 #
37 # Patterns to be searched in topic branch name
38 #   skip-verify
39 #   skip-deployment
40 #   force-verify
41 function override_generic_scenario() {
42     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
43
44     # ensure the metadata we record is consistent for all types of patches including skipped ones
45     # extract releng-xci sha
46     XCI_SHA=$(cd $WORKSPACE && git rev-parse HEAD)
47
48     # extract scenario sha which is same as releng-xci sha for generic scenarios
49     SCENARIO_SHA=$XCI_SHA
50
51     # process topic branch names
52     if [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment|force-verify ]]; then
53         [[ "$GERRIT_TOPIC" =~ force-verify ]] && echo "Forcing CI verification using default scenario and installer!"
54         [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment ]] && echo "Skipping verification!"
55         echo "INSTALLER_TYPE=osa" > $WORK_DIRECTORY/scenario.properties
56         echo "DEPLOY_SCENARIO=os-nosdn-nofeature" >> $WORK_DIRECTORY/scenario.properties
57         echo "XCI_SHA=$XCI_SHA" >> $WORK_DIRECTORY/scenario.properties
58         echo "SCENARIO_SHA=$SCENARIO_SHA" >> $WORK_DIRECTORY/scenario.properties
59         echo "PROJECT_NAME=$GERRIT_PROJECT" >> $WORK_DIRECTORY/scenario.properties
60         exit 0
61     fi
62
63     # process commit message
64     if [[ "$GERRIT_CHANGE_COMMIT_MESSAGE" =~ "installer-type:" && "$GERRIT_CHANGE_COMMIT_MESSAGE" =~ "deploy-scenario:" ]]; then
65         INSTALLER_TYPE=$(echo $GERRIT_CHANGE_COMMIT_MESSAGE | awk '/installer-type:/' RS=" " | cut -d":" -f2)
66         DEPLOY_SCENARIO=$(echo $GERRIT_CHANGE_COMMIT_MESSAGE | awk '/deploy-scenario:/' RS=" " | cut -d":" -f2)
67
68         if [[ -z "$INSTALLER_TYPE" || -z "$DEPLOY_SCENARIO" ]]; then
69             echo "Installer type or deploy scenario is not specified. Falling back to programmatically determining them."
70         else
71             echo "Recording the installer '$INSTALLER_TYPE' and scenario '$DEPLOY_SCENARIO' for downstream jobs"
72             echo "INSTALLER_TYPE=$INSTALLER_TYPE" > $WORK_DIRECTORY/scenario.properties
73             echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
74             echo "XCI_SHA=$XCI_SHA" >> $WORK_DIRECTORY/scenario.properties
75             echo "SCENARIO_SHA=$SCENARIO_SHA" >> $WORK_DIRECTORY/scenario.properties
76             echo "PROJECT_NAME=$GERRIT_PROJECT" >> $WORK_DIRECTORY/scenario.properties
77             exit 0
78         fi
79     else
80         echo "Installer type or deploy scenario is not specified. Falling back to programmatically determining them."
81     fi
82 }
83
84 # This function determines the impacted generic scenario by processing the
85 # change and using diff to see what changed. If changed files belong to a scenario
86 # its name gets recorded for deploying and testing the right scenario.
87 #
88 # Pattern to be searched in Changeset
89 #   releng-xci/scenarios/<scenario>/<impacted files>: <scenario>
90 #   releng-xci/xci/installer/osa/<impacted files>: os-nosdn-nofeature
91 #   releng-xci/xci/installer/kubespray/<impacted files>: k8-nosdn-nofeature
92 #   the rest: os-nosdn-nofeature
93 function determine_generic_scenario() {
94     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
95
96     # get the changeset
97     cd $WORKSPACE
98     SCENARIOS=$(git diff HEAD^..HEAD --name-only -- 'xci/scenarios' | cut -d "/" -f 3 | uniq)
99     # We need to set default scenario for changes that mess with installers
100     INSTALLERS=$(git diff HEAD^..HEAD --name-only -- 'xci/installer' | cut -d "/" -f 3 | uniq)
101     for CHANGED_SCENARIO in $SCENARIOS; do
102         DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]=$CHANGED_SCENARIO
103     done
104     for CHANGED_INSTALLER in $INSTALLERS; do
105         case $CHANGED_INSTALLER in
106             kubespray)
107                 DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='k8-nosdn-nofeature'
108                 ;;
109             # Default case (including OSA changes)
110             *)
111                 DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
112                 ;;
113         esac
114     done
115     # For all other changes, we only need to set a default scenario if it's not set already
116     if git diff HEAD^..HEAD --name-only | grep -q -v 'xci/installer\|xci/scenario'; then
117          [[ ${#DEPLOY_SCENARIO[@]} -eq 0 ]] && DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
118     fi
119
120     # extract releng-xci sha
121     XCI_SHA=$(cd $WORKSPACE && git rev-parse HEAD)
122
123     # extract scenario sha which is same as releng-xci sha for generic scenarios
124     SCENARIO_SHA=$XCI_SHA
125 }
126
127 # This function determines the impacted external scenario by processing the Gerrit
128 # change and using diff to see what changed. If changed files belong to a scenario
129 # its name gets recorded for deploying and testing the right scenario.
130 #
131 # Pattern
132 #   <project-repo>/scenarios/<scenario>/<impacted files>: <scenario>
133 function determine_external_scenario() {
134     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
135
136     # remove the clone that is done via jenkins and place releng-xci there so the
137     # things continue functioning properly
138     cd $HOME && /bin/rm -rf $WORKSPACE
139     git clone -q https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
140
141     # fix the permissions so ssh doesn't complain due to having world-readable keyfiles
142     chmod -R go-rwx $WORKSPACE/xci/scripts/vm
143
144     # clone the project repo and fetch the patchset to process for further processing
145     git clone -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
146     cd $WORK_DIRECTORY/$GERRIT_PROJECT
147     git fetch -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout -q FETCH_HEAD
148
149     # process the diff to find out what scenario(s) are impacted - there should only be 1
150     DEPLOY_SCENARIO+=$(git diff HEAD^..HEAD --name-only | grep scenarios | awk -F '[/|/]' '{print $2}' | uniq)
151
152     # extract releng-xci sha
153     XCI_SHA=$(cd $WORKSPACE && git rev-parse HEAD)
154
155     # extract scenario sha
156     SCENARIO_SHA=$(cd $WORK_DIRECTORY/$GERRIT_PROJECT && git rev-parse HEAD)
157 }
158
159 echo "Determining the impacted scenario"
160
161 declare -a DEPLOY_SCENARIO
162
163 # ensure GERRIT_TOPIC is set
164 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
165
166 # this directory is where the temporary clones and files are created
167 # while extracting the impacted scenario
168 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
169 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
170
171 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
172     override_generic_scenario
173     determine_generic_scenario
174 else
175     determine_external_scenario
176 fi
177
178 # ensure single scenario is impacted
179     if [[ $(IFS=$'\n' echo ${DEPLOY_SCENARIO[@]} | wc -w) != 1 ]]; then
180     echo "Change impacts multiple scenarios!"
181     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
182     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
183     exit 1
184 fi
185
186 # set the installer
187 case ${DEPLOY_SCENARIO[0]} in
188     os-*)
189         INSTALLER_TYPE=osa
190         ;;
191     k8-*)
192         INSTALLER_TYPE=kubespray
193         ;;
194     *)
195         echo "Unable to determine the installer. Exiting!"
196         exit 1
197         ;;
198 esac
199
200 # save the installer and scenario names into java properties file
201 # so they can be injected to downstream jobs via envInject
202 echo "Recording the installer '$INSTALLER_TYPE' and scenario '${DEPLOY_SCENARIO[0]}' and SHAs for downstream jobs"
203 echo "INSTALLER_TYPE=$INSTALLER_TYPE" > $WORK_DIRECTORY/scenario.properties
204 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
205 echo "XCI_SHA=$XCI_SHA" >> $WORK_DIRECTORY/scenario.properties
206 echo "SCENARIO_SHA=$SCENARIO_SHA" >> $WORK_DIRECTORY/scenario.properties
207 echo "PROJECT_NAME=$GERRIT_PROJECT" >> $WORK_DIRECTORY/scenario.properties
208
209 # skip scenario support check if the job is promotion job
210 if [[ "$JOB_NAME" =~ (os|k8) ]]; then
211     exit 0
212 fi
213
214 # skip the deployment if the scenario is not supported on this distro
215 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
216 if ! sed -n "/^- scenario: ${DEPLOY_SCENARIO[0]}$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
217     echo "# SKIPPED: Scenario ${DEPLOY_SCENARIO[0]} is NOT supported on $DISTRO"
218     exit 0
219 fi