From d13efd4527686a2bfde67c00f9a758eba2a4250c Mon Sep 17 00:00:00 2001 From: Modulos Date: Fri, 19 May 2017 16:39:56 +0200 Subject: [PATCH 1/4] Get public ip for vms By using kamilas script we can get the ip from a vms mac address. --- opennebula_api/serializers.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index c8878120..fd339e3c 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -1,4 +1,5 @@ import oca +import ipaddress from rest_framework import serializers @@ -81,9 +82,7 @@ class VirtualMachineSerializer(serializers.Serializer): disk_size = serializers.SerializerMethodField() - ip = serializers.CharField(read_only=True, - source='user_template.ungleich_public_ip', - default='-') + ip = serializers.SerializerMethodField() vm_id = serializers.IntegerField(read_only=True, source='id') state = serializers.CharField(read_only=True, source='str_state') price = serializers.SerializerMethodField() @@ -147,3 +146,30 @@ class VirtualMachineSerializer(serializers.Serializer): template_id = obj.template.template_id template = OpenNebulaManager().get_template(template_id) return template.name + + def get_ip(self, obj): + nic = obj.template.nics[0] + if is_in_v4_range(nic.mac): + return str(v4_from_mac(nic.mac)) + else: + return '-' + + + +def hexstr2int(string): + return int(string.replace(':', ''), 16) + +FIRST_MAC = hexstr2int('02:00:b3:39:79:4d') +FIRST_V4 = ipaddress.ip_address('185.203.112.2') +COUNT = 1000 + +def v4_from_mac(mac): + """Calculates the IPv4 address from a MAC address. + + mac: string (the colon-separated representation) + returns: ipaddress.ip_address object with the v4 address + """ + return FIRST_V4 + (hexstr2int(mac) - FIRST_MAC) + +def is_in_v4_range(mac): + return FIRST_MAC <= hexstr2int(mac) < FIRST_MAC + 1000 From ccb583b20d731b965a9082b5f6ee6547d545d7df Mon Sep 17 00:00:00 2001 From: modulos Date: Mon, 22 May 2017 13:28:14 +0200 Subject: [PATCH 2/4] Add vm-ipv6-nat64-ipv4 network name check --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 3d04d61f..622d89f6 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -155,7 +155,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_ip(self, obj): nic = obj.template.nics[0] - if is_in_v4_range(nic.mac): + if 'vm-ipv6-nat64-ipv4' is in nic.network and is_in_v4_range(nic.mac): return str(v4_from_mac(nic.mac)) else: return '-' From 4c8a4d0be8cd66f3b7ac588d02ecc9c1b047fcc5 Mon Sep 17 00:00:00 2001 From: modulos Date: Tue, 23 May 2017 11:17:38 +0200 Subject: [PATCH 3/4] Typo --- opennebula_api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 622d89f6..d762587f 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -155,7 +155,7 @@ class VirtualMachineSerializer(serializers.Serializer): def get_ip(self, obj): nic = obj.template.nics[0] - if 'vm-ipv6-nat64-ipv4' is in nic.network and is_in_v4_range(nic.mac): + if 'vm-ipv6-nat64-ipv4' in nic.network and is_in_v4_range(nic.mac): return str(v4_from_mac(nic.mac)) else: return '-' From adf9b77e4e1db305dd17cd3ef11a8741b72f9735 Mon Sep 17 00:00:00 2001 From: modulos Date: Tue, 23 May 2017 11:30:10 +0200 Subject: [PATCH 4/4] Show ipv6 address --- .../templates/hosting/virtual_machine_detail.html | 12 +++++++++--- opennebula_api/serializers.py | 8 ++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/hosting/templates/hosting/virtual_machine_detail.html b/hosting/templates/hosting/virtual_machine_detail.html index 0b17878d..188afd8e 100644 --- a/hosting/templates/hosting/virtual_machine_detail.html +++ b/hosting/templates/hosting/virtual_machine_detail.html @@ -41,11 +41,17 @@

{{virtual_machine.hosting_company_name}}

- {% if virtual_machine.ip %} + {% if virtual_machine.ipv6 %}
- +
{% else %} diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index d762587f..32fd139e 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -88,7 +88,8 @@ class VirtualMachineSerializer(serializers.Serializer): disk_size = serializers.SerializerMethodField() - ip = serializers.SerializerMethodField() + ipv4 = serializers.SerializerMethodField() + ipv6 = serializers.SerializerMethodField() vm_id = serializers.IntegerField(read_only=True, source='id') state = serializers.CharField(read_only=True, source='str_state') price = serializers.SerializerMethodField() @@ -153,13 +154,16 @@ class VirtualMachineSerializer(serializers.Serializer): template = OpenNebulaManager().get_template(template_id) return template.name - def get_ip(self, obj): + def get_ipv4(self, obj): nic = obj.template.nics[0] if 'vm-ipv6-nat64-ipv4' in nic.network and is_in_v4_range(nic.mac): return str(v4_from_mac(nic.mac)) else: return '-' + def get_ipv6(self, obj): + nic = obj.template.nics[0] + return nic.ip6_global def hexstr2int(string):