def test_computeResourceAllocation(): chutes = [] assert resource.computeResourceAllocation(chutes) == {} chutes = [MagicMock(), MagicMock()] service1 = MagicMock() service1.get_container_name.return_value = "service1" service1.requests = {} service2 = MagicMock() service2.get_container_name.return_value = "service2" service2.requests = {} chutes[0].name = "chute1" chutes[0].isRunning.return_value = True chutes[0].resources = {} chutes[0].get_services.return_value = [service1] chutes[1].name = "chute2" chutes[1].isRunning.return_value = False chutes[1].resources = {} chutes[1].get_services.return_value = [service2] allocation = resource.computeResourceAllocation(chutes) assert len(allocation) == 1 chutes[1].name = "chute2" chutes[1].isRunning.return_value = True chutes[1].resources = {} allocation = resource.computeResourceAllocation(chutes) assert len(allocation) == 2
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 get_chute(self, request, chute): """ Get information about an installed chute. **Example request**: .. sourcecode:: http GET /api/v1/chutes/hello-world **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "environment": {}, "name": "hello-world", "allocation": { "cpu_shares": 1024, "prioritize_traffic": false }, "state": "running", "version": "x1511808778", "resources": null } """ cors.config_cors(request) request.setHeader('Content-Type', 'application/json') try: chute_obj = ChuteStorage.chuteList[chute] service = chute_obj.get_default_service() container = ChuteContainer(service.get_container_name()) except KeyError: request.setResponseCode(404) return "{}" if not chute_access_allowed(request, chute_obj): return permission_denied(request) chuteStorage = ChuteStorage() allocation = resource.computeResourceAllocation( chuteStorage.getChuteList()) # TODO Return information about all of the chute's services. result = { 'name': chute, 'state': container.getStatus(), 'version': getattr(chute_obj, 'version', None), 'allocation': allocation.get(chute, None), 'environment': getattr(chute_obj, 'environment', None), 'resources': getattr(chute_obj, 'resources', None) } return json.dumps(result, cls=ChuteEncoder)
def get_chutes(self, request): """ List installed chutes. **Example request**: .. sourcecode:: http GET /api/v1/chutes/ **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json [ { "environment": {}, "name": "hello-world", "allocation": { "cpu_shares": 1024, "prioritize_traffic": false }, "state": "running", "version": "x1511808778", "resources": null } ] """ cors.config_cors(request) request.setHeader('Content-Type', 'application/json') chuteStorage = ChuteStorage() chutes = chuteStorage.getChuteList() allocation = resource.computeResourceAllocation(chutes) result = [] for chute in chutes: service = chute.get_default_service() container = ChuteContainer(service.get_container_name()) # TODO Return information about all of the chute's services. result.append({ 'name': chute.name, 'owner': chute.get_owner(), 'state': container.getStatus(), 'version': getattr(chute, 'version', None), 'allocation': allocation.get(chute.name, None), 'environment': getattr(chute, 'environment', None), 'resources': getattr(chute, 'resources', None) }) return json.dumps(result, cls=ChuteEncoder)
def get_chute(self, request, chute): """ Get information about an installed chute. **Example request**: .. sourcecode:: http GET /api/v1/chutes/hello-world **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Type: application/json { "environment": {}, "name": "hello-world", "allocation": { "cpu_shares": 1024, "prioritize_traffic": false }, "state": "running", "version": "x1511808778", "resources": null } """ cors.config_cors(request) request.setHeader('Content-Type', 'application/json') chute_obj = ChuteStorage.chuteList[chute] container = ChuteContainer(chute) chuteStorage = ChuteStorage() allocation = resource.computeResourceAllocation( chuteStorage.getChuteList()) result = { 'name': chute, 'state': container.getStatus(), 'version': getattr(chute_obj, 'version', None), 'allocation': allocation.get(chute, None), 'environment': getattr(chute_obj, 'environment', None), 'resources': getattr(chute_obj, 'resources', None) } return json.dumps(result)
def test_computeResourceAllocation(): chutes = [] assert resource.computeResourceAllocation(chutes) == {} chutes = [MagicMock(), MagicMock()] chutes[0].name = "chute1" chutes[0].isRunning.return_value = True chutes[0].resources = {} chutes[1].name = "chute2" chutes[1].isRunning.return_value = False chutes[1].resources = {} allocation = resource.computeResourceAllocation(chutes) assert len(allocation) == 1 chutes[1].name = "chute2" chutes[1].isRunning.return_value = True chutes[1].resources = {} allocation = resource.computeResourceAllocation(chutes) assert len(allocation) == 2
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__