]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blame - python/reinstall_ovh_cloud_instance.py
Use json for send_nrdp
[perso/Immae/Projets/Puppet.git] / python / reinstall_ovh_cloud_instance.py
CommitLineData
df0c42af
IB
1# -*- encoding: utf-8 -*-
2import json
3try:
4 from ovh import ovh
5except ImportError:
6 # In case it's installed globally
7 import ovh
8import sys
69da835d 9from ovh_helper import find_cloud_instance
df0c42af
IB
10
11# Credentials are stored in ~/.ovh.conf
12# See ovh/README.rst
13client = ovh.Client()
14
69da835d 15project, instance = find_cloud_instance(client, sys.argv[-1])
df0c42af
IB
16
17current_image = instance["imageId"]
18available_images = client.get('/cloud/project/{}/image'.format(project),
19 osType="linux",
20 region=instance["region"])
21available_images_ids = list(map(lambda x: x["id"], available_images))
22
23def print_images(available_images):
24 for image in available_images:
25 print("{}: {}".format(image["name"], image["id"]))
26
27def reinstall(image_id):
28 return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]),
29 imageId=image_id)
30
31if "--get-state" in sys.argv:
32 print(instance["status"])
33elif "--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)
40elif sys.argv[-1] in available_templates:
41 print("Chosen image available, using it")
42 print(reinstall(current_image))
43else:
44 print("Chosen image not available. Chose among:")
45 print_images(available_images)
46