]>
Commit | Line | Data |
---|---|---|
ec1096d8 IB |
1 | #!/bin/bash |
2 | ||
3babc2c6 IB |
3 | usage() { |
4 | cat <<EOF | |
5 | $0 [options] | |
6 | --help,-h This help | |
7 | --vps vps_name Name of the vps | |
8 | --password password Password of the vps (only useful in case of no reboot) | |
9 | --reinstall-first Start with reinstalling the vps | |
10 | --host-user user Use another user (default: root) | |
11 | --no-reboot Don't reboot | |
12 | --no-reboot-start Don't reboot to rescue at the beginning | |
13 | --no-reboot-end Don't reboot to normal at the end | |
14 | --git-branch Use another puppet branch (default: master) | |
15 | EOF | |
16 | } | |
17 | ||
248bd83e IB |
18 | set -e |
19 | ||
20 | host_user=root | |
21 | git_branch=master | |
22 | ||
23 | while [ -n "$1" ]; do | |
24 | case "$1" in | |
25 | --vps) | |
26 | vps_name="$2" | |
27 | shift | |
28 | ;; | |
29 | --reinstall-first) | |
30 | reinstall_first=1 | |
31 | ;; | |
32 | --password) | |
33 | password="$2" | |
34 | shift | |
35 | ;; | |
36 | --host-user) | |
37 | host_user="$2" | |
38 | shift | |
39 | ;; | |
40 | --no-reboot) | |
41 | no_reboot=1 | |
42 | ;; | |
43 | --no-reboot-start) | |
44 | no_reboot_start=1 | |
45 | ;; | |
46 | --no-reboot-end) | |
47 | no_reboot_end=1 | |
48 | ;; | |
49 | --git-branch) | |
50 | git_branch="$2" | |
51 | shift | |
52 | ;; | |
3babc2c6 IB |
53 | --help|-h) |
54 | usage | |
55 | exit 0 | |
56 | ;; | |
248bd83e IB |
57 | esac |
58 | ||
59 | shift | |
60 | done | |
61 | ||
ec1096d8 IB |
62 | DIRECTORY=$(cd `dirname $0` && pwd) |
63 | PYTHON_DIRECTORY="$DIRECTORY/../python" | |
64 | ||
248bd83e | 65 | if [ -z "$vps_name" ]; then |
ec1096d8 IB |
66 | read -p "Nom du vps : " vps_name |
67 | fi | |
68 | ||
248bd83e IB |
69 | if [ -n "$reinstall_first" ]; then |
70 | echo "Réinstallation du système" | |
71 | python $PYTHON_DIRECTORY/reinstall_vps_server.py --use-current "$vps_name" | |
72 | ||
73 | read -p "Appuyer sur une touche quand le serveur est prêt" ready | |
74 | fi | |
75 | ||
76 | if [ -z "$no_reboot" -a -z "$no_reboot_start" ]; then | |
77 | echo "Patienter le temps du reboot" | |
78 | python $PYTHON_DIRECTORY/reboot_vps_server.py --rescue "$vps_name" | |
79 | fi | |
ec1096d8 | 80 | |
248bd83e IB |
81 | if [ -z "$password" ]; then |
82 | stty -echo | |
83 | read -p "Mot de passe reçu par e-mail : " password; echo | |
84 | stty echo | |
85 | fi | |
ec1096d8 IB |
86 | |
87 | ARCH_DIR=`mktemp -d` | |
88 | ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh" | |
89 | ARCH_CHROOT_SCRIPT="$ARCH_DIR/arch_chroot_script.sh" | |
90 | ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh" | |
503e4cf5 IB |
91 | ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_host_puppet_configuration_script.sh" |
92 | ARCH_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_puppet_configuration_script.sh" | |
248bd83e | 93 | ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json" |
ec1096d8 IB |
94 | |
95 | trap "rm -rf $ARCH_DIR" EXIT | |
96 | ||
503e4cf5 | 97 | #### Base installation stage |
248bd83e IB |
98 | python $PYTHON_DIRECTORY/get_initial_configuration.py $vps_name > $ARCH_PUPPET_INITIAL_CONFIGURATION |
99 | ||
ec1096d8 IB |
100 | cat > $ARCH_HOST_SCRIPT <<EOF |
101 | #!/bin/bash | |
102 | ||
103 | apt-get update | |
104 | apt-get install -y haveged | |
105 | haveged & | |
106 | ||
107 | cd /tmp | |
108 | ||
109 | LATEST=\$(curl https://mirrors.kernel.org/archlinux/iso/latest/sha1sums.txt | grep "bootstrap" | head -n1) | |
110 | SHA1=\$(echo "\$LATEST" | cut -d' ' -f1) | |
111 | NAME=\$(echo "\$LATEST" | cut -d' ' -f3) | |
112 | ||
113 | curl -O "https://mirrors.kernel.org/archlinux/iso/latest/\$NAME" | |
114 | ||
115 | tar -xzf "\$NAME" | |
116 | ||
117 | echo 'Server = http://archlinux.mirrors.ovh.net/archlinux/\$repo/os/\$arch' > /tmp/root.x86_64/etc/pacman.d/mirrorlist | |
118 | ||
28f9451d | 119 | DEVICE_STR=\$(cat /proc/mounts | grep "/dev/[sv]d.. /mnt/") |
ec1096d8 IB |
120 | DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1) |
121 | MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2) | |
122 | ||
123 | umount "\$DEVICE" | |
124 | UUID=\$(lsblk -rno UUID "\$DEVICE") | |
125 | ||
126 | echo "\$UUID" > /tmp/root.x86_64/device_uuid | |
127 | ||
128 | cp /tmp/arch_chroot_script.sh /tmp/root.x86_64/ | |
129 | ||
130 | /tmp/root.x86_64/bin/arch-chroot /tmp/root.x86_64/ /arch_chroot_script.sh | |
131 | ||
132 | mount "\$DEVICE" | |
133 | ||
134 | cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/" | |
248bd83e | 135 | cp /tmp/puppet_variables.json "\$MOUNTPOINT/root/" |
ec1096d8 IB |
136 | |
137 | /tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh | |
138 | EOF | |
139 | ||
140 | ||
141 | cat > $ARCH_CHROOT_SCRIPT <<EOF | |
142 | #!/bin/bash | |
143 | ||
144 | pacman-key --init | |
145 | pacman-key --populate archlinux | |
146 | ||
147 | UUID=\$(cat /device_uuid) | |
148 | PART="/dev/disk/by-uuid/\$UUID" | |
149 | DEVICE=\$(realpath "\$PART") | |
150 | ||
151 | # mkfs.ext4 -F -U "\$UUID" "\$DEVICE" | |
152 | mount "\$DEVICE" /mnt | |
153 | ||
154 | ##### FIXME: mkfs.ext4 would be better #### | |
155 | for i in /mnt/*; do | |
156 | if [ "\$i" = "/mnt/boot" ]; then | |
157 | # keep /boot/grub | |
158 | rm -f \$i/* | |
159 | else | |
160 | rm -rf \$i | |
161 | fi | |
162 | done | |
163 | ##### /FIXME #### | |
164 | ||
165 | pacstrap /mnt base git puppet | |
166 | ||
167 | echo "\$PART / auto defaults 0 1" > /mnt/etc/fstab | |
168 | ||
169 | umount /mnt | |
170 | EOF | |
171 | ||
172 | cat > $ARCH_INSTALL_SCRIPT <<EOF | |
173 | CODE_PATH="/etc/puppetlabs/code" | |
174 | rm -rf \$CODE_PATH | |
248bd83e | 175 | git clone -b $git_branch --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH |
ec1096d8 IB |
176 | puppet apply --test \$CODE_PATH/manifests/site.pp |
177 | # The password seed requires puppet to be run twice | |
178 | puppet apply --test \$CODE_PATH/manifests/site.pp | |
179 | EOF | |
180 | ||
181 | chmod a+x $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT | |
182 | ||
183 | expect -f - <<EOF | |
184 | set timeout -1 | |
248bd83e | 185 | spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT $host_user@$vps_name:/tmp |
ec1096d8 IB |
186 | expect "assword:" |
187 | send "$password\n" | |
188 | expect eof | |
248bd83e | 189 | spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_script.sh |
ec1096d8 IB |
190 | expect "assword:" |
191 | send "$password\r" | |
192 | expect eof | |
193 | EOF | |
194 | ||
503e4cf5 IB |
195 | ### Role specific stage |
196 | read -p "Press key when LDAP is configured" i | |
197 | ||
198 | cat > $ARCH_PUPPET_CONFIGURATION_SCRIPT <<EOF | |
199 | CODE_PATH="/etc/puppetlabs/code" | |
200 | puppet apply --test \$CODE_PATH/manifests/site.pp | |
201 | EOF | |
202 | ||
203 | cat > $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT <<EOF | |
28f9451d | 204 | DEVICE_STR=\$(cat /proc/mounts | grep "/dev/[sv]d.. /mnt/") |
503e4cf5 IB |
205 | DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1) |
206 | MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2) | |
207 | ||
208 | cp /tmp/arch_puppet_configuration_script.sh "\$MOUNTPOINT/root/" | |
209 | ||
210 | /tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_puppet_configuration_script.sh | |
211 | EOF | |
212 | ||
213 | chmod a+x $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT | |
214 | ||
215 | expect -f - <<EOF | |
216 | set timeout -1 | |
248bd83e | 217 | spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $host_user@$vps_name:/tmp |
503e4cf5 IB |
218 | expect "assword:" |
219 | send "$password\n" | |
220 | expect eof | |
248bd83e | 221 | spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_puppet_configuration_script.sh |
503e4cf5 IB |
222 | expect "assword:" |
223 | send "$password\r" | |
224 | expect eof | |
225 | EOF | |
226 | ||
227 | ### Installation finished | |
248bd83e | 228 | if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then |
ec1096d8 IB |
229 | echo "Rebooting" |
230 | python $PYTHON_DIRECTORY/reboot_vps_server.py --local "$vps_name" | |
231 | fi |