Merge branch '21606-keep-web-output-buffer'
[arvados.git] / tools / salt-install / installer.sh
1 #!/bin/bash
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: CC-BY-SA-3.0
6
7 #
8 # installer.sh
9 #
10 # Helps manage the configuration in a git repository, and then deploy
11 # nodes by pushing a copy of the git repository to each node and
12 # running the provision script to do the actual installation and
13 # configuration.
14 #
15
16 set -eu
17 set -o pipefail
18
19 # The parameter file
20 declare CONFIG_FILE=local.params
21
22 # The salt template directory
23 declare CONFIG_DIR=local_config_dir
24
25 # The 5-character Arvados cluster id
26 # This will be populated by loadconfig()
27 declare CLUSTER
28
29 # The parent domain (not including the cluster id)
30 # This will be populated by loadconfig()
31 declare DOMAIN
32
33 # A bash associative array listing each node and mapping to the roles
34 # that should be provisioned on those nodes.
35 # This will be populated by loadconfig()
36 declare -A NODES
37
38 # A bash associative array listing each role and mapping to the nodes
39 # that should be provisioned with this role.
40 # This will be populated by loadconfig()
41 declare -A ROLE2NODES
42
43 # The ssh user we'll use
44 # This will be populated by loadconfig()
45 declare DEPLOY_USER
46
47 # The git repository that we'll push to on all the nodes
48 # This will be populated by loadconfig()
49 declare GITTARGET
50
51 # The public host used as an SSH jump host
52 # This will be populated by loadconfig()
53 declare USE_SSH_JUMPHOST
54
55 # The temp file that will get used to disable envvar forwarding to avoid locale
56 # issues in Debian distros.
57 # This will be populated by loadconfig()
58 declare SSH_CONFFILE
59
60 checktools() {
61   local MISSING=''
62   for a in git ip; do
63     if ! which $a; then
64       MISSING="$MISSING $a"
65     fi
66   done
67   if [[ -n "$MISSING" ]]; then
68     echo "Some tools are missing, please make sure you have the 'git' and 'iproute2' packages installed"
69     exit 1
70   fi
71 }
72
73 cleanup() {
74   local NODE=$1
75   local SSH=$(ssh_cmd "$NODE")
76   # Delete the old repository
77   $SSH $DEPLOY_USER@$NODE rm -rf ${GITTARGET}.git ${GITTARGET}
78 }
79
80 sync() {
81   local NODE=$1
82   local BRANCH=$2
83
84   # Synchronizes the configuration by creating a git repository on
85   # each node, pushing our branch, and updating the checkout.
86
87   if [[ "$NODE" != localhost ]]; then
88     SSH=$(ssh_cmd "$NODE")
89     GIT="eval $(git_cmd $NODE)"
90
91     cleanup $NODE
92
93     # Update the git remote for the remote repository.
94     if ! $GIT remote add $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git; then
95       $GIT remote set-url $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git
96     fi
97
98     # Initialize the git repository.  We're
99     # actually going to make two repositories here because git
100     # will complain if you try to push to a repository with a
101     # checkout. So we're going to create a "bare" repository
102     # and then clone a regular repository (with a checkout)
103     # from that.
104
105     $SSH $DEPLOY_USER@$NODE git init --bare --shared=0600 ${GITTARGET}.git
106     if [[ "$BRANCH" == "HEAD" ]]; then
107       # When deploying from an individual commit instead of a branch. This can
108       # happen when deploying from a Jenkins pipeline.
109       $GIT push $NODE HEAD:refs/heads/HEAD
110       $SSH $DEPLOY_USER@$NODE "umask 0077 && git clone -s ${GITTARGET}.git ${GITTARGET} && git -C ${GITTARGET} checkout remotes/origin/HEAD"
111     else
112       $GIT push $NODE $BRANCH
113       $SSH $DEPLOY_USER@$NODE "umask 0077 && git clone -s ${GITTARGET}.git ${GITTARGET} && git -C ${GITTARGET} checkout ${BRANCH}"
114     fi
115   fi
116 }
117
118 deploynode() {
119   local NODE=$1
120   local ROLES=$2
121   local BRANCH=$3
122
123   # Deploy a node.  This runs the provision script on the node, with
124   # the appropriate roles.
125
126   sync $NODE $BRANCH
127
128   if [[ -z "$ROLES" ]]; then
129     echo "No roles specified for $NODE, will deploy all roles"
130   else
131     ROLES="--roles ${ROLES}"
132   fi
133
134   logfile=deploy-${NODE}-$(date -Iseconds).log
135   SSH=$(ssh_cmd "$NODE")
136
137   if [[ "$NODE" = localhost ]]; then
138     SUDO=''
139     if [[ $(whoami) != 'root' ]]; then
140       SUDO=sudo
141     fi
142     $SUDO ./provision.sh --config ${CONFIG_FILE} ${ROLES} 2>&1 | tee $logfile
143   else
144     $SSH $DEPLOY_USER@$NODE "cd ${GITTARGET} && git log -n1 HEAD && DISABLED_CONTROLLER=\"$DISABLED_CONTROLLER\" sudo --preserve-env=DISABLED_CONTROLLER ./provision.sh --config ${CONFIG_FILE} ${ROLES}" 2>&1 | tee $logfile
145     cleanup $NODE
146   fi
147 }
148
149 checkcert() {
150   local CERTNAME=$1
151   local CERTPATH="${CONFIG_DIR}/certs/${CERTNAME}"
152   if [[ ! -f "${CERTPATH}.crt" || ! -e "${CERTPATH}.key" ]]; then
153     echo "Missing ${CERTPATH}.crt or ${CERTPATH}.key files"
154     exit 1
155   fi
156 }
157
158 loadconfig() {
159   if ! [[ -s ${CONFIG_FILE} && -s ${CONFIG_FILE}.secrets ]]; then
160     echo "Must be run from initialized setup dir, maybe you need to 'initialize' first?"
161   fi
162   source common.sh
163   GITTARGET=arvados-deploy-config-${CLUSTER}
164
165   # Set up SSH so that it doesn't forward any environment variable. This is to avoid
166   # getting "setlocale" errors on the first run, depending on the distro being used
167   # to run the installer (like Debian).
168   SSH_CONFFILE=$(mktemp)
169   echo "Include config SendEnv -*" >${SSH_CONFFILE}
170 }
171
172 ssh_cmd() {
173   local NODE=$1
174   if [ -z "${USE_SSH_JUMPHOST}" -o "${NODE}" == "${USE_SSH_JUMPHOST}" -o "${NODE}" == "localhost" ]; then
175     echo "ssh -F ${SSH_CONFFILE}"
176   else
177     echo "ssh -F ${SSH_CONFFILE} -J ${DEPLOY_USER}@${USE_SSH_JUMPHOST}"
178   fi
179 }
180
181 git_cmd() {
182   local NODE=$1
183   echo "GIT_SSH_COMMAND=\"$(ssh_cmd ${NODE})\" git"
184 }
185
186 set +u
187 subcmd="$1"
188 set -u
189
190 if [[ -n "$subcmd" ]]; then
191   shift
192 fi
193 case "$subcmd" in
194 initialize)
195   if [[ ! -f provision.sh ]]; then
196     echo "Must be run from arvados/tools/salt-install"
197     exit
198   fi
199
200   checktools
201
202   set +u
203   SETUPDIR=$1
204   PARAMS=$2
205   SLS=$3
206   TERRAFORM=$4
207   set -u
208
209   err=
210   if [[ -z "$PARAMS" || ! -f local.params.example.$PARAMS ]]; then
211     echo "Not found: local.params.example.$PARAMS"
212     echo "Expected one of multiple_hosts, single_host_multiple_hostnames, single_host_single_hostname"
213     err=1
214   fi
215
216   if [[ -z "$SLS" || ! -d config_examples/$SLS ]]; then
217     echo "Not found: config_examples/$SLS"
218     echo "Expected one of multi_host/aws, single_host/multiple_hostnames, single_host/single_hostname"
219     err=1
220   fi
221
222   if [[ -z "$SETUPDIR" || -z "$PARAMS" || -z "$SLS" ]]; then
223     echo "installer.sh <setup dir to initialize> <params template> <config template>"
224     err=1
225   fi
226
227   if [[ -n "$err" ]]; then
228     exit 1
229   fi
230
231   echo "Initializing $SETUPDIR"
232   git init --shared=0600 $SETUPDIR
233   cp -r *.sh tests $SETUPDIR
234
235   cp local.params.example.$PARAMS $SETUPDIR/${CONFIG_FILE}
236   cp local.params.secrets.example $SETUPDIR/${CONFIG_FILE}.secrets
237   cp -r config_examples/$SLS $SETUPDIR/${CONFIG_DIR}
238
239   if [[ -n "$TERRAFORM" ]]; then
240     mkdir $SETUPDIR/terraform
241     cp -r $TERRAFORM/* $SETUPDIR/terraform/
242   fi
243
244   cd $SETUPDIR
245   echo '*.log' >.gitignore
246   echo '**/.terraform' >>.gitignore
247   echo '**/.infracost' >>.gitignore
248
249   if [[ -n "$TERRAFORM" ]]; then
250     git add terraform
251   fi
252
253   git add *.sh ${CONFIG_FILE} ${CONFIG_FILE}.secrets ${CONFIG_DIR} tests .gitignore
254   git commit -m"initial commit"
255
256   echo
257   echo "Setup directory $SETUPDIR initialized."
258   if [[ -n "$TERRAFORM" ]]; then
259     (cd $SETUPDIR/terraform/vpc && terraform init)
260     (cd $SETUPDIR/terraform/data-storage && terraform init)
261     (cd $SETUPDIR/terraform/services && terraform init)
262     echo "Now go to $SETUPDIR, customize 'terraform/vpc/terraform.tfvars' as needed, then run 'installer.sh terraform'"
263   else
264     echo "Now go to $SETUPDIR, customize '${CONFIG_FILE}', '${CONFIG_FILE}.secrets' and '${CONFIG_DIR}' as needed, then run 'installer.sh deploy'"
265   fi
266   ;;
267
268 terraform)
269   logfile=terraform-$(date -Iseconds).log
270   (cd terraform/vpc && terraform apply -auto-approve) 2>&1 | tee -a $logfile
271   (cd terraform/data-storage && terraform apply -auto-approve) 2>&1 | tee -a $logfile
272   (cd terraform/services && terraform apply -auto-approve) 2>&1 | grep -v letsencrypt_iam_secret_access_key | tee -a $logfile
273   (cd terraform/services && echo -n 'letsencrypt_iam_secret_access_key = ' && terraform output letsencrypt_iam_secret_access_key) 2>&1 | tee -a $logfile
274   ;;
275
276 terraform-destroy)
277   logfile=terraform-$(date -Iseconds).log
278   (cd terraform/services && terraform destroy) 2>&1 | tee -a $logfile
279   (cd terraform/data-storage && terraform destroy) 2>&1 | tee -a $logfile
280   (cd terraform/vpc && terraform destroy) 2>&1 | tee -a $logfile
281   ;;
282
283 generate-tokens)
284   for i in BLOB_SIGNING_KEY MANAGEMENT_TOKEN SYSTEM_ROOT_TOKEN ANONYMOUS_USER_TOKEN DATABASE_PASSWORD; do
285     echo ${i}=$(
286       tr -dc A-Za-z0-9 </dev/urandom | head -c 32
287       echo ''
288     )
289   done
290   ;;
291
292 deploy)
293   set +u
294   NODE=$1
295   set -u
296
297   checktools
298
299   loadconfig
300
301   if grep -rni 'fixme' ${CONFIG_FILE} ${CONFIG_FILE}.secrets ${CONFIG_DIR}; then
302     echo
303     echo "Some parameters still need to be updated.  Please fix them and then re-run deploy."
304     exit 1
305   fi
306
307   if [[ -z "${DATABASE_POSTGRESQL_VERSION:-}" ]]; then
308     echo
309     echo "Please configure DATABASE_POSTGRESQL_VERSION in local.params: It should match the version of the PostgreSQL service you're going to use."
310     exit 1
311   fi
312
313   if [[ ${SSL_MODE} == "bring-your-own" ]]; then
314     if [[ ! -z "${ROLE2NODES['balancer']:-}" ]]; then
315       checkcert balancer
316     fi
317     if [[ ! -z "${ROLE2NODES['controller']:-}" ]]; then
318       checkcert controller
319     fi
320     if [[ ! -z "${ROLE2NODES['keepproxy']:-}" ]]; then
321       checkcert keepproxy
322     fi
323     if [[ ! -z "${ROLE2NODES['keepweb']:-}" ]]; then
324       checkcert collections
325       checkcert download
326     fi
327     if [[ ! -z "${ROLE2NODES['monitoring']:-}" ]]; then
328       checkcert grafana
329       checkcert prometheus
330     fi
331     if [[ ! -z "${ROLE2NODES['webshell']:-}" ]]; then
332       checkcert webshell
333     fi
334     if [[ ! -z "${ROLE2NODES['websocket']:-}" ]]; then
335       checkcert websocket
336     fi
337     if [[ ! -z "${ROLE2NODES['workbench']:-}" ]]; then
338       checkcert workbench
339     fi
340     if [[ ! -z "${ROLE2NODES['workbench2']:-}" ]]; then
341       checkcert workbench2
342     fi
343   fi
344
345   BRANCH=$(git rev-parse --abbrev-ref HEAD)
346
347   set -x
348
349   git add -A
350   if ! git diff --cached --exit-code --quiet; then
351     git commit -m"prepare for deploy"
352   fi
353
354   # Used for rolling updates to disable individual nodes at the
355   # load balancer.
356   export DISABLED_CONTROLLER=""
357   if [[ -z "$NODE" ]]; then
358     for NODE in "${!NODES[@]}"; do
359       # First, just confirm we can ssh to each node.
360       $(ssh_cmd "$NODE") $DEPLOY_USER@$NODE true
361     done
362
363     for NODE in "${!NODES[@]}"; do
364       # Do 'database' role first,
365       if [[ "${NODES[$NODE]}" =~ database ]]; then
366         deploynode $NODE "${NODES[$NODE]}" $BRANCH
367         unset NODES[$NODE]
368       fi
369     done
370
371     BALANCER=${ROLE2NODES['balancer']:-}
372
373     # Check if there are multiple controllers, they'll be comma-separated
374     # in ROLE2NODES
375     if [[ ${ROLE2NODES['controller']} =~ , ]]; then
376       # If we have multiple controllers then there must be
377       # load balancer. We want to do a rolling update, take
378       # down each node at the load balancer before updating
379       # it.
380
381       for NODE in "${!NODES[@]}"; do
382         if [[ "${NODES[$NODE]}" =~ controller ]]; then
383           export DISABLED_CONTROLLER=$NODE
384
385           # Update balancer that the node is disabled
386           deploynode $BALANCER "${NODES[$BALANCER]}" $BRANCH
387
388           # Now update the node itself
389           deploynode $NODE "${NODES[$NODE]}" $BRANCH
390           unset NODES[$NODE]
391         fi
392       done
393     else
394       # Only one controller, check if it wasn't already taken care of.
395       NODE=${ROLE2NODES['controller']}
396       if [[ ! -z "${NODES[$NODE]:-}" ]]; then
397         deploynode $NODE "${NODES[$NODE]}" $BRANCH
398         unset NODES[$NODE]
399       fi
400     fi
401
402     if [[ -n "$BALANCER" ]]; then
403       # Deploy balancer. In the rolling update case, this
404       # will re-enable all the controllers at the balancer.
405       export DISABLED_CONTROLLER=""
406       deploynode $BALANCER "${NODES[$BALANCER]}" $BRANCH
407       unset NODES[$BALANCER]
408     fi
409
410     for NODE in "${!NODES[@]}"; do
411       # Everything else (we removed the nodes that we
412       # already deployed from the list)
413       deploynode $NODE "${NODES[$NODE]}" $BRANCH
414     done
415   else
416     # Just deploy the node that was supplied on the command line.
417     deploynode $NODE "${NODES[$NODE]}" $BRANCH
418   fi
419
420   set +x
421   echo
422   echo "Completed deploy, run 'installer.sh diagnostics' to verify the install"
423
424   ;;
425
426 diagnostics)
427   loadconfig
428
429   set +u
430   declare LOCATION=$1
431   set -u
432
433   if ! which arvados-client; then
434     echo "arvados-client not found, install 'arvados-client' package with 'apt-get' or 'yum'"
435     exit 1
436   fi
437
438   if [[ -z "$LOCATION" ]]; then
439     echo "Need to provide '-internal-client' or '-external-client'"
440     echo
441     echo "-internal-client    You are running this on the same private network as the Arvados cluster (e.g. on one of the Arvados nodes)"
442     echo "-external-client    You are running this outside the private network of the Arvados cluster (e.g. your workstation)"
443     exit 1
444   fi
445
446   export ARVADOS_API_HOST="${DOMAIN}:${CONTROLLER_EXT_SSL_PORT}"
447   export ARVADOS_API_TOKEN="$SYSTEM_ROOT_TOKEN"
448
449   arvados-client diagnostics $LOCATION
450   ;;
451
452 *)
453   echo "Arvados installer"
454   echo ""
455   echo "initialize        initialize the setup directory for configuration"
456   echo "terraform         create cloud resources using terraform"
457   echo "terraform-destroy destroy cloud resources created by terraform"
458   echo "generate-tokens   generate random values for tokens"
459   echo "deploy            deploy the configuration from the setup directory"
460   echo "diagnostics       check your install using diagnostics"
461   ;;
462 esac