hhorak / centos / cbs-tools

Forked from centos/cbs-tools 4 years ago
Clone

Blame cron.d/mash_run.sh

Thomas Oulevey 79d958
usage()
Thomas Oulevey 79d958
{
Thomas Oulevey 79d958
cat << EOF
Thomas Oulevey 79d958
usage: $0 [-t <tag(s)>]
Thomas Oulevey 79d958
Thomas Oulevey 79d958
This script generate mash repo from all SIG tags.
Thomas Oulevey 79d958
Thomas Oulevey 79d958
OPTIONS:
Thomas Oulevey 79d958
   -t          tag(s) : storage7-release, storage7-testing etc...
Thomas Oulevey 79d958
EOF
Thomas Oulevey 79d958
}
Thomas Oulevey 79d958
Thomas Oulevey 79d958
# Global config
Thomas Oulevey 39bd31
# koji binary
Thomas Oulevey 39bd31
KOJI="/usr/bin/koji"
Thomas Oulevey 79d958
# mash binary
Thomas Oulevey 79d958
MASH="/usr/bin/mash"
Thomas Oulevey 79d958
# mash config containing mash.conf
Thomas Oulevey 79d958
MASH_CONF="/etc/mash"
Thomas Oulevey 79d958
# repos destination
Thomas Oulevey 79d958
MASH_DEST="/mnt/kojishare/repos"
Thomas Oulevey 79d958
# repoviewurl in mash config
Thomas Oulevey 79d958
MASH_VIEWURL="http://cbs.centos.org/repos"
Thomas Oulevey 39bd31
#cache
Thomas Oulevey 39bd31
MASH_CACHE="/var/cache/cbs-mash"
Thomas Oulevey 79d958
# log directory
Thomas Oulevey 79d958
LOG_DIR="/var/log/mash"
Thomas Oulevey 79d958
# End Global
Thomas Oulevey 79d958
Thomas Oulevey 79d958
optiont=false
Thomas Oulevey 79d958
optionv=false
Thomas Oulevey 79d958
while getopts ":hvt:" OPTION
Thomas Oulevey 79d958
do
Thomas Oulevey 79d958
	case ${OPTION} in
Thomas Oulevey 79d958
        h)
Thomas Oulevey 79d958
             usage
Thomas Oulevey 79d958
             exit 0
Thomas Oulevey 79d958
             ;;
Thomas Oulevey 79d958
	t)
Thomas Oulevey 79d958
             optiont=true
Thomas Oulevey 79d958
             TAGS="${OPTARG}"
Thomas Oulevey 79d958
             ;;
Thomas Oulevey 79d958
	v)
Thomas Oulevey 79d958
	     optionv=true
Thomas Oulevey 79d958
	     ;;
Thomas Oulevey 79d958
        ?)
Thomas Oulevey 79d958
             exit 0
Thomas Oulevey 79d958
             ;;
Thomas Oulevey 79d958
        :)
Thomas Oulevey 79d958
             echo "Option -${OPTARG} requires an argument."
Thomas Oulevey 79d958
             exit 1
Thomas Oulevey 79d958
             ;;
Thomas Oulevey 79d958
        esac
Thomas Oulevey 79d958
done
Thomas Oulevey 79d958
Thomas Oulevey 79d958
shift $((${OPTIND} - 1))
Thomas Oulevey 79d958
Thomas Oulevey 39bd31
# Check if user is allowed to send command to koji and has 'admin' permission
Thomas Oulevey 39bd31
PERM=`${KOJI} list-permissions --mine`
Thomas Oulevey 39bd31
if [ "${PERM}" != "admin"  ]
Thomas Oulevey 39bd31
then
Thomas Oulevey 39bd31
	echo "[ERROR] Koji misconfigure/missing admin privilege for creating SIG tags"
Thomas Oulevey 39bd31
	exit 1
Thomas Oulevey 39bd31
fi
Thomas Oulevey 39bd31
Thomas Oulevey 79d958
if ( ! ${optiont} )
Thomas Oulevey 79d958
then
Thomas Oulevey 39bd31
	CANDIDATE_TAGS=`${KOJI} list-tags | grep "\-candidate"`
Thomas Oulevey 39bd31
	TEST_TAGS=`${KOJI} list-tags | grep "\-testing"`
Thomas Oulevey 39bd31
	RELEASE_TAGS=`${KOJI} list-tags | grep "\-release"`
Thomas Oulevey 39bd31
	TAGS="${CANDIDATE_TAGS} ${TEST_TAGS} ${RELEASE_TAGS}"
