Created
May 15, 2018 02:46
-
-
Save abhinav1107/6ea9d7a3f288d6c8e65f6333023602f4 to your computer and use it in GitHub Desktop.
Running different Jenkins for different environments leads to use case where Jenkins jobs are imported from one environment to another. And since jenkins slaves might have different labels/names in different environments, we need to ensure lables are correct for each job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Author: Abhinav Kumar | |
# About: Script to check | |
# - if Jenkins is using valid and allowed slave labels only. | |
# - if jobs are configured to run on slaves with labels which are node names. | |
# change IFS for the course of this script | |
OLDIFS=$IFS | |
IFS=$'\n' | |
# make sure /usr/bin is in PATH. | |
PATH="$PATH:/usr/bin" | |
predefined_node_name_array=('build' 'mobile' 'docker' 'node' 'perl') | |
jenkins_user='some-user' | |
jenkins_pass='some-token' | |
# find all nodes in jenkins | |
jenkins_allnodes_details_json="`curl -f --silent http://${jenkins_user}:${jenkins_pass}@localhost:8080/jenkins/computer/api/json`" | |
if [ "$?" -ne '0' ]; then echo "unable to get node details from Jenkins"; exit 1; fi | |
# if there is only one node for this jenkins setup, no need to proceed further, even if the jobs are labeled with wrong node name. | |
# The node names will not even come into picture until there are multiple nodes in jenkins cluster. | |
# in this case, just print everything is good and exit with zero status code. Me happy, BASH happy and System happy :) | |
if [ "`echo $jenkins_allnodes_details_json | jq '.computer | length'`" -eq '1' ]; then echo 'All Good!!' && exit 0; fi | |
# find all jobs in jenkins | |
jenkins_alljobs_details_json="`curl -f --silent http://${jenkins_user}:${jenkins_pass}@localhost:8080/jenkins/api/json`" | |
if [ "$?" -ne '0' ]; then echo "unable to get all job details. Exiting."; exit 1; fi | |
# clean the previous garbage just in case and create a placeholder temp file for later use. | |
rm -f /tmp/jenkins-nc.*.list | |
tmp_file="`mktemp /tmp/jenkins-nc.XXXXXXXXXX.list`" | |
if [ "$?" -ne '0' ]; then echo "Unable to create new file in /tmp directory"; exit 1; fi | |
# get where the job is running | |
for job_name in `echo $jenkins_alljobs_details_json | jq -r .jobs[].name`; do | |
sed -n "s/.*<assignedNode>\(.*\)<.*/\1/p" /mnt1/jenkins/jobs/$job_name/config.xml | tr ' ' '\n' | sed "s/\$/ $job_name/g" >> $tmp_file | |
done | |
check_variable1='0' | |
for node_name in `echo $jenkins_allnodes_details_json | jq -r .computer[].displayName | grep -v master`; do | |
IFS=$OLDIFS | |
while read node_n job_n; do | |
if [ ! -z "${node_n}" ]; then echo "job : ${job_n}, node: ${node_n}" && check_variable1='1'; fi | |
done <<< "$(awk -v n_name="$node_name" '$1 == n_name {print}' $tmp_file)" | |
IFS=$'\n' | |
done | |
rm -f $tmp_file | |
# now check if all the node labels that are present are subset of `predefined_node_name_array` | |
check_variable2='0' | |
for label_name in `comm -23 <(sed -n 's/.*<label>\(.\+\)<\/label>/\1/p' /mnt1/jenkins/nodes/*/config.xml | tr ' ' '\n' | sort | uniq) <(printf '%s\n' "${predefined_node_name_array[@]}" | sort)`; do | |
if [ ! -z "$label_name" ]; then | |
for node_name in `curl --silent http://${jenkins_user}:${jenkins_pass}@localhost:8080/jenkins/label/${label_name}/api/json| jq -r .nodes[].nodeName`; do | |
echo "${node_name} assigned unknown label: ${label_name}" && check_variable2='1' | |
done | |
fi | |
done | |
IFS=$OLDIFS | |
check_variable="$(( check_variable1 + check_variable2 ))" | |
if [ "$check_variable" -eq "0" ]; then echo 'All Good!!'; fi | |
exit $check_variable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment