2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2020 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> jjb-verify-build-nodes.sh"
13 # Checks build-node labels used in ci-management templates and projects,
14 # and prints file names with invalid values.
16 # Uses -l to get $HOME/.local/bin on path, where pip puts yq
19 # - Python tool yq is installed; e.g., by python-tools-install.sh
20 # - Working directory is a ci-management repo with subdirs
21 # jenkins-config/clouds/openstack and jjb
22 # Environment variable:
23 # - EXTERNAL_LABELS - a space-separated list of build-node labels
24 # for nodes not managed in the jenkins-config area (optional)
28 # function to search an array for a value
30 # $2 is array, passed via ${array[@]}
34 for e; do [[ "$e" == "$match" ]] && return 0; done
38 # discover build node labels
41 while IFS= read -r ; do
43 # valid files contain IMAGE_NAME; skip the cloud config file
44 if grep -q "IMAGE_NAME" "$file" && ! grep -q "CLOUD_CREDENTIAL_ID" "$file"; then
45 # file name is a valid label, without path prefix and suffix
46 name=$(basename -s "$suffix" "$file")
48 # a file can define custom labels
49 if custom=$(grep "LABELS=" "$file" | cut -d= -f2); then
50 # TODO: confirm separator for multiple labels
51 read -r -a customarray <<< "$custom"
52 for c in "${customarray[@]}"; do
53 if ! isValueInArray "$c" "${labels[@]}"; then
59 done < <(find "jenkins-config/clouds/openstack" -name \*$suffix)
60 echo "Found ${#labels[@]} configured label(s):"
62 declare -a externals=()
63 if [[ -n ${EXTERNAL_LABELS:-} ]]; then
64 read -r -a externals <<< "$EXTERNAL_LABELS"
65 echo "Received ${#externals[@]} external label(s):"
66 echo "${externals[@]}"
67 labels=("${externals[@]}" "${labels[@]}")
70 # check build node label uses
72 while IFS= read -r ; do
75 # this includes job-templates which may be annoying
76 nodes=$(yq 'recurse | ."build-node"? | values' "$file" | sort -u | tr -d '"')
77 for node in $nodes; do
78 # may be a yaml list; e.g., '[ foo, bar, baz ]'
79 node="${node//[\[\],]/}"
80 if [[ -n $node ]] && ! isValueInArray "$node" "${labels[@]}"; then
81 echo "ERROR: file $file uses unknown build-node $node"
85 done < <(find "jjb" -name '*.yaml')
87 echo "---> jjb-verify-build-nodes.sh ends"