5e97b8b43f922c6f9a50a64ac40c21fb2ba0f56f
[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 fi
96
97 if [[ $GERRIT_PROJECT == "releng-xci" ]]; then
98     determine_generic_scenario
99 else
100     determine_external_scenario
101 fi
102
103 # ensure single scenario is impacted
104     if [[ $(IFS=$'\n' echo ${DEPLOY_SCENARIO[@]} | wc -w) != 1 ]]; then
105     echo "Change impacts multiple scenarios!"
106     echo "XCI doesn't support testing of changes that impact multiple scenarios currently."
107     echo "Please split your change into multiple different/dependent changes, each modifying single scenario."
108     exit 1
109 fi
110
111 # set the installer
112 case ${DEPLOY_SCENARIO[0]} in
113     os-*)
114         XCI_INSTALLER=osa
115         ;;
116     k8-*)
117         XCI_INSTALLER=kubespray
118         ;;
119     *)
120         echo "Unable to determine the installer. Exiting!"
121         exit 1
122         ;;
123 esac
124
125 # save the installer and scenario names into java properties file
126 # so they can be injected to downstream jobs via envInject
127 echo "Recording the installer '$XCI_INSTALLER' and scenario '${DEPLOY_SCENARIO[0]}' for downstream jobs"
128 echo "XCI_INSTALLER=$XCI_INSTALLER" > $WORK_DIRECTORY/scenario.properties
129 echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> $WORK_DIRECTORY/scenario.properties
130
131 # skip the deployment if the scenario is not supported on this distro
132 OPNFV_SCENARIO_REQUIREMENTS=$WORKSPACE/xci/opnfv-scenario-requirements.yml
133 if ! sed -n "/^- scenario: ${DEPLOY_SCENARIO[0]}$/,/^$/p" $OPNFV_SCENARIO_REQUIREMENTS | grep -q $DISTRO; then
134     echo "# SKIPPED: Scenario ${DEPLOY_SCENARIO[0]} is NOT supported on $DISTRO"
135     exit 0
136 fi