def get_hostconfig(self, request): """ Get the device's current host configuration. **Example request**: .. sourcecode:: http GET /api/v1/config/hostconfig **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "firewall": { "defaults": { "forward": "ACCEPT", "input": "ACCEPT", "output": "ACCEPT" } }, ... } For a complete example, please see the Host Configuration section. """ cors.config_cors(request) config = hostconfig.prepareHostConfig() request.setHeader('Content-Type', 'application/json') return json.dumps(config, separators=(',',':'))
def get_hostconfig(self, request): """ Get the device's current host configuration. **Example request**: .. sourcecode:: http GET /api/v1/config/hostconfig **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "firewall": { "defaults": { "forward": "ACCEPT", "input": "ACCEPT", "output": "ACCEPT" } }, ... } For a complete example, please see the Host Configuration section. """ cors.config_cors(request) config = hostconfig.prepareHostConfig() request.setHeader('Content-Type', 'application/json') return json.dumps(config, separators=(',', ':'))
def new_config(self, request): """ Generate a new node configuration based on the hardware. **Example request**: .. sourcecode:: http GET /api/v1/config/new_config **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "firewall": { "defaults": { "forward": "ACCEPT", "input": "ACCEPT", "output": "ACCEPT" } }, ... } For a complete example, please see the Host Configuration section. """ cors.config_cors(request) config = hostconfig.prepareHostConfig(hostConfigPath='/dev/null') request.setHeader('Content-Type', 'application/json') return json.dumps(config, separators=(',',':'))
def getSubnetReservations(exclude=None): """ Get current set of subnet reservations. Returns an instance of SubnetReservationSet. exclude: name of chute whose reservations should be excluded """ reservations = SubnetReservationSet() if exclude != settings.RESERVED_CHUTE: hostConfig = prepareHostConfig() ipaddr = datastruct.getValue(hostConfig, 'lan.ipaddr', None) netmask = datastruct.getValue(hostConfig, 'lan.netmask', None) if ipaddr is not None and netmask is not None: network = ipaddress.ip_network(u'{}/{}'.format(ipaddr, netmask), strict=False) reservations.add(network) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): if 'subnet' not in iface: continue subnet = iface['subnet'] reservations.add(subnet) return reservations
def getInterfaceReservations(exclude=None): """ Get current set of interface reservations. Returns an instance of InterfaceReservationSet. exclude: name of chute whose interfaces should be excluded """ reservations = InterfaceReservationSet() if exclude != settings.RESERVED_CHUTE: hostConfig = prepareHostConfig() wifiInterfaces = hostConfig.get('wifi-interfaces', []) for iface in wifiInterfaces: if 'ifname' not in iface: continue ifname = iface['ifname'] reservations.add(ifname) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): if 'externalIntf' not in iface: continue ifname = iface['externalIntf'] reservations.add(ifname) return reservations
def getSubnetReservations(exclude=None): """ Get current set of subnet reservations. Returns an instance of SubnetReservationSet. exclude: name of chute whose reservations should be excluded """ reservations = SubnetReservationSet() if exclude != constants.RESERVED_CHUTE_NAME: hostConfig = prepareHostConfig() ipaddr = datastruct.getValue(hostConfig, 'lan.ipaddr', None) netmask = datastruct.getValue(hostConfig, 'lan.netmask', None) if ipaddr is not None and netmask is not None: network = ipaddress.ip_network(u'{}/{}'.format(ipaddr, netmask), strict=False) reservations.add(network) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): if 'subnet' not in iface: continue subnet = iface['subnet'] reservations.add(subnet) return reservations
def getInterfaceReservations(exclude=None): """ Get current set of interface reservations. Returns an instance of InterfaceReservationSet. exclude: name of chute whose interfaces should be excluded """ reservations = InterfaceReservationSet() if exclude != constants.RESERVED_CHUTE_NAME: hostConfig = prepareHostConfig() wifiInterfaces = hostConfig.get('wifi-interfaces', []) for iface in wifiInterfaces: if 'ifname' not in iface: continue ifname = iface['ifname'] reservations.add(ifname) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): if 'externalIntf' not in iface: continue ifname = iface['externalIntf'] reservations.add(ifname) return reservations
def prepare(self): report = StateReport() report.name = nexus.core.info.pdid report.osVersion = system_info.getOSVersion() # We can get the paradrop version from the installed python package. report.paradropVersion = system_info.getPackageVersion('paradrop') # TODO: Get pdinstall version - we will have to work with snappy or # devise some other mechanism, since it installs as a completely # separate snap. report.chutes = [] chuteStore = ChuteStorage() chutes = chuteStore.getChuteList() allocation = resource.computeResourceAllocation(chutes) for chute in chutes: container = ChuteContainer(chute.name) report.chutes.append({ 'name': chute.name, 'desired': chute.state, 'state': container.getStatus(), 'warning': chute.warning, 'version': getattr(chute, 'version', None), 'allocation': allocation.get(chute.name, None), 'environment': getattr(chute, 'environment', None), 'external': getattr(chute, 'external', None), 'resources': getattr(chute, 'resources', None) }) report.devices = devices.listSystemDevices() report.hostConfig = hostconfig.prepareHostConfig(write=False) client = SnapdClient() report.snaps = client.listSnaps() report.zerotierAddress = zerotier.getAddress() report.dmi = system_info.getDMI() # Add CPU, memory, disk, and network interface information. This gives # the controller useful debugging information such as high memory or # disk utilization and IP addresses. status_source = SystemStatus() report.status = status_source.getStatus(max_age=None) return report.__dict__
def getDeviceReservations(exclude=None): """ Produce a dictionary mapping device names to DeviceReservations objects that describe the current usage of the device. The returned type is a defaultdict, so there is no need to check if a key exists before accessing it. exclude: name of chute whose device reservations should be excluded """ reservations = collections.defaultdict(DeviceReservations) if exclude != settings.RESERVED_CHUTE: hostConfig = prepareHostConfig() wifiInterfaces = hostConfig.get('wifi-interfaces', []) for iface in wifiInterfaces: if 'device' not in iface: continue dev = iface['device'] phy = getWirelessPhyName(dev) if phy is not None: # It is annoying to do this conversion everywhere, but it would # be painful to break compatibility with all of the devices out # there that use e.g. wlan0 instead of phy0 in their hostconfig. dev = phy reservations[dev].add(settings.RESERVED_CHUTE, 'wifi', iface.get('mode', 'ap')) lanInterfaces = datastruct.getValue(hostConfig, 'lan.interfaces', []) for iface in lanInterfaces: reservations[iface].add(settings.RESERVED_CHUTE, 'lan', None) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): dev = iface.get('device', None) # Device is not set in cases such as vlan interfaces. if dev is None: continue reservations[dev].add(chute.name, iface['netType'], iface.get('mode', None)) return reservations
def getDeviceReservations(exclude=None): """ Produce a dictionary mapping device names to DeviceReservations objects that describe the current usage of the device. The returned type is a defaultdict, so there is no need to check if a key exists before accessing it. exclude: name of chute whose device reservations should be excluded """ reservations = collections.defaultdict(DeviceReservations) if exclude != constants.RESERVED_CHUTE_NAME: hostConfig = prepareHostConfig() wifiInterfaces = hostConfig.get('wifi-interfaces', []) for iface in wifiInterfaces: if 'device' not in iface: continue dev = iface['device'] phy = getWirelessPhyName(dev) if phy is not None: # It is annoying to do this conversion everywhere, but it would # be painful to break compatibility with all of the devices out # there that use e.g. wlan0 instead of phy0 in their hostconfig. dev = phy reservations[dev].add(constants.RESERVED_CHUTE_NAME, 'wifi', iface.get('mode', 'ap')) lanInterfaces = datastruct.getValue(hostConfig, 'lan.interfaces', []) for iface in lanInterfaces: reservations[iface].add(constants.RESERVED_CHUTE_NAME, 'lan', None) for chute in ChuteStorage.chuteList.values(): if chute.name == exclude: continue for iface in chute.getCache('networkInterfaces'): dev = iface.get('device', None) # Device is not set in cases such as vlan interfaces. if dev is None: continue reservations[dev].add(chute.name, iface['type'], iface.get('mode', None)) return reservations
def test_prepareHostConfig(settings, detectSystemDevices): """ Test paradrop.core.config.hostconfig.prepareHostConfig """ from paradrop.core.config.hostconfig import prepareHostConfig devices = {'wan': [{'name': 'eth0'}], 'lan': list(), 'wifi': list()} detectSystemDevices.return_value = devices source = tempfile.NamedTemporaryFile(delete=True) source.write("{test: value}") source.flush() settings.HOST_CONFIG_FILE = source.name settings.DEFAULT_LAN_ADDRESS = "1.2.3.4" settings.DEFAULT_LAN_NETWORK = "1.0.0.0/24" config = prepareHostConfig() assert config['test'] == 'value'
def test_prepareHostConfig(settings, detectSystemDevices): """ Test paradrop.core.config.hostconfig.prepareHostConfig """ from paradrop.core.config.hostconfig import prepareHostConfig devices = { 'wan': [{'name': 'eth0'}], 'lan': list(), 'wifi': list() } detectSystemDevices.return_value = devices source = tempfile.NamedTemporaryFile(delete=True) source.write("{test: value}") source.flush() settings.HOST_CONFIG_FILE = source.name settings.DEFAULT_LAN_ADDRESS = "1.2.3.4" settings.DEFAULT_LAN_NETWORK = "1.0.0.0/24" config = prepareHostConfig() assert config['test'] == 'value'
def prepare(self): report = StateReport() report.name = nexus.core.info.pdid report.osVersion = system_info.getOSVersion() # We can get the paradrop version from the installed python package. report.paradropVersion = system_info.getPackageVersion('paradrop') report.chutes = [] chuteStore = ChuteStorage() chutes = chuteStore.getChuteList() allocation = resource.computeResourceAllocation(chutes) for chute in chutes: service_info = {} for service in chute.get_services(): container_name = service.get_container_name() container = ChuteContainer(container_name) service_info[service.name] = { 'allocation': allocation.get(container_name, None), 'state': container.getStatus() } # Use the default service (e.g. "main") to report the chute's # current state. service = chute.get_default_service() container = ChuteContainer(service.get_container_name()) report.chutes.append({ 'name': chute.name, 'desired': chute.state, 'state': container.getStatus(), 'services': service_info, 'warning': None, 'version': getattr(chute, 'version', None), 'allocation': None, # deprecated 'environment': getattr(chute, 'environment', None), 'external': getattr(chute, 'external', None), 'resources': getattr(chute, 'resources', None) }) report.devices = devices.listSystemDevices() report.hostConfig = hostconfig.prepareHostConfig(write=False) if GovernorClient.isAvailable(): client = GovernorClient() report.snaps = client.listSnaps() report.zerotierAddress = zerotier.getAddress() report.dmi = system_info.getDMI() # Add CPU, memory, disk, and network interface information. This gives # the controller useful debugging information such as high memory or # disk utilization and IP addresses. status_source = SystemStatus() report.status = status_source.getStatus(max_age=None) return report.__dict__
def prepare(self): report = StateReport() report.name = nexus.core.info.pdid report.osVersion = system_info.getOSVersion() # We can get the paradrop version from the installed python package. report.paradropVersion = system_info.getPackageVersion('paradrop') report.chutes = [] chuteStore = ChuteStorage() chutes = chuteStore.getChuteList() allocation = resource.computeResourceAllocation(chutes) for chute in chutes: service_info = {} for service in chute.get_services(): container_name = service.get_container_name() container = ChuteContainer(container_name) service_info[service.name] = { 'allocation': allocation.get(container_name, None), 'state': container.getStatus() } # Use the default service (e.g. "main") to report the chute's # current state. service = chute.get_default_service() container = ChuteContainer(service.get_container_name()) report.chutes.append({ 'name': chute.name, 'desired': chute.state, 'state': container.getStatus(), 'services': service_info, 'warning': None, 'version': getattr(chute, 'version', None), 'allocation': None, # deprecated 'environment': getattr(chute, 'environment', None), 'external': getattr(chute, 'external', None), 'resources': getattr(chute, 'resources', None) }) report.devices = devices.listSystemDevices() report.hostConfig = hostconfig.prepareHostConfig(write=False) if GovernorClient.isAvailable(): client = GovernorClient() report.snaps = client.listSnaps() report.zerotierAddress = zerotier.getAddress() report.dmi = system_info.getDMI() # Add CPU, memory, disk, and network interface information. This gives # the controller useful debugging information such as high memory or # disk utilization and IP addresses. status_source = SystemStatus() report.status = status_source.getStatus(max_age=None) return report.__dict__