ykarel / centos / cbs-tools

Forked from centos/cbs-tools 4 years ago
Clone
Blob Blame History Raw
usage()
{
cat << EOF
usage: $0 [-t <tag(s)>]

This script generate mash repo from all SIG tags.

OPTIONS:
   -t          tag(s) : storage7-release, storage7-testing etc...
EOF
}

# Global config
# koji binary
KOJI="/usr/bin/koji"
# mash binary
MASH="/usr/bin/mash"
# mash config containing mash.conf
MASH_CONF="/etc/mash"
# repos destination
MASH_DEST="/mnt/kojishare/repos"
# repoviewurl in mash config
MASH_VIEWURL="http://cbs.centos.org/repos"
#cache
MASH_CACHE="/var/cache/cbs-mash"
# log directory
LOG_DIR="/var/log/mash"
# End Global

optiont=false
optionv=false
while getopts ":hvt:" OPTION
do
	case ${OPTION} in
        h)
             usage
             exit 0
             ;;
	t)
             optiont=true
             TAGS="${OPTARG}"
             ;;
	v)
	     optionv=true
	     ;;
        ?)
             exit 0
             ;;
        :)
             echo "Option -${OPTARG} requires an argument."
             exit 1
             ;;
        esac
done

shift $((${OPTIND} - 1))

# Check if user is allowed to send command to koji and has 'admin' permission
PERM=`${KOJI} list-permissions --mine`
if [ "${PERM}" != "admin"  ]
then
	echo "[ERROR] Koji misconfigure/missing admin privilege for creating SIG tags"
	exit 1
fi

if ( ! ${optiont} )
then
	CANDIDATE_TAGS=`${KOJI} list-tags | grep "\-candidate"`
	TEST_TAGS=`${KOJI} list-tags | grep "\-testing"`
	RELEASE_TAGS=`${KOJI} list-tags | grep "\-release"`
	TAGS="${CANDIDATE_TAGS} ${TEST_TAGS} ${RELEASE_TAGS}"
fi

print_mash_template()
{
	local tag=$1
cat << EOF
[${tag}]
rpm_path = ${MASH_DEST}/${tag}/%(arch)s/os/Packages
repodata_path = ${MASH_DEST}/${tag}/%(arch)s/os/
source_path = source/SRPMS
debuginfo = True
multilib = True
multilib_method = devel
tag = ${tag}
inherit = False
strict_keys = False
repoviewurl = ${MASH_VIEWURL}/${tag}/%(arch)s/os/
repoviewtitle = "${tag^^}"
arches = i386 x86_64
delta = True
EOF
}

mash_prepare ()
{
	local tag=$1
	local log=$2
	( $optionv ) && echo "* Checking ${tag} mash config..."
	# config mash already ok 
	[ -f ${MASH_CONF}/${tag}.mash ] && return
	( $optionv ) && echo " -> [INFO] creating mash config: ${tag}.mash..."
	print_mash_template "${tag}" > ${MASH_CONF}/${tag}.mash
}

mash_run () {
	local tag=$1
	local log=$2
	${MASH} -p ${MASH_DEST}/ -o ${MASH_DEST}/ ${tag} &> ${log}
	[ $? -gt 0 ] && echo " -> [ERROR] mash run failed ${log} "
	( $optionv ) && echo " -> [INFO] mash run succeeded ${log}"
}

# Ensure the cache directory is available
[ ! -d $MASH_CACHE ] && mkdir -p $MASH_CACHE

# Ensure we do not have parallel runs
pidfile=/var/run/mash-run.pid
if [ -e $pidfile ]; then
    pid=`cat $pidfile`
    if kill -0 &> /dev/null $pid; then
        ( $optionv ) && echo "Mash is already running PID:$pid"
        exit 1
    else
        rm $pidfile &>/dev/null
    fi
fi

echo $$ > $pidfile

for TAG in ${TAGS}
do
	LOG="${LOG_DIR}/mash.${TAG}.log"
	if [ ! -f $MASH_CACHE/$TAG.buildlist ]
	then
        	${KOJI} list-tagged $TAG > $MASH_CACHE/$TAG.buildlist
	else
        	BUILDLIST=`mktemp`
        	${KOJI} list-tagged $TAG > $BUILDLIST
		diff $BUILDLIST $MASH_CACHE/$TAG.buildlist &>> $LOG
		if [ $? -eq 0 ]
		then
			echo " -> skipping. No new build in $TAG" &>> $LOG
        		rm -rf $BUILDLIST
			continue
		else
                	echo " -> updating cache for $TAG" &>> $LOG
                	cp $BUILDLIST $MASH_CACHE/$TAG.buildlist
       		fi
        	rm -rf $BUILDLIST
	fi
	mash_prepare "${TAG}" "${LOG}"
	mash_run "${TAG}" "${LOG}" &
done

wait
rm $pidfile
exit 0