blob: 417019328b314d0d78b39a93948be2d211f8e3b8 [file] [log] [blame]
#!/bin/bash
#
# This script create temporary yum repositories with updated
# updates in CBS tags according to changed buildsys-tags in
# rdoinfo reviews. Additionally it creates a file cbs_actions.txt
# with the list of required actions in CBS (package registrations
# and tagging) according to the review.
#
set -o pipefail
RDOINFO_LOCATION=$1
WORKDIR=$2
INFO_FILE=$3
if [ -z "$INFO_FILE" ]; then
export INFO_FILE=rdo-full.yml
fi
source "$(dirname $0)/common_functions"
if [ $# -lt 2 ]; then
echo "usage: $(basename $0) RDOINFO_LOCATION WORKING_DIR [INFO_FILE]"
exit 1
fi
FAILED=0
rm -rf $WORKDIR/{repos,info}
mkdir -p $WORKDIR/{repos,info}
echo > $WORKDIR/repos/changed_repos.txt
ACTIONS_FILE=$WORKDIR/info/cbs_actions.txt
# Create empty files in case no changes are detected
touch $WORKDIR/info/{cbs_actions.txt,tags.yaml,register.yaml}
function get_nvr_in_tag()
{
local PKG_NAME=$1
local TAG=$2
rdopkg info -l $RDOINFO_LOCATION -i $INFO_FILE "name:^${PKG_NAME}$"| grep "${TAG}"|awk '{print $2}'
}
function validate_pkg_tag()
{
local NVR=$1
local TAG=$2
local RETCODE=0
# If package is going to be tagged in XXX-release it must be in XXX-testing previously
previous_tag=$(echo $TAG|sed 's/testing$/candidate/;s/release$/testing/')
if [ $(grep -c -w $previous_tag $WORKDIR/info/$NVR.info.txt) -eq 0 ]&&[[ $TAG =~ "release" ]]; then
log "ERROR" "Package $NVR is not assigned to $previous_tag"
RETCODE=1
fi
return $RETCODE
}
function exist_in_cbs(){
local NVR=$1
local INFO_FILE=$WORKDIR/info/$NVR.info.txt
if [ ! -f $INFO_FILE ]; then
cbs buildinfo $NVR > $INFO_FILE 2>&1
if [ $? -ne 0 ]; then
return 1
fi
fi
# cbs buildinfo doesn't return != 0 when package doesn't exist
# so i parse output
local NOBUILD=$(grep -c '^No such build' $INFO_FILE)
if [ $NOBUILD -ne 0 ]; then
log "ERROR" "Package $NVR not found"
return 1
fi
STATE=$(grep ^State $INFO_FILE|awk '{print $2}')
if [ $STATE != "COMPLETE" ]; then
log "ERROR" "Build $NVR failed to build in CBS"
return 1
fi
}
function get_packages(){
local NVR=$1
local TAG=$2
local INFO_FILE=$WORKDIR/info/$NVR.info.txt
if [ ! -f $INFO_FILE ]; then
cbs buildinfo $NVR > $INFO_FILE 2>&1
if [ $? -ne 0 ]; then
return 1
fi
fi
# If package is already in the tag, we don't need to add it to the
# testing repo for that tag.
if [ $(grep -c "^Tags:.*$TAG" $INFO_FILE) -eq 0 ]; then
# Validate package meet required conditions to be added to the tag
validate_pkg_tag $NVR $TAG
if [ $? -ne 0 ]; then
return 1
fi
# If package is not registered in the tag, let's add an action
if [[ $TAG =~ testing ]]; then
CHECK_TAGS="$previous_tag $TAG"
else
CHECK_TAGS=$TAG
fi
for CHECK_TAG in $CHECK_TAGS
do
if [ ! -f $WORKDIR/info/$CHECK_TAG.pkgs.txt ]; then
cbs list-pkgs --tag=$CHECK_TAG > $WORKDIR/info/$CHECK_TAG.pkgs.txt
fi
PKG_NAME=$(echo $NVR|rev|cut -d- -f3-|rev)
grep -q "^$PKG_NAME " $WORKDIR/info/$CHECK_TAG.pkgs.txt
if [[ $? != 0 ]]; then
echo "register,$CHECK_TAG,$PKG_NAME" >> $ACTIONS_FILE
fi
done
# Add action to tag the package
echo "tag,$TAG,$NVR" >> $ACTIONS_FILE
# Get the list of RPMs in the new package and add to the content file
packages=$(grep '^/mnt' $INFO_FILE|awk '{print $1}'|grep -v -f excludes)
for p in $packages
do
package=$(echo $p| sed 's/\/mnt\/koji/http:\/\/cbs.centos.org\/kojifiles/')
echo $package >> $WORKDIR/repos/$TAG.content.txt
log "INFO" "Adding $(basename $package) to $tag"
done
else
log "INFO" "Package $NVR is already in tag $TAG"
fi
}
function create_repo_file(){
# create .repo files which will be used to run weirdo jobs using this
# temporary repositories from logs server.
REPO=$1
job="update-buildsys-tags"
repository="${logs}/repos/${REPO}/"
cat << EOF > temp-$name.repo
[temp-${REPO}]
name=temp-${REPO}
baseurl=$repository
enabled=1
gpgcheck=0
priority=1
module_hotfixes=1
EOF
}
while read line
do
pkg_name=$(echo $line |cut -d " " -f 1)
tags=$(echo $line |cut -d " " -f 2-| tr -d [],\')
for tag in $tags
do
if [ ${tag} = "version-locked" ]; then
continue
fi
# Changes in candidate tags are managed in a separated
# job to build dependencies automatically
PHASE=$(echo $tag|cut -d- -f4-)
if [ "$PHASE" = "candidate" ]; then
continue
fi
log "INFO" "Processing package $pkg_name in tag $tag"
nvr=$(get_nvr_in_tag $pkg_name $tag)
if [ $? -ne 0 ]; then
log "ERROR" "Failed to get nvr for $nvr in $tag"
FAILED=1
break
fi
log "INFO" "Checking if build $nvr exist in CBS"
exist_in_cbs $nvr
if [ $? -ne 0 ]; then
log "ERROR" "Package $nvr does not exist in CBS"
FAILED=1
break
fi
log "INFO" "Getting packages for $nvr in $tag"
get_packages $nvr $tag
if [ $? -ne 0 ]; then
log "ERROR" "Failed to process $nvr for $tag"
FAILED=1
break
fi
done
done <<< "$(rdopkg info-tags-diff $RDOINFO_LOCATION $INFO_FILE -b)"
# If no repos have been actually modified don't try
# to create them
if [ "$(ls $WORKDIR/repos/*.content.txt 2>/dev/null)" ]; then
for r in $(ls $WORKDIR/repos/*.content.txt)
do
name=$(basename -s .content.txt $r)
prefix=$(echo $name|cut -d '-' -f 1-3)
el_version=$(echo $name|cut -c 6)
for dir in ${prefix}-testing ${prefix}-release ${prefix}-el${el_version}-build ${prefix}-el${el_version}s-build
do
if [ ! -d $WORKDIR/repos/$dir ]; then
mkdir $WORKDIR/repos/$dir
fi
done
echo $name >> $WORKDIR/repos/changed_repos.txt
log "INFO" "Creating repo $name"
pushd $WORKDIR/repos/$name
wget -i $r
createrepo -v $WORKDIR/repos/$name
if [ $? -ne 0 ]; then
log "ERROR" "Failed to create repo $name failed"
FAILED=1
fi
create_repo_file $name
popd
done
fi
# Create graffiti files for required CBS actions
log "INFO" "Creating graffiti input files"
python "$(dirname $0)/create_graffiti_files.py" $ACTIONS_FILE
if [ $? -ne 0 ]; then
log "ERROR" "Failed creating graffiti input files"
FAILED=1
fi
exit $FAILED