9a6cb51c5fca454492ff90cc78ed8ce810b239a4
[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 nounset
12 set -o pipefail
13
14 #----------------------------------------------------------------------
15 # This script is used by CI and executed by Jenkins jobs.
16 # You are not supposed to use this script manually if you don't know
17 # what you are doing.
18 #----------------------------------------------------------------------
19
20 # This function will determine the impacted generic scenario by processing the
21 # change and using diff to see what changed.
22 # It currently sets the scenario to os-nosdn-nofeature.
23 function determine_generic_scenario() {
24     echo "Processing $GERRIT_PROJECT $GERRIT_REFSPEC"
25
26     # set the default scenario
27     DEPLOY_SCENARIO="os-nosdn-nofeature"
28 }
29
30 # This function determines the impacted external scenario by processing the Gerrit
31 # change and using diff to see what changed. If changed files belong to a scenario
32 # its name gets recorded for deploying and testing the right scenario.
33 #
34 # Pattern
35 # <project-repo>/scenarios/<scenario>/<impacted files>: <scenario>
36 function determine_external_scenario() {
37     echo "Processing $GERRIT_PROJECT $GERRIT_REFSPEC"
38
39     # remove the clone that is done via jenkins and place releng-xci there so the
40     # things continue functioning properly
41     cd $HOME && /bin/rm -rf $WORKSPACE
42     git clone -q https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
43
44     # fix the permissions so ssh doesn't complain due to having world-readable keyfiles
45     chmod -R go-rwx $WORKSPACE/xci/scripts/vm
46
47     # clone the project repo and fetch the patchset to process for further processing
48     git clone -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
49     cd $WORK_DIRECTORY/$GERRIT_PROJECT
50     git fetch -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout -q FETCH_HEAD
51
52     # process the diff to find out what scenario(s) are impacted - there should only be 1
53     DEPLOY_SCENARIO=$(git diff HEAD^..HEAD --name-only | grep scenarios | awk -F '[/|/]' '{print $2}' | uniq)
54 }
55
56 echo "Determining the impacted scenario"
57
58 # ensure GERRIT_TOPIC is set
59 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
60
61 # this directory is where the temporary clones and files are created
62 # while extracting the impacted scenario
63 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
64 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
65
66 # skip the healthcheck if the patch doesn't impact the deployment
67 if [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment ]]; then
68     echo "Skipping verify!"
69     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
70     exit 0
71 fi
72
73 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
74     determine_generic_scenario
75 else
76     determine_external_scenario
77 fi
78
79 # ensure single scenario is impacted
80 if [[ $(echo $DEPLOY_SCENARIO | wc -w) != 1 ]]; then
81     echo "Change impacts multiple scenarios!"
82     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
83     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
84     exit 1
85 fi
86
87 # set the installer
88 case $DEPLOY_SCENARIO in
89     os-*)
90         XCI_INSTALLER=osa
91         ;;
92     k8-*)
93         XCI_INSTALLER=kubespray
94         ;;
95     *)
96         echo "Unable to determine the installer. Exiting!"
97         exit 1
98         ;;
99 esac
100
101 # save the installer and scenario names into java properties file
102 # so they can be injected to downstream jobs via envInject
103 echo "Recording the installer '$XCI_INSTALLER' and scenario '$DEPLOY_SCENARIO' for downstream jobs"
104 echo "XCI_INSTALLER=$XCI_INSTALLER" > $WORK_DIRECTORY/scenario.properties
105 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
106
107 # skip the deployment if the scenario is not supported on this distro
108 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
109 if ! sed -n "/^- scenario: $DEPLOY_SCENARIO$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
110     echo "# SKIPPED: Scenario $DEPLOY_SCENARIO is NOT supported on $DISTRO"
111     exit 0
112 fi