From 8a42512443986326f3bdcf25fadc0c2aad9cccff Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Thu, 1 Sep 2011 10:07:05 -0400 Subject: improve DataSource's get_hostname function This allows for the chance that platform.node returned a fully qualified domainname. Per its doc: Returns the computer's network name (which may not be fully qualified) --- cloudinit/DataSource.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py index 3c4ca995..3f851e14 100644 --- a/cloudinit/DataSource.py +++ b/cloudinit/DataSource.py @@ -98,9 +98,31 @@ class DataSource: return(self.metadata['instance-id']) def get_hostname(self, fqdn=False): - domain = "localdomain" + defdomain = "localdomain" + defhost = "localhost" + + domain = defdomain if not 'local-hostname' in self.metadata: - toks = [ platform.node(), domain ] + + # this is somewhat questionable really. + # the cloud datasource was asked for a hostname + # and didn't have one. raising error might be more appropriate + # but instead, basically look up the existing hostname + toks = [] + pfn = platform.node() + + # platform.node says: Returns the computer's network + # name (which may not be fully qualified) + toks = pfn.split(".") + if pfn.find(".") > 0: + toks = pfn.split(".") + elif pfn: + toks = [ pfn, defdomain ] + + if len(toks) == 0: + toks = [ defhost, defdomain ] + #log.warn("unable to find hostname, using defaults") + else: toks = self.metadata['local-hostname'].split('.') -- cgit v1.2.3