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