From: Sage Weil Date: Wed, 29 Aug 2018 13:21:32 +0000 (-0500) Subject: common/hostname: use NODE_NAME environment variable insetad, if set X-Git-Tag: v13.2.3~82^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d9f1a7d6addc095ab92d27e093c79a46f1cb61f9;p=ceph.git common/hostname: use NODE_NAME environment variable insetad, if set This is set by rook to the physical node. Signed-off-by: Sage Weil (cherry picked from commit e583f80086d6afb960bbb44b85fe4b2dc73be549) --- diff --git a/src/common/hostname.cc b/src/common/hostname.cc index 32436534ea70..879fc93969ee 100644 --- a/src/common/hostname.cc +++ b/src/common/hostname.cc @@ -18,6 +18,12 @@ std::string ceph_get_hostname() { + // are we in a container? if so we would prefer the *real* hostname. + const char *node_name = getenv("NODE_NAME"); + if (node_name) { + return node_name; + } + char buf[1024]; gethostname(buf, 1024); return std::string(buf); diff --git a/src/test/common/test_hostname.cc b/src/test/common/test_hostname.cc index 54f5e3e23343..4f9aeef4d29d 100644 --- a/src/test/common/test_hostname.cc +++ b/src/test/common/test_hostname.cc @@ -44,12 +44,24 @@ std::string exec(const char* cmd) { TEST(Hostname, full) { std::string hn = ceph_get_hostname(); - ASSERT_EQ(hn, exec("hostname")) ; - - + if (const char *nn = getenv("NODE_NAME")) { + // we are in a container + std::cout << "we are in a container on " << nn << ", reporting " << hn + << std::endl; + ASSERT_EQ(hn, nn); + } else { + ASSERT_EQ(hn, exec("hostname")) ; + } } TEST(Hostname, short) { std::string shn = ceph_get_short_hostname(); - ASSERT_EQ(shn, exec("hostname -s")) ; + if (const char *nn = getenv("NODE_NAME")) { + // we are in a container + std::cout << "we are in a container on " << nn << ", reporting short " << shn + << ", skipping test because env var may or may not be short form" + << std::endl; + } else { + ASSERT_EQ(shn, exec("hostname -s")) ; + } }