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