aboutsummaryrefslogtreecommitdiff
path: root/bin/install_script_ovh_cloud_instance.sh
blob: 26e410e214d921dcf1be7bdf679533172ecf22d3 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash

usage() {
cat <<EOF
  $0 [options]
  --help,-h               This help
  --instance-id id        Id of the instance
  --reinstall-first       Start with reinstalling the vps
  --host-user user        Use another user (default: arch)
  --no-reboot             Don't reboot
  --no-reboot-start       Don't reboot to rescue at the beginning
  --no-reboot-end         Don't reboot to normal at the end
  --git-branch            Use another puppet branch (default: master)
  --environment           Environment to use for the installl (default: production)
EOF
}

set -e

host_user=arch
git_branch=master
environment=production

while [ -n "$1" ]; do
  case "$1" in
    --instance-id)
      instance_id="$2"
      shift
      ;;
    --reinstall-first)
      reinstall_first=1
      ;;
    --host-user)
      host_user="$2"
      shift
      ;;
    --no-reboot)
      no_reboot=1
      ;;
    --no-reboot-start)
      no_reboot_start=1
      ;;
    --no-reboot-end)
      no_reboot_end=1
      ;;
    --git-branch)
      git_branch="$2"
      shift
      ;;
    --environment)
      environment="$2"
      shift
      ;;
    --help|-h)
      usage
      exit 0
      ;;
  esac

  shift
done

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

if [ -z "$instance_id" ]; then
  read -p "Id de l'instance : " instance_id
fi

if [ -n "$reinstall_first" ]; then
  echo "Réinstallation du système"
  python $PYTHON_DIRECTORY/reinstall_cloud_instance.py --use-current "$instance_id"

  read -p "Appuyer sur une touche quand le serveur est prêt" ready
fi

if [ -z "$no_reboot" -a -z "$no_reboot_start" ]; then
  echo "Patienter le temps du reboot"
  python $PYTHON_DIRECTORY/reboot_cloud_instance.py --rescue "$instance_id"

  read -p "Appuyer sur une touche quand l'instance a redémarré" ready
fi

ARCH_DIR=`mktemp -d`
ARCH_HOST_SCRIPT="$ARCH_DIR/arch_host_script.sh"
ARCH_INSTALL_SCRIPT="$ARCH_DIR/arch_install_script.sh"
ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_host_puppet_configuration_script.sh"
ARCH_PUPPET_CONFIGURATION_SCRIPT="$ARCH_DIR/arch_puppet_configuration_script.sh"
ARCH_PUPPET_INITIAL_CONFIGURATION="$ARCH_DIR/puppet_variables.json"

trap "rm -rf $ARCH_DIR" EXIT

#### Base installation stage
python $PYTHON_DIRECTORY/get_initial_configuration_cloud_instance.py $instance_id > $ARCH_PUPPET_INITIAL_CONFIGURATION
host_address=$(python $PYTHON_DIRECTORY/get_initial_configuration_cloud_instance.py $instance_id | jq -r '.ips.v4.ipAddress')

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

sudo haveged &
sudo pacman -Sy --noconfirm arch-install-scripts

DEVICE=/dev/vdb1
MOUNTPOINT=/mnt

UUID=\$(lsblk -rno UUID "\$DEVICE")
PART="/dev/disk/by-uuid/\$UUID"

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

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

sudo pacstrap /mnt base git puppet

echo "\$PART / auto defaults 0 1" | sudo tee /mnt/etc/fstab

sudo cp /tmp/arch_install_script.sh "\$MOUNTPOINT/root/"
sudo cp /tmp/puppet_variables.json "\$MOUNTPOINT/root/"

sudo arch-chroot "\$MOUNTPOINT" /root/arch_install_script.sh
EOF

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

chmod a+x $ARCH_HOST_SCRIPT $ARCH_INSTALL_SCRIPT

expect -f - <<EOF
set timeout -1
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_HOST_SCRIPT $ARCH_INSTALL_SCRIPT $host_user@$host_address:/tmp
expect eof
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$host_address /tmp/arch_host_script.sh
expect eof
EOF

### Role specific stage
read -p "Press key when LDAP is configured" i

cat > $ARCH_PUPPET_CONFIGURATION_SCRIPT <<EOF
CODE_PATH="/etc/puppetlabs/code"
puppet apply --environment $environment --tags base_installation --test \$CODE_PATH/manifests/site.pp
EOF

cat > $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT <<EOF
MOUNTPOINT=/mnt

sudo cp /tmp/arch_puppet_configuration_script.sh "\$MOUNTPOINT/root/"

sudo arch-chroot "\$MOUNTPOINT" /root/arch_puppet_configuration_script.sh
EOF

chmod a+x $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT

expect -f - <<EOF
set timeout -1
spawn scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $ARCH_PUPPET_CONFIGURATION_SCRIPT $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $host_user@$host_address:/tmp
expect eof
spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$host_address /tmp/arch_host_puppet_configuration_script.sh
expect eof
EOF

### Installation finished
if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then
  echo "Rebooting"
  python $PYTHON_DIRECTORY/reboot_cloud_instance.py --local "$instance_id"
fi