blob: 8b969dc55d662f61020aefa6c9c16d6170e7fff3 [file] [log] [blame]
#!/bin/bash
# ============LICENSE_START=======================================================
# Copyright (C) 2019 The Nordix Foundation. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# ============LICENSE_END=========================================================
set -o nounset
set -o errexit
set -o pipefail
# NOTE (fdegir): in case if a change depends on another open change in different repo
# we determine it here and override the version and refspec for that repo
function determine_dependency() {
echo "Info : Determining dependency"
echo "Info : Commit message is"
echo "-------------------------------------------------------------------------"
echo "$GERRIT_CHANGE_COMMIT_MESSAGE"
echo "-------------------------------------------------------------------------"
if ! echo "$GERRIT_CHANGE_COMMIT_MESSAGE" | grep -qs "^Depends-On:"; then
echo "Info : No dependency found"
return
else
echo "Info : Dependency found!"
DEPENDENT_CHANGE_ID=$(echo "$GERRIT_CHANGE_COMMIT_MESSAGE" | grep "^Depends-On:" | cut -d":" -f2 | sed 's/\s*//g')
fi
echo "Info : Processing change $DEPENDENT_CHANGE_ID"
declare -a DEPENDENT_CHANGE_METADATA
readarray -t DEPENDENT_CHANGE_METADATA < <(curl -s "https://gerrit.nordix.org/changes/?q=$DEPENDENT_CHANGE_ID&o=CURRENT_REVISION" 2>&1 \
| grep "project\|status\|current_revision\|ref\".*," | sed -e 's/\s//g' -e 's/,//g' -e 's/"//g'| cut -d: -f2)
# NOTE (fdegir): check if the dependeny change exists
# and ignore if it doesn't
if [[ -z "${DEPENDENT_CHANGE_METADATA+x}" ]]; then
echo "Warn : Unable to determine dependent change. Ignoring dependency!"
return
fi
DEPENDENT_CHANGE_PROJECT="${DEPENDENT_CHANGE_METADATA[0]}"
DEPENDENT_CHANGE_STATUS="${DEPENDENT_CHANGE_METADATA[1]}"
DEPENDENT_CHANGE_REVISION="${DEPENDENT_CHANGE_METADATA[2]}"
DEPENDENT_CHANGE_REFSPEC="${DEPENDENT_CHANGE_METADATA[3]}"
echo "-------------------------------------------------------------------------"
echo " DEPENDENT_CHANGE_PROJECT=$DEPENDENT_CHANGE_PROJECT"
echo " DEPENDENT_CHANGE_STATUS=$DEPENDENT_CHANGE_STATUS"
echo " DEPENDENT_CHANGE_REVISION=$DEPENDENT_CHANGE_REVISION"
echo " DEPENDENT_CHANGE_REFSPEC=$DEPENDENT_CHANGE_REFSPEC"
echo "-------------------------------------------------------------------------"
# NOTE (fdegir): we only follow the dependency if the change status is NEW
if [[ "$DEPENDENT_CHANGE_STATUS" != "NEW" ]]; then
echo "Warn : Change is not open. Ignoring dependency!"
return
fi
# NOTE (fdegir): we do not accept dependency of a change that is sent to same project as the dependent change
if [[ "$DEPENDENT_CHANGE_PROJECT" == "$GERRIT_PROJECT" ]]; then
echo "Warn : Change is for the same project. Ignoring dependency!"
return
fi
DEPENDENT_COMPONENT="${DEPENDENT_CHANGE_PROJECT//*\//}"
echo "# dependent change" >> "$CHANGE_METADATA_FILE"
# NOTE (fdegir): if dependent change is on stack repo, we need to treat it
# differently
if [[ "$DEPENDENT_CHANGE_PROJECT" =~ /stack/ ]]; then
# set vars
echo "STACK_TYPE=$DEPENDENT_COMPONENT" >> "$CHANGE_METADATA_FILE"
echo "STACK_VERSION=$DEPENDENT_CHANGE_REVISION" >> "$CHANGE_METADATA_FILE"
echo "STACK_REFSPEC=$DEPENDENT_CHANGE_REFSPEC" >> "$CHANGE_METADATA_FILE"
else
echo "NORDIX_$(echo $DEPENDENT_COMPONENT | tr a-z A-Z)_VERSION=$DEPENDENT_CHANGE_REVISION" >> "$CHANGE_METADATA_FILE"
echo "NORDIX_$(echo $DEPENDENT_COMPONENT | tr a-z A-Z)_REFSPEC=$DEPENDENT_CHANGE_REFSPEC" >> "$CHANGE_METADATA_FILE"
fi
}
# file to record change metadata
export CHANGE_METADATA_FILE="$WORKSPACE/change.properties"
/bin/rm -f "$CHANGE_METADATA_FILE"
echo "Info : Generating change metadata"
# determine the project
# this scripts is used by all engine repos so we need to determine what this change is for
export ENGINE_COMPONENT="${GERRIT_PROJECT//*\//}"
# Check if we need to override the deploy scenario.
if ! echo "$GERRIT_CHANGE_COMMIT_MESSAGE" | grep -qs "^Deploy-Scenario:"; then
echo "Info : No DEPLOY_SCENARIO override found."
else
echo "Info : DEPLOY_SCENARIO override found!"
echo "Info : Before override: DEPLOY_SCENARIO=${DEPLOY_SCENARIO}"
DEPLOY_SCENARIO=$(echo "$GERRIT_CHANGE_COMMIT_MESSAGE" | grep '^Deploy-Scenario:' | cut -d ":" -f2 | sed 's/\s*//g')
export DEPLOY_SCENARIO
echo "Info : After override: DEPLOY_SCENARIO=${DEPLOY_SCENARIO}"
fi
echo "Info : Setting STACK_TYPE and STACK_VERSION"
if [[ "$GERRIT_PROJECT" =~ /stack/ ]]; then
# set vars
export STACK_TYPE="$ENGINE_COMPONENT"
export STACK_VERSION="$GERRIT_PATCHSET_REVISION"
export STACK_REFSPEC="$GERRIT_REFSPEC"
# record vars to properties file
echo "STACK_TYPE=$STACK_TYPE" >> "$CHANGE_METADATA_FILE"
echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> "$CHANGE_METADATA_FILE"
echo "STACK_VERSION=$STACK_VERSION" >> "$CHANGE_METADATA_FILE"
echo "STACK_REFSPEC=$STACK_REFSPEC" >> "$CHANGE_METADATA_FILE"
elif [[ "$GERRIT_PROJECT" =~ /installer/ ]]; then
# set vars
case "${ENGINE_COMPONENT}" in
kolla)
export STACK_TYPE="openstack"
echo "Info : Deploy scenario is overriden by job to $DEPLOY_SCENARIO"
;;
kubespray)
export STACK_TYPE="kubernetes"
echo "Info : Deploy scenario is overriden by job to $DEPLOY_SCENARIO"
;;
oom)
export STACK_TYPE="onap"
echo "Info : Deploy scenario is overriden by job to $DEPLOY_SCENARIO"
;;
*) echo "ERROR : Invalid engine component $ENGINE_COMPONENT"; exit 1;;
esac
export STACK_VERSION="$GERRIT_BRANCH"
# record vars to properties file
echo "STACK_TYPE=$STACK_TYPE" >> "$CHANGE_METADATA_FILE"
echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> "$CHANGE_METADATA_FILE"
echo "STACK_VERSION=$STACK_VERSION" >> "$CHANGE_METADATA_FILE"
echo "NORDIX_$(echo $ENGINE_COMPONENT | tr a-z A-Z)_VERSION=$GERRIT_PATCHSET_REVISION" >> "$CHANGE_METADATA_FILE"
echo "NORDIX_$(echo $ENGINE_COMPONENT | tr a-z A-Z)_REFSPEC=$GERRIT_REFSPEC" >> "$CHANGE_METADATA_FILE"
else
# NOTE (fdegir): if the change comes into provisioner repos, we need to set the stack type
# properly for the jobs verifying those changes
# set vars
export STACK_TYPE="${STACK_TYPE:-kubernetes}"
export STACK_VERSION=$GERRIT_BRANCH
# record vars to properties file
echo "STACK_TYPE=$STACK_TYPE" >> "$CHANGE_METADATA_FILE"
echo "DEPLOY_SCENARIO=$DEPLOY_SCENARIO" >> "$CHANGE_METADATA_FILE"
echo "STACK_VERSION=$STACK_VERSION" >> "$CHANGE_METADATA_FILE"
echo "NORDIX_$(echo $ENGINE_COMPONENT | tr a-z A-Z)_VERSION=$GERRIT_PATCHSET_REVISION" >> "$CHANGE_METADATA_FILE"
echo "NORDIX_$(echo $ENGINE_COMPONENT | tr a-z A-Z)_REFSPEC=$GERRIT_REFSPEC" >> "$CHANGE_METADATA_FILE"
fi
# global vars
export NORDIX_ARM_HTTPS_URL="https://artifactory.nordix.org/artifactory"
export ARTIFACT_ARM_FOLDER="gerrit"
export BUILD_IDENTIFIER=$GERRIT_CHANGE_NUMBER
export OFFLINE_INSTALLER_FILE="/tmp/${STACK_TYPE}-${GERRIT_BRANCH}-${DISTRO}.bsx"
export OFFLINE_XTESTING_FILE="/tmp/${STACK_TYPE}-test-${GERRIT_BRANCH}.bsx"
export BUILD_ARTIFACTS="/tmp/${STACK_TYPE}-${GERRIT_BRANCH}-${DISTRO}.bsx"
export NORDIX_ARM_REPO="nordix-${STACK_TYPE}"
export NORDIX_ARTIFACT_URL="$NORDIX_ARM_HTTPS_URL/$NORDIX_ARM_REPO/oss/$ARTIFACT_ARM_FOLDER/$BUILD_IDENTIFIER"
cat << EOF >> "$CHANGE_METADATA_FILE"
BUILD_IDENTIFIER=$BUILD_IDENTIFIER
BUILD_DATE=$(date '+%Y%m%d%H%M')
BUILT_BY=$BUILD_URL
DISTRO=$DISTRO
CI_LOOP=$CI_LOOP
ENGINE_SHA=$(git rev-parse HEAD)
OFFLINE_INSTALLER_FILE=$OFFLINE_INSTALLER_FILE
OFFLINE_XTESTING_FILE=$OFFLINE_XTESTING_FILE
BUILD_ARTIFACTS=$BUILD_ARTIFACTS
NORDIX_ARM_REPO=nordix-${STACK_TYPE}
NORDIX_ARTIFACT_URL=$NORDIX_ARTIFACT_URL
EOF
# NOTE (fdegir): we generated the change metadata file normally until here
# we now determine the dependency as the last step to ensure we override vars
determine_dependency
echo "Info : Change metadata"
echo "-------------------------------------------------------------------------"
cat "$CHANGE_METADATA_FILE"
echo "-------------------------------------------------------------------------"
# vim: set ts=2 sw=2 expandtab: