SingularityMesosTaskBuilder { public SingularityTask buildTask(Protos.Offer offer, List<Resource> availableResources, SingularityTaskRequest taskRequest, Resources desiredTaskResources, Resources desiredExecutorResources) { final String sanitizedRackId = JavaUtils.getReplaceHyphensWithUnderscores(slaveAndRackHelper.getRackIdOrDefault(offer)); final String sanitizedHost = JavaUtils.getReplaceHyphensWithUnderscores(slaveAndRackHelper.getMaybeTruncatedHost(offer)); final SingularityTaskId taskId = new SingularityTaskId(taskRequest.getPendingTask().getPendingTaskId().getRequestId(), taskRequest.getDeploy().getId(), System.currentTimeMillis(), taskRequest.getPendingTask().getPendingTaskId().getInstanceNo(), sanitizedHost, sanitizedRackId); final TaskInfo.Builder bldr = TaskInfo.newBuilder() .setTaskId(TaskID.newBuilder().setValue(taskId.toString())); Optional<long[]> ports = Optional.absent(); Optional<Resource> portsResource = Optional.absent(); final Optional<SingularityContainerInfo> containerInfo = taskRequest.getDeploy().getContainerInfo(); if (desiredTaskResources.getNumPorts() > 0 || hasLiteralPortMapping(containerInfo)) { List<Long> requestedPorts = new ArrayList<>(); if (hasLiteralPortMapping(containerInfo)) { requestedPorts.addAll(containerInfo.get().getDocker().get().getLiteralHostPorts()); } portsResource = Optional.of(MesosUtils.getPortsResource(desiredTaskResources.getNumPorts(), availableResources, requestedPorts)); ports = Optional.of(MesosUtils.getPorts(portsResource.get(), desiredTaskResources.getNumPorts())); } if (containerInfo.isPresent()) { prepareContainerInfo(offer, taskId, bldr, containerInfo.get(), ports); } if (taskRequest.getDeploy().getCustomExecutorCmd().isPresent()) { prepareCustomExecutor(bldr, taskId, taskRequest, offer, ports, desiredExecutorResources); } else { prepareCommand(bldr, taskId, taskRequest, offer, ports); } if (portsResource.isPresent()) { bldr.addResources(portsResource.get()); } bldr.addResources(MesosUtils.getCpuResource(desiredTaskResources.getCpus())); bldr.addResources(MesosUtils.getMemoryResource(desiredTaskResources.getMemoryMb())); bldr.setSlaveId(offer.getSlaveId()); bldr.setName(taskRequest.getRequest().getId()); if (taskRequest.getDeploy().getLabels().isPresent() && !taskRequest.getDeploy().getLabels().get().isEmpty()) { Builder labelsBuilder = Labels.newBuilder(); for (Map.Entry<String, String> label : taskRequest.getDeploy().getLabels().get().entrySet()) { labelsBuilder.addLabels(Label.newBuilder().setKey(label.getKey()).setValue(label.getValue()).build()); } bldr.setLabels(labelsBuilder); } TaskInfo task = bldr.build(); return new SingularityTask(taskRequest, taskId, offer, task, slaveAndRackHelper.getRackId(offer)); } @Inject  SingularityMesosTaskBuilder(ObjectMapper objectMapper, SingularitySlaveAndRackHelper slaveAndRackHelper, ExecutorIdGenerator idGenerator, SingularityConfiguration configuration); SingularityTask buildTask(Protos.Offer offer, List<Resource> availableResources, SingularityTaskRequest taskRequest, Resources desiredTaskResources, Resources desiredExecutorResources);  }
@Test public void testShellCommand() { final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build(); final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1") .setCommand(Optional.of("/bin/echo hi")) .build(); final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask); final SingularityTask task = builder.buildTask(offer, null, taskRequest, taskResources, executorResources); assertEquals("/bin/echo hi", task.getMesosTask().getCommand().getValue()); assertEquals(0, task.getMesosTask().getCommand().getArgumentsCount()); assertTrue(task.getMesosTask().getCommand().getShell()); }
@Test public void testArgumentCommand() { final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build(); final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1") .setCommand(Optional.of("/bin/echo")) .setArguments(Optional.of(Collections.singletonList("wat"))) .build(); final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask); final SingularityTask task = builder.buildTask(offer, null, taskRequest, taskResources, executorResources); assertEquals("/bin/echo", task.getMesosTask().getCommand().getValue()); assertEquals(1, task.getMesosTask().getCommand().getArgumentsCount()); assertEquals("wat", task.getMesosTask().getCommand().getArguments(0)); assertFalse(task.getMesosTask().getCommand().getShell()); }
@Test public void testDockerTask() { taskResources = new Resources(1, 1, 1); final Protos.Resource portsResource = Protos.Resource.newBuilder() .setName("ports") .setType(Protos.Value.Type.RANGES) .setRanges(Protos.Value.Ranges.newBuilder() .addRange(Protos.Value.Range.newBuilder() .setBegin(31000) .setEnd(31000).build()).build()).build(); final SingularityDockerPortMapping literalMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 80, Optional.of(SingularityPortMappingType.LITERAL), 8080, Optional.<String>absent()); final SingularityDockerPortMapping offerMapping = new SingularityDockerPortMapping(Optional.<SingularityPortMappingType>absent(), 81, Optional.of(SingularityPortMappingType.FROM_OFFER), 0, Optional.of("udp")); final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build(); final SingularityContainerInfo containerInfo = new SingularityContainerInfo( SingularityContainerType.DOCKER, Optional.of(Arrays.asList( new SingularityVolume("/container", Optional.of("/host"), SingularityDockerVolumeMode.RW), new SingularityVolume("/container/${TASK_REQUEST_ID}/${TASK_DEPLOY_ID}", Optional.of("/host/${TASK_ID}"), SingularityDockerVolumeMode.RO))), Optional.of(new SingularityDockerInfo("docker-image", true, SingularityDockerNetworkType.BRIDGE, Optional.of(Arrays.asList(literalMapping, offerMapping)), Optional.of(false), Optional.<Map<String, String>>of(ImmutableMap.of("env", "var=value")) ))); final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1") .setContainerInfo(Optional.of(containerInfo)) .setCommand(Optional.of("/bin/echo")) .setArguments(Optional.of(Collections.singletonList("wat"))) .build(); final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask); final SingularityTask task = builder.buildTask(offer, Collections.singletonList(portsResource), taskRequest, taskResources, executorResources); assertEquals("/bin/echo", task.getMesosTask().getCommand().getValue()); assertEquals(1, task.getMesosTask().getCommand().getArgumentsCount()); assertEquals("wat", task.getMesosTask().getCommand().getArguments(0)); assertFalse(task.getMesosTask().getCommand().getShell()); assertEquals(Type.DOCKER, task.getMesosTask().getContainer().getType()); assertEquals("docker-image", task.getMesosTask().getContainer().getDocker().getImage()); assertTrue(task.getMesosTask().getContainer().getDocker().getPrivileged()); assertEquals("/container", task.getMesosTask().getContainer().getVolumes(0).getContainerPath()); assertEquals("/host", task.getMesosTask().getContainer().getVolumes(0).getHostPath()); assertEquals(Mode.RW, task.getMesosTask().getContainer().getVolumes(0).getMode()); Parameter envParameter = Parameter.newBuilder().setKey("env").setValue("var=value").build(); assertTrue(task.getMesosTask().getContainer().getDocker().getParametersList().contains(envParameter)); assertEquals(String.format("/container/%s/%s", task.getTaskRequest().getDeploy().getRequestId(), task.getTaskRequest().getDeploy().getId()), task.getMesosTask().getContainer().getVolumes(1).getContainerPath()); assertEquals(String.format("/host/%s", task.getMesosTask().getTaskId().getValue()), task.getMesosTask().getContainer().getVolumes(1).getHostPath()); assertEquals(Mode.RO, task.getMesosTask().getContainer().getVolumes(1).getMode()); assertEquals(80, task.getMesosTask().getContainer().getDocker().getPortMappings(0).getContainerPort()); assertEquals(8080, task.getMesosTask().getContainer().getDocker().getPortMappings(0).getHostPort()); assertEquals("tcp", task.getMesosTask().getContainer().getDocker().getPortMappings(0).getProtocol()); assertTrue(MesosUtils.getAllPorts(task.getMesosTask().getResourcesList()).contains(8080L)); assertEquals(81, task.getMesosTask().getContainer().getDocker().getPortMappings(1).getContainerPort()); assertEquals(31000, task.getMesosTask().getContainer().getDocker().getPortMappings(1).getHostPort()); assertEquals("udp", task.getMesosTask().getContainer().getDocker().getPortMappings(1).getProtocol()); assertEquals(Protos.ContainerInfo.DockerInfo.Network.BRIDGE, task.getMesosTask().getContainer().getDocker().getNetwork()); }
@Test public void testDockerMinimalNetworking() { taskResources = new Resources(1, 1, 0); final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build(); final SingularityContainerInfo containerInfo = new SingularityContainerInfo( SingularityContainerType.DOCKER, Optional.<List<SingularityVolume>>absent(), Optional.of(new SingularityDockerInfo("docker-image", true, SingularityDockerNetworkType.NONE, Optional.<List<SingularityDockerPortMapping>>absent()))); final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1") .setContainerInfo(Optional.of(containerInfo)) .build(); final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask); final SingularityTask task = builder.buildTask(offer, Collections.<Protos.Resource>emptyList(), taskRequest, taskResources, executorResources); assertEquals(Type.DOCKER, task.getMesosTask().getContainer().getType()); assertEquals(Protos.ContainerInfo.DockerInfo.Network.NONE, task.getMesosTask().getContainer().getDocker().getNetwork()); }