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