18870: Check for 'fixme's
[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 set -e
8
9 declare -A NODES
10
11 sync() {
12     if [[ "$NODE" != localhost ]] ; then
13         if ! ssh $NODE test -d ${GITTARGET}.git ; then
14             ssh $NODE git init --bare ${GITTARGET}.git
15             if ! git remote add $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git ; then
16                 git remote set-url $NODE $DEPLOY_USER@$NODE:${GITTARGET}.git
17             fi
18             git push $NODE $BRANCH
19             ssh $NODE git clone ${GITTARGET}.git ${GITTARGET}
20         fi
21
22         git push $NODE $BRANCH
23         ssh $NODE git -C ${GITTARGET} checkout $BRANCH
24         ssh $NODE git -C ${GITTARGET} pull
25     fi
26 }
27
28 deploynode() {
29     if [[ -z "${NODES[$NODE]}" ]] ; then
30         echo "No roles declared for '$NODE' in local.params"
31         exit 1
32     fi
33
34     if [[ "$NODE" = localhost ]] ; then
35         sudo ./provision.sh --config local.params --roles ${NODES[$NODE]}
36     else
37         ssh $DEPLOY_USER@$NODE "cd ${GITTARGET} && sudo ./provision.sh --config local.params --roles ${NODES[$NODE]}"
38     fi
39 }
40
41 loadconfig() {
42     CONFIG_FILE=local.params
43     CONFIG_DIR=local_config_dir
44     if [[ ! -s $CONFIG_FILE ]] ; then
45         echo "Must be run from initialized setup dir, maybe you need to 'initialize' first?"
46     fi
47     source ${CONFIG_FILE}
48     GITTARGET=arvados-deploy-config-${CLUSTER}
49 }
50
51 subcmd="$1"
52 if [[ -n "$subcmd" ]] ; then
53     shift
54 fi
55 case "$subcmd" in
56     initialize)
57         if [[ ! -f provision.sh ]] ; then
58             echo "Must be run from arvados/tools/salt-install"
59             exit
60         fi
61
62         SETUPDIR=$1
63         PARAMS=$2
64         SLS=$3
65
66         err=
67         if [[ -z "$PARAMS" || ! -f local.params.example.$PARAMS ]] ; then
68             echo "Not found: local.params.example.$PARAMS"
69             echo "Expected one of multiple_hosts, single_host_multiple_hostnames, single_host_single_hostname"
70             err=1
71         fi
72
73         if [[ -z "$SLS" || ! -d config_examples/$SLS ]] ; then
74             echo "Not found: config_examples/$SLS"
75             echo "Expected one of multi_host/aws, single_host/multiple_hostnames, single_host/single_hostname"
76             err=1
77         fi
78
79         if [[ -z "$SETUPDIR" || -z "$PARAMS" || -z "$SLS" ]]; then
80             echo "installer.sh <setup dir to initialize> <params template> <config template>"
81             err=1
82         fi
83
84         if [[ -n "$err" ]] ; then
85             exit 1
86         fi
87
88         echo "Initializing $SETUPDIR"
89         git init $SETUPDIR
90         cp -r *.sh tests $SETUPDIR
91
92         cp local.params.example.$PARAMS $SETUPDIR/local.params
93         cp -r config_examples/$SLS $SETUPDIR/local_config_dir
94
95         cd $SETUPDIR
96         git add *.sh local.params local_config_dir tests
97         git commit -m"initial commit"
98
99         echo "setup directory initialized, now go to $SETUPDIR, edit 'local.params' and 'local_config_dir' as needed, then run 'installer.sh deploy'"
100         ;;
101     deploy)
102         NODE=$1
103
104         loadconfig
105
106         if grep -rni 'fixme' ${CONFIG_FILE} ${CONFIG_DIR} ; then
107             echo
108             echo "Some parameters still need to be updated.  Please fix them and then re-run deploy."
109             exit 1
110         fi
111
112         BRANCH=$(git branch --show-current)
113
114         set -x
115
116         git add -A
117         if ! git diff --cached --exit-code ; then
118             git commit -m"prepare for deploy"
119         fi
120
121         if [[ -z "$NODE" ]]; then
122             for NODE in "${!NODES[@]}"
123             do
124                 # Do 'database' role first,
125                 if [[ "${NODES[$NODE]}" =~ database ]] ; then
126                     sync
127                     deploynode
128                     unset NODES[$NODE]
129                 fi
130             done
131
132             for NODE in "${!NODES[@]}"
133             do
134                 # then  'api' or 'controller' roles
135                 if [[ "${NODES[$NODE]}" =~ (api|controller) ]] ; then
136                     sync
137                     deploynode
138                     unset NODES[$NODE]
139                 fi
140             done
141
142             for NODE in "${!NODES[@]}"
143             do
144                 # Everything else
145                 sync
146                 deploynode
147             done
148         else
149             sync
150             deploynode
151         fi
152
153         ;;
154     diagnostics)
155         loadconfig
156
157         if ! which arvados-client ; then
158             apt-get install arvados-client
159         fi
160
161         export ARVADOS_API_HOST="${CLUSTER}.${DOMAIN}"
162         export ARVADOS_API_TOKEN="$SYSTEM_ROOT_TOKEN"
163
164         arvados-client diagnostics -internal-client
165         ;;
166     *)
167         echo "Arvados installer"
168         echo ""
169         echo "initialize   initialize the setup directory for configuration"
170         echo "deploy       deploy the configuration from the setup directory"
171         echo "diagnostics  check your install using diagnostics"
172         ;;
173 esac