diff options
Diffstat (limited to 'python/reboot_cloud_instance.py')
-rw-r--r-- | python/reboot_cloud_instance.py | 39 |
1 files changed, 39 insertions, 0 deletions
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 @@ | |||
1 | # -*- encoding: utf-8 -*- | ||
2 | import json | ||
3 | try: | ||
4 | from ovh import ovh | ||
5 | except ImportError: | ||
6 | # In case it's installed globally | ||
7 | import ovh | ||
8 | import sys | ||
9 | |||
10 | # Credentials are stored in ~/.ovh.conf | ||
11 | # See ovh/README.rst | ||
12 | client = ovh.Client() | ||
13 | |||
14 | projects_list = client.get('/cloud/project/') | ||
15 | if len(projects_list) > 1: | ||
16 | print("More than one project is not supported, taking the first one") | ||
17 | project = projects_list[0] | ||
18 | instances_list = client.get('/cloud/project/{}/instance'.format(project)) | ||
19 | instances = dict(map(lambda x: (x["id"], x), instances_list)) | ||
20 | if sys.argv[-1] in instances: | ||
21 | instance = instances[sys.argv[-1]] | ||
22 | else: | ||
23 | print("Instance not in list:") | ||
24 | for instance in instances_list: | ||
25 | print("{}: {}".format(instance["name"], instance["id"])) | ||
26 | sys.exit(1) | ||
27 | |||
28 | if "--rescue" in sys.argv: | ||
29 | netboot_mode="rescue" | ||
30 | elif "--local" in sys.argv: | ||
31 | netboot_mode="local" | ||
32 | else: | ||
33 | netboot_mode=None | ||
34 | |||
35 | if netboot_mode is not None: | ||
36 | result = client.post("/cloud/project/{}/instance/{}/rescueMode".format(project, | ||
37 | instance["id"]), imageId=instance["imageId"], rescue=(netboot_mode == "rescue")) | ||
38 | print(result) | ||
39 | |||