def _net_scan(cls): config_d = get_net_config(all=True) for dconfig in config_d.values(): ni = None if (NetworkInterface.objects.filter( name=dconfig['name']).exists()): ni = NetworkInterface.objects.get(name=dconfig['name']) ni = cls._update_ni_obj(ni, dconfig) else: ni = NetworkInterface(name=dconfig.get('name', None), dname=dconfig.get('dname', None), dtype=dconfig.get('dtype', None), dspeed=dconfig.get('dspeed', None), mac=dconfig.get('mac', None), method=dconfig.get('method', None), autoconnect=dconfig.get('autoconnect', None), netmask=dconfig.get('netmask', None), ipaddr=dconfig.get('ipaddr', None), gateway=dconfig.get('gateway', None), dns_servers=dconfig.get('dns_servers', None), ctype=dconfig.get('ctype', None), state=dconfig.get('state', None)) ni.save() devices = [] for ni in NetworkInterface.objects.all(): if (ni.dname not in config_d): logger.debug('network interface(%s) does not exist in the ' 'system anymore. Removing from db' % (ni.name)) ni.delete() else: devices.append(ni) serializer = NetworkInterfaceSerializer(devices, many=True) return Response(serializer.data)
def _refresh_ni(cls): config_d = get_net_config(all=True) for dconfig in config_d.values(): ni = None if (NetworkInterface.objects.filter( name=dconfig['name']).exists()): ni = NetworkInterface.objects.get(name=dconfig['name']) ni = cls._update_ni_obj(ni, dconfig) else: ni = NetworkInterface( name=dconfig.get('name', None), dname=dconfig.get('dname', None), dtype=dconfig.get('dtype', None), dspeed=dconfig.get('dspeed', None), mac=dconfig.get('mac', None), method=dconfig.get('method', None), autoconnect=dconfig.get('autoconnect', None), netmask=dconfig.get('netmask', None), ipaddr=dconfig.get('ipaddr', None), gateway=dconfig.get('gateway', None), dns_servers=dconfig.get('dns_servers', None), ctype=dconfig.get('ctype', None), state=dconfig.get('state', None)) ni.save() for ni in NetworkInterface.objects.all(): if (ni.dname not in config_d): logger.debug('network interface(%s) does not exist in the ' 'system anymore. Removing from db' % (ni.name)) ni.delete()
def put(self, request, iname): with self._handle_exception(request): if (not NetworkInterface.objects.filter(name=iname).exists()): e_msg = ('Netowrk interface(%s) does not exist.' % iname) handle_exception(Exception(e_msg), request) ni = NetworkInterface.objects.get(name=iname) itype = request.data.get('itype', 'unassigned') method = request.data.get('method') ni.onboot = 'yes' if (method == 'auto'): config_network_device(ni.name) elif (method == 'manual'): ipaddr = request.data.get('ipaddr') for i in NetworkInterface.objects.filter(ipaddr=ipaddr): if (i.id != ni.id): e_msg = ('IP: %s already in use by another ' 'interface: %s' % (ni.ipaddr, i.name)) handle_exception(Exception(e_msg), request) netmask = self._validate_netmask(request) gateway = request.data.get('gateway', None) dns_servers = request.data.get('dns_servers', None) config_network_device(ni.name, dtype=ni.dtype, method='manual', ipaddr=ipaddr, netmask=netmask, gateway=gateway, dns_servers=dns_servers) else: e_msg = ( 'Method must be auto(for dhcp) or manual(for static IP). not: %s' % method) handle_exception(Exception(e_msg), request) dconfig = get_net_config(name=ni.name)[ni.dname] ni = self._update_ni_obj(ni, dconfig) if (itype == 'management' and ni.itype != 'management'): for i in NetworkInterface.objects.filter(itype='management'): if (i.name != ni.name): e_msg = ('Another interface(%s) is already configured ' 'for management. You must disable it first ' 'before making this change.' % i.name) handle_exception(Exception(e_msg), request) a = Appliance.objects.get(current_appliance=True) a.ip = ni.ipaddr a.save() ni.itype = itype ni.save() if (ni.itype == 'management'): try: update_issue(ni.ipaddr) except Exception, e: logger.error('Unable to update /etc/issue. Exception: %s' % e.__str__()) try: self._update_nginx(ni.ipaddr) except Exception, e: logger.error('Failed to update Nginx. Exception: %s' % e.__str__())
def put(self, request, iname): with self._handle_exception(request): if (not NetworkInterface.objects.filter(name=iname).exists()): e_msg = ('Netowrk interface(%s) does not exist.' % iname) handle_exception(Exception(e_msg), request) ni = NetworkInterface.objects.get(name=iname) itype = request.data.get('itype', 'unassigned') method = request.data.get('method') ni.onboot = 'yes' if (method == 'auto'): config_network_device(ni.name) elif (method == 'manual'): ipaddr = request.data.get('ipaddr') for i in NetworkInterface.objects.filter(ipaddr=ipaddr): if (i.id != ni.id): e_msg = ('IP: %s already in use by another ' 'interface: %s' % (ni.ipaddr, i.name)) handle_exception(Exception(e_msg), request) netmask = self._validate_netmask(request) gateway = request.data.get('gateway', None) dns_servers = request.data.get('dns_servers', None) config_network_device(ni.name, dtype=ni.dtype, method='manual', ipaddr=ipaddr, netmask=netmask, gateway=gateway, dns_servers=dns_servers) else: e_msg = ('Method must be auto(for dhcp) or manual(for static IP). not: %s' % method) handle_exception(Exception(e_msg), request) dconfig = get_net_config(name=ni.name)[ni.dname] ni = self._update_ni_obj(ni, dconfig) if (itype == 'management' and ni.itype != 'management'): for i in NetworkInterface.objects.filter(itype='management'): if (i.name != ni.name): e_msg = ('Another interface(%s) is already configured ' 'for management. You must disable it first ' 'before making this change.' % i.name) handle_exception(Exception(e_msg), request) a = Appliance.objects.get(current_appliance=True) a.ip = ni.ipaddr a.save() ni.itype = itype ni.save() if (ni.itype == 'management'): try: update_issue(ni.ipaddr) self._update_nginx(ni.ipaddr) except Exception, e: logger.error('Unable to update /etc/issue. Exception: %s' % e.__str__()) try: self._update_nginx(ni.ipaddr) except Exception, e: logger.error('Failed to update Nginx. Exception: %s' % e.__str__())
def post(self, request): for d in network_devices(): if (NetworkInterface.objects.filter(name=d).exists()): continue dconfig = get_net_config(d) new_device = NetworkInterface(name=d, mac=dconfig['mac'], boot_proto=dconfig['bootproto'], onboot=dconfig['onboot'], network=dconfig['network'], netmask=dconfig['netmask'], ipaddr=dconfig['ipaddr']) new_device.save() devices = NetworkInterface.objects.all() serializer = NetworkInterfaceSerializer(devices) return Response(serializer.data)