aboutsummaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/get_initial_configuration_cloud_instance.py41
-rw-r--r--python/reboot_cloud_instance.py39
-rw-r--r--python/reinstall_cloud_instance.py57
3 files changed, 137 insertions, 0 deletions
diff --git a/python/get_initial_configuration_cloud_instance.py b/python/get_initial_configuration_cloud_instance.py
new file mode 100644
index 0000000..4157716
--- /dev/null
+++ b/python/get_initial_configuration_cloud_instance.py
@@ -0,0 +1,41 @@
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
9
10infos = {}
11
12# Credentials are stored in ~/.ovh.conf
13# See ovh/README.rst
14client = ovh.Client()
15
16projects_list = client.get('/cloud/project/')
17if len(projects_list) > 1:
18 print("More than one project is not supported, taking the first one")
19project = projects_list[0]
20instances_list = client.get('/cloud/project/{}/instance'.format(project))
21instances = dict(map(lambda x: (x["id"], x), instances_list))
22if sys.argv[-1] in instances:
23 instance = instances[sys.argv[-1]]
24else:
25 print("Instance not in list:")
26 for instance in instances_list:
27 print("{}: {}".format(instance["name"], instance["id"]))
28 sys.exit(1)
29
30infos["ips"] = {}
31for ip_infos in instance["ipAddresses"]:
32 ip_infos["ipAddress"] = ip_infos.pop("ip")
33 ip_infos["gateway"] = ip_infos.pop("gatewayIp")
34
35 if ip_infos["version"] == 4:
36 infos["ips"]["v4"] = ip_infos
37 else:
38 infos["ips"]["v6"] = ip_infos
39 infos["ips"]["v6"]["mask"] = 128
40
41print(json.dumps(infos))
diff --git a/python/reboot_cloud_instance.py b/python/reboot_cloud_instance.py
new file mode 100644
index 0000000..b90f488
--- /dev/null
+++ b/python/reboot_cloud_instance.py
@@ -0,0 +1,39 @@
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
9
10# Credentials are stored in ~/.ovh.conf
11# See ovh/README.rst
12client = ovh.Client()
13
14projects_list = client.get('/cloud/project/')
15if len(projects_list) > 1:
16 print("More than one project is not supported, taking the first one")
17project = projects_list[0]
18instances_list = client.get('/cloud/project/{}/instance'.format(project))
19instances = dict(map(lambda x: (x["id"], x), instances_list))
20if sys.argv[-1] in instances:
21 instance = instances[sys.argv[-1]]
22else:
23 print("Instance not in list:")
24 for instance in instances_list:
25 print("{}: {}".format(instance["name"], instance["id"]))
26 sys.exit(1)
27
28if "--rescue" in sys.argv:
29 netboot_mode="rescue"
30elif "--local" in sys.argv:
31 netboot_mode="local"
32else:
33 netboot_mode=None
34
35if 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
diff --git a/python/reinstall_cloud_instance.py b/python/reinstall_cloud_instance.py
new file mode 100644
index 0000000..c488fda
--- /dev/null
+++ b/python/reinstall_cloud_instance.py
@@ -0,0 +1,57 @@
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
9
10# Credentials are stored in ~/.ovh.conf
11# See ovh/README.rst
12client = ovh.Client()
13
14projects_list = client.get('/cloud/project/')
15if len(projects_list) > 1:
16 print("More than one project is not supported, taking the first one")
17project = projects_list[0]
18instances_list = client.get('/cloud/project/{}/instance'.format(project))
19instances = dict(map(lambda x: (x["id"], x), instances_list))
20if sys.argv[-1] in instances:
21 instance = instances[sys.argv[-1]]
22else:
23 print("Instance not in list:")
24 for instance in instances_list:
25 print("{}: {}".format(instance["name"], instance["id"]))
26 sys.exit(1)
27
28current_image = instance["imageId"]
29available_images = client.get('/cloud/project/{}/image'.format(project),
30 osType="linux",
31 region=instance["region"])
32available_images_ids = list(map(lambda x: x["id"], available_images))
33
34def print_images(available_images):
35 for image in available_images:
36 print("{}: {}".format(image["name"], image["id"]))
37
38def reinstall(image_id):
39 return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]),
40 imageId=image_id)
41
42if "--get-state" in sys.argv:
43 print(instance["status"])
44elif "--use-current" in sys.argv:
45 if current_image in available_images_ids:
46 print("Current image still available, using it")
47 print(reinstall(current_image))
48 else:
49 print("Current image no more available. Chose among:")
50 print_images(available_images)
51elif sys.argv[-1] in available_templates:
52 print("Chosen image available, using it")
53 print(reinstall(current_image))
54else:
55 print("Chosen image not available. Chose among:")
56 print_images(available_images)
57