def get(self, *args, **kwargs): try: data = NetworkInterface.objects.get(name=self.kwargs['iname']) serialized_data = NetworkInterfaceSerializer(data) return Response(serialized_data.data) except: return Response()
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 put(self, request, iname): try: if (not NetworkInterface.objects.filter(name=iname).exists()): e_msg = ('Interface with name: %s does not exist.' % iname) handle_exception(Exception(e_msg), request) ni = NetworkInterface.objects.get(name=iname) ipaddr = request.DATA['ipaddr'] for i in NetworkInterface.objects.filter(ipaddr=ipaddr): if (i.id != ni.id): e_msg = ('IP: %s already in use' % ipaddr) handle_exception(Exception(e_msg), request) ni.boot_proto = 'static' ni.onboot = 'yes' ni.network = request.DATA['network'] ni.netmask = request.DATA['netmask'] ni.ipaddr = ipaddr ni.save() config_network_device(ni.name, ni.mac, ni.ipaddr, ni.netmask) restart_network() return Response(NetworkInterfaceSerializer(ni).data) except RockStorAPIException: raise except Exception, e: handle_exception(e, request)
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['itype'] if (itype != 'management'): itype = 'io' boot_proto = request.data['boot_protocol'] ni.onboot = 'yes' if (boot_proto == 'dhcp'): config_network_device(ni.alias, ni.mac) elif (boot_proto == 'static'): ipaddr = request.data['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 = request.data['netmask'] gateway = request.data['gateway'] dns_servers = request.data['dns_servers'].split(',') domain = request.data['domain'] config_network_device(ni.alias, ni.mac, boot_proto='static', ipaddr=ipaddr, netmask=netmask, gateway=gateway, dns_servers=dns_servers, domain=domain) else: e_msg = ('Boot protocol must be dhcp or static. not: %s' % boot_proto) handle_exception(Exception(e_msg), request) self._restart_wrapper(ni, request) dconfig = get_net_config_fedora([ni.name])[ni.name] ni.boot_proto = dconfig['bootproto'] ni.netmask = dconfig['netmask'] ni.ipaddr = dconfig['ipaddr'] ni.itype = itype ni.gateway = dconfig['gateway'] ni.dns_servers = dconfig['dns_servers'] ni.domain = dconfig['domain'] ni.save() if (itype == 'management'): a = Appliance.objects.get(current_appliance=True) a.ip = ni.ipaddr a.save() try: update_issue(ni.ipaddr) except: logger.error('Unable to update /etc/issue') return Response(NetworkInterfaceSerializer(ni).data)
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)
def _net_scan(): default_if = get_default_interface() config_d = get_net_config_fedora(network_devices()) for dconfig in config_d.values(): ni = None if (NetworkInterface.objects.filter( name=dconfig['name']).exists()): ni = NetworkInterface.objects.get(name=dconfig['name']) ni.alias = dconfig['alias'] ni.mac = dconfig['mac'] ni.boot_proto = dconfig['bootproto'] ni.onboot = dconfig['onboot'] ni.network = dconfig['network'] ni.netmask = dconfig['netmask'] ni.ipaddr = dconfig['ipaddr'] ni.gateway = dconfig['gateway'] ni.dns_servers = dconfig['dns_servers'] ni.domain = dconfig['domain'] else: ni = NetworkInterface(name=dconfig['name'], alias=dconfig['alias'], mac=dconfig['mac'], boot_proto=dconfig['bootproto'], onboot=dconfig['onboot'], network=dconfig['network'], netmask=dconfig['netmask'], ipaddr=dconfig['ipaddr'], gateway=dconfig['gateway'], dns_servers=dconfig['dns_servers'], domain=dconfig['domain']) if (default_if == ni.name): ni.itype = 'management' try: update_issue(dconfig['ipaddr']) except: logger.error('Unable to update /etc/issue') ni.save() devices = [] for ni in NetworkInterface.objects.all(): if (ni.name 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 post(self, request): default_if = get_default_interface() config_list = get_net_config_fedora(network_devices()) for dconfig in config_list: ni = None if (NetworkInterface.objects.filter( name=dconfig['name']).exists()): ni = NetworkInterface.objects.get(name=dconfig['name']) ni.alias = dconfig['alias'] ni.mac = dconfig['mac'] ni.boot_proto = dconfig['bootproto'] ni.onboot = dconfig['onboot'] ni.network = dconfig['network'] ni.netmask = dconfig['netmask'] ni.ipaddr = dconfig['ipaddr'] ni.gateway = dconfig['gateway'] ni.dns_servers = dconfig['dns_servers'] ni.domain = dconfig['domain'] else: ni = NetworkInterface(name=dconfig['name'], alias=dconfig['alias'], mac=dconfig['mac'], boot_proto=dconfig['bootproto'], onboot=dconfig['onboot'], network=dconfig['network'], netmask=dconfig['netmask'], ipaddr=dconfig['ipaddr'], gateway=dconfig['gateway'], dns_servers=dconfig['dns_servers'], domain=dconfig['domain']) if (default_if == ni.name): ni.itype = 'management' update_samba_discovery(dconfig['ipaddr'], settings.AVAHI_SMB_CONF) try: update_issue(dconfig['ipaddr']) except: logger.error('Unable to update /etc/issue') ni.save() devices = NetworkInterface.objects.all() serializer = NetworkInterfaceSerializer(devices) return Response(serializer.data)
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__()) else: try: self._update_nginx() except Exception, e: logger.error('Failed to update Nginx. Exception: %s' % e.__str__()) return Response(NetworkInterfaceSerializer(ni).data)