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