3 # Copyright (C) The Arvados Authors. All rights reserved.
5 # SPDX-License-Identifier: AGPL-3.0
11 read -d] -r SCOPES <<EOF
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"]'
36 echo >&2 "usage: $0 [options] <identifier>"
38 echo >&2 " <identifier> Arvados cluster name"
40 echo >&2 "$0 options:"
41 echo >&2 " -p, --port <ssh port> SSH port to use (default 22)"
42 echo >&2 " -c, --concurrency <max> Maximum concurrency for puppet runs (default 5)"
43 echo >&2 " -d, --debug Enable debug output"
44 echo >&2 " -h, --help Display this help and exit"
46 echo >&2 "Note: this script requires an arvados token created with these permissions:"
47 echo >&2 ' arv api_client_authorization create_system_auth \'
48 echo -e $SCOPES"]'" >&2
53 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
54 TEMP=`getopt -o hdp:c: \
55 --long help,debug,port:,concurrency: \
58 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
59 # Note the quotes around `$TEMP': they are essential!
66 SSH_PORT="$2"; shift 2
69 PUPPET_CONCURRENCY="$2"; shift 2
88 if [[ "$IDENTIFIER" == '' ]]; then
99 let endtime="$(now) + 600"
100 while [ "$endtime" -gt "$(now)" ]; do
101 puppet agent --test --detailed-exitcodes
103 if [ 0 = "$agent_exitcode" ] || [ 2 = "$agent_exitcode" ]; then
109 exit ${agent_exitcode:-99}
113 date=`date +'%Y-%m-%d %H:%M:%S'`
117 function run_puppet() {
120 title "Running puppet on $node"
121 sleep $[ $RANDOM / 6000 ].$[ $RANDOM / 1000 ]
123 if [[ "$DEBUG" != "0" ]]; then
124 ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" | tee $TMP_FILE
126 ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 5" root@$node -C bash -c "'$PUPPET_AGENT'" > $TMP_FILE 2>&1
129 ECODE=${PIPESTATUS[0]}
130 RESULT=$(cat $TMP_FILE)
132 if [[ "$ECODE" != "255" && ! ("$RESULT" =~ 'already in progress') && "$ECODE" != "2" && "$ECODE" != "0" ]]; then
133 # Ssh exits 255 if the connection timed out. Just ignore that.
134 # Puppet exits 2 if there are changes. For real!
135 # Puppet prints 'Notice: Run of Puppet configuration client already in progress' if another puppet process
136 # was already running
137 echo "ERROR running puppet on $node: exit code $ECODE"
138 if [[ "$DEBUG" == "0" ]]; then
139 title "Command output follows:"
143 if [[ "$ECODE" == "255" ]]; then
144 title "Connection timed out"
147 if [[ "$ECODE" == "2" ]]; then
151 if [[ "$ECODE" == "0" ]]; then
153 echo $node successfully updates
155 echo $node exit code: $ECODE see $TMP_FILE for details
159 function run_command() {
164 title "Running '$command' on $node"
166 if [[ "$DEBUG" != "0" ]]; then
167 ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" root@$node -C "$command" | tee $TMP_FILE
169 ssh -t -p$SSH_PORT -o "StrictHostKeyChecking no" -o "ConnectTimeout 125" root@$node -C "$command" > $TMP_FILE 2>&1
173 RESULT=$(cat $TMP_FILE)
175 if [[ "$ECODE" != "255" && "$ECODE" != "0" ]]; then
176 # Ssh exists 255 if the connection timed out. Just ignore that, it's possible that this node is
177 # a shell node that is down.
178 title "ERROR running command on $node: exit code $ECODE"
179 if [[ "$DEBUG" == "0" ]]; then
180 title "Command output follows:"
184 if [[ "$ECODE" == "255" ]]; then
185 title "Connection timed out"
189 eval "$return_var=$ECODE"
192 title "Updating API server"
194 run_puppet $IDENTIFIER.arvadosapi.com ECODE
195 SUM_ECODE=$(($SUM_ECODE + $ECODE))
197 if [[ "$SUM_ECODE" != "0" ]]; then
198 title "ERROR: Updating API server FAILED"
199 EXITCODE=$(($EXITCODE + $SUM_ECODE))
203 title "Loading ARVADOS_API_HOST and ARVADOS_API_TOKEN"
204 if [[ -f "$HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf" ]]; then
205 . $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf
207 title "WARNING: $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf not found."
209 if [[ "$ARVADOS_API_HOST" == "" ]] || [[ "$ARVADOS_API_TOKEN" == "" ]]; then
210 title "ERROR: ARVADOS_API_HOST and/or ARVADOS_API_TOKEN environment variables are not set."
214 title "Gathering list of shell and Keep nodes"
215 SHELL_NODES=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv virtual_machine list |jq .items[].hostname -r`
216 KEEP_NODES=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv keep_service list |jq .items[].service_host -r`
219 for n in workbench manage switchyard $SHELL_NODES $KEEP_NODES; do
221 if [[ $n =~ $ARVADOS_API_HOST$ ]]; then
222 # e.g. keep.qr1hi.arvadosapi.com
226 node=$n.$ARVADOS_API_HOST
229 nodes="$nodes ${node%.arvadosapi.com}"
232 ## at this point nodes should be an array containing
233 ## manage.qr1hi, keep.qr1hi, etc
234 ## that should be defined in the .ssh/config file
235 title "Updating in parallel: $nodes"
240 echo $nodes|xargs -d " " -n 1 -P $PUPPET_CONCURRENCY -I {} bash -c "run_puppet {}"
242 title "Locating Arvados Standard Docker images project"
244 JSON_FILTER="[[\"name\", \"=\", \"Arvados Standard Docker Images\"], [\"owner_uuid\", \"=\", \"$IDENTIFIER-tpzed-000000000000000\"]]"
245 DOCKER_IMAGES_PROJECT=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv --format=uuid group list --filters="$JSON_FILTER"`
247 if [[ "$DOCKER_IMAGES_PROJECT" == "" ]]; then
248 title "Warning: Arvados Standard Docker Images project not found. Creating it."
250 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\"}"`
251 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\" }"
252 if [[ "$?" != "0" ]]; then
253 title "ERROR: could not create standard Docker images project Please create it, cf. http://doc.arvados.org/install/create-standard-objects.html"
258 title "Found Arvados Standard Docker Images project with uuid $DOCKER_IMAGES_PROJECT"
259 GIT_COMMIT=`ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "python -c 'import arvados_cwl ; print arvados_cwl.__version__'" 2>&1 |grep -v INFO:rdflib:RDFLib`
261 if [[ "$?" != "0" ]] || [[ "$GIT_COMMIT" == "" ]]; then
262 title "ERROR: unable to get arvados/jobs Docker image git revision"
265 title "Found git commit for arvados/jobs Docker image: $GIT_COMMIT"
268 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
270 if [[ "$?" == "0" ]]; then
271 title "Found latest arvados/jobs Docker image, nothing to upload"
272 # Just in case it isn't yet, tag the image as latest
273 ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER "ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv-keepdocker arvados/jobs latest"
275 title "Installing latest arvados/jobs Docker image"
276 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"
277 ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER docker tag --force >/dev/null 2>&1
278 # docker 1.13 no longer supports --force. Sigh.
279 if [[ "$?" == "125" ]]; then
284 ## adding latest tag too refs 9254
285 ssh -o "StrictHostKeyChecking no" shell.$IDENTIFIER docker tag $FORCE_TAG arvados/jobs:$GIT_COMMIT arvados/jobs:latest
286 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"
287 if [[ "$?" -ne 0 ]]; then
288 title "'git pull' failed exiting..."