bc78101e489891d1d952eacec6a4dfaf7606a0c9
[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 determines the impacted generic scenario by processing the
20 # change and using diff to see what changed. If changed files belong to a scenario
21 # its name gets recorded for deploying and testing the right scenario.
22 #
23 # Pattern
24 # releng-xci/scenarios/<scenario>/<impacted files>: <scenario>
25 # releng-xci/xci/installer/osa/<impacted files>: os-nosdn-nofeature
26 # releng-xci/xci/installer/kubespray/<impacted files>: k8-nosdn-nofeature
27 # the rest: os-nosdn-nofeature
28 function determine_generic_scenario() {
29     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
30
31     # get the changeset
32     cd $WORKSPACE
33     CHANGESET=$(git diff HEAD^..HEAD --name-only)
34     for CHANGED_FILE in $CHANGESET; do
35         case $CHANGED_FILE in
36             *k8-nosdn*|*kubespray*)
37                 [[ ${DEPLOY_SCENARIO[@]} =~ "k8-nosdn-nofeature" ]] || DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='k8-nosdn-nofeature'
38                 ;;
39             *os-odl*)
40                 [[ ${DEPLOY_SCENARIO[@]} =~ "os-odl-nofeature" ]] || DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-odl-nofeature'
41                 ;;
42             *os-nosdn*|*osa*)
43                 [[ ${DEPLOY_SCENARIO[@]} =~ "os-nosdn-nofeature" ]] || DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
44                 ;;
45             *)
46                 [[ ${DEPLOY_SCENARIO[@]} =~ "os-nosdn-nofeature" ]] || DEPLOY_SCENARIO[${#DEPLOY_SCENARIO[@]}]='os-nosdn-nofeature'
47                 ;;
48             esac
49     done
50 }
51
52 # This function determines the impacted external scenario by processing the Gerrit
53 # change and using diff to see what changed. If changed files belong to a scenario
54 # its name gets recorded for deploying and testing the right scenario.
55 #
56 # Pattern
57 # <project-repo>/scenarios/<scenario>/<impacted files>: <scenario>
58 function determine_external_scenario() {
59     echo "Processing $GERRIT_PROJECT patchset $GERRIT_REFSPEC"
60
61     # remove the clone that is done via jenkins and place releng-xci there so the
62     # things continue functioning properly
63     cd $HOME && /bin/rm -rf $WORKSPACE
64     git clone -q https://gerrit.opnfv.org/gerrit/releng-xci $WORKSPACE && cd $WORKSPACE
65
66     # fix the permissions so ssh doesn't complain due to having world-readable keyfiles
67     chmod -R go-rwx $WORKSPACE/xci/scripts/vm
68
69     # clone the project repo and fetch the patchset to process for further processing
70     git clone -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $WORK_DIRECTORY/$GERRIT_PROJECT
71     cd $WORK_DIRECTORY/$GERRIT_PROJECT
72     git fetch -q https://gerrit.opnfv.org/gerrit/$GERRIT_PROJECT $GERRIT_REFSPEC && git checkout -q FETCH_HEAD
73
74     # process the diff to find out what scenario(s) are impacted - there should only be 1
75     DEPLOY_SCENARIO+=$(git diff HEAD^..HEAD --name-only | grep scenarios | awk -F '[/|/]' '{print $2}' | uniq)
76 }
77
78 echo "Determining the impacted scenario"
79
80 declare -a DEPLOY_SCENARIO
81
82 # ensure GERRIT_TOPIC is set
83 GERRIT_TOPIC="${GERRIT_TOPIC:-''}"
84
85 # this directory is where the temporary clones and files are created
86 # while extracting the impacted scenario
87 WORK_DIRECTORY=/tmp/$GERRIT_CHANGE_NUMBER/$DISTRO
88 /bin/rm -rf $WORK_DIRECTORY && mkdir -p $WORK_DIRECTORY
89
90 # skip the healthcheck if the patch doesn't impact the deployment
91 if [[ "$GERRIT_TOPIC" =~ skip-verify|skip-deployment ]]; then
92     echo "Skipping verify!"
93     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" > $WORK_DIRECTORY/scenario.properties
94     exit 0
95 elif [[ "$GERRIT_TOPIC" =~ 'force-verify' ]]; then
96     # Run the deployment with default installer and scenario when multiple things change
97     # and we want to force that.
98     echo "Forcing CI verification of default scenario and installer!"
99     echo "INSTALLER_TYPE=osa" > $WORK_DIRECTORY/scenario.properties
100     echo "DEPLOY_SCENARIO=os-nosdn-nofeature" >> $WORK_DIRECTORY/scenario.properties
101     exit 0
102 fi
103
104 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
105     determine_generic_scenario
106 else
107     determine_external_scenario
108 fi
109
110 # ensure single scenario is impacted
111     if [[ $(IFS=$'\n' echo ${DEPLOY_SCENARIO[@]} | wc -w) != 1 ]]; then
112     echo "Change impacts multiple scenarios!"
113     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
114     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
115     exit 1
116 fi
117
118 # set the installer
119 case ${DEPLOY_SCENARIO[0]} in
120     os-*)
121         INSTALLER_TYPE=osa
122         ;;
123     k8-*)
124         INSTALLER_TYPE=kubespray
125         ;;
126     *)
127         echo "Unable to determine the installer. Exiting!"
128         exit 1
129         ;;
130 esac
131
132 # save the installer and scenario names into java properties file
133 # so they can be injected to downstream jobs via envInject
134 echo "Recording the installer '$INSTALLER_TYPE' and scenario '${DEPLOY_SCENARIO[0]}' for downstream jobs"
135 echo "INSTALLER_TYPE=$INSTALLER_TYPE" > $WORK_DIRECTORY/scenario.properties
136 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
137
138 # skip the deployment if the scenario is not supported on this distro
139 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
140 if ! sed -n "/^- scenario: ${DEPLOY_SCENARIO[0]}$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
141     echo "# SKIPPED: Scenario ${DEPLOY_SCENARIO[0]} is NOT supported on $DISTRO"
142     exit 0
143 fi