From ec1096d8c0d897ebd1ea445d9c5404a13c33ce2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Fri, 1 Sep 2017 00:22:47 +0200 Subject: Add install scripts --- python/ovh | 1 + python/ovh_helper.py | 19 ++++++++++ python/reboot_vps_server.py | 36 +++++++++++++++++++ python/reinstall_vps_server.py | 82 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 160000 python/ovh create mode 100644 python/ovh_helper.py create mode 100644 python/reboot_vps_server.py create mode 100644 python/reinstall_vps_server.py (limited to 'python') diff --git a/python/ovh b/python/ovh new file mode 160000 index 0000000..2a0cc19 --- /dev/null +++ b/python/ovh @@ -0,0 +1 @@ +Subproject commit 2a0cc19a96d8efc38ca8e1897c8fa38e459beb8a diff --git a/python/ovh_helper.py b/python/ovh_helper.py new file mode 100644 index 0000000..a49a245 --- /dev/null +++ b/python/ovh_helper.py @@ -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 index 0000000..7ea301a --- /dev/null +++ b/python/reboot_vps_server.py @@ -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 index 0000000..2eea203 --- /dev/null +++ b/python/reinstall_vps_server.py @@ -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: "..." -- cgit v1.2.3