18870: Ensure sbin is in cron job PATH
[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     if [[ ! -s $CONFIG_FILE ]] ; then
44         echo "Must be run from initialized setup dir, maybe you need to 'initialize' first?"
45     fi
46     source ${CONFIG_FILE}
47     GITTARGET=arvados-deploy-config-${CLUSTER}
48 }
49
50 subcmd="$1"
51 if [[ -n "$subcmd" ]] ; then
52     shift
53 fi
54 case "$subcmd" in
55     initialize)
56         if [[ ! -f provision.sh ]] ; then
57             echo "Must be run from arvados/tools/salt-install"
58             exit
59         fi
60
61         SETUPDIR=$1
62         PARAMS=$2
63         SLS=$3
64
65         err=
66         if [[ -z "$PARAMS" || ! -f local.params.example.$PARAMS ]] ; then
67             echo "Not found: local.params.example.$PARAMS"
68             echo "Expected one of multiple_hosts, single_host_multiple_hostnames, single_host_single_hostname"
69             err=1
70         fi
71
72         if [[ -z "$SLS" || ! -d config_examples/$SLS ]] ; then
73             echo "Not found: config_examples/$SLS"
74             echo "Expected one of multi_host/aws, single_host/multiple_hostnames, single_host/single_hostname"
75             err=1
76         fi
77
78         if [[ -z "$SETUPDIR" || -z "$PARAMS" || -z "$SLS" ]]; then
79             echo "installer.sh <setup dir to initialize> <params template> <config template>"
80             err=1
81         fi
82
83         if [[ -n "$err" ]] ; then
84             exit 1
85         fi
86
87         echo "Initializing $SETUPDIR"
88         git init $SETUPDIR
89         cp -r *.sh tests $SETUPDIR
90
91         cp local.params.example.$PARAMS $SETUPDIR/local.params
92         cp -r config_examples/$SLS $SETUPDIR/local_config_dir
93
94         cd $SETUPDIR
95         git add *.sh local.params local_config_dir tests
96         git commit -m"initial commit"
97
98         echo "setup directory initialized, now go to $SETUPDIR, edit 'local.params' and 'local_config_dir' as needed, then run 'installer.sh deploy'"
99         ;;
100     deploy)
101         NODE=$1
102
103         loadconfig
104
105         set -x
106
107         BRANCH=$(git branch --show-current)
108
109         git add -A
110         if ! git diff --cached --exit-code ; then
111             git commit -m"prepare for deploy"
112         fi
113
114         if [[ -z "$NODE" ]]; then
115             for NODE in "${!NODES[@]}"
116             do
117                 # Do 'database' role first,
118                 if [[ "${NODES[$NODE]}" =~ database ]] ; then
119                     sync
120                     deploynode
121                     unset NODES[$NODE]
122                 fi
123             done
124
125             for NODE in "${!NODES[@]}"
126             do
127                 # then  'api' or 'controller' roles
128                 if [[ "${NODES[$NODE]}" =~ (api|controller) ]] ; then
129                     sync
130                     deploynode
131                     unset NODES[$NODE]
132                 fi
133             done
134
135             for NODE in "${!NODES[@]}"
136             do
137                 # Everything else
138                 sync
139                 deploynode
140             done
141         else
142             sync
143             deploynode
144         fi
145
146         ;;
147     diagnostics)
148         loadconfig
149
150         if ! which arvados-client ; then
151             apt-get install arvados-client
152         fi
153
154         export ARVADOS_API_HOST="${CLUSTER}.${DOMAIN}"
155         export ARVADOS_API_TOKEN="$SYSTEM_ROOT_TOKEN"
156
157         arvados-client diagnostics -internal-client
158         ;;
159     *)
160         echo "Arvados installer"
161         echo ""
162         echo "initialize   initialize the setup directory for configuration"
163         echo "deploy       deploy the configuration from the setup directory"
164         echo "diagnostics  check your install using diagnostics"
165         ;;
166 esac