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