Merge branch '20482-installer-improvements'. Closes #20482
[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 # The ssh user we'll use
39 # This will be populated by loadconfig()
40 declare DEPLOY_USER
41
42 # The git repository that we'll push to on all the nodes
43 # This will be populated by loadconfig()
44 declare GITTARGET
45
46 # The public host used as an SSH jump host
47 # This will be populated by loadconfig()
48 declare USE_SSH_JUMPHOST
49
50 checktools() {
51     local MISSING=''
52     for a in git ip ; do
53         if ! which $a ; then
54             MISSING="$MISSING $a"
55         fi
56     done
57     if [[ -n "$MISSING" ]] ; then
58         echo "Some tools are missing, please make sure you have the 'git' and 'iproute2' packages installed"
59         exit 1
60     fi
61 }
62
63 sync() {
64     local NODE=$1
65     local BRANCH=$2
66
67     # Synchronizes the configuration by creating a git repository on
68     # each node, pushing our branch, and updating the checkout.
69
70     if [[ "$NODE" != localhost ]] ; then
71                 SSH=`ssh_cmd "$NODE"`
72                 GIT="eval `git_cmd $NODE`"
73                 if ! $SSH $DEPLOY_USER@$NODE test -d ${GITTARGET}.git ; then
74
75                         # Initialize the git repository (1st time case).  We're
76                         # actually going to make two repositories here because git
77                         # will complain if you try to push to a repository with a
78                         # checkout. So we're going to create a "bare" repository
79                         # and then clone a regular repository (with a checkout)
80                         # from that.
81
82                         $SSH $DEPLOY_USER@$NODE git init --bare --shared=0600 ${GITTARGET}.git
83                         if ! $GIT remote add $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git ; then
84                                 $GIT remote set-url $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git
85                         fi
86                         $GIT push $NODE $BRANCH
87                         $SSH $DEPLOY_USER@$NODE "umask 0077 && git clone ${GITTARGET}.git ${GITTARGET}"
88                 fi
89
90                 # The update case.
91                 #
92                 # Push to the bare repository on the remote node, then in the
93                 # remote node repository with the checkout, pull the branch
94                 # from the bare repository.
95
96                 $GIT push $NODE $BRANCH
97                 $SSH $DEPLOY_USER@$NODE "git -C ${GITTARGET} checkout ${BRANCH} && git -C ${GITTARGET} pull"
98     fi
99 }
100
101 deploynode() {
102     local NODE=$1
103     local ROLES=$2
104
105     # Deploy a node.  This runs the provision script on the node, with
106     # the appropriate roles.
107
108     if [[ -z "$ROLES" ]] ; then
109                 echo "No roles specified for $NODE, will deploy all roles"
110     else
111                 ROLES="--roles ${ROLES}"
112     fi
113
114     logfile=deploy-${NODE}-$(date -Iseconds).log
115         SSH=`ssh_cmd "$NODE"`
116
117     if [[ "$NODE" = localhost ]] ; then
118             SUDO=''
119                 if [[ $(whoami) != 'root' ]] ; then
120                         SUDO=sudo
121                 fi
122                 $SUDO ./provision.sh --config ${CONFIG_FILE} ${ROLES} 2>&1 | tee $logfile
123         else
124                 $SSH $DEPLOY_USER@$NODE "cd ${GITTARGET} && sudo ./provision.sh --config ${CONFIG_FILE} ${ROLES}" 2>&1 | tee $logfile
125     fi
126 }
127
128 loadconfig() {
129     if [[ ! -s $CONFIG_FILE ]] ; then
130                 echo "Must be run from initialized setup dir, maybe you need to 'initialize' first?"
131     fi
132     source ${CONFIG_FILE}
133     GITTARGET=arvados-deploy-config-${CLUSTER}
134 }
135
136 ssh_cmd() {
137         local NODE=$1
138         if [ -z "${USE_SSH_JUMPHOST}" -o "${NODE}" == "${USE_SSH_JUMPHOST}" -o "${NODE}" == "localhost" ]; then
139                 echo "ssh"
140         else
141                 echo "ssh -J ${DEPLOY_USER}@${USE_SSH_JUMPHOST}"
142         fi
143 }
144
145 git_cmd() {
146         local NODE=$1
147         echo "GIT_SSH_COMMAND=\"`ssh_cmd ${NODE}`\" git"
148 }
149
150 set +u
151 subcmd="$1"
152 set -u
153
154 if [[ -n "$subcmd" ]] ; then
155     shift
156 fi
157 case "$subcmd" in
158     initialize)
159         if [[ ! -f provision.sh ]] ; then
160             echo "Must be run from arvados/tools/salt-install"
161             exit
162         fi
163
164         checktools
165
166         set +u
167         SETUPDIR=$1
168         PARAMS=$2
169         SLS=$3
170         TERRAFORM=$4
171         set -u
172
173         err=
174         if [[ -z "$PARAMS" || ! -f local.params.example.$PARAMS ]] ; then
175             echo "Not found: local.params.example.$PARAMS"
176             echo "Expected one of multiple_hosts, single_host_multiple_hostnames, single_host_single_hostname"
177             err=1
178         fi
179
180         if [[ -z "$SLS" || ! -d config_examples/$SLS ]] ; then
181             echo "Not found: config_examples/$SLS"
182             echo "Expected one of multi_host/aws, single_host/multiple_hostnames, single_host/single_hostname"
183             err=1
184         fi
185
186         if [[ -z "$SETUPDIR" || -z "$PARAMS" || -z "$SLS" ]]; then
187             echo "installer.sh <setup dir to initialize> <params template> <config template>"
188             err=1
189         fi
190
191         if [[ -n "$err" ]] ; then
192             exit 1
193         fi
194
195         echo "Initializing $SETUPDIR"
196         git init --shared=0600 $SETUPDIR
197         cp -r *.sh tests $SETUPDIR
198
199         cp local.params.example.$PARAMS $SETUPDIR/${CONFIG_FILE}
200         cp -r config_examples/$SLS $SETUPDIR/${CONFIG_DIR}
201
202         if [[ -n "$TERRAFORM" ]] ; then
203             mkdir $SETUPDIR/terraform
204             cp -r $TERRAFORM/* $SETUPDIR/terraform/
205         fi
206
207         cd $SETUPDIR
208         echo '*.log' > .gitignore
209         echo '**/.terraform' >> .gitignore
210         echo '**/.infracost' >> .gitignore
211
212         if [[ -n "$TERRAFORM" ]] ; then
213                 git add terraform
214         fi
215
216         git add *.sh ${CONFIG_FILE} ${CONFIG_DIR} tests .gitignore
217         git commit -m"initial commit"
218
219         echo
220         echo "Setup directory $SETUPDIR initialized."
221         if [[ -n "$TERRAFORM" ]] ; then
222             (cd $SETUPDIR/terraform/vpc && terraform init)
223             (cd $SETUPDIR/terraform/data-storage && terraform init)
224             (cd $SETUPDIR/terraform/services && terraform init)
225             echo "Now go to $SETUPDIR, customize 'terraform/vpc/terraform.tfvars' as needed, then run 'installer.sh terraform'"
226         else
227             echo "Now go to $SETUPDIR, customize '${CONFIG_FILE}' and '${CONFIG_DIR}' as needed, then run 'installer.sh deploy'"
228         fi
229         ;;
230
231     terraform)
232         logfile=terraform-$(date -Iseconds).log
233         (cd terraform/vpc && terraform apply -auto-approve) 2>&1 | tee -a $logfile
234         (cd terraform/data-storage && terraform apply -auto-approve) 2>&1 | tee -a $logfile
235         (cd terraform/services && terraform apply -auto-approve) 2>&1 | grep -v letsencrypt_iam_secret_access_key | tee -a $logfile
236         (cd terraform/services && echo -n 'letsencrypt_iam_secret_access_key = ' && terraform output letsencrypt_iam_secret_access_key) 2>&1 | tee -a $logfile
237         ;;
238
239     terraform-destroy)
240         logfile=terraform-$(date -Iseconds).log
241         (cd terraform/services && terraform destroy) 2>&1 | tee -a $logfile
242         (cd terraform/data-storage && terraform destroy) 2>&1 | tee -a $logfile
243         (cd terraform/vpc && terraform destroy) 2>&1 | tee -a $logfile
244         ;;
245
246     generate-tokens)
247         for i in BLOB_SIGNING_KEY MANAGEMENT_TOKEN SYSTEM_ROOT_TOKEN ANONYMOUS_USER_TOKEN WORKBENCH_SECRET_KEY DATABASE_PASSWORD; do
248             echo ${i}=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 32 ; echo '')
249         done
250         ;;
251
252     deploy)
253         set +u
254         NODE=$1
255         set -u
256
257         checktools
258
259         loadconfig
260
261         if grep -rni 'fixme' ${CONFIG_FILE} ${CONFIG_DIR} ; then
262             echo
263             echo "Some parameters still need to be updated.  Please fix them and then re-run deploy."
264             exit 1
265         fi
266
267         BRANCH=$(git rev-parse --abbrev-ref HEAD)
268
269         set -x
270
271         git add -A
272         if ! git diff --cached --exit-code ; then
273             git commit -m"prepare for deploy"
274         fi
275
276         if [[ -z "$NODE" ]]; then
277             for NODE in "${!NODES[@]}"
278             do
279                 # First, push the git repo to each node.  This also
280                 # confirms that we have git and can log into each
281                 # node.
282                 sync $NODE $BRANCH
283             done
284
285             for NODE in "${!NODES[@]}"
286             do
287                 # Do 'database' role first,
288                 if [[ "${NODES[$NODE]}" =~ database ]] ; then
289                     deploynode $NODE "${NODES[$NODE]}"
290                     unset NODES[$NODE]
291                 fi
292             done
293
294             for NODE in "${!NODES[@]}"
295             do
296                 # then  'api' or 'controller' roles
297                 if [[ "${NODES[$NODE]}" =~ (api|controller) ]] ; then
298                     deploynode $NODE "${NODES[$NODE]}"
299                     unset NODES[$NODE]
300                 fi
301             done
302
303             for NODE in "${!NODES[@]}"
304             do
305                 # Everything else (we removed the nodes that we
306                 # already deployed from the list)
307                 deploynode $NODE "${NODES[$NODE]}"
308             done
309         else
310             # Just deploy the node that was supplied on the command line.
311             sync $NODE $BRANCH
312             deploynode $NODE "${NODES[$NODE]}"
313         fi
314
315         set +x
316         echo
317         echo "Completed deploy, run 'installer.sh diagnostics' to verify the install"
318
319         ;;
320
321     diagnostics)
322         loadconfig
323
324         set +u
325         declare LOCATION=$1
326         set -u
327
328         if ! which arvados-client ; then
329             echo "arvados-client not found, install 'arvados-client' package with 'apt-get' or 'yum'"
330             exit 1
331         fi
332
333         if [[ -z "$LOCATION" ]] ; then
334             echo "Need to provide '-internal-client' or '-external-client'"
335             echo
336             echo "-internal-client    You are running this on the same private network as the Arvados cluster (e.g. on one of the Arvados nodes)"
337             echo "-external-client    You are running this outside the private network of the Arvados cluster (e.g. your workstation)"
338             exit 1
339         fi
340
341         export ARVADOS_API_HOST="${DOMAIN}:${CONTROLLER_EXT_SSL_PORT}"
342         export ARVADOS_API_TOKEN="$SYSTEM_ROOT_TOKEN"
343
344         arvados-client diagnostics $LOCATION
345         ;;
346
347     *)
348         echo "Arvados installer"
349         echo ""
350         echo "initialize        initialize the setup directory for configuration"
351         echo "terraform         create cloud resources using terraform"
352         echo "terraform-destroy destroy cloud resources created by terraform"
353         echo "generate-tokens   generate random values for tokens"
354         echo "deploy            deploy the configuration from the setup directory"
355         echo "diagnostics       check your install using diagnostics"
356         ;;
357 esac