summaryrefslogtreecommitdiff
path: root/cloudinit/DataSource.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2011-07-19 22:53:05 -0400
committerScott Moser <smoser@ubuntu.com>2011-07-19 22:53:05 -0400
commit6d25c040ee566f6ef85352d7b52eb5947230f78a (patch)
treecb3f8f99926b3423d623b9a00a95ad5b47b2de7c /cloudinit/DataSource.py
parent20a0cf9bf34408706f34edee5e1e75fd9676774c (diff)
downloadvyos-cloud-init-6d25c040ee566f6ef85352d7b52eb5947230f78a.tar.gz
vyos-cloud-init-6d25c040ee566f6ef85352d7b52eb5947230f78a.zip
improve the updating of /etc/hosts with correct fqdn when possible
Thanks to Adam Gandalman and Marc Cluet for this fix. LP: #812539
Diffstat (limited to 'cloudinit/DataSource.py')
-rw-r--r--cloudinit/DataSource.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py
index 350b5015..65c45166 100644
--- a/cloudinit/DataSource.py
+++ b/cloudinit/DataSource.py
@@ -22,6 +22,7 @@ DEP_NETWORK = "NETWORK"
import UserDataHandler as ud
import cloudinit.util as util
+import platform
class DataSource:
userdata = None
@@ -96,20 +97,33 @@ class DataSource:
return "ubuntuhost"
return(self.metadata['instance-id'])
- def get_hostname(self):
+ def get_hostname(self, fqdn=False):
+ domain = "localdomain"
if not 'local-hostname' in self.metadata:
- return None
+ toks = [ platform.node(), domain ]
+ else:
+ toks = self.metadata['local-hostname'].split('.')
- 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:
- return("ip-%s" % '-'.join(r))
- except: pass
- return toks[0]
+ toks = [ "ip-%s" % '-'.join(r) ]
+ except:
+ pass
+
+ if len(toks) > 1:
+ hostname = toks[0]
+ domain = '.'.join(toks[1:])
+ else:
+ hostname = toks[0]
+
+ if fqdn:
+ return "%s.%s" % (hostname,domain)
+ else:
+ return hostname
# return a list of classes that have the same depends as 'depends'
# iterate through cfg_list, loading "DataSourceCollections" modules