]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Add install scripts
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 31 Aug 2017 22:22:47 +0000 (00:22 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 31 Aug 2017 22:42:25 +0000 (00:42 +0200)
.gitignore [new file with mode: 0644]
.gitmodules
bin/install_script.sh [new file with mode: 0755]
python/ovh [new submodule]
python/ovh_helper.py [new file with mode: 0644]
python/reboot_vps_server.py [new file with mode: 0644]
python/reinstall_vps_server.py [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..c7e8afb
--- /dev/null
@@ -0,0 +1 @@
+python/__pycache__/
index 24305eabca74b273e1ddcc5776484889ae2b6166..fa48ebf28430c7995c657fc2939c718223e25492 100644 (file)
@@ -28,3 +28,6 @@
 [submodule "modules/pacman"]
        path = modules/pacman
        url = git://git.immae.eu/github/aboe76/puppet-pacman
+[submodule "python/ovh"]
+       path = python/ovh
+       url = git://git.immae.eu/github/ovh/python-ovh
diff --git a/bin/install_script.sh b/bin/install_script.sh
new file mode 100755 (executable)
index 0000000..f250285
--- /dev/null
@@ -0,0 +1,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
diff --git a/python/ovh b/python/ovh
new file mode 160000 (submodule)
index 0000000..2a0cc19
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 2a0cc19a96d8efc38ca8e1897c8fa38e459beb8a
diff --git a/python/ovh_helper.py b/python/ovh_helper.py
new file mode 100644 (file)
index 0000000..a49a245
--- /dev/null
@@ -0,0 +1,19 @@
+import time
+
+def show_progress(client, vps, task_type):
+    running_task_id = client.get("/vps/{}/tasks?type={}".format(vps, task_type))[0]
+
+    progress = 0
+    state = "todo"
+    print("  0 %", end='')
+    while state != "done":
+        old_progress = progress
+        task = client.get("/vps/{}/tasks/{}".format(vps, running_task_id))
+        progress = task["progress"]
+        state = task["state"]
+
+        if progress != old_progress:
+            print("\r{:>3} %".format(progress), end="")
+        time.sleep(3)
+
+    print("\rFinished")
diff --git a/python/reboot_vps_server.py b/python/reboot_vps_server.py
new file mode 100644 (file)
index 0000000..7ea301a
--- /dev/null
@@ -0,0 +1,36 @@
+# -*- encoding: utf-8 -*-
+import json
+from ovh import ovh
+import sys
+import ovh_helper
+
+# Credentials are stored in ~/.ovh.conf
+# See ovh/README.rst
+client = ovh.Client()
+
+vps_list = client.get('/vps/')
+if sys.argv[-1] in vps_list:
+    vps = sys.argv[-1]
+else:
+    print("VPS not in list:")
+    for vps in vps_list:
+        print(vps)
+    sys.exit(1)
+
+if "--rescue" in sys.argv:
+    netboot_mode="rescue"
+elif "--local" in sys.argv:
+    netboot_mode="local"
+else:
+    netboot_mode=None
+
+current_state=client.get("/vps/{}".format(vps))["netbootMode"]
+
+if netboot_mode is None or current_state == netboot_mode:
+    client.post("/vps/{}/reboot".format(vps))
+    task_type="rebootVm"
+else:
+    client.put("/vps/{}".format(vps), netbootMode=netboot_mode)
+    task_type="setNetboot"
+
+ovh_helper.show_progress(client, vps, task_type)
diff --git a/python/reinstall_vps_server.py b/python/reinstall_vps_server.py
new file mode 100644 (file)
index 0000000..2eea203
--- /dev/null
@@ -0,0 +1,82 @@
+# -*- encoding: utf-8 -*-
+import json
+from ovh import ovh
+import sys
+import ovh_helper
+
+# Credentials are stored in ~/.ovh.conf
+# See ovh/README.rst
+client = ovh.Client()
+
+vps_list = client.get('/vps/')
+if sys.argv[-1] in vps_list:
+    vps = sys.argv[-1]
+else:
+    print("VPS not in list:")
+    for vps in vps_list:
+        print(vps)
+    sys.exit(1)
+
+current_distribution = client.get('/vps/{}/distribution'.format(vps))
+
+available_templates = client.get('/vps/{}/templates'.format(vps))
+
+def print_templates(client, vps, available_templates):
+    for tid in available_templates:
+        template = client.get('/vps/{}/templates/{}'.format(vps, tid))
+        print("{}: {}".format(template["id"], template["distribution"]))
+
+
+if "--get-state" in sys.argv:
+    print(client.get('/vps/{}'.format(vps))["state"])
+elif "--use-current" in sys.argv:
+    if current_distribution['id'] in available_templates:
+        print("Current template still available, using it")
+        result = client.post('/vps/{}/reinstall'.format(vps), templateId=current_distribution['id'])
+        print(result)
+        ovh_helper.show_progress(client, vps, "reinstallVm")
+    else:
+        print("Current template no more available. Chose among:")
+        print_templates(client, vps, available_templates)
+elif sys.argv[-1] in available_templates:
+    print("Chosen template available, using it")
+    result = client.post('/vps/{}/reinstall'.format(vps), templateId=int(sys.argv[-1]))
+    print(result)
+    ovh_helper.show_progress(client, vps, "reinstallVm")
+else:
+    print("Chosen template not available. Chose among:")
+    print_templates(client, vps, available_templates)
+
+# Buy new:
+# POST /order/cart
+#    ovhSubsidiary FR
+# POST /order/cart/{cartId}/assign
+# GET /vps/datacenter?country=FR
+# Get list of vps: 
+# GET /order/cart/{cartId}/vps
+# POST /order/cart/{cartId}/vps
+#    duration "P1M"
+#    planCode "vps_ssd_model1"
+#    pricingMode "default"
+#    quantity 1
+# POST /order/cart/{cartId}/item/{item}/configuration
+#    label: "vps_ssd_datacenter"
+#    value: "gra"
+# POST /order/cart/{cartId}/item/{item}/configuration
+#    label: "vps_ssd_os"
+#    value: "linux--archlinux--64--en"
+# POST /order/cart/{cartId}/item/{item}/configuration
+#    label: "AUTO_RENEW_VPS"
+#    value: false
+# GET /order/cart/{carId}/summary
+# GET /order/cart/{cartId}/checkout
+# POST /order/cart/{cartId}/checkout
+#    waiveRetractationPeriod
+
+# /me/paymentMean ? /me/order/{orderId}/debt/pay ?
+
+# Reboot in rescue:
+# PUT /vps/{serviceName}
+#   netbootMode "rescue" / "local"
+#  changer son nom:
+#   displayName: "..."