diff options
Diffstat (limited to 'cloudinit/DataSource.py')
-rw-r--r-- | cloudinit/DataSource.py | 50 |
1 files changed, 43 insertions, 7 deletions
diff --git a/cloudinit/DataSource.py b/cloudinit/DataSource.py index 350b5015..3f851e14 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 @@ -93,23 +94,58 @@ class DataSource: def get_instance_id(self): if 'instance-id' not in self.metadata: - return "ubuntuhost" + return "iid-datasource" return(self.metadata['instance-id']) - def get_hostname(self): + def get_hostname(self, fqdn=False): + defdomain = "localdomain" + defhost = "localhost" + + domain = defdomain if not 'local-hostname' in self.metadata: - return None - toks = self.metadata['local-hostname'].split('.') + # 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('.') + # 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 |