summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2011-09-14 14:28:44 -0400
committerScott Moser <smoser@ubuntu.com>2011-09-14 14:28:44 -0400
commita826e7b9197d0001fb4d615040cae3c84890575d (patch)
tree0efa7694e38abc8e3045908cb4935b7ed3081e0f /cloudinit/util.py
parent5c520f4e15cb20b694332aa03080f34666873885 (diff)
downloadvyos-cloud-init-a826e7b9197d0001fb4d615040cae3c84890575d.tar.gz
vyos-cloud-init-a826e7b9197d0001fb4d615040cae3c84890575d.zip
try a little harder to get a fqdn rather than defaulting to localdomain
This fixes (LP: #850206). See bug for more info. LP: #850206
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index bdc1fce2..7c33ad85 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -443,3 +443,31 @@ def get_hostname_fqdn(cfg, cloud):
else:
hostname = cloud.get_hostname()
return(hostname, fqdn)
+
+def get_fqdn_from_hosts(hostname, filename="/etc/hosts"):
+ # this parses /etc/hosts to get a fqdn. It should return the same
+ # result as 'hostname -f <hostname>' if /etc/hosts.conf
+ # did not have did not have 'bind' in the order attribute
+ fqdn = None
+ try:
+ with open(filename, "r") as hfp:
+ for line in hfp.readlines():
+ hashpos = line.find("#")
+ if hashpos >= 0:
+ line = line[0:hashpos]
+ toks = line.split()
+
+ # if there there is less than 3 entries (ip, canonical, alias)
+ # then ignore this line
+ if len(toks) < 3:
+ continue
+
+ if hostname in toks[2:]:
+ fqdn = toks[1]
+ break
+ hfp.close()
+ except IOError as e:
+ if e.errno == errno.ENOENT:
+ pass
+
+ return fqdn