Thomas Oulevey 79d958
fi
Thomas Oulevey 79d958
Thomas Oulevey 79d958
print_mash_template()
Thomas Oulevey 79d958
{
Thomas Oulevey 79d958
	local tag=$1
Thomas Oulevey 79d958
cat << EOF
Thomas Oulevey 79d958
[${tag}]
Thomas Oulevey 79d958
rpm_path = ${MASH_DEST}/${tag}/%(arch)s/os/Packages
Thomas Oulevey 79d958
repodata_path = ${MASH_DEST}/${tag}/%(arch)s/os/
Thomas Oulevey 79d958
source_path = source/SRPMS
Thomas Oulevey 79d958
debuginfo = True
Thomas Oulevey 79d958
multilib = True
Thomas Oulevey 79d958
multilib_method = devel
Thomas Oulevey 79d958
tag = ${tag}
Thomas Oulevey 79d958
inherit = False
Thomas Oulevey 79d958
strict_keys = False
Thomas Oulevey 79d958
repoviewurl = ${MASH_VIEWURL}/${tag}/%(arch)s/os/
Thomas Oulevey 79d958
repoviewtitle = "${tag^^}"
Thomas Oulevey 79d958
arches = i386 x86_64
Thomas Oulevey 79d958
delta = True
Thomas Oulevey 79d958
EOF
Thomas Oulevey 79d958
}
Thomas Oulevey 79d958
Thomas Oulevey 79d958
mash_prepare ()
Thomas Oulevey 79d958
{
Thomas Oulevey 79d958
	local tag=$1
Thomas Oulevey 79d958
	local log=$2
Thomas Oulevey 79d958
	( $optionv ) && echo "* Checking ${tag} mash config..."
Thomas Oulevey 79d958
	# config mash already ok 
Thomas Oulevey 79d958
	[ -f ${MASH_CONF}/${tag}.mash ] && return
Thomas Oulevey 79d958
	( $optionv ) && echo " -> [INFO] creating mash config: ${tag}.mash..."
Thomas Oulevey 79d958
	print_mash_template "${tag}" > ${MASH_CONF}/${tag}.mash
Thomas Oulevey 79d958
}
Thomas Oulevey 79d958
Thomas Oulevey 79d958
mash_run () {
Thomas Oulevey 79d958
	local tag=$1
Thomas Oulevey 79d958
	local log=$2
Thomas Oulevey 79d958
	${MASH} -p ${MASH_DEST}/ -o ${MASH_DEST}/ ${tag} &> ${log}
Thomas Oulevey 79d958
	[ $? -gt 0 ] && echo " -> [ERROR] mash run failed ${log} "
Thomas Oulevey 79d958
	( $optionv ) && echo " -> [INFO] mash run succeeded ${log}"
Thomas Oulevey 79d958
}
Thomas Oulevey 79d958
Thomas Oulevey 39bd31
# Ensure the cache directory is available
Thomas Oulevey 39bd31
[ ! -d $MASH_CACHE ] && mkdir -p $MASH_CACHE
Thomas Oulevey 39bd31
Thomas Oulevey 79d958
# Ensure we do not have parallel runs
Thomas Oulevey 79d958
pidfile=/var/run/mash-run.pid
Thomas Oulevey 79d958
if [ -e $pidfile ]; then
Thomas Oulevey 79d958
    pid=`cat $pidfile`
Thomas Oulevey 79d958
    if kill -0 &> /dev/null $pid; then
Thomas Oulevey 79d958
        ( $optionv ) && echo "Mash is already running PID:$pid"
Thomas Oulevey 79d958
        exit 1
Thomas Oulevey 79d958
    else
Thomas Oulevey 79d958
        rm $pidfile &>/dev/null
Thomas Oulevey 79d958
    fi
Thomas Oulevey 79d958
fi
Thomas Oulevey 79d958
Thomas Oulevey 79d958
echo $$ > $pidfile
Thomas Oulevey 79d958
Thomas Oulevey 79d958
for TAG in ${TAGS}
Thomas Oulevey 79d958
do
Thomas Oulevey 79d958
	LOG="${LOG_DIR}/mash.${TAG}.log"
Thomas Oulevey 39bd31
	if [ ! -f $MASH_CACHE/$TAG.buildlist ]
Thomas Oulevey 39bd31
	then
Thomas Oulevey 39bd31
        	${KOJI} list-tagged $TAG > $MASH_CACHE/$TAG.buildlist
Thomas Oulevey 39bd31
	else
Thomas Oulevey 39bd31
        	BUILDLIST=`mktemp`
Thomas Oulevey 39bd31
        	${KOJI} list-tagged $TAG > $BUILDLIST
Thomas Oulevey 39bd31
		diff $BUILDLIST $MASH_CACHE/$TAG.buildlist &>> $LOG
Thomas Oulevey 39bd31
		if [ $? -eq 0 ]
Thomas Oulevey 39bd31
		then
Thomas Oulevey 39bd31
			echo " -> skipping. No new build in $TAG" &>> $LOG
Thomas Oulevey 39bd31
        		rm -rf $BUILDLIST
Thomas Oulevey 39bd31
			continue
Thomas Oulevey 39bd31
		else
Thomas Oulevey 39bd31
                	echo " -> updating cache for $TAG" &>> $LOG
Thomas Oulevey 39bd31
                	cp $BUILDLIST $MASH_CACHE/$TAG.buildlist
Thomas Oulevey 39bd31
       		fi
Thomas Oulevey 39bd31
        	rm -rf $BUILDLIST
Thomas Oulevey 39bd31
	fi
Thomas Oulevey 79d958
	mash_prepare "${TAG}" "${LOG}"
Thomas Oulevey 79d958
	mash_run "${TAG}" "${LOG}" &
Thomas Oulevey 79d958
done
Thomas Oulevey 79d958
Thomas Oulevey 79d958
wait
Thomas Oulevey 79d958
rm $pidfile
Thomas Oulevey 79d958
exit 0