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