aboutsummaryrefslogtreecommitdiff
path: root/bin/install_script.sh
blob: f250285840785abeb0025d4e32778cc5f94f208c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash

DIRECTORY=$(cd `dirname $0` && pwd)
PYTHON_DIRECTORY="$DIRECTORY/../python"

if [ -n "$1" ]; then
  vps_name="$1"
else
  read -p "Nom du vps : " vps_name
fi

echo "Patienter le temps du reboot"
python $PYTHON_DIRECTORY/reboot_vps_server.py --rescue "$vps_name"

stty -echo
read -p "Mot de passe reçu par e-mail : " password; echo
stty echo

ARCH_DIR=`mktemp -d`
ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh"
ARCH_CHROOT_SCRIPT="$ARCH_DIR/arch_chroot_script.sh"
ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh"

trap "rm -rf $ARCH_DIR" EXIT

cat > $ARCH_HOST_SCRIPT <<EOF
#!/bin/bash

apt-get update
apt-get install -y haveged
haveged &

cd /tmp

LATEST=\$(curl https://mirrors.kernel.org/archlinux/iso/latest/sha1sums.txt | grep "bootstrap" | head -n1)
SHA1=\$(echo "\$LATEST" | cut -d' ' -f1)
NAME=\$(echo "\$LATEST" | cut -d' ' -f3)

curl -O "https://mirrors.kernel.org/archlinux/iso/latest/\$NAME"

tar -xzf "\$NAME"

echo 'Server = http://archlinux.mirrors.ovh.net/archlinux/\$repo/os/\$arch' > /tmp/root.x86_64/etc/pacman.d/mirrorlist

DEVICE_STR=\$(cat /proc/mounts | grep "/dev/sd.. /mnt/")
DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1)
MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2)

umount "\$DEVICE"
UUID=\$(lsblk -rno UUID "\$DEVICE")

echo "\$UUID" > /tmp/root.x86_64/device_uuid

cp /tmp/arch_chroot_script.sh /tmp/root.x86_64/

/tmp/root.x86_64/bin/arch-chroot /tmp/root.x86_64/ /arch_chroot_script.sh

mount "\$DEVICE"

cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/"

/tmp/root.x86_64/bin/arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh
EOF


cat > $ARCH_CHROOT_SCRIPT <<EOF
#!/bin/bash

pacman-key --init
pacman-key --populate archlinux

UUID=\$(cat /device_uuid)
PART="/dev/disk/by-uuid/\$UUID"
DEVICE=\$(realpath "\$PART")

# mkfs.ext4 -F -U "\$UUID" "\$DEVICE"
mount "\$DEVICE" /mnt

##### FIXME: mkfs.ext4 would be better ####
for i in /mnt/*; do
  if [ "\$i" = "/mnt/boot" ]; then
    # keep /boot/grub
    rm -f \$i/*
  else
    rm -rf \$i
  fi
done
##### /FIXME ####

pacstrap /mnt base git puppet

echo "\$PART / auto defaults 0 1" > /mnt/etc/fstab

umount /mnt
EOF

cat > $ARCH_INSTALL_SCRIPT <<EOF
CODE_PATH="/etc/puppetlabs/code"
rm -rf \$CODE_PATH
git clone -b master --recursive https://git.immae.eu/perso/Immae/Projets/Puppet.git \$CODE_PATH
puppet apply --test \$CODE_PATH/manifests/site.pp
# The password seed requires puppet to be run twice
puppet apply --test \$CODE_PATH/manifests/site.pp
EOF

chmod a+x $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT

expect -f - <<EOF
set timeout -1
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT root@$vps_name:/tmp
expect "assword:"
send "$password\n"
expect eof
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no root@$vps_name /tmp/arch_host_script.sh
expect "assword:"
send "$password\r"
expect eof
EOF

read -p "Reboot to normal? [Y/n]" reboot
if [ "x$reboot" != "xn" ]; then
  echo "Rebooting"
  python $PYTHON_DIRECTORY/reboot_vps_server.py --local "$vps_name"
fi