Merge branch '16921-run-deploy-salt' into master
[arvados-dev.git] / jenkins / run-deploy-salt.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 -e
8
9 DEBUG=0
10 UNMANAGED=0
11 SSH_PORT=22
12 PUPPET_CONCURRENCY=5
13
14 read -d] -r SCOPES <<EOF
15 --scopes
16 '["GET /arvados/v1/virtual_machines",\n
17 "GET /arvados/v1/keep_services",\n
18 "GET /arvados/v1/keep_services/",\n
19 "GET /arvados/v1/groups",\n
20 "GET /arvados/v1/groups/",\n
21 "GET /arvados/v1/links",\n
22 "GET /arvados/v1/collections",\n
23 "POST /arvados/v1/collections",\n
24 "POST /arvados/v1/links",\n
25 "GET /arvados/v1/users/current",\n
26 "POST /arvados/v1/users/current",\n
27 "GET /arvados/v1/jobs",\n
28 "POST /arvados/v1/jobs",\n
29 "GET /arvados/v1/pipeline_instances",\n
30 "POST /arvados/v1/pipeline_instances",\n
31 "PUT /arvados/v1/pipeline_instances/",\n
32 "GET /arvados/v1/collections/",\n
33 "POST /arvados/v1/collections/",\n
34 "GET /arvados/v1/logs"]'
35 EOF
36
37 function usage {
38     echo >&2
39     echo >&2 "usage: $0 [options] <identifier>"
40     echo >&2
41     echo >&2 "   <identifier>                 Arvados cluster name"
42     echo >&2
43     echo >&2 "$0 options:"
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: the SALT_MASTER environment variable needs to be set to the ssh host"
48     echo >&2 "      of your salt master."
49     echo >&2
50     echo >&2 "Note: this script requires an arvados token created with these permissions:"
51     echo >&2 '  arv api_client_authorization create_system_auth \'
52     echo -e $SCOPES"]'" >&2
53     echo >&2
54 }
55
56
57 # NOTE: This requires GNU getopt (part of the util-linux package on Debian-based distros).
58 TEMP=`getopt -o hd \
59     --long help,debug \
60     -n "$0" -- "$@"`
61
62 if [ $? != 0 ] ; then echo "Use -h for help"; exit 1 ; fi
63 # Note the quotes around `$TEMP': they are essential!
64 eval set -- "$TEMP"
65
66 while [ $# -ge 1 ]
67 do
68     case $1 in
69         -d | --debug)
70             DEBUG=1
71             set -x
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 title () {
93   date=`date +'%Y-%m-%d %H:%M:%S'`
94   printf "$date $1\n"
95 }
96
97 function run_salt() {
98   cluster=$1
99   if [[ ! -z "$2" ]]; then
100     E="env=$2"
101   else
102     E=""
103   fi
104   shift
105   shift
106   ssh -o "ConnectTimeout 5" -o "LogLevel QUIET" $SALT_MASTER sudo salt --out=txt \'*$cluster*\' cmd.run \'$(IFS=\0;echo "$@")\' $E
107 }
108
109 if [[ -z "$SALT_MASTER" ]]; then
110   title "ERROR: SALT_MASTER environment variable is not set."
111   exit 1
112 fi
113
114 run_salt $IDENTIFIER '' 'apt update && apt -y upgrade'
115
116 title "Loading ARVADOS_API_HOST and ARVADOS_API_TOKEN"
117 if [[ -f "$HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf" ]]; then
118   . $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf
119 else
120   title "WARNING: $HOME/.config/arvados/$IDENTIFIER.arvadosapi.com.conf not found."
121 fi
122 if [[ "$ARVADOS_API_HOST" == "" ]] || [[ "$ARVADOS_API_TOKEN" == "" ]]; then
123   title "ERROR: ARVADOS_API_HOST and/or ARVADOS_API_TOKEN environment variables are not set."
124   exit 1
125 fi
126
127 title "Locating Arvados Standard Docker images project"
128
129 JSON_FILTER="[[\"name\", \"=\", \"Arvados Standard Docker Images\"], [\"owner_uuid\", \"=\", \"$IDENTIFIER-tpzed-000000000000000\"]]"
130 DOCKER_IMAGES_PROJECT=`ARVADOS_API_HOST=$ARVADOS_API_HOST ARVADOS_API_TOKEN=$ARVADOS_API_TOKEN arv --format=uuid group list --filters="$JSON_FILTER"`
131
132 if [[ "$DOCKER_IMAGES_PROJECT" == "" ]]; then
133   title "Warning: Arvados Standard Docker Images project not found. Creating it."
134
135   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\"}"`
136   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\" }"
137   if [[ "$?" != "0" ]]; then
138     title "ERROR: could not create standard Docker images project Please create it, cf. http://doc.arvados.org/install/create-standard-objects.html"
139     exit 1
140   fi
141 fi
142
143 title "Found Arvados Standard Docker Images project with uuid $DOCKER_IMAGES_PROJECT"
144 VERSION=$(run_salt shell.$IDENTIFIER '' 'apt-cache policy python3-arvados-cwl-runner' | grep Candidate |awk '{print $3}' |cut -f1 -d-)
145
146 if [[ "$?" != "0" ]] || [[ "$VERSION" == "" ]]; then
147   title "ERROR: unable to get python3-arvados-cwl-runner version"
148   exit 1
149 else
150   title "Found version for python3-arvados-cwl-runner: $VERSION"
151 fi
152
153 set +e
154 run_salt "shell.$IDENTIFIER" "'{\"ARVADOS_API_HOST\": \"$ARVADOS_API_HOST\", \"ARVADOS_API_TOKEN\": \"$ARVADOS_API_TOKEN\"}'" "arv-keepdocker" |grep -qP "arvados/jobs +$VERSION "
155 if [[ $? -eq 0 ]]; then
156   set -e
157   title "Found arvados/jobs Docker image version $VERSION, nothing to upload"
158 else
159   set -e
160   title "Installing arvados/jobs Docker image version $VERSION"
161   run_salt "shell.$IDENTIFIER" "'{\"ARVADOS_API_HOST\": \"$ARVADOS_API_HOST\", \"ARVADOS_API_TOKEN\": \"$ARVADOS_API_TOKEN\"}'" "arv-keepdocker --pull --project-uuid=$DOCKER_IMAGES_PROJECT arvados/jobs $VERSION"
162   if [[ $? -ne 0 ]]; then
163     title "'arv-keepdocker' failed..."
164     exit 1
165   fi
166 fi