#/bin/bash # $@: Jenkins labels, typically tcwg-t[kx]1_{32/64}-test # Returns node from one of the labels with least number of running containers. print_node_with_least_containers () { ( set -euf -o pipefail local tester_labels="$@" local tester local testers local load_value local tester_min_load_name="" local tester_min_load_value="999" testers=$( set -euf -o pipefail local label for label in $tester_labels; do wget -O - https://ci.linaro.org/label/$label/api/json?pretty=true 2>/dev/null | grep nodeName | cut -d: -f 2 | sed -e 's/"//g' done) for tester in $testers; do load_value=$(docker -H ${tester}.tcwglab:2375 ps | wc -l || echo 999) if [ $load_value -lt $tester_min_load_value ]; then tester_min_load_name=$tester tester_min_load_value=$load_value fi done echo $tester_min_load_name ) } # $1: Jenkins tcwg-*-build label # Prints out architecture for container image print_arch_for_label () { ( set -euf -o pipefail local label="$1" case $label in tcwg-x86_64-*) echo amd64 ;; tcwg-x86_32-*) echo i386 ;; tcwg-tx1_64-*|tcwg-apm_64-*) echo arm64 ;; tcwg-tk1_32-*|tcwg-tx1_32-*|tcwg-apm_32-*) echo armhf ;; *) echo "ERROR: Unsupported label: $label" 1>&2; exit 1 ;; esac ) } # $1: Jenkins tcwg-*-build label # Prints out host type print_type_for_label () { ( set -euf -o pipefail echo "$1" | sed -e "s/^tcwg-\(.*\)-build\$/\1/" ) } # $1: Jenkins $NODE_NAME # Prints DNS hostname print_host_for_node () { ( set -euf -o pipefail local NODE="$1" local ID=$(echo ${NODE} | sed 's/.*-\([0-9][0-9]\)$/\1/') case ${NODE} in tcwg-apm-0[1-2]) echo apm-${ID}.tcwglab ;; tcwg-apm-0[3-4]) echo test-armv8-$(echo "${ID}" | sed -e 'y/34/12/').tcwglab ;; tcwg-bmk-*) echo dev-01.tcwglab ;; tcwg-tk1-0[1-5]) echo ${NODE}.tcwglab ;; tcwg-tx1-0[1-5]) echo ${NODE}.tcwglab ;; tcwg-x86_64-build-0[1-8]) echo build-${ID}.tcwglab ;; tcwg-x86_64-dev-0[1-2]) echo dev-${ID}.tcwglab ;; tcwg-x86_64-dev-0[1-2]-short) ID=$(echo ${NODE} | sed 's/.*-\([0-9][0-9]\)-short$/\1/') NAME=dev-${ID}.tcwglab ;; *) echo "Error: unsupported NODE value: ${NODE}" 1>&2; exit 1 ;; esac ) } # $1: target triplet # Prints tester label for remote cross-testing print_tester_label_for_target () { ( set -euf -o pipefail local target="$1" case "$target" in aarch64-linux*) echo "tcwg-tx1_64-test" ;; armv8l-linux*) echo "tcwg-tx1_32-test" ;; arm-linux*) echo "tcwg-tk1_32-test" ;; esac ) } # Run command on remote machine in given directory via ssh on a given port # "$1" -- [:[:]] # "$2, $3, etc" -- command and its arguments # E.g., remote_exec dev-01.tcwglab::/tmp find -name "my file.bak" remote_exec () { local host="$(echo $1 | cut -d: -f 1)" local port="$(echo $1 | cut -s -d: -f 2)" local dir="$(echo $1 | cut -s -d: -f 3)" shift local -a cmd cmd=() # Add quotes to every parameter for i in "$@"; do cmd+=($(printf '%q' "$i")); done ssh ${port:+-p$port} $host "${dir:+cd "$(printf '%q' "$dir")" &&} exec ${cmd[@]}" } # Clone or update a git repo # $1 -- repo directory # $2 -- branch, tag or refspec # $3 -- master git repo clone_or_update_repo () { local dir="$1" local ref="$2" local url="$3" if ! [ -d "$dir/.git" ]; then rm -rf "$dir" git clone "$url" "$dir" fi ( cd "$dir" # Convert git branch/tag names into SHA1 local sha1 sha1=$(git ls-remote "$url" "$ref" | cut -f 1) if [ x"$sha1" = x"" ]; then # If "git ls-remote" can't resolve $ref, then assume it is # already SHA1. sha1="$ref" fi # Update from URL. git remote set-url origin "$url" git remote update -p # Checkout git reset --hard git clean -dfx git checkout --detach "$sha1" ) } # Wget files from URL that may have wildcards; only the last "basename" # part of URL is allowed to contain wildcards. Safe to use on normal URLs. # Return N-1 of files downloaded, or 127 if no files were downloaded. # $1 -- URL # $2,... -- additional parameters to wget wget_wildcard_url () { local url="$1" shift local url_basename url_basename="$(basename "$url")" local tmpdir tmpdir="$(mktemp -d)" wget --progress=dot:giga -r --no-parent --no-directories --level 1 "--directory-prefix=$tmpdir" -A "$url_basename" "$@" "$(dirname "$url")/" local count=-1 for i in "$tmpdir"/$url_basename; do mv "$i" . count=$((count+1)) done rm -rf "$tmpdir" return $count }