]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - python/reinstall_ovh_vps_ssd.py
Merge branch 'dev'
[perso/Immae/Projets/Puppet.git] / python / reinstall_ovh_vps_ssd.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 import ovh_helper
10
11 # Credentials are stored in ~/.ovh.conf
12 # See ovh/README.rst
13 client = ovh.Client()
14
15 vps_list = client.get('/vps/')
16 if sys.argv[-1] in vps_list:
17 vps = sys.argv[-1]
18 else:
19 print("VPS not in list:")
20 for vps in vps_list:
21 print(vps)
22 sys.exit(1)
23
24 current_distribution = client.get('/vps/{}/distribution'.format(vps))
25
26 available_templates = client.get('/vps/{}/templates'.format(vps))
27
28 def print_templates(client, vps, available_templates):
29 for tid in available_templates:
30 template = client.get('/vps/{}/templates/{}'.format(vps, tid))
31 print("{}: {}".format(template["id"], template["distribution"]))
32
33
34 if "--get-state" in sys.argv:
35 print(client.get('/vps/{}'.format(vps))["state"])
36 elif "--use-current" in sys.argv:
37 if current_distribution['id'] in available_templates:
38 print("Current template still available, using it")
39 result = client.post('/vps/{}/reinstall'.format(vps), templateId=current_distribution['id'])
40 print(result)
41 ovh_helper.show_progress(client, vps, "reinstallVm")
42 else:
43 print("Current template no more available. Chose among:")
44 print_templates(client, vps, available_templates)
45 elif sys.argv[-1] in available_templates:
46 print("Chosen template available, using it")
47 result = client.post('/vps/{}/reinstall'.format(vps), templateId=int(sys.argv[-1]))
48 print(result)
49 ovh_helper.show_progress(client, vps, "reinstallVm")
50 else:
51 print("Chosen template not available. Chose among:")
52 print_templates(client, vps, available_templates)
53
54 # Reboot in rescue:
55 # PUT /vps/{serviceName}
56 # netbootMode "rescue" / "local"
57 # changer son nom:
58 # displayName: "..."