def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False): """Get hostname or fqdn from the datasource. Look it up if desired. @param fqdn: Boolean, set True to return hostname with domain. @param resolve_ip: Boolean, set True to attempt to resolve an ipv4 address provided in local-hostname meta-data. @param metadata_only: Boolean, set True to avoid looking up hostname if meta-data doesn't have local-hostname present. @return: hostname or qualified hostname. Optionally return None when metadata_only is True and local-hostname data is not available. """ defdomain = "localdomain" defhost = "localhost" domain = defdomain if not self.metadata or 'local-hostname' not in self.metadata: if metadata_only: return None # 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 = [] hostname = util.get_hostname() hosts_fqdn = util.get_fqdn_from_hosts(hostname) if hosts_fqdn and hosts_fqdn.find(".") > 0: toks = str(hosts_fqdn).split(".") elif hostname and hostname.find(".") > 0: toks = str(hostname).split(".") elif hostname: toks = [hostname, defdomain] else: toks = [defhost, defdomain] else: # if there is an ipv4 address in 'local-hostname', then # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx lhost = self.metadata['local-hostname'] if util.is_ipv4(lhost): toks = [] if resolve_ip: toks = util.gethostbyaddr(lhost) if toks: toks = str(toks).split('.') else: toks = ["ip-%s" % lhost.replace(".", "-")] else: toks = lhost.split(".") if len(toks) > 1: hostname = toks[0] domain = '.'.join(toks[1:]) else: hostname = toks[0] if fqdn and domain != defdomain: return "%s.%s" % (hostname, domain) else: return hostname
def getfqdn(name=""): name = name.strip() if not name or name == "0.0.0.0": name = util.get_hostname() try: address = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME) except socket.error: pass else: for addr in address: if addr[3]: name = addr[3] break return name
def get_hostname(self, fqdn=False, resolve_ip=False): defdomain = "localdomain" defhost = "localhost" domain = defdomain if not self.metadata or 'local-hostname' not in self.metadata: # 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 = [] hostname = util.get_hostname() fqdn = util.get_fqdn_from_hosts(hostname) if fqdn and fqdn.find(".") > 0: toks = str(fqdn).split(".") elif hostname and hostname.find(".") > 0: toks = str(hostname).split(".") elif hostname: toks = [hostname, defdomain] else: toks = [defhost, defdomain] else: # if there is an ipv4 address in 'local-hostname', then # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx lhost = self.metadata['local-hostname'] if util.is_ipv4(lhost): toks = [] if resolve_ip: toks = util.gethostbyaddr(lhost) if toks: toks = str(toks).split('.') else: toks = ["ip-%s" % lhost.replace(".", "-")] else: toks = lhost.split(".") if len(toks) > 1: hostname = toks[0] domain = '.'.join(toks[1:]) else: hostname = toks[0] if fqdn and domain != defdomain: return "%s.%s" % (hostname, domain) else: return hostname
def get_hostname(self, fqdn=False, resolve_ip=False): defdomain = "localdomain" defhost = "localhost" domain = defdomain if not self.metadata or 'local-hostname' not in self.metadata: # 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 = [] hostname = util.get_hostname() fqdn = util.get_fqdn_from_hosts(hostname) if fqdn and fqdn.find(".") > 0: toks = str(fqdn).split(".") elif hostname: toks = [hostname, defdomain] else: toks = [defhost, defdomain] else: # if there is an ipv4 address in 'local-hostname', then # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx lhost = self.metadata['local-hostname'] if util.is_ipv4(lhost): toks = [] if resolve_ip: toks = util.gethostbyaddr(lhost) if toks: toks = str(toks).split('.') else: toks = ["ip-%s" % lhost.replace(".", "-")] else: toks = lhost.split(".") 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
def getfqdn(name=""): """Get fully qualified domain name from name. An empty argument is interpreted as meaning the local host. """ # TODO(may want to promote this function to util.getfqdn) # TODO(may want to extend util.get_hostname to accept fqdn=True param) name = name.strip() if not name or name == "0.0.0.0": name = util.get_hostname() try: addrs = socket.getaddrinfo(name, None, 0, socket.SOCK_DGRAM, 0, socket.AI_CANONNAME) except socket.error: pass else: for addr in addrs: if addr[3]: name = addr[3] break return name
def get_host_info(): """ Returns host information such as the host name and network interfaces. """ # TODO(look to promote netifices use up in cloud-init netinfo funcs) host_info = { "network": { "interfaces": { "by-mac": collections.OrderedDict(), "by-ipv4": collections.OrderedDict(), "by-ipv6": collections.OrderedDict(), }, }, } hostname = getfqdn(util.get_hostname()) if hostname: host_info["hostname"] = hostname host_info["local-hostname"] = hostname host_info["local_hostname"] = hostname default_ipv4, default_ipv6 = get_default_ip_addrs() if default_ipv4: host_info[LOCAL_IPV4] = default_ipv4 if default_ipv6: host_info[LOCAL_IPV6] = default_ipv6 by_mac = host_info["network"]["interfaces"]["by-mac"] by_ipv4 = host_info["network"]["interfaces"]["by-ipv4"] by_ipv6 = host_info["network"]["interfaces"]["by-ipv6"] ifaces = netifaces.interfaces() for dev_name in ifaces: addr_fams = netifaces.ifaddresses(dev_name) af_link = addr_fams.get(netifaces.AF_LINK) af_inet4 = addr_fams.get(netifaces.AF_INET) af_inet6 = addr_fams.get(netifaces.AF_INET6) mac = None if af_link and "addr" in af_link[0]: mac = af_link[0]["addr"] # Do not bother recording localhost if mac == "00:00:00:00:00:00": continue if mac and (af_inet4 or af_inet6): key = mac val = {} if af_inet4: af_inet4_vals = [] for ip_info in af_inet4: if not is_valid_ip_addr(ip_info["addr"]): continue af_inet4_vals.append(ip_info) val["ipv4"] = af_inet4_vals if af_inet6: af_inet6_vals = [] for ip_info in af_inet6: if not is_valid_ip_addr(ip_info["addr"]): continue af_inet6_vals.append(ip_info) val["ipv6"] = af_inet6_vals by_mac[key] = val if af_inet4: for ip_info in af_inet4: key = ip_info["addr"] if not is_valid_ip_addr(key): continue val = copy.deepcopy(ip_info) del val["addr"] if mac: val["mac"] = mac by_ipv4[key] = val if af_inet6: for ip_info in af_inet6: key = ip_info["addr"] if not is_valid_ip_addr(key): continue val = copy.deepcopy(ip_info) del val["addr"] if mac: val["mac"] = mac by_ipv6[key] = val return host_info
def get_hostname(self, fqdn=False, resolve_ip=False, metadata_only=False): """Get hostname or fqdn from the datasource. Look it up if desired. @param fqdn: Boolean, set True to return hostname with domain. @param resolve_ip: Boolean, set True to attempt to resolve an ipv4 address provided in local-hostname meta-data. @param metadata_only: Boolean, set True to avoid looking up hostname if meta-data doesn't have local-hostname present. @return: a DataSourceHostname namedtuple <hostname or qualified hostname>, <is_default> (str, bool). is_default is a bool and it's true only if hostname is localhost and was returned by util.get_hostname() as a default. This is used to differentiate with a user-defined localhost hostname. Optionally return (None, False) when metadata_only is True and local-hostname data is not available. """ defdomain = "localdomain" defhost = "localhost" domain = defdomain is_default = False if not self.metadata or not self.metadata.get("local-hostname"): if metadata_only: return DataSourceHostname(None, is_default) # 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 = [] hostname = util.get_hostname() if hostname == "localhost": # default hostname provided by socket.gethostname() is_default = True hosts_fqdn = util.get_fqdn_from_hosts(hostname) if hosts_fqdn and hosts_fqdn.find(".") > 0: toks = str(hosts_fqdn).split(".") elif hostname and hostname.find(".") > 0: toks = str(hostname).split(".") elif hostname: toks = [hostname, defdomain] else: toks = [defhost, defdomain] else: # if there is an ipv4 address in 'local-hostname', then # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx lhost = self.metadata["local-hostname"] if net.is_ipv4_address(lhost): toks = [] if resolve_ip: toks = util.gethostbyaddr(lhost) if toks: toks = str(toks).split(".") else: toks = ["ip-%s" % lhost.replace(".", "-")] else: toks = lhost.split(".") if len(toks) > 1: hostname = toks[0] domain = ".".join(toks[1:]) else: hostname = toks[0] if fqdn and domain != defdomain: hostname = "%s.%s" % (hostname, domain) return DataSourceHostname(hostname, is_default)