diff options
author | Scott Moser <smoser@ubuntu.com> | 2011-12-16 09:23:22 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2011-12-16 09:23:22 -0500 |
commit | 0c6228a36eebb3bc84924afa99f984c37c3245ca (patch) | |
tree | 57d3c4d51990b409d0fb602eb7a32f18cee3143c /cloudinit | |
parent | 01e5b4f1b8f88018fa4b3a856d0ef4cec06630f2 (diff) | |
download | vyos-cloud-init-0c6228a36eebb3bc84924afa99f984c37c3245ca.tar.gz vyos-cloud-init-0c6228a36eebb3bc84924afa99f984c37c3245ca.zip |
move logic of "is this an ipv4 address" to a function is_ipv4
Diffstat (limited to 'cloudinit')
-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 |