11332: clone repos with --depth 1 to reduce bandwidth/disk usage.
[arvados-dev.git] / jenkins / run-cwl-test.sh
1 #!/bin/bash
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: AGPL-3.0
6
7 set -o pipefail
8
9 DEBUG=0
10 SSH_PORT=22
11 ACCT=ci
12
13 function usage {
14     echo >&2
15     echo >&2 "usage: $0 [options] <identifier>"
16     echo >&2
17     echo >&2 "   <identifier>                 Arvados cluster name"
18     echo >&2
19     echo >&2 "$0 options:"
20     echo >&2 "  -p, --port <ssh port>         SSH port to use (default 22)"
21     echo >&2 "      --acct <username>         Account to log in with"
22     echo >&2 "  -d, --debug                   Enable debug output"
23     echo >&2 "  -h, --help                    Display this help and exit"
24     echo >&2
25     echo >&2 " Required scope for the token used to run the tests:"
26     echo >&2
27     echo >&2 " arv api_client_authorization create_system_auth     --scopes "
28     echo >&2 "[\"GET /arvados/v1/virtual_machines\","
29     echo >&2 "\"GET /arvados/v1/keep_services\","
30     echo >&2 "\"GET /arvados/v1/keep_services/\","
31     echo >&2 "\"GET /arvados/v1/groups\","
32     echo >&2 "\"GET /arvados/v1/groups/\","
33     echo >&2 "\"GET /arvados/v1/links\","
34     echo >&2 "\"GET /arvados/v1/collections\","
35     echo >&2 "\"POST /arvados/v1/collections\","
36     echo >&2 "\"POST /arvados/v1/links\","
37     echo >&2 "\"GET /arvados/v1/users/current\","
38     echo >&2 "\"POST /arvados/v1/users/current\","
39     echo >&2 "\"GET /arvados/v1/jobs\","
40     echo >&2 "\"POST /arvados/v1/jobs\","
41     echo >&2 "\"GET /arvados/v1/pipeline_instances\","
42     echo >&2 "\"GET /arvados/v1/pipeline_instances/\","
43     echo >&2 "\"POST /arvados/v1/pipeline_instances\","
44     echo >&2 "\"GET /arvados/v1/collections/\","
45     echo >&2 "\"POST /arvados/v1/collections/\","
46     echo >&2 "\"GET /arvados/v1/container_requests\","
47     echo >&2 "\"GET /arvados/v1/container_requests/\","
48     echo >&2 "\"POST /arvados/v1/container_requests\","
49     echo >&2 "\"POST /arvados/v1/container_requests/\","
50     echo >&2 "\"GET /arvados/v1/containers\","
51     echo >&2 "\"GET /arvados/v1/containers/\","
52     echo >&2 "\"GET /arvados/v1/logs\" ]"
53     echo >&2
54 }
55
56 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
57 TEMP=`getopt -o hdp: \
58     --long help,debug,port:,acct: \
59     -n "$0" -- "$@"`
60
61 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
62 # Note the quotes around `$TEMP': they are essential!
63 eval set -- "$TEMP"
64
65 while [ $# -ge 1 ]
66 do
67     case $1 in
68         -p | --port)
69             SSH_PORT="$2"; shift 2
70             ;;
71         --acct)
72             ACCT="$2"; shift 2
73             ;;
74         -d | --debug)
75             DEBUG=1
76             shift
77             ;;
78         --)
79             shift
80             break
81             ;;
82         *)
83             usage
84             exit 1
85             ;;
86     esac
87 done
88
89 IDENTIFIER=$1
90
91 if [[ "$IDENTIFIER" == '' ]]; then
92   usage
93   exit 1
94 fi
95
96 EXITCODE=0
97
98 COLUMNS=80
99
100 PUPPET_AGENT='
101 now() { date +%s; }
102 let endtime="$(now) + 600"
103 while [ "$endtime" -gt "$(now)" ]; do
104     puppet agent --test --detailed-exitcodes
105     agent_exitcode=$?
106     if [ 0 = "$agent_exitcode" ] || [ 2 = "$agent_exitcode" ]; then
107         break
108     else
109         sleep 10s
110     fi
111 done
112 exit ${agent_exitcode:-99}
113 '
114
115 title () {
116   date=`date +'%Y-%m-%d %H:%M:%S'`
117   printf "%s\n" "$date $1"
118 }
119
120 function run_command() {
121   node=$1
122   return_var=$2
123   command=$3
124
125   title "Running '${command/ARVADOS_API_TOKEN=*/ARVADOS_API_TOKEN=suppressed}' on $node"
126   TMP_FILE=`mktemp`
127   if [[ "$DEBUG" != "0" ]]; then
128     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" $ACCT@$node -C "$command" | tee $TMP_FILE
129     ECODE=$?
130   else
131     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" $ACCT@$node -C "$command" > $TMP_FILE 2>&1
132     ECODE=$?
133   fi
134
135   if [[ "$ECODE" != "255" && "$ECODE" != "0"  ]]; then
136     # Ssh exists 255 if the connection timed out. Just ignore that, it's possible that this node is
137     #   a shell node that is down.
138     title "ERROR running command on $node: exit code $ECODE"
139     if [[ "$DEBUG" == "0" ]]; then
140       title "Command output follows:"
141       cat $TMP_FILE
142     fi
143   fi
144   if [[ "$ECODE" == "255" ]]; then
145     title "Connection denied or timed out"
146   fi
147   rm -f $TMP_FILE
148   eval "$return_var=$ECODE"
149 }
150
151 title "Loading ARVADOS_API_HOST and ARVADOS_API_TOKEN"
152 if [[ -f "$HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf" ]]; then
153   . $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf
154 else
155   title "WARNING: $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf not found."
156 fi
157 if [[ "$ARVADOS_API_HOST" == "" ]] || [[ "$ARVADOS_API_TOKEN" == "" ]]; then
158   title "ERROR: ARVADOS_API_HOST and/or ARVADOS_API_TOKEN environment variables are not set."
159   exit 1
160 fi
161
162 run_command shell.$IDENTIFIER ECODE "if [[ ! -e common-workflow-language ]]; then git clone --depth 1 https://github.com/common-workflow-language/common-workflow-language.git; fi"
163
164 if [[ "$ECODE" != "0" ]]; then
165   echo "Failed to git clone --depth 1 https://github.com/common-workflow-language/common-workflow-language.git"
166   exit $ECODE
167 fi
168
169 run_command shell.$IDENTIFIER ECODE "if [[ ! -e arvados-cwl-runner-with-checksum.sh ]]; then printf \"%s\n%s\n\" '#!/bin/sh' 'exec arvados-cwl-runner --compute-checksum \"\$@\"' > arvados-cwl-runner-with-checksum.sh; chmod 755 arvados-cwl-runner-with-checksum.sh; fi"
170
171 if [[ "$ECODE" != "0" ]]; then
172   echo "Failed to create ~$ACCT/arvados-cwl-runner-with-checksum.sh"
173   exit $ECODE
174 fi
175
176 run_command shell.$IDENTIFIER ECODE "cd common-workflow-language; git pull; ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN ./run_test.sh RUNNER=/home/$ACCT/arvados-cwl-runner-with-checksum.sh "
177
178 run_command shell.$IDENTIFIER ECODE "if [[ ! -e arvados ]]; then ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN git clone --depth 1 https://git.$IDENTIFIER.arvadosapi.com/arvados.git; fi"
179
180 if [[ "$ECODE" != "0" ]]; then
181   echo "Failed to git clone --depth 1 git@git.$IDENTIFIER.arvadosapi.com:arvados.git"
182   exit $ECODE
183 fi
184
185 run_command shell.$IDENTIFIER ECODE "cd arvados/sdk/cwl/tests; git pull; ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN ./arvados-tests.sh"
186
187 exit $ECODE