jjb: xci: Only set default scenario if no scenario is set
[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 errexit
11 set -o pipefail
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_generic_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         exit 0
62     fi
63
64     # process commit message
65     if [[ "$GERRIT_CHANGE_COMMIT_MESSAGE" =~ "installer-type:" && "$GERRIT_CHANGE_COMMIT_MESSAGE" =~ "deploy-scenario:" ]]; then
66         INSTALLER_TYPE=$(echo $GERRIT_CHANGE_COMMIT_MESSAGE | awk '/installer-type:/' RS=" " | cut -d":" -f2)
67         DEPLOY_SCENARIO=$(echo $GERRIT_CHANGE_COMMIT_MESSAGE | awk '/deploy-scenario:/' RS=" " | cut -d":" -f2)
68
69         if [[ -z "$INSTALLER_TYPE" || -z "$DEPLOY_SCENARIO" ]]; then
70             echo "Installer type or deploy scenario is not specified. Falling back to programmatically determining them."
71         else
72             echo "Recording the installer '$INSTALLER_TYPE' and scenario '$DEPLOY_SCENARIO' for downstream jobs"
73             echo "INSTALLER_TYPE=$INSTALLER_TYPE" > $WORK_DIRECTORY/scenario.properties
74             echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
75             echo "XCI_SHA=$XCI_SHA" >> $WORK_DIRECTORY/scenario.properties
76             echo "SCENARIO_SHA=$SCENARIO_SHA" >> $WORK_DIRECTORY/scenario.properties
77             echo "PROJECT_NAME=$GERRIT_PROJECT" >> $WORK_DIRECTORY/scenario.properties
78             exit 0
79         fi
80     else
81         echo "Installer type or deploy scenario is not specified. Falling back to programmatically determining them."
82     fi
83 }
84
85 # This function determines the impacted generic scenario by processing the
86 # change and using diff to see what changed. If changed files belong to a scenario
87 # its name gets recorded for deploying and testing the right scenario.
88 #
89 # Pattern to be searched in Changeset
90 #   releng-xci/scenarios/<scenario>/<impacted files>: <scenario>
91 #   releng-xci/xci/installer/osa/<impacted files>: os-nosdn-nofeature
92 #   releng-xci/xci/installer/kubespray/<impacted files>: k8-nosdn-nofeature
93 #   the rest: os-nosdn-nofeature
94 function determine_generic_scenario() {
95     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
96
97     # get the changeset
98     cd $WORKSPACE
99     SCENARIOS=$(git diff HEAD^..HEAD --name-only -- 'xci/scenarios' | cut -d "/" -f 3 | uniq)
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_SCENARIO in $SCENARIOS; do
103         DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]=$CHANGED_SCENARIO
104     done
105     for CHANGED_INSTALLER in $INSTALLERS; do
106         case $CHANGED_INSTALLER in
107             kubespray)
108                 DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='k8-nosdn-nofeature'
109                 ;;
110             # Default case (including OSA changes)
111             *)
112                 DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
113                 ;;
114         esac
115     done
116     # For all other changes, we only need to set a default scenario if it's not set already
117     if git diff HEAD^..HEAD --name-only | grep -q -v 'xci/installer\|xci/scenario'; then
118          [[ ${#DEPLOY_SCENARIO[@]} -gt 0 ]] && DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
119     fi
120
121     # extract releng-xci sha
122     XCI_SHA=$(cd $WORKSPACE && git rev-parse HEAD)
123
124     # extract scenario sha which is same as releng-xci sha for generic scenarios
125     SCENARIO_SHA=$XCI_SHA
126 }
127
128 # This function determines the impacted external scenario by processing the Gerrit
129 # change and using diff to see what changed. If changed files belong to a scenario
130 # its name gets recorded for deploying and testing the right scenario.
131 #
132 # Pattern
133 #   <project-repo>/scenarios/<scenario>/<impacted files>: <scenario>
134 function determine_external_scenario() {
135     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
136
137     # remove the clone that is done via jenkins and place releng-xci there so the
138     # things continue functioning properly
139     cd $HOME && /bin/rm -rf $WORKSPACE
140     git clone -q https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
141
142     # fix the permissions so ssh doesn't complain due to having world-readable keyfiles
143     chmod -R go-rwx $WORKSPACE/xci/scripts/vm
144
145     # clone the project repo and fetch the patchset to process for further processing
146     git clone -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
147     cd $WORK_DIRECTORY/$GERRIT_PROJECT
148     git fetch -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout -q FETCH_HEAD
149
150     # process the diff to find out what scenario(s) are impacted - there should only be 1
151     DEPLOY_SCENARIO+=$(git diff HEAD^..HEAD --name-only | grep scenarios | awk -F '[/|/]' '{print $2}' | uniq)
152
153     # extract releng-xci sha
154     XCI_SHA=$(cd $WORKSPACE && git rev-parse HEAD)
155
156     # extract scenario sha
157     SCENARIO_SHA=$(cd $WORK_DIRECTORY/$GERRIT_PROJECT && git rev-parse HEAD)
158 }
159
160 echo "Determining the impacted scenario"
161
162 declare -a DEPLOY_SCENARIO
163
164 # ensure GERRIT_TOPIC is set
165 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
166
167 # this directory is where the temporary clones and files are created
168 # while extracting the impacted scenario
169 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
170 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
171
172 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
173     override_generic_scenario
174     determine_generic_scenario
175 else
176     determine_external_scenario
177 fi
178
179 # ensure single scenario is impacted
180     if [[ $(IFS=$'\n' echo ${DEPLOY_SCENARIO[@]} | wc -w) != 1 ]]; then
181     echo "Change impacts multiple scenarios!"
182     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
183     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
184     exit 1
185 fi
186
187 # set the installer
188 case ${DEPLOY_SCENARIO[0]} in
189     os-*)
190         INSTALLER_TYPE=osa
191         ;;
192     k8-*)
193         INSTALLER_TYPE=kubespray
194         ;;
195     *)
196         echo "Unable to determine the installer. Exiting!"
197         exit 1
198         ;;
199 esac
200
201 # save the installer and scenario names into java properties file
202 # so they can be injected to downstream jobs via envInject
203 echo "Recording the installer '$INSTALLER_TYPE' and scenario '${DEPLOY_SCENARIO[0]}' and SHAs for downstream jobs"
204 echo "INSTALLER_TYPE=$INSTALLER_TYPE" > $WORK_DIRECTORY/scenario.properties
205 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
206 echo "XCI_SHA=$XCI_SHA" >> $WORK_DIRECTORY/scenario.properties
207 echo "SCENARIO_SHA=$SCENARIO_SHA" >> $WORK_DIRECTORY/scenario.properties
208 echo "PROJECT_NAME=$GERRIT_PROJECT" >> $WORK_DIRECTORY/scenario.properties
209
210 # skip scenario support check if the job is promotion job
211 if [[ "$JOB_NAME" =~ (os|k8) ]]; then
212     exit 0
213 fi
214
215 # skip the deployment if the scenario is not supported on this distro
216 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
217 if ! sed -n "/^- scenario: ${DEPLOY_SCENARIO[0]}$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
218     echo "# SKIPPED: Scenario ${DEPLOY_SCENARIO[0]} is NOT supported on $DISTRO"
219     exit 0
220 fi