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