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