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