diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/get_initial_configuration_cloud_instance.py | 41 | ||||
-rw-r--r-- | python/reboot_cloud_instance.py | 39 | ||||
-rw-r--r-- | python/reinstall_cloud_instance.py | 57 |
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 -*- | ||
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 | infos = {} | ||
11 | |||
12 | # Credentials are stored in ~/.ovh.conf | ||
13 | # See ovh/README.rst | ||
14 | client = ovh.Client() | ||
15 | |||
16 | projects_list = client.get('/cloud/project/') | ||
17 | if len(projects_list) > 1: | ||
18 | print("More than one project is not supported, taking the first one") | ||
19 | project = projects_list[0] | ||
20 | instances_list = client.get('/cloud/project/{}/instance'.format(project)) | ||
21 | instances = dict(map(lambda x: (x["id"], x), instances_list)) | ||
22 | if sys.argv[-1] in instances: | ||
23 | instance = instances[sys.argv[-1]] | ||
24 | else: | ||
25 | print("Instance not in list:") | ||
26 | for instance in instances_list: | ||
27 | print("{}: {}".format(instance["name"], instance["id"])) | ||
28 | sys.exit(1) | ||
29 | |||
30 | infos["ips"] = {} | ||
31 | for 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 | |||
41 | print(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 -*- | ||
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 | |||
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 -*- | ||
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 | current_image = instance["imageId"] | ||
29 | available_images = client.get('/cloud/project/{}/image'.format(project), | ||
30 | osType="linux", | ||
31 | region=instance["region"]) | ||
32 | available_images_ids = list(map(lambda x: x["id"], available_images)) | ||
33 | |||
34 | def print_images(available_images): | ||
35 | for image in available_images: | ||
36 | print("{}: {}".format(image["name"], image["id"])) | ||
37 | |||
38 | def reinstall(image_id): | ||
39 | return client.post('/cloud/project/{}/instance/{}/reinstall'.format(project, instance["id"]), | ||
40 | imageId=image_id) | ||
41 | |||
42 | if "--get-state" in sys.argv: | ||
43 | print(instance["status"]) | ||
44 | elif "--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) | ||
51 | elif sys.argv[-1] in available_templates: | ||
52 | print("Chosen image available, using it") | ||
53 | print(reinstall(current_image)) | ||
54 | else: | ||
55 | print("Chosen image not available. Chose among:") | ||
56 | print_images(available_images) | ||
57 | |||