def test_puppet_class(self): # This will break if death is ever renamed, but it's a useful test. # # We choose to test death because it is in lots of university DNS # records, so it is probably one of the more unlikely hosts to be # renamed. assert "death" in self._hostnames(hosts_by_filter("(puppetClass=ocf_www)"))
def test_type_server(self): # This will break if death is ever renamed, but it's a useful test. # # We choose to test death because it is in lots of university DNS # records, so it is probably one of the more unlikely hosts to be # renamed. assert 'death' in self._hostnames(hosts_by_filter('(type=server)'))
def test_puppet_class(self): # This will break if death is ever renamed, but it's a useful test. # # We choose to test death because it is in lots of university DNS # records, so it is probably one of the more unlikely hosts to be # renamed. assert ('death' in self._hostnames(hosts_by_filter('(puppetClass=ocf_www)')))
def _get_desktops() -> Dict[Any, Any]: """Return IPv4 and 6 address to fqdn mapping for OCF desktops from LDAP.""" desktops = {} for e in hosts_by_filter('(type=desktop)'): host = e['cn'][0] + '.ocf.berkeley.edu' v4 = ip_address(e['ipHostNumber'][0]) v6 = ipv4_to_ipv6(v4) desktops[v4] = host desktops[v6] = host return desktops
def from_ldap(cls, hostname, type='vm', children=()): host, = hosts_by_filter('(cn={})'.format(hostname)) if 'description' in host: description, = host['description'] else: description = '' return cls( hostname=hostname, type=type, description=description, children=children, )
def get_hosts() -> List[Any]: ldap_output = hosts_by_filter( '(|(type=server)(type=desktop)(type=printer))') servers: Dict[Any, Any] = dict( ldap_to_host(item) for item in ldap_output if not is_hidden(item)) hypervisors_hostnames: Dict[Any, Any] = dict( format_query_output(item) for item in query_puppet(PQL_IS_HYPERVISOR)) all_children: Dict[Any, Any] = dict( format_query_output(item) for item in query_puppet(PQL_GET_VMS)) hostnames_seen = { # These are manually added later, with the correct type 'overheat', 'tornado', } servers_to_display = [] # Add children to hypervisors for hypervisor_hostname in hypervisors_hostnames: children = [] for child_hostname in all_children.get(hypervisor_hostname, []): child = servers.get(child_hostname) if child: children.append(child._replace(type='vm')) hostnames_seen.add(child.hostname) description = servers[ hypervisor_hostname].description if hypervisor_hostname in servers else None servers_to_display.append( Host( hostname=hypervisor_hostname, type='hypervisor', description=description, children=children, ), ) hostnames_seen.add(hypervisor_hostname) # Handle special cases for host in servers.values(): if host.hostname not in hostnames_seen: servers_to_display.append(host) servers_to_display.extend([ Host( hostname='blackhole', type='network', description='Arista 7050SX Switch.', children=[], ), servers['overheat']._replace(type='raspi'), servers['tornado']._replace(type='nuc'), ]) return sorted(servers_to_display)
def from_ldap(cls: Any, hostname: str, type: str = 'vm', children: Any = ()) -> Any: host = hosts_by_filter(f'(cn={hostname})') if 'description' in host: description, = host['description'] else: description = '' return cls( hostname=hostname, type=type, description=description, children=children, )
def host(bot, msg): """Print information about an internal or external hostname.""" # TODO: also support reverse DNS lookup if given an IP # TODO: ipv6 support host = msg.match.group(1).lower().rstrip('.') # Find the IP if '.' not in host: host += '.ocf.berkeley.edu' try: ip = socket.gethostbyname(host) except socket.error as ex: msg.respond(str(ex)) return reverse_dns: Optional[str] try: reverse_dns, _, _ = socket.gethostbyaddr(ip) except socket.error: reverse_dns = None if net.is_ocf_ip(ipaddress.ip_address(ip)): ocf_host_info = 'OCF host' hosts_from_ldap = hosts.hosts_by_filter(f'(ipHostNumber={ip})') if hosts_from_ldap: ldap, = hosts_from_ldap ocf_host_info += ' ({}, puppet env: {})'.format( ldap['type'], ldap['environment'][0] if 'environment' in ldap else None, ) else: ocf_host_info += ' (not in LDAP?)' else: ocf_host_info = 'not an OCF host' msg.respond( '{host}: {ip} ({reverse_dns}) | {ocf_host_info}'.format( host=host, ip=ip, reverse_dns=reverse_dns if reverse_dns else 'no reverse dns', ocf_host_info=ocf_host_info, ), ping=False, )
def test_invalid_filters(self, filter_str): with pytest.raises(Exception): hosts_by_filter(filter_str)
def test_hosts_by_filter(self, filter_str, expected): results = self._hostnames(hosts_by_filter(filter_str)) assert set(results) == set(expected)
def test_invalid_ldap_attr(self, filter_str='(herp=derp)'): with pytest.raises(LDAPAttributeError): hosts_by_filter(filter_str)
def _get_hostname(ip): return {e['ipHostNumber'][0]: e['cn'][0] for e in hosts_by_filter('(type=desktop)')}.get(ip)