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