]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blame - bin/install_script.sh
Fix install script to take vd type discs
[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)
15EOF
16}
17
248bd83e
IB
18set -e
19
20host_user=root
21git_branch=master
22
23while [ -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
60done
61
ec1096d8
IB
62DIRECTORY=$(cd `dirname $0` && pwd)
63PYTHON_DIRECTORY="$DIRECTORY/../python"
64
248bd83e 65if [ -z "$vps_name" ]; then
ec1096d8
IB
66 read -p "Nom du vps : " vps_name
67fi
68
248bd83e
IB
69if [ -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
74fi
75
76if [ -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"
79fi
ec1096d8 80
248bd83e
IB
81if [ -z "$password" ]; then
82 stty -echo
83 read -p "Mot de passe reçu par e-mail : " password; echo
84 stty echo
85fi
ec1096d8
IB
86
87ARCH_DIR=`mktemp -d`
88ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh"
89ARCH_CHROOT_SCRIPT="$ARCH_DIR/arch_chroot_script.sh"
90ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh"
503e4cf5
IB
91ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_host_puppet_configuration_script.sh"
92ARCH_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_puppet_configuration_script.sh"
248bd83e 93ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json"
ec1096d8
IB
94
95trap "rm -rf $ARCH_DIR" EXIT
96
503e4cf5 97#### Base installation stage
248bd83e
IB
98python $PYTHON_DIRECTORY/get_initial_configuration.py $vps_name > $ARCH_PUPPET_INITIAL_CONFIGURATION
99
ec1096d8
IB
100cat > $ARCH_HOST_SCRIPT <<EOF
101#!/bin/bash
102
103apt-get update
104apt-get install -y haveged
105haveged &
106
107cd /tmp
108
109LATEST=\$(curl https://mirrors.kernel.org/archlinux/iso/latest/sha1sums.txt | grep "bootstrap" | head -n1)
110SHA1=\$(echo "\$LATEST" | cut -d' ' -f1)
111NAME=\$(echo "\$LATEST" | cut -d' ' -f3)
112
113curl -O "https://mirrors.kernel.org/archlinux/iso/latest/\$NAME"
114
115tar -xzf "\$NAME"
116
117echo 'Server = http://archlinux.mirrors.ovh.net/archlinux/\$repo/os/\$arch' > /tmp/root.x86_64/etc/pacman.d/mirrorlist
118
28f9451d 119DEVICE_STR=\$(cat /proc/mounts | grep "/dev/[sv]d.. /mnt/")
ec1096d8
IB
120DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1)
121MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2)
122
123umount "\$DEVICE"
124UUID=\$(lsblk -rno UUID "\$DEVICE")
125
126echo "\$UUID" > /tmp/root.x86_64/device_uuid
127
128cp /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
132mount "\$DEVICE"
133
134cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/"
248bd83e 135cp /tmp/puppet_variables.json "\$MOUNTPOINT/root/"
ec1096d8
IB
136
137/tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh
138EOF
139
140
141cat > $ARCH_CHROOT_SCRIPT <<EOF
142#!/bin/bash
143
144pacman-key --init
145pacman-key --populate archlinux
146
147UUID=\$(cat /device_uuid)
148PART="/dev/disk/by-uuid/\$UUID"
149DEVICE=\$(realpath "\$PART")
150
151# mkfs.ext4 -F -U "\$UUID" "\$DEVICE"
152mount "\$DEVICE" /mnt
153
154##### FIXME: mkfs.ext4 would be better ####
155for 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
162done
163##### /FIXME ####
164
165pacstrap /mnt base git puppet
166
167echo "\$PART / auto defaults 0 1" > /mnt/etc/fstab
168
169umount /mnt
170EOF
171
172cat > $ARCH_INSTALL_SCRIPT <<EOF
173CODE_PATH="/etc/puppetlabs/code"
174rm -rf \$CODE_PATH
248bd83e 175git clone -b $git_branch --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH
ec1096d8
IB
176puppet apply --test \$CODE_PATH/manifests/site.pp
177# The password seed requires puppet to be run twice
178puppet apply --test \$CODE_PATH/manifests/site.pp
179EOF
180
181chmod a+x $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT
182
183expect -f - <<EOF
184set timeout -1
248bd83e 185spawn 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
186expect "assword:"
187send "$password\n"
188expect eof
248bd83e 189spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_script.sh
ec1096d8
IB
190expect "assword:"
191send "$password\r"
192expect eof
193EOF
194
503e4cf5
IB
195### Role specific stage
196read -p "Press key when LDAP is configured" i
197
198cat > $ARCH_PUPPET_CONFIGURATION_SCRIPT <<EOF
199CODE_PATH="/etc/puppetlabs/code"
200puppet apply --test \$CODE_PATH/manifests/site.pp
201EOF
202
203cat > $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT <<EOF
28f9451d 204DEVICE_STR=\$(cat /proc/mounts | grep "/dev/[sv]d.. /mnt/")
503e4cf5
IB
205DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1)
206MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2)
207
208cp /tmp/arch_puppet_configuration_script.sh "\$MOUNTPOINT/root/"
209
210/tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_puppet_configuration_script.sh
211EOF
212
213chmod a+x $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT
214
215expect -f - <<EOF
216set timeout -1
248bd83e 217spawn 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
218expect "assword:"
219send "$password\n"
220expect eof
248bd83e 221spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_puppet_configuration_script.sh
503e4cf5
IB
222expect "assword:"
223send "$password\r"
224expect eof
225EOF
226
227### Installation finished
248bd83e 228if [ -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"
231fi