#! /bin/bash
# ----------------------------------------------------------------------------
# checkForNewPackages.sh - Check if every package on the repo${${jenkinsCliJar}
# jenkins job and if not, create one.
#
# Creation : 2013-04-19 starwarsfan
#
# Copyright (c) 2013-2018 the eisfair team, team(at)eisfair(dot)org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# ----------------------------------------------------------------------------
#exec 2> /tmp/checkForNewPackages$$.log
#set -x
# Backup where we came from
callDir=$(pwd)
ownLocation="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
scriptName=$(basename $0)
cd ${ownLocation}
rtc=0
# -----------------------------
# Check if settings file exists
if [ -f "settings.txt" ] ; then
# ----------------------------------------------------------------
# This should be the normal case: settings.txt exists, so load it.
. settings.txt
echo "Settings loaded."
elif [ -f "settings.default.txt" ] ; then
echo ""
echo "Settings file not existing, creating new one out of default file."
echo "The new one is on .gitignore, so it is secured that personal settings"
echo "are not commited to the repository."
cp settings.default.txt settings.txt
echo "The script will be aborted here, please modify"
echo " ${ownLocation}/settings.txt"
echo "to fit your needs. If the settings are OK, restart the script."
echo ""
exit 2
else
# ---------------------------------------------------------------------
# Thats odd: Not even 'settings.txt' nor 'settings.default.txt' exists!
echo ""
echo "ERROR: No settings file existing!"
echo "The default file 'settings.default.txt' must exist on the folder"
echo "'_ADMIN/'. This file is used to create"
echo "a personal settings file, which you can setup to your needs."
echo "Please check for that and restart the script again."
echo ""
exit 1
fi
# ============================================================================
# The main part of the menu script
helpMe ()
{
echo "
Usage: ${0##*/} [options]
This script checks if a jenkins job for every package on this
repository is existing. If not, jobs based on the templates will
be created.
Options:
-n .. Do not trigger a build of new jobs immediately after their creation.
-f .. Comma separated list of package folders following Jenkins logical structure e. g.
'e-ng/previous/testing/x86_64,e-ng/previous/testing/x86'
"
}
# ============================================================================
# Iterate over all folders on local working copy. In fact every folder
# represents a package (except folder _ADMIN), so for every folder the
# corresponding build jobs must exist or will be created.
iteratePackageFolders ()
{
cd ${jobsFolder}
local tmpCounter=0
info "=============================================================================="
for currentJobFolder in ${jobFolderList} ; do
info ${currentJobFolder}
info "Getting template job configuration"
info "curl -u jenkins:************ -L -X GET ${jenkinsUrl}/${currentJobFolder}/${jobTemplateName}/config.xml -o ${tmpLocalTemplate}"
curl -u jenkins:${jenkinsPassword} -L -X GET ${jenkinsUrl}/${currentJobFolder}/${jobTemplateName}/config.xml -o ${tmpLocalTemplate}
info "Iterating over package folders"
for currentFolder in $(ls -d ${workspaceFolder}/*/ | grep -v "_ADMIN") ; do
info "${currentFolder}"
# Cut last '/' and everything beyond
currentCheckedPackage="${currentFolder%/*}"
# Cut everything before last '/' including the '/' itself
currentCheckedPackage=${currentCheckedPackage##*/}
createJob "$currentCheckedPackage" "$currentJobFolder" ${jobTemplateName}
# For testing: Only create 3 jobs
tmpCounter=$((tmpCounter+1))
if [ ${tmpCounter} -gt 3 ] ; then
break
fi
done
# For testing: Do only one step
break
done
rm -f ${tmpLocalTemplate} ${tmpUpdatedTemplate}
info "=============================================================================="
}
# ============================================================================
# Create new jenkins job using the jenkins-cli
#
# $1 .. Package name
# $2 .. Logical Jenkins folder name, which should contain generated job
# $3 .. Name of the template-job which should be used
# as the base for the new job
createJob ()
{
local currentPackage=$1
local logicalJobFolder=$2
local jobTemplateName=$3
local physicalJobFolder="$(echo "$logicalJobFolder" | sed "s#/#/jobs/#g")/jobs"
local rtc=0
if [ ! -d ${physicalJobFolder}/${currentPackage} -o ! -f ${physicalJobFolder}/${currentPackage}/config.xml ] ; then
info "Creating job for package ${currentPackage}"
# Update template for current package and activate it
sed -e "s/TEMPLATE/$currentPackage/g" \
-e "s#true#false#g" \
${tmpLocalTemplate} > ${tmpUpdatedTemplate}
# Create job
info "curl -u jenkins:************ -L -X POST ${jenkinsUrl}/${currentJobFolder}/${currentPackage}/config.xml --data-binary @${tmpUpdatedTemplate}"
curl -u jenkins:${jenkinsPassword} -L -X POST ${jenkinsUrl}/${currentJobFolder}/${currentPackage}/config.xml --data-binary "@${tmpUpdatedTemplate}"
rtc=$?
if [ ${rtc} != 0 ] ; then
error "ERROR: Something went wrong during creation of build-job '${logicalJobFolder}/${currentPackage}'"
else
# Trigger job
info "curl -u jenkins:************ -L -X POST ${jenkinsUrl}/${currentJobFolder}/${currentPackage}/build"
curl -u jenkins:${jenkinsPassword} -L -X POST ${jenkinsUrl}/${currentJobFolder}/${currentPackage}/build
rtc=$?
if [ ${rtc} != 0 ] ; then
error "ERROR: Something went wrong during trigger of build-job '${logicalJobFolder}/${currentPackage}'"
fi
fi
else
info "Job for package ${currentPackage} already existing"
fi
}
######### HELPER-FUNCTIONS #########
_init() {
if [ -n "${TERM}" -a "${TERM}" != "dumb" ]; then
GREEN=$(tput setaf 2) RED=$(tput setaf 1) BLUE="$(tput setaf 4)"
LTGREYBG="$(tput setab 7)"
NORMAL=$(tput sgr0) BLINK=$(tput blink)
else
GREEN="" RED="" BLUE="" LTGREYBG="" NORMAL="" BLINK=""
fi
}
die() {
error=${1:-1}
shift
error "$*" >&2
exit ${error}
}
info() {
printf "${NORMAL}%-7s: %s${NORMAL}\n" "Info" "$*"
}
error() {
printf "${RED}%-7s: %s${NORMAL}\n" "Error" "$*"
}
warning() {
printf "${BLUE}%-7s: %s${NORMAL}\n" "Warning" "$*"
}
######### HANDLE OPTIONS, CALL MAIN #########
_init
# Go into the repo root folder for the next steps and store it's full path
cd ${ownLocation}/..
workspaceFolder=$(pwd)
# Set some defaults
buildNewJobs=true
jobFolderList='job/e-ng/job/edge/job/testing/job/x86/job job/e-ng/job/edge/job/testing/job/x86_64/job'
tmpLocalTemplate=${workspaceFolder}/template.config.xml
tmpUpdatedTemplate=${workspaceFolder}/newPackage.config.xml
while getopts nf:h? option; do
case ${option} in
n) buildNewJobs=false;;
f) jobFolderList=$(echo "${OPTARG}" | sed "s/,/ /g");;
h|?) helpMe && exit 0;;
*) die 90 "invalid option \"${OPTARG}\"";;
esac
done
# $jobsFolder is defined on settings.txt
if [ ! -d ${jobsFolder} ] ; then
die 100 "Jenkins job folder '$jobsFolder' not existing! Exiting."
fi
if [ ! -e "${jenkinsPasswordFile}" ] ; then
die 101 "Password file not existing! Exiting."
fi
jenkinsPassword=$(cat ${jenkinsPasswordFile} )
# Now do the job :-)
#getTemplateJobs
iteratePackageFolders
exit ${rtc}
# ============================================================================
# End
# ============================================================================