protected InternalEnvironment(InternalEnvironment internalEnvironment) { this.recipe = internalEnvironment.getRecipe(); this.type = recipe != null ? recipe.getType() : null; this.machines = internalEnvironment.getMachines(); this.warnings = new CopyOnWriteArrayList<>(internalEnvironment.getWarnings()); this.attributes = internalEnvironment.getAttributes(); this.commands = internalEnvironment.getCommands(); }
@Override public DockerfileEnvironment setType(String type) { return (DockerfileEnvironment) super.setType(type); }
@Override public void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment) throws InfrastructureException { LOG.debug("Provisioning machine names for workspace '{}'", id.getWorkspaceId()); for (Entry<String, InternalMachineConfig> machineEntry : internalEnvironment.getMachines().entrySet()) { machineEntry.getValue().getEnv().put(CHE_MACHINE_NAME_ENV_VAR, machineEntry.getKey()); } } }
@VisibleForTesting InternalEnvironment createInternalEnvironment( @Nullable Environment environment, Map<String, String> workspaceConfigAttributes, List<? extends Command> commands) throws InfrastructureException, ValidationException, NotFoundException { String recipeType; if (environment == null) { recipeType = Constants.NO_ENVIRONMENT_RECIPE_TYPE; } else { recipeType = environment.getRecipe().getType(); } InternalEnvironmentFactory factory = environmentFactories.get(recipeType); if (factory == null) { throw new NotFoundException( format("InternalEnvironmentFactory is not configured for recipe type: '%s'", recipeType)); } InternalEnvironment internalEnvironment = factory.create(environment); internalEnvironment.setAttributes(new HashMap<>(workspaceConfigAttributes)); internalEnvironment.setCommands( commands.stream().map(CommandImpl::new).collect(Collectors.toList())); return internalEnvironment; }
/** Returns warnings that occurred while runtime preparing and starting. */ public List<? extends Warning> getWarnings() { return context.getEnvironment().getWarnings(); }
private OpenShiftEnvironment asOpenShiftEnv(InternalEnvironment source) throws InfrastructureException { if (source instanceof NoEnvInternalEnvironment) { return OpenShiftEnvironment.builder(source).build(); } else if (source instanceof OpenShiftEnvironment) { return (OpenShiftEnvironment) source; } else if (source instanceof KubernetesEnvironment) { return new OpenShiftEnvironment((KubernetesEnvironment) source); } else if (source instanceof DockerImageEnvironment) { KubernetesEnvironment k8sEnv = dockerImageEnvConverter.convert((DockerImageEnvironment) source); return new OpenShiftEnvironment(k8sEnv); } throw new InternalInfrastructureException( format( "Environment type '%s' is not supported. Supported environment types: %s", source.getType(), Joiner.on(",").join(OpenShiftEnvironment.TYPE, KubernetesEnvironment.TYPE))); } }
/** * Finds machine in provided environment which contains wsagent server. * * @param environment environment to find a machine with the wsagent server * @return {@link Optional} with name of the machine which contains wsagent server or empty if * such machine is not present in provided environment * @throws IllegalArgumentException if more than 1 machine with the wsagent server are found */ public static Optional<String> getWsAgentServerMachine(InternalEnvironment environment) { List<String> machinesWithWsAgentServer = environment .getMachines() .entrySet() .stream() .filter(entry -> containsWsAgentServer(entry.getValue())) .map(Map.Entry::getKey) .collect(Collectors.toList()); if (machinesWithWsAgentServer.isEmpty()) { return Optional.empty(); } if (machinesWithWsAgentServer.size() == 1) { return Optional.of(machinesWithWsAgentServer.get(0)); } throw new IllegalArgumentException( format( "Environment contains '%s' machines with wsagent server. Machines names: '%s'", machinesWithWsAgentServer.size(), machinesWithWsAgentServer)); }
context .getEnvironment() .getWarnings() .add( new WarningImpl(
@Override public void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment) throws InfrastructureException { LOG.debug("Provisioning environment variables for workspace '{}'", id.getWorkspaceId()); for (EnvVarProvider envVarProvider : envVarProviders) { Pair<String, String> envVar = envVarProvider.get(id); if (envVar != null) { internalEnvironment .getMachines() .values() .forEach(m -> m.getEnv().putIfAbsent(envVar.first, envVar.second)); } } LOG.debug("Environment variables provisioning done for workspace '{}'", id.getWorkspaceId()); } }
@Override public DockerImageEnvironment setType(String type) { return (DockerImageEnvironment) super.setType(type); }
@Override public void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment) throws InfrastructureException { LOG.debug("Provisioning project volumes for workspace '{}'", id.getWorkspaceId()); Optional<String> wsAgentServerMachine = WsAgentMachineFinderUtil.getWsAgentServerMachine(internalEnvironment); if (wsAgentServerMachine.isPresent()) { InternalMachineConfig machineConfig = internalEnvironment.getMachines().get(wsAgentServerMachine.get()); machineConfig .getVolumes() .put(PROJECTS_VOLUME_NAME, new VolumeImpl().withPath(projectFolderPath)); } } }
@Override public ComposeEnvironment setType(String type) { return (ComposeEnvironment) super.setType(type); }
/** * Parses (and fetches if needed) recipe of environment and sums RAM size of all machines in * environment in megabytes. */ public long calculate(@Nullable Environment environment) throws ServerException { if (environment == null) { return 0; } try { return getInternalEnvironment(environment) .getMachines() .values() .stream() .mapToLong( m -> parseMemoryAttributeValue(m.getAttributes().get(MEMORY_LIMIT_ATTRIBUTE))) .sum() / BYTES_TO_MEGABYTES_DIVIDER; } catch (InfrastructureException | ValidationException | NotFoundException ex) { throw new ServerException(ex.getMessage(), ex); } }
@Override public void provision(RuntimeIdentity id, InternalEnvironment internalEnvironment) throws InfrastructureException { for (InternalMachineConfig machineConfig : internalEnvironment.getMachines().values()) { LOG.debug("Start provisioning installer configs for workspace '{}'", id.getWorkspaceId()); fillEnv(machineConfig.getEnv(), machineConfig.getInstallers()); fillServers(id.getWorkspaceId(), machineConfig.getServers(), machineConfig.getInstallers()); } }
.getMachines() .values() .forEach(m -> m.getAttributes().put(CONTAINER_SOURCE_ATTRIBUTE, RECIPE_CONTAINER_SOURCE));