HostedMachineProviderImpl extends MachineProviderImpl { @Override protected void buildImage( CheServiceImpl service, String machineImageName, boolean doForcePullOnBuild, ProgressMonitor progressMonitor) throws MachineException { File workDir = null; try { BuildImageParams buildImageParams; if (service.getBuild() != null && service.getBuild().getDockerfileContent() != null) { workDir = Files.createTempDirectory(null).toFile(); final File dockerfileFile = new File(workDir, "Dockerfile"); try (FileWriter output = new FileWriter(dockerfileFile)) { output.append(service.getBuild().getDockerfileContent()); } buildImageParams = BuildImageParams.create(dockerfileFile); } else { buildImageParams = BuildImageParams.create(service.getBuild().getContext()) .withDockerfile(service.getBuild().getDockerfilePath()); } Map<String, String> buildArgs; if (service.getBuild().getArgs() == null || service.getBuild().getArgs().isEmpty()) { buildArgs = this.buildArgs; } else { buildArgs = new HashMap<>(this.buildArgs); buildArgs.putAll(service.getBuild().getArgs()); } buildImageParams .withForceRemoveIntermediateContainers(true) .withRepository(machineImageName) .withAuthConfigs(dockerCredentials.getCredentials()) .withDoForcePull(doForcePullOnBuild) .withMemoryLimit(service.getMemLimit()) .withMemorySwapLimit(-1) .withCpusetCpus(cpusetCpus) .withCpuPeriod(cpuPeriod) .withCpuQuota(cpuQuota) .withBuildArgs(buildArgs); docker.buildImage(buildImageParams, progressMonitor); } catch (IOException e) { throw new MachineException(e.getLocalizedMessage(), e); } finally { if (workDir != null) { FileCleaner.addFile(workDir); } } } @Inject  HostedMachineProviderImpl(
      DockerConnectorProvider dockerConnectorProvider,
      UserSpecificDockerRegistryCredentialsProvider dockerCredentials,
      DockerMachineFactory dockerMachineFactory,
      DockerInstanceStopDetector dockerInstanceStopDetector,
      WindowsPathEscaper windowsPathEscaper,
      RequestTransmitter requestTransmitter,
      JsonRpcEndpointToMachineNameHolder endpointIdsHolder,
      @Named("machine.docker.dev_machine.machine_servers") Set<ServerConf> devMachineServers,
      @Named("machine.docker.machine_servers") Set<ServerConf> allMachinesServers,
      @Named("machine.docker.dev_machine.machine_volumes") Set<String> devMachineSystemVolumes,
      @Named("machine.docker.machine_volumes") Set<String> allMachinesSystemVolumes,
      @Named("che.docker.always_pull_image") boolean doForcePullOnBuild,
      @Named("che.docker.privileged") boolean privilegedMode,
      SecurityOptProvider securityOptProvider,
      @Named("che.docker.pids_limit") int pidsLimit,
      @Named("machine.docker.dev_machine.machine_env") Set<String> devMachineEnvVariables,
      @Named("machine.docker.machine_env") Set<String> allMachinesEnvVariables,
      @Named("che.docker.registry_for_snapshots") boolean snapshotUseRegistry,
      @Named("che.docker.swap") double memorySwapMultiplier,
      MachineTokenRegistry tokenRegistry,
      @Named("machine.docker.networks") Set<Set<String>> additionalNetworks,
      @Nullable @Named("che.docker.network_driver") String networkDriver,
      @Nullable @Named("che.docker.parent_cgroup") String parentCgroup,
      @Nullable @Named("che.docker.cpuset_cpus") String cpusetCpus,
      @Named("che.docker.cpu_period") long cpuPeriod,
      @Named("che.docker.cpu_quota") long cpuQuota,
      @Named("che.docker.extra_hosts") Set<Set<String>> additionalHosts,
      @Nullable @Named("che.docker.dns_resolvers") String[] dnsResolvers,
      @Named("che.docker.build_args") Map<String, String> buildArgs);   }
@Test public void shouldAddMaintenanceConstraintWhenBuildImage() throws Exception { createInstanceFromRecipe(); ArgumentCaptor<BuildImageParams> argumentCaptor = ArgumentCaptor.forClass(BuildImageParams.class); verify(dockerConnector).buildImage(argumentCaptor.capture(), anyObject()); assertNotNull(argumentCaptor.getValue().getBuildArgs().get(MAINTENANCE_CONSTRAINT_KEY)); assertEquals( argumentCaptor.getValue().getBuildArgs().get(MAINTENANCE_CONSTRAINT_KEY), MAINTENANCE_CONSTRAINT_VALUE); }
@Test public void shouldAddCpuConsumptionLimitsWhenBuildImage() throws Exception { final String cpusetCpus = "2,4"; final Long cpuPeriod = 10000L; final Long cpuQuota = 7500L; provider = new HostedMachineProviderImpl( dockerConnectorProviderMock, credentialsReader, dockerMachineFactory, dockerInstanceStopDetector, windowsPathEscaper, requestTransmitter, endpointIdsHolder, emptySet(), emptySet(), emptySet(), emptySet(), false, false, securityOptProvider, -1, emptySet(), emptySet(), SNAPSHOT_USE_REGISTRY, MEMORY_SWAP_MULTIPLIER, machineTokenRegistry, emptySet(), null, null, cpusetCpus, cpuPeriod, cpuQuota, emptySet(), null, emptyMap()); createInstanceFromRecipe(); ArgumentCaptor<BuildImageParams> argumentCaptor = ArgumentCaptor.forClass(BuildImageParams.class); verify(dockerConnector).buildImage(argumentCaptor.capture(), anyObject()); assertEquals(argumentCaptor.getValue().getCpusetCpus(), cpusetCpus); assertEquals(argumentCaptor.getValue().getCpuPeriod(), cpuPeriod); assertEquals(argumentCaptor.getValue().getCpuQuota(), cpuQuota); }