]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blobdiff - bin/install_script.sh
Merge branch 'dev'
[perso/Immae/Projets/Puppet.git] / bin / install_script.sh
index 6b1aa39cc2d9a0153f666894f75df17155a41603..322a1edf2297d016427ec181313b724b9b8f1405 100755 (executable)
@@ -2,39 +2,74 @@
 
 usage() {
 cat <<EOF
-  $0 [options]
+$(basename $0) [options]
   --help,-h               This help
-  --vps vps_name          Name of the vps
-  --password password     Password of the vps (only useful in case of no reboot)
+
+  Please chose environment:
+  --environment env       Environment to use for the install
+
+  One of the following options is necessary:
+  --instance-id id        Id of the cloud instance
+  --vps-id id             Id of the vps
+  --hetzner-id id         Id of the Hetzner server
+
+  Optional arguments:
+  --password password     Password of the host (only useful in case of no reboot and vps)
   --reinstall-first       Start with reinstalling the vps
-  --host-user user        Use another user (default: root)
+  --host-user user        Use another user than the default one
   --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)
+  --git-branch branch     Use another puppet branch (default: master)
 EOF
 }
 
 set -e
 
-host_user=root
 git_branch=master
-environment=production
+host_user=""
+password=""
+T=""
 
 while [ -n "$1" ]; do
   case "$1" in
-    --vps)
-      vps_name="$2"
+    --instance-id)
+      host_id="$2"
+      if [ -z "$host_user" ]; then
+        host_user="arch"
+      fi
+      if [ -z "$password" ]; then
+        password="x"
+      fi
+      [ -n "$T" ] && usage && exit 1
+      T="ovh_cloud_instance"
       shift
       ;;
-    --reinstall-first)
-      reinstall_first=1
+    --vps-id)
+      host_id="$2"
+      if [ -z "$host_user" ]; then
+        host_user="root"
+      fi
+      [ -n "$T" ] && usage && exit 1
+      T="ovh_vps_ssd"
+      shift
+      ;;
+    --hetzner-id)
+      host_id="$2"
+      if [ -z "$host_user" ]; then
+        host_user="root"
+      fi
+      [ -n "$T" ] && usage && exit 1
+      T="hetzner_server"
+      shift
       ;;
     --password)
       password="$2"
       shift
       ;;
+    --reinstall-first)
+      reinstall_first=1
+      ;;
     --host-user)
       host_user="$2"
       shift
@@ -65,23 +100,27 @@ while [ -n "$1" ]; do
   shift
 done
 
+if [ -z "$T" -o -z "$host_id" -o -z "$environment" ]; then
+  usage
+  exit 1
+fi
+
 DIRECTORY=$(cd `dirname $0` && pwd)
 PYTHON_DIRECTORY="$DIRECTORY/../python"
-
-if [ -z "$vps_name" ]; then
-  read -p "Nom du vps : " vps_name
-fi
+SCRIPTS="$DIRECTORY/../scripts"
 
 if [ -n "$reinstall_first" ]; then
   echo "Réinstallation du système"
-  python $PYTHON_DIRECTORY/reinstall_vps_server.py --use-current "$vps_name"
+  python $PYTHON_DIRECTORY/reinstall_$T.py --use-current "$host_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_vps_server.py --rescue "$vps_name"
+  python $PYTHON_DIRECTORY/reboot_$T.py --rescue "$host_id"
+
+  read -p "Appuyer sur une touche quand l'instance a redémarré" ready
 fi
 
 if [ -z "$password" ]; then
@@ -91,147 +130,37 @@ if [ -z "$password" ]; then
 fi
 
 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"
-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_HOST_SCRIPT="$SCRIPTS/$T/arch_host_script.sh"
+if [ -f "$SCRIPTS/$T/arch_chroot_script.sh" ]; then
+  ARCH_CHROOT_SCRIPT="$SCRIPTS/$T/arch_chroot_script.sh"
+else
+  ARCH_CHROOT_SCRIPT=""
+fi
+ARCH_INSTALL_SCRIPT="$SCRIPTS/arch_install_script.sh"
+ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT="$SCRIPTS/$T/arch_host_puppet_configuration_script.sh"
+ARCH_PUPPET_CONFIGURATION_SCRIPT="$SCRIPTS/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.py $vps_name > $ARCH_PUPPET_INITIAL_CONFIGURATION
-
-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/[sv]d.. /mnt/")
-DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1)
-MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2)
-
-umount "\$DEVICE"
-UUID=\$(lsblk -rno UUID "\$DEVICE")
+python $PYTHON_DIRECTORY/get_initial_configuration_$T.py $host_id > $ARCH_PUPPET_INITIAL_CONFIGURATION
+host_address=$(python $PYTHON_DIRECTORY/get_initial_configuration_$T.py $host_id | jq -r '.ips.v4.ipAddress')
 
-echo "\$UUID" > /tmp/root.x86_64/device_uuid
+dest="$host_user@$host_address"
+files="$ARCH_HOST_SCRIPT $ARCH_CHROOT_SCRIPT $ARCH_PUPPET_INITIAL_CONFIGURATION $ARCH_INSTALL_SCRIPT"
 
-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/"
-cp /tmp/puppet_variables.json "\$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 $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_CHROOT_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_CHROOT_SCRIPT $ARCH_INSTALL_SCRIPT $host_user@$vps_name:/tmp
-expect "assword:"
-send "$password\n"
-expect eof
-spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_script.sh
-expect "assword:"
-send "$password\r"
-expect eof
-EOF
+$SCRIPTS/send_and_run.tcl "$dest" "$password" "$git_branch" "$environment" $files
 
 ### 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
-DEVICE_STR=\$(cat /proc/mounts | grep "/dev/[sv]d.. /mnt/")
-DEVICE=\$(echo "\$DEVICE_STR" | cut -d' ' -f1)
-MOUNTPOINT=\$(echo "\$DEVICE_STR" | cut -d' ' -f2)
-
-cp /tmp/arch_puppet_configuration_script.sh "\$MOUNTPOINT/root/"
+files="$ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT $ARCH_PUPPET_CONFIGURATION_SCRIPT"
 
-/tmp/root.x86_64/bin/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@$vps_name:/tmp
-expect "assword:"
-send "$password\n"
-expect eof
-spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=no $host_user@$vps_name /tmp/arch_host_puppet_configuration_script.sh
-expect "assword:"
-send "$password\r"
-expect eof
-EOF
+$SCRIPTS/send_and_run.tcl "$dest" "$password" "$git_branch" "$environment" $files
 
 ### Installation finished
 if [ -z "$no_reboot" -a -z "$no_reboot_end" ]; then
   echo "Rebooting"
-  python $PYTHON_DIRECTORY/reboot_vps_server.py --local "$vps_name"
+  python $PYTHON_DIRECTORY/reboot_$T.py --local "$host_id"
 fi