summaryrefslogtreecommitdiff
path: root/jenkins-helpers.sh
blob: 58c120edc35f6f3f576d5ac99c3011f421b6ffc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#/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" -- <host>[:<port>[:<dir>]]
# "$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[@]}"
}