Now python-arvados-cwl-runner includes a virtualenv, make sure to use its
[arvados-dev.git] / jenkins / run-deploy.sh
1 #!/bin/bash
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: AGPL-3.0
6
7 DEBUG=0
8 SSH_PORT=22
9 PUPPET_CONCURRENCY=5
10
11 read -d] -r SCOPES <<EOF
12 --scopes
13 '["GET /arvados/v1/virtual_machines",\n
14 "GET /arvados/v1/keep_services",\n
15 "GET /arvados/v1/keep_services/",\n
16 "GET /arvados/v1/groups",\n
17 "GET /arvados/v1/groups/",\n
18 "GET /arvados/v1/links",\n
19 "GET /arvados/v1/collections",\n
20 "POST /arvados/v1/collections",\n
21 "POST /arvados/v1/links",\n
22 "GET /arvados/v1/users/current",\n
23 "POST /arvados/v1/users/current",\n
24 "GET /arvados/v1/jobs",\n
25 "POST /arvados/v1/jobs",\n
26 "GET /arvados/v1/pipeline_instances",\n
27 "POST /arvados/v1/pipeline_instances",\n
28 "PUT /arvados/v1/pipeline_instances/",\n
29 "GET /arvados/v1/collections/",\n
30 "POST /arvados/v1/collections/",\n
31 "GET /arvados/v1/logs"]'
32 EOF
33
34 function usage {
35     echo >&2
36     echo >&2 "usage: $0 [options] <identifier>"
37     echo >&2
38     echo >&2 "   <identifier>                 Arvados cluster name"
39     echo >&2
40     echo >&2 "$0 options:"
41     echo >&2 "  -n, --node <node>             Single machine to deploy, use fqdn, optional"
42     echo >&2 "  -p, --port <ssh port>         SSH port to use (default 22)"
43     echo >&2 "  -c, --concurrency <max>       Maximum concurrency for puppet runs (default 5)"
44     echo >&2 "  -d, --debug                   Enable debug output"
45     echo >&2 "  -h, --help                    Display this help and exit"
46     echo >&2
47     echo >&2 "Note: this script requires an arvados token created with these permissions:"
48     echo >&2 '  arv api_client_authorization create_system_auth \'
49     echo -e $SCOPES"]'" >&2
50     echo >&2
51 }
52
53
54 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
55 TEMP=`getopt -o hdp:c:n: \
56     --long help,debug,port:,concurrency:,node: \
57     -n "$0" -- "$@"`
58
59 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
60 # Note the quotes around `$TEMP': they are essential!
61 eval set -- "$TEMP"
62
63 while [ $# -ge 1 ]
64 do
65     case $1 in
66         -n | --node)
67             NODE="$2"; shift 2
68             ;;
69         -p | --port)
70             SSH_PORT="$2"; shift 2
71             ;;
72         -c | --concurrency)
73             PUPPET_CONCURRENCY="$2"; shift 2
74             ;;
75         -d | --debug)
76             DEBUG=1
77             shift
78             ;;
79         --)
80             shift
81             break
82             ;;
83         *)
84             usage
85             exit 1
86             ;;
87     esac
88 done
89
90 IDENTIFIER=$1
91
92 if [[ "$IDENTIFIER" == '' ]]; then
93   usage
94   exit 1
95 fi
96
97 EXITCODE=0
98
99 COLUMNS=80
100
101 PUPPET_AGENT='
102 now() { date +%s; }
103 let endtime="$(now) + 600"
104 while [ "$endtime" -gt "$(now)" ]; do
105     puppet agent --test --detailed-exitcodes
106     agent_exitcode=$?
107     if [ 0 = "$agent_exitcode" ] || [ 2 = "$agent_exitcode" ]; then
108         break
109     else
110         sleep 10s
111     fi
112 done
113 exit ${agent_exitcode:-99}
114 '
115
116 title () {
117   date=`date +'%Y-%m-%d %H:%M:%S'`
118   printf "$date $1\n"
119 }
120
121 function run_puppet() {
122   node=$1
123
124   title "Running puppet on $node"
125   sleep $[ $RANDOM / 6000 ].$[ $RANDOM / 1000 ]
126   TMP_FILE=`mktemp`
127   if [[ "$DEBUG" != "0" ]]; then
128     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" | tee $TMP_FILE
129   else
130     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" > $TMP_FILE 2>&1
131   fi
132
133   ECODE=${PIPESTATUS[0]}
134   RESULT=$(cat $TMP_FILE)
135
136   if [[ "$ECODE" != "255" && ! ("$RESULT" =~ 'already in progress') && "$ECODE" != "2" && "$ECODE" != "0"  ]]; then
137     # Ssh exits 255 if the connection timed out. Just ignore that.
138     # Puppet exits 2 if there are changes. For real!
139     # Puppet prints 'Notice: Run of Puppet configuration client already in progress' if another puppet process
140     #   was already running
141     echo "ERROR running puppet on $node: exit code $ECODE"
142     if [[ "$DEBUG" == "0" ]]; then
143       title "Command output follows:"
144       echo $RESULT
145     fi
146   fi
147   if [[ "$ECODE" == "255" ]]; then
148     title "Connection timed out"
149     ECODE=0
150   fi
151   if [[ "$ECODE" == "2" ]]; then
152     ECODE=0
153   fi
154
155   if [[ "$ECODE" == "0" ]]; then
156       rm -f $TMP_FILE
157       echo $node successfully updates
158   else
159       echo $node exit code: $ECODE see $TMP_FILE for details
160   fi
161 }
162
163 function run_command() {
164   node=$1
165   return_var=$2
166   command=$3
167
168   title "Running '$command' on $node"
169   TMP_FILE=`mktemp`
170   if [[ "$DEBUG" != "0" ]]; then
171     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" root@$node -C "$command" | tee $TMP_FILE
172   else
173     ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" root@$node -C "$command" > $TMP_FILE 2>&1
174   fi
175
176   ECODE=$?
177   RESULT=$(cat $TMP_FILE)
178
179   if [[ "$ECODE" != "255" && "$ECODE" != "0"  ]]; then
180     # Ssh exists 255 if the connection timed out. Just ignore that, it's possible that this node is
181     #   a shell node that is down.
182     title "ERROR running command on $node: exit code $ECODE"
183     if [[ "$DEBUG" == "0" ]]; then
184       title "Command output follows:"
185       echo $RESULT
186     fi
187   fi
188   if [[ "$ECODE" == "255" ]]; then
189     title "Connection timed out"
190     ECODE=0
191   fi
192   rm -f $TMP_FILE
193   eval "$return_var=$ECODE"
194 }
195
196 if [[ "$NODE" == "" ]] || [[ "$NODE" == "$IDENTIFIER.arvadosapi.com" ]]; then
197   title "Updating API server"
198   SUM_ECODE=0
199   run_puppet $IDENTIFIER.arvadosapi.com ECODE
200   SUM_ECODE=$(($SUM_ECODE + $ECODE))
201
202   if [[ "$SUM_ECODE" != "0" ]]; then
203     title "ERROR: Updating API server FAILED"
204     EXITCODE=$(($EXITCODE + $SUM_ECODE))
205     exit $EXITCODE
206   fi
207 fi
208
209 if [[ "$NODE" == "$IDENTIFIER.arvadosapi.com" ]]; then
210         # we are done
211         exit 0
212 fi
213
214 title "Loading ARVADOS_API_HOST and ARVADOS_API_TOKEN"
215 if [[ -f "$HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf" ]]; then
216   . $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf
217 else
218   title "WARNING: $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf not found."
219 fi
220 if [[ "$ARVADOS_API_HOST" == "" ]] || [[ "$ARVADOS_API_TOKEN" == "" ]]; then
221   title "ERROR: ARVADOS_API_HOST and/or ARVADOS_API_TOKEN environment variables are not set."
222   exit 1
223 fi
224
225 title "Gathering list of shell and Keep nodes"
226 SHELL_NODES=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN rvm-exec default arv virtual_machine list |jq .items[].hostname -r`
227 KEEP_NODES=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN rvm-exec default arv keep_service list |jq .items[].service_host -r`
228
229 nodes=""
230 for n in workbench manage switchyard $SHELL_NODES $KEEP_NODES; do
231   ECODE=0
232   if [[ $n =~ $ARVADOS_API_HOST$ ]]; then
233     # e.g. keep.qr1hi.arvadosapi.com
234     node=$n
235   else
236     # e.g. shell
237     node=$n.$ARVADOS_API_HOST
238   fi
239         if [[ "$NODE" == "" ]] || [[ "$NODE" == "$node" ]]; then
240           # e.g. keep.qr1hi
241           nodes="$nodes ${node%.arvadosapi.com}"
242         fi
243 done
244
245 if [[ "$nodes" != "" ]]; then
246   ## at this point nodes should be an array containing
247   ## manage.qr1hi,  keep.qr1hi, etc
248   ## that should be defined in the .ssh/config file
249   title "Updating in parallel: $nodes"
250   export -f run_puppet
251   export -f title
252   export SSH_PORT
253   export PUPPET_AGENT
254   echo $nodes|xargs -d " " -n 1 -P $PUPPET_CONCURRENCY -I {} bash -c "run_puppet {}"
255 fi
256
257 if [[ "$NODE" == "" ]]; then
258   title "Locating Arvados Standard Docker images project"
259
260   JSON_FILTER="[[\"name\", \"=\", \"Arvados Standard Docker Images\"], [\"owner_uuid\", \"=\", \"$IDENTIFIER-tpzed-000000000000000\"]]"
261   DOCKER_IMAGES_PROJECT=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv --format=uuid group list --filters="$JSON_FILTER"`
262
263   if [[ "$DOCKER_IMAGES_PROJECT" == "" ]]; then
264     title "Warning: Arvados Standard Docker Images project not found. Creating it."
265
266     DOCKER_IMAGES_PROJECT=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv --format=uuid group create --group "{\"owner_uuid\":\"$IDENTIFIER-tpzed-000000000000000\", \"name\":\"Arvados Standard Docker Images\", \"group_class\":\"project\"}"`
267     ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv link create --link "{\"tail_uuid\":\"$IDENTIFIER-j7d0g-fffffffffffffff\", \"head_uuid\":\"$DOCKER_IMAGES_PROJECT\", \"link_class\":\"permission\", \"name\":\"can_read\" }"
268     if [[ "$?" != "0" ]]; then
269       title "ERROR: could not create standard Docker images project Please create it, cf. http://doc.arvados.org/install/create-standard-objects.html"
270       exit 1
271     fi
272   fi
273
274   title "Found Arvados Standard Docker Images project with uuid $DOCKER_IMAGES_PROJECT"
275   GIT_COMMIT=`ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "/usr/share/python2.7/dist/python-arvados-cwl-runner/bin/python -c 'import arvados_cwl ; print arvados_cwl.__version__'" 2>&1 |grep -v INFO:rdflib:RDFLib`
276
277   if [[ "$?" != "0" ]] || [[ "$GIT_COMMIT" == "" ]]; then
278     title "ERROR: unable to get arvados/jobs Docker image git revision"
279     exit 1
280   else
281     title "Found git commit for arvados/jobs Docker image: $GIT_COMMIT"
282   fi
283
284   run_command shell.$IDENTIFIER ECODE "ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN /usr/local/rvm/bin/rvm-exec default arv keep docker" |grep -q $GIT_COMMIT
285
286   if [[ "$?" == "0" ]]; then
287     title "Found latest arvados/jobs Docker image, nothing to upload"
288     # Just in case it isn't yet, tag the image as latest
289     ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv-keepdocker arvados/jobs latest"
290   else
291     title "Installing latest arvados/jobs Docker image"
292     ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN /usr/local/rvm/bin/rvm-exec default arv keep docker --pull --project-uuid=$DOCKER_IMAGES_PROJECT arvados/jobs $GIT_COMMIT"
293     ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER docker tag --force >/dev/null 2>&1
294     # docker 1.13 no longer supports --force. Sigh.
295     if [[ "$?" == "125" ]]; then
296       FORCE_TAG=""
297     else
298       FORCE_TAG="--force"
299     fi
300     ## adding latest tag too  refs 9254
301     ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER docker tag $FORCE_TAG arvados/jobs:$GIT_COMMIT arvados/jobs:latest
302     ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv-keepdocker --project-uuid=$DOCKER_IMAGES_PROJECT arvados/jobs latest"
303     if [[ "$?" -ne 0 ]]; then
304       title "'git pull' failed exiting..."
305       exit 1
306     fi
307   fi
308 fi