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