def clean_new_hostname(self): old_ip = self.cleaned_data.get('address') new_hostname = self.cleaned_data['new_hostname'] if not is_valid_hostname(new_hostname): raise forms.ValidationError("Invalid hostname") try: get_domain(new_hostname) except Domain.DoesNotExist: raise forms.ValidationError("Invalid domain") try: ipaddress = IPAddress.objects.get(hostname=new_hostname) except IPAddress.DoesNotExist: if find_addresses_for_hostname(new_hostname): raise forms.ValidationError("Hostname already in DNS.") else: if ipaddress.device and not ipaddress.device.deleted: if not old_ip: raise forms.ValidationError("Hostname in use.") device = Device.objects.get(ipaddress__address=old_ip) if ipaddress.device.id != device.id: raise forms.ValidationError( "Hostname used by %s" % device, ) elif Record.objects.filter(name=new_hostname).exists(): raise forms.ValidationError("Hostname already in DNS.") return new_hostname
def _get_address_candidates(address): try: ip_address = str(ipaddr.IPAddress(address)) except ValueError: ip_address = None try: mac = MACAddressField.normalize(address) except ValueError: mac = None if not mac: hostname = address if ip_address: candidates = IPAddress.objects.filter( address=ip_address, ) elif mac: ips = { str(ip) for ip in DHCPEntry.objects.filter(mac=mac).values_list('ip', flat=True) } candidates = IPAddress.objects.filter(address__in=ips) else: candidates = IPAddress.objects.filter( Q(hostname=hostname) | Q(address__in=find_addresses_for_hostname(hostname)) ) return candidates.filter( device__deleted=False, device__model__type__in={ DeviceType.rack_server, DeviceType.blade_server, DeviceType.virtual_server, DeviceType.unknown, } )
def _validate_hostname(hostname, mac, parsed_hostnames, row_number): mac = MACAddressField.normalize(mac) try: dev = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: if hostname_exists(hostname): raise forms.ValidationError( "Row %s: Hostname already exists." % row_number ) else: ip_addresses = list( dev.ipaddress_set.values_list('address', flat=True) ) ip_addresses_in_dns = find_addresses_for_hostname(hostname) for ip in ip_addresses_in_dns: if ip not in ip_addresses: raise forms.ValidationError( "Row %s: Using an old device %s failed. " "Exists A or PTR records in DNS which are not assigned " "to device IP addresses." % (row_number, dev) ) if Deployment.objects.filter(hostname=hostname).exists(): raise forms.ValidationError( "Row %s: Running deployment with hostname: %s already " "exists." % (row_number, hostname) ) if hostname in parsed_hostnames: raise forms.ValidationError( "Row %s: Duplicated hostname. " "Please check previous rows..." % row_number )
def clean_new_hostname(self): old_ip = self.cleaned_data.get('address') new_hostname = self.cleaned_data['new_hostname'] if not is_valid_hostname(new_hostname): raise forms.ValidationError("Invalid hostname") try: get_domain(new_hostname) except Domain.DoesNotExist: raise forms.ValidationError("Invalid domain") try: ipaddress = IPAddress.objects.get(hostname=new_hostname) except IPAddress.DoesNotExist: if find_addresses_for_hostname(new_hostname): raise forms.ValidationError("Hostname already in DNS.") else: if ipaddress.device and not ipaddress.device.deleted: if not old_ip: raise forms.ValidationError("Hostname in use.") device = Device.objects.get(ipaddress__address=old_ip) if ipaddress.device.id != device.id: raise forms.ValidationError("Hostname used by %s" % device, ) elif Record.objects.filter(name=new_hostname).exists(): raise forms.ValidationError("Hostname already in DNS.") return new_hostname
def _get_address_candidates(address): try: ip_address = str(ipaddr.IPAddress(address)) except ValueError: ip_address = None try: mac = MACAddressField.normalize(address) except ValueError: mac = None if not mac: hostname = address if ip_address: candidates = IPAddress.objects.filter(address=ip_address, ) elif mac: ips = { str(ip) for ip in DHCPEntry.objects.filter( mac=mac).values_list('ip', flat=True) } candidates = IPAddress.objects.filter(address__in=ips) else: candidates = IPAddress.objects.filter( Q(hostname=hostname) | Q(address__in=find_addresses_for_hostname(hostname))) return candidates.filter(device__deleted=False, device__model__type__in={ DeviceType.rack_server, DeviceType.blade_server, DeviceType.virtual_server, DeviceType.unknown, })
def _validate_hostname(hostname, mac, parsed_hostnames, row_number): mac = MACAddressField.normalize(mac) try: dev = Device.admin_objects.get(ethernet__mac=mac) except Device.DoesNotExist: if hostname_exists(hostname): raise forms.ValidationError("Row %s: Hostname already exists." % row_number) else: ip_addresses = list(dev.ipaddress_set.values_list('address', flat=True)) ip_addresses_in_dns = find_addresses_for_hostname(hostname) for ip in ip_addresses_in_dns: if ip not in ip_addresses: raise forms.ValidationError( "Row %s: Using an old device %s failed. " "Exists A or PTR records in DNS which are not assigned " "to device IP addresses." % (row_number, dev)) if Deployment.objects.filter(hostname=hostname).exists(): raise forms.ValidationError( "Row %s: Running deployment with hostname: %s already " "exists." % (row_number, hostname)) if hostname in parsed_hostnames: raise forms.ValidationError("Row %s: Duplicated hostname. " "Please check previous rows..." % row_number)