From: Loic Dachary Date: Tue, 3 Nov 2015 22:45:13 +0000 (+0100) Subject: openstack: try to get private IP from neutron X-Git-Tag: 1.1.0~749^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=5e9240a604e7cb4eb00d6153894e3b7598d8082e;p=teuthology.git openstack: try to get private IP from neutron Signed-off-by: Loic Dachary --- diff --git a/teuthology/openstack/__init__.py b/teuthology/openstack/__init__.py index b65be1b7e0..11f6775ade 100644 --- a/teuthology/openstack/__init__.py +++ b/teuthology/openstack/__init__.py @@ -313,14 +313,44 @@ class OpenStack(object): if found: return addresses + @staticmethod + def get_ip_neutron(instance_id): + subnets = json.loads(misc.sh("neutron subnet-list -f json -c id -c ip_version")) + subnet_id = None + for subnet in subnets: + if subnet['ip_version'] == 4: + subnet_id = subnet['id'] + break + if not subnet_id: + raise Exception("no subnet with ip_version == 4") + ports = json.loads(misc.sh("neutron port-list -f json -c fixed_ips -c device_id")) + fixed_ips = None + for port in ports: + if port['device_id'] == instance_id: + fixed_ips = port['fixed_ips'].split("\n") + break + if not fixed_ips: + raise Exception("no fixed ip record found") + ip = None + for fixed_ip in fixed_ips: + record = json.loads(fixed_ip) + if record['subnet_id'] == subnet_id: + ip = record['ip_address'] + break + if not ip: + raise Exception("no ip") + return ip + def get_ip(self, instance_id, network): """ - Return the private IP of the OpenStack instance_id. The network, - if not the empty string, disambiguate multiple networks attached - to the instance. + Return the private IP of the OpenStack instance_id. """ - return re.findall(network + '=([\d.]+)', - self.get_addresses(instance_id))[0] + try: + return self.get_ip_neutron(instance_id) + except Exception as e: + log.debug("ignoring get_ip_neutron exception " + str(e)) + return re.findall(network + '=([\d.]+)', + self.get_addresses(instance_id))[0] class TeuthologyOpenStack(OpenStack): diff --git a/teuthology/openstack/test/test_openstack.py b/teuthology/openstack/test/test_openstack.py index c066cc8ace..feebaa0c84 100644 --- a/teuthology/openstack/test/test_openstack.py +++ b/teuthology/openstack/test/test_openstack.py @@ -26,6 +26,7 @@ import logging import os import pytest import tempfile +from mock import patch import teuthology from teuthology import misc @@ -95,6 +96,49 @@ class TestOpenStack(object): else: del os.environ['OS_AUTH_URL'] + def test_get_ip_neutron(self): + instance_id = '8e1fd70a-3065-46f8-9c30-84dc028c1834' + ip = '10.10.10.4' + def sh(cmd): + if 'neutron subnet-list' in cmd: + return """ +[ + { + "ip_version": 6, + "id": "c45b9661-b2ba-4817-9e3a-f8f63bf32989" + }, + { + "ip_version": 4, + "id": "e03a3dbc-afc8-4b52-952e-7bf755397b50" + } +] + """ + elif 'neutron port-list' in cmd: + return (""" +[ + { + "device_id": "915504ad-368b-4cce-be7c-4f8a83902e28", + "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.1\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc::1\\"}" + }, + { + "device_id": "{instance_id}", + "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"{ip}\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe07:76c1\\"}" + }, + { + "device_id": "17e4a968-4caa-4cee-8e4b-f950683a02bd", + "fixed_ips": "{\\"subnet_id\\": \\"e03a3dbc-afc8-4b52-952e-7bf755397b50\\", \\"ip_address\\": \\"10.10.10.5\\"}\\n{\\"subnet_id\\": \\"c45b9661-b2ba-4817-9e3a-f8f63bf32989\\", \\"ip_address\\": \\"2607:f298:6050:9afc:f816:3eff:fe9c:37f0\\"}" + } +] + """.replace('{instance_id}', instance_id). + replace('{ip}', ip)) + else: + raise Exception("unexpected " + cmd) + with patch.multiple( + misc, + sh=sh, + ): + assert ip == OpenStack.get_ip_neutron(instance_id) + class TestTeuthologyOpenStack(object): @classmethod