summaryrefslogtreecommitdiff
path: root/jenkins-helpers.sh
blob: 671ebed2a74f82db2822fbb0178c65d6c98de296 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#/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 ()
{
    (
    set -euf -o pipefail

    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 ()
{
    (
    set -euf -o pipefail

    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 ()
{
    (
    set -eu -o pipefail

    local url="$1"
    shift

    local url_basename
    url_basename="$(basename "$url")"

    local tmpdir
    tmpdir="$(mktemp -d)"

    wget_opts=""
    case "$(echo "$url" | cut -d/ -f3)" in
	*".tcwglab") wget_opts="$wget_opts --no-check-certificate" ;;
    esac

    wget --progress=dot:giga -r --no-parent --no-directories --level 1 "--directory-prefix=$tmpdir" -A "$url_basename" $wget_opts "$@" "$(dirname "$url")/"

    local count=-1
    for i in "$tmpdir"/$url_basename; do
	mv "$i" .
	count=$((count+1))
    done

    rm -rf "$tmpdir"

    return $count
    )
}