From df0c42af3366f013afa1ee13eed04307260beae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 11 Mar 2018 12:26:28 +0100 Subject: Add scripts for ovh cloud --- bin/install_script.sh | 237 --------------------- bin/install_script_ovh_cloud_instance.sh | 182 ++++++++++++++++ bin/install_script_ovh_vps_ssd.sh | 237 +++++++++++++++++++++ python/get_initial_configuration_cloud_instance.py | 41 ++++ python/reboot_cloud_instance.py | 39 ++++ python/reinstall_cloud_instance.py | 57 +++++ 6 files changed, 556 insertions(+), 237 deletions(-) delete mode 100755 bin/install_script.sh create mode 100755 bin/install_script_ovh_cloud_instance.sh create mode 100755 bin/install_script_ovh_vps_ssd.sh create mode 100644 python/get_initial_configuration_cloud_instance.py create mode 100644 python/reboot_cloud_instance.py create mode 100644 python/reinstall_cloud_instance.py diff --git a/bin/install_script.sh b/bin/install_script.sh deleted file mode 100755 index 6b1aa39..0000000 --- a/bin/install_script.sh +++ /dev/null @@ -1,237 +0,0 @@ -#!/bin/bash - -usage() { -cat < $ARCH_PUPPET_INITIAL_CONFIGURATION - -cat > $ARCH_HOST_SCRIPT < /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") - -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/" -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 < /mnt/etc/fstab - -umount /mnt -EOF - -cat > $ARCH_INSTALL_SCRIPT < $ARCH_PUPPET_CONFIGURATION_SCRIPT < $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT < $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 < $ARCH_INSTALL_SCRIPT < $ARCH_PUPPET_CONFIGURATION_SCRIPT < $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT < $ARCH_PUPPET_INITIAL_CONFIGURATION + +cat > $ARCH_HOST_SCRIPT < /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") + +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/" +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 < /mnt/etc/fstab + +umount /mnt +EOF + +cat > $ARCH_INSTALL_SCRIPT < $ARCH_PUPPET_CONFIGURATION_SCRIPT < $ARCH_HOST_PUPPET_CONFIGURATION_SCRIPT < 1: + print("More than one project is not supported, taking the first one") +project = projects_list[0] +instances_list = client.get('/cloud/project/{}/instance'.format(project)) +instances = dict(map(lambda x: (x["id"], x), instances_list)) +if sys.argv[-1] in instances: + instance = instances[sys.argv[-1]] +else: + print("Instance not in list:") + for instance in instances_list: + print("{}: {}".format(instance["name"], instance["id"])) + sys.exit(1) + +infos["ips"] = {} +for ip_infos in instance["ipAddresses"]: + ip_infos["ipAddress"] = ip_infos.pop("ip") + ip_infos["gateway"] = ip_infos.pop("gatewayIp") + + if ip_infos["version"] == 4: + infos["ips"]["v4"] = ip_infos + else: + infos["ips"]["v6"] = ip_infos + infos["ips"]["v6"]["mask"] = 128 + +print(json.dumps(infos)) diff --git a/python/reboot_cloud_instance.py b/python/reboot_cloud_instance.py new file mode 100644 index 0000000..b90f488 --- /dev/null +++ b/python/reboot_cloud_instance.py @@ -0,0 +1,39 @@ +# -*- encoding: utf-8 -*- +import json +try: + from ovh import ovh +except ImportError: + # In case it's installed globally + import ovh +import sys + +# Credentials are stored in ~/.ovh.conf +# See ovh/README.rst +client = ovh.Client() + +projects_list = client.get('/cloud/project/') +if len(projects_list) > 1: + print("More than one project is not supported, taking the first one") +project = projects_list[0] +instances_list = client.get('/cloud/project/{}/instance'.format(project)) +instances = dict(map(lambda x: (x["id"], x), instances_list)) +if sys.argv[-1] in instances: + instance = instances[sys.argv[-1]] +else: + print("Instance not in list:") + for instance in instances_list: + print("{}: {}".format(instance["name"], instance["id"])) + sys.exit(1) + +if "--rescue" in sys.argv: + netboot_mode="rescue" +elif "--local" in sys.argv: + netboot_mode="local" +else: + netboot_mode=None + +if netboot_mode is not None: + result = client.post("/cloud/project/{}/instance/{}/rescueMode".format(project, + instance["id"]), imageId=instance["imageId"], rescue=(netboot_mode == "rescue")) + print(result) + diff --git a/python/reinstall_cloud_instance.py b/python/reinstall_cloud_instance.py new file mode 100644 index 0000000..c488fda --- /dev/null +++ b/python/reinstall_cloud_instance.py @@ -0,0 +1,57 @@ +# -*- encoding: utf-8 -*- +import json +try: + from ovh import ovh +except ImportError: + # In case it's installed globally + import ovh +import sys + +# Credentials are stored in ~/.ovh.conf +# See ovh/README.rst +client = ovh.Client() + +projects_list = client.get('/cloud/project/') +if len(projects_list) > 1: + print("More than one project is not supported, taking the first one") +project = projects_list[0] +instances_list = client.get('/cloud/project/{}/instance'.format(project)) +instances = dict(map(lambda x: (x["id"], x), instances_list)) +if sys.argv[-1] in instances: + instance = instances[sys.argv[-1]] +else: + print("Instance not in list:") + for instance in instances_list: + print("{}: {}".format(instance["name"], instance["id"])) + sys.exit(1) + +current_image = instance["imageId"] +available_images = client.get('/cloud/project/{}/image'.format(project), + osType="linux", + region=instance["region"]) +available_images_ids = list(map(lambda x: x["id"], available_images)) + +def print_images(available_images): + for image in available_images: + print("{}: {}".format(image["name"], image["id"])) + +def reinstall(image_id): + return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]), + imageId=image_id) + +if "--get-state" in sys.argv: + print(instance["status"]) +elif "--use-current" in sys.argv: + if current_image in available_images_ids: + print("Current image still available, using it") + print(reinstall(current_image)) + else: + print("Current image no more available. Chose among:") + print_images(available_images) +elif sys.argv[-1] in available_templates: + print("Chosen image available, using it") + print(reinstall(current_image)) +else: + print("Chosen image not available. Chose among:") + print_images(available_images) + -- cgit v1.2.3