diff options
-rw-r--r-- | cloudinit/DataSource.py | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py index e79be09e..5bb7c3b0 100644 --- a/cloudinit/DataSource.py +++ b/cloudinit/DataSource.py @@ -127,16 +127,13 @@ class DataSource: else: - toks = self.metadata['local-hostname'].split('.') # if there is an ipv4 address in 'local-hostname', then - # make up a hostname (LP: #475354) - if len(toks) == 4: - try: - r = filter(lambda x: int(x) < 256 and x > 0, toks) - if len(r) == 4: - toks = [ "ip-%s" % '-'.join(r) ] - except: - pass + # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx + lhost = self.metadata['local-hostname'] + if util.is_ipv4(lhost): + toks = "ip-%s" % lhost.replace(".","-") + else: + toks = lhost.split(".") if len(toks) > 1: hostname = toks[0] @@ -191,3 +188,17 @@ def list_from_depends(depends, dslist): if depset == set(deps): retlist.append(cls) return(retlist) + + +def is_ipv4(instr): + """ determine if input string is a ipv4 address. return boolean""" + toks = instr.split('.') + if len(toks) != 4: + return False + + try: + r = filter(lambda x: int(x) < 256 and x > 0, toks) + except: + return False + + return True |