diff options
Diffstat (limited to 'python/reinstall_cloud_instance.py')
-rw-r--r-- | python/reinstall_cloud_instance.py | 57 |
1 files changed, 57 insertions, 0 deletions
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 @@ | |||
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 | current_image = instance["imageId"] | ||
29 | available_images = client.get('/cloud/project/{}/image'.format(project), | ||
30 | osType="linux", | ||
31 | region=instance["region"]) | ||
32 | available_images_ids = list(map(lambda x: x["id"], available_images)) | ||
33 | |||
34 | def print_images(available_images): | ||
35 | for image in available_images: | ||
36 | print("{}: {}".format(image["name"], image["id"])) | ||
37 | |||
38 | def reinstall(image_id): | ||
39 | return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]), | ||
40 | imageId=image_id) | ||
41 | |||
42 | if "--get-state" in sys.argv: | ||
43 | print(instance["status"]) | ||
44 | elif "--use-current" in sys.argv: | ||
45 | if current_image in available_images_ids: | ||
46 | print("Current image still available, using it") | ||
47 | print(reinstall(current_image)) | ||
48 | else: | ||
49 | print("Current image no more available. Chose among:") | ||
50 | print_images(available_images) | ||
51 | elif sys.argv[-1] in available_templates: | ||
52 | print("Chosen image available, using it") | ||
53 | print(reinstall(current_image)) | ||
54 | else: | ||
55 | print("Chosen image not available. Chose among:") | ||
56 | print_images(available_images) | ||
57 | |||