aboutsummaryrefslogtreecommitdiffstats
path: root/jjb/common/include-agl-lava-labs-prepare.sh
blob: ea025469119959471dcaebee5d2dd65c2ad37a7d (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
# (c) 2017 Kevin Hilman <khilman@baylibre.com>
# License GPLv2

#
# Setup LAVA API authentication tokens for various LAVA labs
#
# Uses user/token pairs from Jenkins secrets
#
# And macthing LAVA lab names for releng-scripts
#
# Strucuture: [JENKINS_LAB_NAME]="LAB_URL;JENKINS_LAB_USER;JENKINS_LAB_TOKEN;RELENG_LAB_NAME"
#

# Let the script handle errors nicely
set +e

declare -A labs
labs=(
      [agl]="https://lava.automotivelinux.org/;$LAB_AGL_USER;$LAB_AGL_TOKEN;lab-agl-core"
#      [baylibre]="http://lava.baylibre.com:10080/;$LAB_BAYLIBRE_USER;$LAB_BAYLIBRE_TOKEN;lab-baylibre-legacy"
)

#
# Ensure python_keyring is set to plaintext.  Required for
# non-interactive use
#
echo "default keyring config"
mkdir -p ~/.local/share/python_keyring/
cat <<EOF >  ~/.local/share/python_keyring/keyringrc.cfg
[backend]
default-keyring=keyring.backends.file.PlaintextKeyring
EOF

device_available=0
for lab in "${!labs[@]}"; do
    val=${labs[$lab]}
    OFS=${IFS}
    IFS=';'
    arr=(${labs[$lab]})
    IFS=${OFS}

    url=${arr[0]}
    user=${arr[1]}
    token=${arr[2]}
    lava_lab=${arr[3]}
    token_file=$HOME/.local/lab-$lab-token

    if [ -z ${user} ]; then
        echo "WARNING: Lab ${lab}: missing user. Ignoring."
        continue
    fi
    if [ -z ${token} ]; then
        echo "WARNING: Lab ${lab}: missing token. Ignoring."
        continue
    fi

    # LAVA URL with username
    lava_url=${url/:\/\//:\/\/${user}\@}
    full_url=${lava_url}
    full_url+="RPC2/" # entry point for XML-RPC API
    echo "LAVA auth-add for lab: $lab, URL: $full_url"

    # LAVA auth using token
    echo ${token} > $token_file
    lava-tool auth-add --token $token_file $full_url
    if [ $? != 0 ]; then
        echo "ERROR: Lab ${lab}: lava-tool auth-add failed."
        continue
    fi
    rm -f $token_file

    # Find the LAVA Lab that has the device available to run the job
    echo -n "Checking for $lava_device at $full_url... "
    line=$(lava-tool devices-list $full_url | grep $lava_device)
    line=$(echo "$line" | tr -d '[:space:]')

    if [ -z "$line" ]; then
        echo "not found."
        continue
    fi
    IFS='|'
    arr=($line)
    device_status=${arr[2]}
    IFS=${OFS}

    if [ x"$device_status" = x"reserved" ]; then
        retries=10
    else
        retries=1
    fi

    # If the device is reserved poll it's status every minutes.
    # The max polling time is set to $retries * 60 seconds = 10 minutes.
    for i in `seq 1 $retries`
    do
        # device is only available if "idle" or "running"
        device_available=0
        if [ x"$device_status" = x"reserved" ]; then
            sleep 60s
            # Look if the status of the board has changed from reserved in the lab
            echo -n "Checking for $lava_device at $full_url... "
            line=$(lava-tool devices-list $full_url | grep $lava_device)
            line=$(echo "$line" | tr -d '[:space:]')

            if [ -z "$line" ]; then
                echo "not found."
                continue
            fi
            IFS='|'
            arr=($line)
            device_status=${arr[2]}
            IFS=${OFS}
        fi
        if [ x"$device_status" = x"idle" ]; then
            device_available=1
            break
        elif [ x"$device_status" = x"running" ]; then
            device_available=1;
            break
        fi
    done

    if [ $device_available = 0 ]; then
        echo " Not Available.  Status: $device_status"
        continue
    else
        echo " Found and available.  Status: $device_status"
        export LAVA_LAB=$lava_lab
        break
    fi
done

if [ "$device_available" -eq 0 ]; then
    echo ""
    echo "ERROR: device not found in any available lab."
    exit 0
fi

# Re-enable error detection
set -e