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