From bf68afba5fd4a31ad924ca088bf123321583f628 Mon Sep 17 00:00:00 2001 From: Jeremy Freudberg Date: Mon, 4 Jun 2018 15:34:29 -0400 Subject: [PATCH] Allow overriding of /etc/hosts entries Sometimes the DNS lookup performed where Sahara services are running may be inaccurate. Change-Id: I27d2bc4ad9340440878a2f342549d7fe74ee7e68 Story: 2001737 Task: 12092 --- sahara/config.py | 16 ++++++++++++++++ sahara/tests/unit/utils/test_cluster.py | 16 ++++++++++++++++ sahara/utils/cluster.py | 7 +++++++ 3 files changed, 39 insertions(+) diff --git a/sahara/config.py b/sahara/config.py index a5250b8c2a..b154a31021 100644 --- a/sahara/config.py +++ b/sahara/config.py @@ -103,6 +103,20 @@ dns_opts = [ "This is required if 'use_designate' is True") ] +accessible_ip_opts = [ + cfg.IPOpt('identity_ip_accessible', + default=None, + help='IP address of Keystone endpoint, accessible by tenant' + ' machines. If not set, the results of the DNS lookup' + ' performed where Sahara services are running will be' + ' used.'), + cfg.IPOpt('object_store_ip_accessible', + default=None, + help='IP address of Swift endpoint, accessible by tenant' + ' machines. If not set, the results of the DNS lookup' + ' performed where Sahara services are running will be' + ' used.'), +] CONF = cfg.CONF CONF.register_cli_opts(cli_opts) @@ -110,6 +124,7 @@ CONF.register_opts(networking_opts) CONF.register_opts(edp_opts) CONF.register_opts(db_opts) CONF.register_opts(dns_opts) +CONF.register_opts(accessible_ip_opts) log.register_options(CONF) @@ -158,6 +173,7 @@ def list_opts(): networking_opts, dns_opts, db_opts, + accessible_ip_opts, plugins_base.opts, topology_helper.opts, keystone.opts, diff --git a/sahara/tests/unit/utils/test_cluster.py b/sahara/tests/unit/utils/test_cluster.py index c1f719322c..cda7d34581 100644 --- a/sahara/tests/unit/utils/test_cluster.py +++ b/sahara/tests/unit/utils/test_cluster.py @@ -170,3 +170,19 @@ class UtilsClusterTest(base.SaharaWithDbTestCase): expected = ("nameserver 1.1.1.1\n" "nameserver 2.2.2.2\n") self.assertEqual(expected, value) + + @mock.patch("socket.gethostbyname") + @mock.patch("sahara.utils.openstack.base.url_for") + def test_etc_hosts_entry_for_service_overrides(self, mock_url, + mock_get_host): + self.override_config("object_store_ip_accessible", None) + mock_url.return_value = "http://swift.org" + mock_get_host.return_value = '1.1.1.1' + + res = cluster_utils.etc_hosts_entry_for_service('object-store') + self.assertEqual('1.1.1.1 swift.org\n', res) + + self.override_config("object_store_ip_accessible", '2.2.2.2') + + res = cluster_utils.etc_hosts_entry_for_service('object-store') + self.assertEqual('2.2.2.2 swift.org\n', res) diff --git a/sahara/utils/cluster.py b/sahara/utils/cluster.py index b359a6ae7d..9aa5125451 100644 --- a/sahara/utils/cluster.py +++ b/sahara/utils/cluster.py @@ -143,6 +143,13 @@ def etc_hosts_entry_for_service(service): except keystone_ex.EndpointNotFound: LOG.debug("Endpoint not found for service: '{}'".format(service)) return result + + overridden_ip = ( + getattr(CONF, "%s_ip_accessible" % service.replace('-', '_'), None) + ) + if overridden_ip is not None: + return "%s %s\n" % (overridden_ip, hostname) + try: result = "%s %s\n" % (socket.gethostbyname(hostname), hostname) except socket.gaierror: