def test_get_firstfreeip(self): ip = get_first_free_ip('net2') self.assertEqual(ip, '127.0.0.10') # first ten addresses are reserved Record.objects.create( domain=self.domain_temp1, name='host123.temp1', content='127.0.1.3', type='A' ) DHCPEntry.objects.create( mac='aa:43:c2:11:22:33', ip='127.0.1.5' ) ip = get_first_free_ip('net1', ['127.0.1.1']) # 127.0.1.1 - reserved # 127.0.1.2 - deployment # 127.0.1.3 - dns # 127.0.1.4 - discovery # 127.0.1.5 - dhcp # 127.0.1.6 - should be free self.assertEqual(ip, '127.0.1.6') ip = get_first_free_ip('net3') self.assertEqual(ip, None) # bad margins...
def get_context_data(self, **kwargs): if not self.device.verified: messages.error( self.request, "{} - is not verified, you cannot " "deploy this device".format(self.device), ) # find next available hostname and first free IP address for all # networks in which deployed machine is... next_hostname = None first_free_ip_addresses = [] rack = self.device.find_rack() if rack: networks = rack.network_set.filter( environment__isnull=False, ).order_by('name') for network in networks: next_hostname = get_next_free_hostname(network.environment) if next_hostname: break for network in networks: first_free_ip = get_first_free_ip(network.name) if first_free_ip: first_free_ip_addresses.append({ 'network_name': network.name, 'first_free_ip': first_free_ip, }) return { 'form': kwargs['form'], 'device': self.device, 'next_hostname': next_hostname, 'first_free_ip_addresses': first_free_ip_addresses, }
def get_context_data(self, **kwargs): ret = super(NetworksInfo, self).get_context_data(**kwargs) next_free_ip = get_first_free_ip(self.network.name) ret['next_free_ip'] = next_free_ip ret['editable'] = True for error in ret['form'].non_field_errors(): messages.error(self.request, error) return ret
def get_context_data(self, **kwargs): ret = super(Addresses, self).get_context_data(**kwargs) if self.dns_formset is None: dns_records = self.get_dns(self.limit_types) ips = {ip.address for ip in self.object.ipaddress_set.all()} self.dns_formset = DNSFormSet( hostnames=self.get_hostnames(), queryset=dns_records, prefix='dns', limit_types=self.limit_types, ips=ips, ) if self.dhcp_formset is None: dhcp_records = self.get_dhcp() macs = {e.mac for e in self.object.ethernet_set.all()} ips = {ip.address for ip in self.object.ipaddress_set.all()} self.dhcp_formset = DHCPFormSet( dhcp_records, macs, ips, prefix='dhcp', ) if self.ip_formset is None: self.ip_formset = IPAddressFormSet( queryset=self.object.ipaddress_set.order_by('address'), prefix='ip' ) profile = self.request.user.get_profile() can_edit = profile.has_perm(self.edit_perm, self.object.venture) next_hostname = None first_free_ip_addresses = [] rack = self.object.find_rack() if rack: networks = rack.network_set.order_by('name') for network in networks: next_hostname = get_next_free_hostname(network.data_center) if next_hostname: break for network in networks: first_free_ip = get_first_free_ip(network.name) if first_free_ip: first_free_ip_addresses.append({ 'network_name': network.name, 'first_free_ip': first_free_ip, }) balancers = list(_get_balancers(self.object)) ret.update({ 'canedit': can_edit, 'balancers': balancers, 'dnsformset': self.dns_formset, 'dhcpformset': self.dhcp_formset, 'ipformset': self.ip_formset, 'next_hostname': next_hostname, 'first_free_ip_addresses': first_free_ip_addresses, }) return ret
def handle(self, *args, **options): if not args: raise CommandError('Please specify the network name.') try: ip = get_first_free_ip(args[0]) except Network.DoesNotExist: raise CommandError("Specified network doesn't exists.") if not ip: raise CommandError("Couldn't determine the first free IP.") self.stdout.write("First free IP: %s\n" % ip)
def post(self, request, *args, **kwargs): from ralph.urls import LATEST_API actor = User.objects.get( username=ApiKeyAuthentication().get_identifier(request) ) if not actor.has_perm('create_devices'): raise HttpResponse(_('You cannot create new devices'), status=401) data = Serializer().deserialize( request.body, format=request.META.get('CONTENT_TYPE', 'application/json') ) mac = MACAddressField.normalize(data['mac']) parent = self.get_parent_device(data) ip_addr = get_first_free_ip(data['network']) hostname = self.get_hostname(parent) venture = get_object_or_404( Venture, symbol=data['venture'] ) role = get_object_or_404( VentureRole, venture=venture, name=data['venture-role'] ) ethernets = [Eth( 'DEPLOYMENT MAC', mac, EthernetSpeed.unknown, )] device = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) device.name = hostname device.parent = parent device.venture = venture device.venture_role = role device.save() IPAddress.objects.create( address=ip_addr, device=device, hostname=hostname, ) reset_dns(hostname, ip_addr) reset_dhcp(ip_addr, data['mac']) resp = HttpResponse('', status=201) resp['Location'] = LATEST_API.canonical_resource_for( 'dev' ).get_resource_uri(device) return resp
def step3_initial(self): ips = set() names = set() for f in self.formset: network = Network.objects.get(id=f.cleaned_data['network']) ip = get_first_free_ip(network.name, ips) ips.add(ip) name = get_next_free_hostname(network.data_center, names) names.add(name) yield { 'address': f.cleaned_data['address'], 'new_ip': ip, 'new_hostname': name, }
def test_get_firstfreeip(self): ip = get_first_free_ip('net2') self.assertEqual(ip, '127.0.0.10') # first ten addresses are reserved Record.objects.create(domain=self.domain_temp1, name='host123.temp1', content='127.0.1.3', type='A') DHCPEntry.objects.create(mac='aa:43:c2:11:22:33', ip='127.0.1.5') Record.objects.create(domain=self.domain_temp1, name='6.1.0.127.in-addr.arpa', content='host321.temp1', type='PTR') ip = get_first_free_ip('net1', ['127.0.1.1']) # 127.0.1.1 - reserved # 127.0.1.2 - deployment # 127.0.1.3 - dns (A) # 127.0.1.4 - discovery # 127.0.1.5 - dhcp # 127.0.1.6 - dns (PTR) # 127.0.1.7 - should be free self.assertEqual(ip, '127.0.1.7') ip = get_first_free_ip('net3') self.assertEqual(ip, None) # bad margins...
def post(self, request, *args, **kwargs): from ralph.urls import LATEST_API actor = User.objects.get( username=ApiKeyAuthentication().get_identifier(request)) if not actor.has_perm('create_devices'): raise HttpResponse(_('You cannot create new devices'), status=401) data = Serializer().deserialize(request.body, format=request.META.get( 'CONTENT_TYPE', 'application/json')) mac = MACAddressField.normalize(data['mac']) parent = self.get_parent_device(data) ip_addr = get_first_free_ip(data['network']) hostname = self.get_hostname(parent) venture = get_object_or_404(Venture, symbol=data['venture']) role = get_object_or_404(VentureRole, venture=venture, name=data['venture-role']) ethernets = [Eth( 'DEPLOYMENT MAC', mac, EthernetSpeed.unknown, )] device = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) device.name = hostname device.parent = parent device.venture = venture device.venture_role = role device.save() IPAddress.objects.create( address=ip_addr, device=device, hostname=hostname, ) reset_dns(hostname, ip_addr) reset_dhcp(ip_addr, data['mac']) resp = HttpResponse('', status=201) resp['Location'] = LATEST_API.canonical_resource_for( 'dev').get_resource_uri(device) return resp
def _find_network_ip(network_name, reserved_ip_addresses, device=None): """Find the network and IP address to be used for deployment. If ``device`` is specified, re-use an IP of that device that matches the specified network. If no network is specified, any IP matches. If no suitable network is found, ``Network.DoesNotExist`` is raised. If no suitable IP address is found, "" is returned for the IP. Never uses an IP that is in ``reserved_ip_addresses``. When an IP is found, it is added to ``reserved_ip_addresses``. """ if network_name: network = Network.objects.get(name=network_name) if device: for ipaddress in device.ipaddress_set.filter( is_management=False, network=network, ).order_by('hostname', 'address'): ip = ipaddress.address if ip in reserved_ip_addresses: continue reserved_ip_addresses.append(ip) return network, ip ip = get_first_free_ip(network.name, reserved_ip_addresses) or '' if ip: reserved_ip_addresses.append(ip) return network, ip if device: for ipaddress in device.ipaddress_set.filter( is_management=False, ).order_by('hostname', 'address'): ip = ipaddress.address if ip in reserved_ip_addresses: continue try: network = Network.from_ip(ip) except IndexError: continue reserved_ip_addresses.append(ip) return network, ip raise Network.DoesNotExist("No default network for this device")
def post(self, request, *args, **kwargs): from ralph.urls import LATEST_API try: actor = User.objects.get( username=ApiKeyAuthentication().get_identifier(request) ) except User.DoesNotExist: actor = None if not (actor and actor.profile.has_perm(Perm.has_core_access)): return HttpResponse( unicode(_('You cannot create new devices')), status=401 ) data = Serializer().deserialize( request.body, format=request.META.get('CONTENT_TYPE', 'application/json') ) missing_keys = { 'mac', 'network', 'management-ip', 'venture', 'venture-role' } - set(data.keys()) if missing_keys: return HttpResponse( _('Missing data: {}').format(', '.join(missing_keys)), status=400, ) mac = MACAddressField.normalize(data['mac']) existing_mac = DHCPEntry.objects.filter(mac=mac).count() if existing_mac: return HttpResponse(unicode(_('MAC already exists')), status=400) parent = self.get_parent_device(data) try: ip_addr = get_first_free_ip(data['network']) except Network.DoesNotExist: raise Http404('No network with such name exists') hostname = self.get_hostname(parent) venture = get_object_or_404( Venture, symbol=data['venture'] ) role_name = data.get('venture-role') role = role_name and get_object_or_404( VentureRole, venture=venture, name=role_name, ) ethernets = [Eth( 'DEPLOYMENT MAC', mac, EthernetSpeed.unknown, )] device = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) device.name = hostname device.parent = parent device.venture = venture device.venture_role = role device.rack = parent.rack device.dc = parent.dc device.save() IPAddress.objects.create( address=ip_addr, device=device, hostname=hostname, ) reset_dns(hostname, ip_addr) reset_dhcp(ip_addr, data['mac']) resp = HttpResponse('', status=201) resp['Location'] = LATEST_API.canonical_resource_for( 'dev' ).get_resource_uri(device) return resp
def post(self, request, *args, **kwargs): from ralph.urls import LATEST_API try: actor = User.objects.get( username=ApiKeyAuthentication().get_identifier(request)) except User.DoesNotExist: actor = None if not (actor and actor.has_perm('create_devices')): return HttpResponse(unicode(_('You cannot create new devices')), status=401) data = Serializer().deserialize(request.body, format=request.META.get( 'CONTENT_TYPE', 'application/json')) missing_keys = { 'mac', 'network', 'management-ip', 'venture', 'venture-role' } - set(data.keys()) if missing_keys: return HttpResponse( _('Missing data: {}').format(', '.join(missing_keys)), status=400, ) mac = MACAddressField.normalize(data['mac']) existing_mac = DHCPEntry.objects.filter(mac=mac).count() if existing_mac: return HttpResponse(unicode(_('MAC already exists')), status=400) parent = self.get_parent_device(data) try: ip_addr = get_first_free_ip(data['network']) except Network.DoesNotExist: raise Http404('No network with such name exists') hostname = self.get_hostname(parent) venture = get_object_or_404(Venture, symbol=data['venture']) role_name = data.get('venture-role') role = role_name and get_object_or_404( VentureRole, venture=venture, name=role_name, ) ethernets = [Eth( 'DEPLOYMENT MAC', mac, EthernetSpeed.unknown, )] device = Device.create( ethernets=ethernets, model_type=DeviceType.unknown, model_name='Unknown', verified=True, ) device.name = hostname device.parent = parent device.venture = venture device.venture_role = role device.rack = parent.rack device.dc = parent.dc device.save() IPAddress.objects.create( address=ip_addr, device=device, hostname=hostname, ) reset_dns(hostname, ip_addr) reset_dhcp(ip_addr, data['mac']) resp = HttpResponse('', status=201) resp['Location'] = LATEST_API.canonical_resource_for( 'dev').get_resource_uri(device) return resp