diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-16 19:14:29 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-16 20:13:46 +0100 |
commit | 69da835d04e741f4e85da3c473ba86c8801931fd (patch) | |
tree | 0cfb28cfd468a5b681f2b5af2b935c81d2221950 /python/reinstall_ovh_cloud_instance.py | |
parent | c15f2234474ff8a8266e26856702b3c561050667 (diff) | |
download | Puppet-69da835d04e741f4e85da3c473ba86c8801931fd.tar.gz Puppet-69da835d04e741f4e85da3c473ba86c8801931fd.tar.zst Puppet-69da835d04e741f4e85da3c473ba86c8801931fd.zip |
Merge install scripts
Diffstat (limited to 'python/reinstall_ovh_cloud_instance.py')
-rw-r--r-- | python/reinstall_ovh_cloud_instance.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/python/reinstall_ovh_cloud_instance.py b/python/reinstall_ovh_cloud_instance.py new file mode 100644 index 0000000..c0d2617 --- /dev/null +++ b/python/reinstall_ovh_cloud_instance.py | |||
@@ -0,0 +1,46 @@ | |||
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 | from ovh_helper import find_cloud_instance | ||
10 | |||
11 | # Credentials are stored in ~/.ovh.conf | ||
12 | # See ovh/README.rst | ||
13 | client = ovh.Client() | ||
14 | |||
15 | project, instance = find_cloud_instance(client, sys.argv[-1]) | ||
16 | |||
17 | current_image = instance["imageId"] | ||
18 | available_images = client.get('/cloud/project/{}/image'.format(project), | ||
19 | osType="linux", | ||
20 | region=instance["region"]) | ||
21 | available_images_ids = list(map(lambda x: x["id"], available_images)) | ||
22 | |||
23 | def print_images(available_images): | ||
24 | for image in available_images: | ||
25 | print("{}: {}".format(image["name"], image["id"])) | ||
26 | |||
27 | def reinstall(image_id): | ||
28 | return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]), | ||
29 | imageId=image_id) | ||
30 | |||
31 | if "--get-state" in sys.argv: | ||
32 | print(instance["status"]) | ||
33 | elif "--use-current" in sys.argv: | ||
34 | if current_image in available_images_ids: | ||
35 | print("Current image still available, using it") | ||
36 | print(reinstall(current_image)) | ||
37 | else: | ||
38 | print("Current image no more available. Chose among:") | ||
39 | print_images(available_images) | ||
40 | elif sys.argv[-1] in available_templates: | ||
41 | print("Chosen image available, using it") | ||
42 | print(reinstall(current_image)) | ||
43 | else: | ||
44 | print("Chosen image not available. Chose among:") | ||
45 | print_images(available_images) | ||
46 | |||