]> WPIA git - infra.git/blob - manager/setup
add: backup mechanism for postgresql
[infra.git] / manager / setup
1 #!/bin/bash
2 targetHost=$1
3 targetHost=${targetHost%/}
4 source config
5 source "$targetHost/config"
6
7 eval $(ssh-agent)
8 trap "eval \$(ssh-agent -k)" EXIT
9 ssh-add vm-key
10 function title {
11     printf '\e]0;%s\007' "$1"
12 }
13
14 function reset_vm {
15     if ! [[ -f reset-vm-key ]]; then
16         ssh-keygen -f reset-vm-key -t ed25519 -N ""
17     fi
18     title 'S0: VM-reset'
19     printf "command=\"bash reset1\" "
20     cat reset-vm-key.pub
21
22     echo "Powering off"
23     ssh_target -t 'ip a show dev enp0s3; sudo poweroff'
24
25     echo "Resetting VM"
26     do_reset_vm
27     echo "Waiting for VM to respond: "
28     while ! ping -c 1 -W 1 "${to#*@}" > /dev/null; do
29         printf "."
30     done
31     printf "\n"
32     while ! ssh_target 'echo Got into VM'; do
33         sleep 5
34         echo "Please open the VM for my key"
35     done
36 }
37 function update {
38     title 'S1: apt-get; clone'
39     ssh_target -t "sudo apt-get update && DEBIAN_FRONTEND=noninteractive sudo -E apt-get upgrade -o Dpkg::Options::=\"--force-confold\" -y && sudo apt-get install -y git curl $devPkgs"
40     echo "Git init"
41     ssh_target -t '[[ -d .git ]] || git init;'
42     echo "Git push"
43     git bundle create .fullBundle HEAD
44     cat .fullBundle | ssh_target 'cat > .fullBundle'
45     ssh_target 'git fetch .fullBundle HEAD:refs/remotes/origin/master; rm .fullBundle'
46     rm .fullBundle
47     echo "Git update"
48     ssh_target -t 'if ! git rev-parse --verify master &> /dev/null; then git checkout origin/master; else git merge --ff-only origin/master; fi'
49 }
50
51 function ensure_nre {
52     first=true
53     while true; do
54         if [[ $first == "true" ]]; then
55             first=false
56         else
57             echo "Please provide the NRE archives in 'nre-results/*.tar.gz' or install them manually into the target."
58             read -p "press enter to continue" tmp
59         fi
60         install_nre
61         if ssh_target '[ -d modules/cassiopeia_client/files/profiles ] && [ -d modules/nre/files/config/profiles ]'; then
62             echo cassiopeia-client and nre-conf files found
63         else
64             echo cassiopeia-client or nre-conf files not found
65             continue
66         fi
67         if [[ "$signerLocation" == "self" ]]; then
68             if ssh_target '[ -d modules/cassiopeia_signer/files/profiles ]'; then
69                 echo cassiopeia-signer files found
70             else
71                 echo cassiopeia-signer files not found
72                 continue
73             fi
74             if ssh_target '[ -d modules/cassiopeia/files ]'; then
75                 echo external keys with self-signer?? wrong!
76                 #continue
77                 #TODO make more intelligent... only allow if key-pair exists
78             fi
79         else
80             if ssh_target '[ -d modules/cassiopeia/files ]'; then
81                 echo external keys with self-signer found
82             else
83                 echo external keys with self-signer not found
84                 continue
85             fi
86         fi
87         break
88     done
89 }
90 function execute-bootstrap-user-auto {
91
92     coproc {
93         ssh_target -t -t 'bash bootstrap-user'
94     }
95     echo "waiting for bootstrap-user..."
96     if [[ $need_sudo == "true" ]]; then
97         read -sp "sudo: " sudo
98         if [[ $sudo != "" ]]; then
99             printf '%s\n' "$sudo" >&${COPROC[1]}
100         fi
101     fi
102     line=""
103     while [[ $line != "We need a first "* ]]; do
104         if ! read -r line <&${COPROC[0]}; then
105             echo "Error, bootstrap terminated early"
106             exit -1
107         fi
108         echo "Line: $line"
109     done
110     bootstrapper_details >&${COPROC[1]}
111
112     while [[ $line != "You should now have been sent an activation link to the email"* ]]; do
113         if ! read -r line <&${COPROC[0]}; then
114             echo "Error, bootstrap terminated early"
115             exit -1
116         fi
117         printf '%s\n' "$line"
118     done
119     read_activation_link >&${COPROC[1]}
120     cat <&${COPROC[0]}
121 }
122
123 if [[ "$2" == "fresh" ]]; then
124     reset_vm
125 fi
126 need_sudo=true
127 if ssh_target "sudo -n whoami" > /dev/null; then
128     echo "Sudo does not need a password, great!"
129     need_sudo=false
130 fi
131
132 update
133 if [[ "$2" == "update" ]]; then
134     exit 0;
135 fi
136 configure
137 ensure_nre
138
139 install_backup
140
141 title 'S3: puppet-1'
142 ssh_target -t 'sudo ./bootstrap'
143 title 'S3: puppet-2'
144 ssh_target -t 'sudo ./bootstrap'
145
146 if [[ -f tricks ]]; then
147     cat tricks | ssh_target 'cat > tricks && chmod +x tricks'
148     ssh_target -t 'bash tricks'
149 fi
150
151 if [[ $signerLocation == "self" ]] && [[ $(ssh_target 'ps -ef | grep tcpseria[l] | wc -l') != "2" ]]; then
152     echo "Error some services weren't started"
153     exit 1
154 fi
155
156 populate_system
157
158 title 'bash'
159 eval $(ssh-agent -k)