]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - python/reboot_cloud_instance.py
Merge branch 'dev'
[perso/Immae/Projets/Puppet.git] / python / reboot_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
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