]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - python/reinstall_ovh_cloud_instance.py
Use json for send_nrdp
[perso/Immae/Projets/Puppet.git] / python / reinstall_ovh_cloud_instance.py
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