Merge branch 'main' from workbench2.git
[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     $GIT push $NODE $BRANCH
107     $SSH $DEPLOY_USER@$NODE "umask 0077 && git clone -s ${GITTARGET}.git ${GITTARGET} && git -C ${GITTARGET} checkout ${BRANCH}"
108   fi
109 }
110
111 deploynode() {
112   local NODE=$1
113   local ROLES=$2
114   local BRANCH=$3
115
116   # Deploy a node.  This runs the provision script on the node, with
117   # the appropriate roles.
118
119   sync $NODE $BRANCH
120
121   if [[ -z "$ROLES" ]]; then
122     echo "No roles specified for $NODE, will deploy all roles"
123   else
124     ROLES="--roles ${ROLES}"
125   fi
126
127   logfile=deploy-${NODE}-$(date -Iseconds).log
128   SSH=$(ssh_cmd "$NODE")
129
130   if [[ "$NODE" = localhost ]]; then
131     SUDO=''
132     if [[ $(whoami) != 'root' ]]; then
133       SUDO=sudo
134     fi
135     $SUDO ./provision.sh --config ${CONFIG_FILE} ${ROLES} 2>&1 | tee $logfile
136   else
137     $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
138     cleanup $NODE
139   fi
140 }
141
142 checkcert() {
143   local CERTNAME=$1
144   local CERTPATH="${CONFIG_DIR}/certs/${CERTNAME}"
145   if [[ ! -f "${CERTPATH}.crt" || ! -e "${CERTPATH}.key" ]]; then
146     echo "Missing ${CERTPATH}.crt or ${CERTPATH}.key files"
147     exit 1
148   fi
149 }
150
151 loadconfig() {
152   if ! [[ -s ${CONFIG_FILE} && -s ${CONFIG_FILE}.secrets ]]; then
153     echo "Must be run from initialized setup dir, maybe you need to 'initialize' first?"
154   fi
155   source common.sh
156   GITTARGET=arvados-deploy-config-${CLUSTER}
157
158   # Set up SSH so that it doesn't forward any environment variable. This is to avoid
159   # getting "setlocale" errors on the first run, depending on the distro being used
160   # to run the installer (like Debian).
161   SSH_CONFFILE=$(mktemp)
162   echo "Include config SendEnv -*" >${SSH_CONFFILE}
163 }
164
165 ssh_cmd() {
166   local NODE=$1
167   if [ -z "${USE_SSH_JUMPHOST}" -o "${NODE}" == "${USE_SSH_JUMPHOST}" -o "${NODE}" == "localhost" ]; then
168     echo "ssh -F ${SSH_CONFFILE}"
169   else
170     echo "ssh -F ${SSH_CONFFILE} -J ${DEPLOY_USER}@${USE_SSH_JUMPHOST}"
171   fi
172 }
173
174 git_cmd() {
175   local NODE=$1
176   echo "GIT_SSH_COMMAND=\"$(ssh_cmd ${NODE})\" git"
177 }
178
179 set +u
180 subcmd="$1"
181 set -u
182
183 if [[ -n "$subcmd" ]]; then
184   shift
185 fi
186 case "$subcmd" in
187 initialize)
188   if [[ ! -f provision.sh ]]; then
189     echo "Must be run from arvados/tools/salt-install"
190     exit
191   fi
192
193   checktools
194
195   set +u
196   SETUPDIR=$1
197   PARAMS=$2
198   SLS=$3
199   TERRAFORM=$4
200   set -u
201
202   err=
203   if [[ -z "$PARAMS" || ! -f local.params.example.$PARAMS ]]; then
204     echo "Not found: local.params.example.$PARAMS"
205     echo "Expected one of multiple_hosts, single_host_multiple_hostnames, single_host_single_hostname"
206     err=1
207   fi
208
209   if [[ -z "$SLS" || ! -d config_examples/$SLS ]]; then
210     echo "Not found: config_examples/$SLS"
211     echo "Expected one of multi_host/aws, single_host/multiple_hostnames, single_host/single_hostname"
212     err=1
213   fi
214
215   if [[ -z "$SETUPDIR" || -z "$PARAMS" || -z "$SLS" ]]; then
216     echo "installer.sh <setup dir to initialize> <params template> <config template>"
217     err=1
218   fi
219
220   if [[ -n "$err" ]]; then
221     exit 1
222   fi
223
224   echo "Initializing $SETUPDIR"
225   git init --shared=0600 $SETUPDIR
226   cp -r *.sh tests $SETUPDIR
227
228   cp local.params.example.$PARAMS $SETUPDIR/${CONFIG_FILE}
229   cp local.params.secrets.example $SETUPDIR/${CONFIG_FILE}.secrets
230   cp -r config_examples/$SLS $SETUPDIR/${CONFIG_DIR}
231
232   if [[ -n "$TERRAFORM" ]]; then
233     mkdir $SETUPDIR/terraform
234     cp -r $TERRAFORM/* $SETUPDIR/terraform/
235   fi
236
237   cd $SETUPDIR
238   echo '*.log' >.gitignore
239   echo '**/.terraform' >>.gitignore
240   echo '**/.infracost' >>.gitignore
241
242   if [[ -n "$TERRAFORM" ]]; then
243     git add terraform
244   fi
245
246   git add *.sh ${CONFIG_FILE} ${CONFIG_FILE}.secrets ${CONFIG_DIR} tests .gitignore
247   git commit -m"initial commit"
248
249   echo
250   echo "Setup directory $SETUPDIR initialized."
251   if [[ -n "$TERRAFORM" ]]; then
252     (cd $SETUPDIR/terraform/vpc && terraform init)
253     (cd $SETUPDIR/terraform/data-storage && terraform init)
254     (cd $SETUPDIR/terraform/services && terraform init)
255     echo "Now go to $SETUPDIR, customize 'terraform/vpc/terraform.tfvars' as needed, then run 'installer.sh terraform'"
256   else
257     echo "Now go to $SETUPDIR, customize '${CONFIG_FILE}', '${CONFIG_FILE}.secrets' and '${CONFIG_DIR}' as needed, then run 'installer.sh deploy'"
258   fi
259   ;;
260
261 terraform)
262   logfile=terraform-$(date -Iseconds).log
263   (cd terraform/vpc && terraform apply -auto-approve) 2>&1 | tee -a $logfile
264   (cd terraform/data-storage && terraform apply -auto-approve) 2>&1 | tee -a $logfile
265   (cd terraform/services && terraform apply -auto-approve) 2>&1 | grep -v letsencrypt_iam_secret_access_key | tee -a $logfile
266   (cd terraform/services && echo -n 'letsencrypt_iam_secret_access_key = ' && terraform output letsencrypt_iam_secret_access_key) 2>&1 | tee -a $logfile
267   ;;
268
269 terraform-destroy)
270   logfile=terraform-$(date -Iseconds).log
271   (cd terraform/services && terraform destroy) 2>&1 | tee -a $logfile
272   (cd terraform/data-storage && terraform destroy) 2>&1 | tee -a $logfile
273   (cd terraform/vpc && terraform destroy) 2>&1 | tee -a $logfile
274   ;;
275
276 generate-tokens)
277   for i in BLOB_SIGNING_KEY MANAGEMENT_TOKEN SYSTEM_ROOT_TOKEN ANONYMOUS_USER_TOKEN WORKBENCH_SECRET_KEY DATABASE_PASSWORD; do
278     echo ${i}=$(
279       tr -dc A-Za-z0-9 </dev/urandom | head -c 32
280       echo ''
281     )
282   done
283   ;;
284
285 deploy)
286   set +u
287   NODE=$1
288   set -u
289
290   checktools
291
292   loadconfig
293
294   if grep -rni 'fixme' ${CONFIG_FILE} ${CONFIG_FILE}.secrets ${CONFIG_DIR}; then
295     echo
296     echo "Some parameters still need to be updated.  Please fix them and then re-run deploy."
297     exit 1
298   fi
299
300   if [[ -z "${DATABASE_POSTGRESQL_VERSION:-}" ]]; then
301     echo
302     echo "Please configure DATABASE_POSTGRESQL_VERSION in local.params: It should match the version of the PostgreSQL service you're going to use."
303     exit 1
304   fi
305
306   if [[ ${SSL_MODE} == "bring-your-own" ]]; then
307     if [[ ! -z "${ROLE2NODES['balancer']:-}" ]]; then
308       checkcert balancer
309     fi
310     if [[ ! -z "${ROLE2NODES['controller']:-}" ]]; then
311       checkcert controller
312     fi
313     if [[ ! -z "${ROLE2NODES['keepproxy']:-}" ]]; then
314       checkcert keepproxy
315     fi
316     if [[ ! -z "${ROLE2NODES['keepweb']:-}" ]]; then
317       checkcert collections
318       checkcert download
319     fi
320     if [[ ! -z "${ROLE2NODES['monitoring']:-}" ]]; then
321       checkcert grafana
322       checkcert prometheus
323     fi
324     if [[ ! -z "${ROLE2NODES['webshell']:-}" ]]; then
325       checkcert webshell
326     fi
327     if [[ ! -z "${ROLE2NODES['websocket']:-}" ]]; then
328       checkcert websocket
329     fi
330     if [[ ! -z "${ROLE2NODES['workbench']:-}" ]]; then
331       checkcert workbench
332     fi
333     if [[ ! -z "${ROLE2NODES['workbench2']:-}" ]]; then
334       checkcert workbench2
335     fi
336   fi
337
338   BRANCH=$(git rev-parse --abbrev-ref HEAD)
339
340   set -x
341
342   git add -A
343   if ! git diff --cached --exit-code --quiet; then
344     git commit -m"prepare for deploy"
345   fi
346
347   # Used for rolling updates to disable individual nodes at the
348   # load balancer.
349   export DISABLED_CONTROLLER=""
350   if [[ -z "$NODE" ]]; then
351     for NODE in "${!NODES[@]}"; do
352       # First, just confirm we can ssh to each node.
353       $(ssh_cmd "$NODE") $DEPLOY_USER@$NODE true
354     done
355
356     for NODE in "${!NODES[@]}"; do
357       # Do 'database' role first,
358       if [[ "${NODES[$NODE]}" =~ database ]]; then
359         deploynode $NODE "${NODES[$NODE]}" $BRANCH
360         unset NODES[$NODE]
361       fi
362     done
363
364     BALANCER=${ROLE2NODES['balancer']:-}
365
366     # Check if there are multiple controllers, they'll be comma-separated
367     # in ROLE2NODES
368     if [[ ${ROLE2NODES['controller']} =~ , ]]; then
369       # If we have multiple controllers then there must be
370       # load balancer. We want to do a rolling update, take
371       # down each node at the load balancer before updating
372       # it.
373
374       for NODE in "${!NODES[@]}"; do
375         if [[ "${NODES[$NODE]}" =~ controller ]]; then
376           export DISABLED_CONTROLLER=$NODE
377
378           # Update balancer that the node is disabled
379           deploynode $BALANCER "${NODES[$BALANCER]}" $BRANCH
380
381           # Now update the node itself
382           deploynode $NODE "${NODES[$NODE]}" $BRANCH
383           unset NODES[$NODE]
384         fi
385       done
386     else
387       # Only one controller, check if it wasn't already taken care of.
388       NODE=${ROLE2NODES['controller']}
389       if [[ ! -z "${NODES[$NODE]:-}" ]]; then
390         deploynode $NODE "${NODES[$NODE]}" $BRANCH
391         unset NODES[$NODE]
392       fi
393     fi
394
395     if [[ -n "$BALANCER" ]]; then
396       # Deploy balancer. In the rolling update case, this
397       # will re-enable all the controllers at the balancer.
398       export DISABLED_CONTROLLER=""
399       deploynode $BALANCER "${NODES[$BALANCER]}" $BRANCH
400       unset NODES[$BALANCER]
401     fi
402
403     for NODE in "${!NODES[@]}"; do
404       # Everything else (we removed the nodes that we
405       # already deployed from the list)
406       deploynode $NODE "${NODES[$NODE]}" $BRANCH
407     done
408   else
409     # Just deploy the node that was supplied on the command line.
410     deploynode $NODE "${NODES[$NODE]}" $BRANCH
411   fi
412
413   set +x
414   echo
415   echo "Completed deploy, run 'installer.sh diagnostics' to verify the install"
416
417   ;;
418
419 diagnostics)
420   loadconfig
421
422   set +u
423   declare LOCATION=$1
424   set -u
425
426   if ! which arvados-client; then
427     echo "arvados-client not found, install 'arvados-client' package with 'apt-get' or 'yum'"
428     exit 1
429   fi
430
431   if [[ -z "$LOCATION" ]]; then
432     echo "Need to provide '-internal-client' or '-external-client'"
433     echo
434     echo "-internal-client    You are running this on the same private network as the Arvados cluster (e.g. on one of the Arvados nodes)"
435     echo "-external-client    You are running this outside the private network of the Arvados cluster (e.g. your workstation)"
436     exit 1
437   fi
438
439   export ARVADOS_API_HOST="${DOMAIN}:${CONTROLLER_EXT_SSL_PORT}"
440   export ARVADOS_API_TOKEN="$SYSTEM_ROOT_TOKEN"
441
442   arvados-client diagnostics $LOCATION
443   ;;
444
445 *)
446   echo "Arvados installer"
447   echo ""
448   echo "initialize        initialize the setup directory for configuration"
449   echo "terraform         create cloud resources using terraform"
450   echo "terraform-destroy destroy cloud resources created by terraform"
451   echo "generate-tokens   generate random values for tokens"
452   echo "deploy            deploy the configuration from the setup directory"
453   echo "diagnostics       check your install using diagnostics"
454   ;;
455 esac