Search in sources :

Example 36 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class WorkspaceManager method doCreateWorkspace.

private WorkspaceImpl doCreateWorkspace(WorkspaceConfig config, Devfile devfile, Account account, Map<String, String> attributes, boolean isTemporary) throws ConflictException, ServerException {
    WorkspaceImpl workspace = WorkspaceImpl.builder().generateId().setAccount(account).setConfig(config).setDevfile(devfile).setAttributes(attributes).setTemporary(isTemporary).setStatus(STOPPED).build();
    workspace.getAttributes().put(CREATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis()));
    String targetNamespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
    if (isNullOrEmpty(targetNamespace)) {
        try {
            targetNamespace = runtimes.evalInfrastructureNamespace(buildResolutionContext(workspace));
            workspace.getAttributes().put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, targetNamespace);
        } catch (InfrastructureException e) {
            throw new ServerException(e);
        }
    }
    if (targetNamespace == null || !runtimes.isInfrastructureNamespaceValid(targetNamespace)) {
        throw new ServerException(format("The workspace would be started in a namespace/project" + " '%s', which is not a valid namespace/project name.", targetNamespace));
    }
    workspaceDao.create(workspace);
    LOG.info("Workspace '{}/{}' with id '{}' created by user '{}'", account.getName(), workspace.getName(), workspace.getId(), sessionUserNameOrUndefined());
    eventService.publish(new WorkspaceCreatedEvent(workspace));
    return workspace;
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) ServerException(org.eclipse.che.api.core.ServerException) WorkspaceCreatedEvent(org.eclipse.che.api.workspace.shared.event.WorkspaceCreatedEvent) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 37 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class WorkspaceManager method startAsync.

/**
 * Asynchronously starts given workspace.
 */
private void startAsync(WorkspaceImpl workspace, @Nullable String envName, Map<String, String> options) throws ConflictException, NotFoundException, ServerException {
    try {
        runtimes.validate(workspace, envName);
    } catch (ValidationException e) {
        throw new ConflictException(e.getMessage(), e);
    }
    // handle the situation where a workspace created by a previous Che version doesn't have a
    // namespace stored for it. We use the legacy-aware method to figure out the namespace to
    // correctly capture the workspaces which have PVCs already in a namespace defined by the legacy
    // configuration variable.
    String targetNamespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
    if (isNullOrEmpty(targetNamespace)) {
        try {
            targetNamespace = runtimes.evalInfrastructureNamespace(buildResolutionContext(workspace));
            workspace.getAttributes().put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, targetNamespace);
        } catch (InfrastructureException e) {
            throw new ServerException(e);
        }
    }
    if (!runtimes.isInfrastructureNamespaceValid(targetNamespace)) {
        try {
            targetNamespace = runtimes.evalInfrastructureNamespace(buildResolutionContext(workspace));
            if (targetNamespace == null || !runtimes.isInfrastructureNamespaceValid(targetNamespace)) {
                throw new ServerException(format("The workspace would be started in a namespace/project" + " '%s', which is not a valid namespace/project name.", targetNamespace));
            }
            workspace.getAttributes().put(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE, targetNamespace);
        } catch (InfrastructureException e) {
            throw new ServerException(e);
        }
    }
    workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis()));
    workspaceDao.update(workspace);
    runtimes.startAsync(workspace, envName, firstNonNull(options, Collections.emptyMap())).thenAccept(aVoid -> handleStartupSuccess(workspace.getId())).exceptionally(ex -> {
        if (workspace.isTemporary()) {
            removeWorkspaceQuietly(workspace.getId());
        } else {
            handleStartupError(workspace.getId(), ex.getCause());
        }
        return null;
    });
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) WorkspaceConfig(org.eclipse.che.api.core.model.workspace.WorkspaceConfig) STOPPED_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ATTRIBUTE_NAME) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) WorkspaceConfigImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl) RUNNING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) LAST_ACTIVE_INFRASTRUCTURE_NAMESPACE(org.eclipse.che.api.workspace.shared.Constants.LAST_ACTIVE_INFRASTRUCTURE_NAMESPACE) PreferenceManager(org.eclipse.che.api.user.server.PreferenceManager) WORKSPACE_GENERATE_NAME_CHARS_APPEND(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_GENERATE_NAME_CHARS_APPEND) Map(java.util.Map) Account(org.eclipse.che.account.shared.model.Account) WorkspaceDao(org.eclipse.che.api.workspace.server.spi.WorkspaceDao) NameGenerator(org.eclipse.che.commons.lang.NameGenerator) EventService(org.eclipse.che.api.core.notification.EventService) Devfile(org.eclipse.che.api.core.model.workspace.devfile.Devfile) Set(java.util.Set) CREATED_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.CREATED_ATTRIBUTE_NAME) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) String.format(java.lang.String.format) STOPPED_ABNORMALLY_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.STOPPED_ABNORMALLY_ATTRIBUTE_NAME) Nullable(org.eclipse.che.commons.annotation.Nullable) STARTING(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) TracingTags(org.eclipse.che.commons.tracing.TracingTags) WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE(org.eclipse.che.api.workspace.shared.Constants.WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE) Optional(java.util.Optional) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) STOPPED(org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED) Workspace(org.eclipse.che.api.core.model.workspace.Workspace) Instant.now(java.time.Instant.now) System.currentTimeMillis(java.lang.System.currentTimeMillis) Page(org.eclipse.che.api.core.Page) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) ValidationException(org.eclipse.che.api.core.ValidationException) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) ERROR_MESSAGE_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.ERROR_MESSAGE_ATTRIBUTE_NAME) Subject(org.eclipse.che.commons.subject.Subject) Objects.requireNonNull(java.util.Objects.requireNonNull) REMOVE_WORKSPACE_AFTER_STOP(org.eclipse.che.api.workspace.shared.Constants.REMOVE_WORKSPACE_AFTER_STOP) ConflictException(org.eclipse.che.api.core.ConflictException) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) DevfileIntegrityValidator(org.eclipse.che.api.workspace.server.devfile.validator.DevfileIntegrityValidator) Logger(org.slf4j.Logger) Constants(org.eclipse.che.api.workspace.shared.Constants) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) LAST_ACTIVITY_TIME(org.eclipse.che.api.workspace.shared.Constants.LAST_ACTIVITY_TIME) NotFoundException(org.eclipse.che.api.core.NotFoundException) FileContentProvider(org.eclipse.che.api.workspace.server.devfile.FileContentProvider) Boolean.parseBoolean(java.lang.Boolean.parseBoolean) UPDATED_ATTRIBUTE_NAME(org.eclipse.che.api.workspace.shared.Constants.UPDATED_ATTRIBUTE_NAME) ServerException(org.eclipse.che.api.core.ServerException) WorkspaceCreatedEvent(org.eclipse.che.api.workspace.shared.event.WorkspaceCreatedEvent) AccountManager(org.eclipse.che.account.api.AccountManager) Collections(java.util.Collections) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) ValidationException(org.eclipse.che.api.core.ValidationException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 38 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class WorkspaceRuntimes method stop.

void stop() {
    Set<RuntimeIdentity> identities;
    try {
        identities = infrastructure.getIdentities();
    } catch (UnsupportedOperationException e) {
        LOG.warn("Not recoverable infrastructure: '{}'", infrastructure.getName());
        return;
    } catch (InfrastructureException e) {
        LOG.error("An error occurred while attempting to get runtime identities for infrastructure '{}'. Reason: '{}'", infrastructure.getName(), e.getMessage());
        return;
    }
    LOG.info("Infrastructure is tracking {} active runtimes that need to be stopped", identities.size());
    if (identities.isEmpty()) {
        return;
    }
    for (RuntimeIdentity identity : identities) {
        try {
            String workspaceId = identity.getWorkspaceId();
            WorkspaceImpl workspace = workspaceDao.get(workspaceId);
            try (Unlocker ignored = lockService.writeLock(workspaceId)) {
                statuses.putIfAbsent(workspaceId, STARTING);
            }
            String namespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
            stopAsync(workspace, emptyMap()).whenComplete((aVoid, throwable) -> {
                LOG.info("Workspace '{}' owned by '{}' has been stopped in namespace '{}'", workspaceId, namespace);
            });
        } catch (Exception e) {
            LOG.error("An error occurred while attempting to stop runtime '{}' using infrastructure '{}'. Reason: '{}'", workspaceRuntimesId, infrastructure.getName(), e.getMessage(), e);
        }
    }
}
Also used : RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Unlocker(org.eclipse.che.commons.lang.concurrent.Unlocker) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) RuntimeStartInterruptedException(org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) ValidationException(org.eclipse.che.api.core.ValidationException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) ConflictException(org.eclipse.che.api.core.ConflictException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException)

Example 39 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class PluginFQNParser method evaluateFqn.

/**
 * Evaluates plugin FQN from provided reference by trying to fetch and parse its meta information.
 *
 * @param reference plugin reference to evaluate FQN from
 * @param fileContentProvider content provider instance to perform plugin meta requests
 * @return plugin FQN evaluated from given reference
 * @throws InfrastructureException if plugin reference is invalid or inaccessible
 */
public ExtendedPluginFQN evaluateFqn(String reference, FileContentProvider fileContentProvider) throws InfrastructureException {
    JsonNode contentNode;
    try {
        String pluginMetaContent = fileContentProvider.fetchContent(reference);
        contentNode = yamlReader.readTree(pluginMetaContent);
    } catch (DevfileException | IOException e) {
        throw new InfrastructureException(format("Plugin reference URL '%s' is invalid.", reference), e);
    }
    JsonNode publisher = contentNode.path("publisher");
    if (publisher.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "publisher"));
    }
    JsonNode name = contentNode.get("name");
    if (name.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "name"));
    }
    JsonNode version = contentNode.get("version");
    if (version.isMissingNode()) {
        throw new InfrastructureException(formatMessage(reference, "version"));
    }
    if (!version.isValueNode()) {
        throw new InfrastructureException(format("Plugin specified by reference URL '%s' has version field that cannot be parsed to string", reference));
    }
    return new ExtendedPluginFQN(reference, publisher.textValue(), name.textValue(), version.asText());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 40 with InfrastructureException

use of org.eclipse.che.api.workspace.server.spi.InfrastructureException in project che-server by eclipse-che.

the class PluginFQNParser method parsePluginFQN.

public ExtendedPluginFQN parsePluginFQN(String plugin) throws InfrastructureException {
    String url;
    String id;
    String publisher;
    String name;
    String version;
    URI registryURI = null;
    Matcher matcher = PLUGIN_PATTERN.matcher(plugin);
    if (matcher.matches()) {
        url = matcher.group("url");
        id = matcher.group("id");
        publisher = matcher.group("publisher");
        name = matcher.group("name");
        version = matcher.group("version");
    } else {
        throw new InfrastructureException(format(INCORRECT_PLUGIN_FORMAT_TEMPLATE, plugin));
    }
    if (!isNullOrEmpty(url)) {
        if (isNullOrEmpty(id)) {
            // reference only
            return new ExtendedPluginFQN(url);
        } else {
            // registry + id
            try {
                registryURI = new URI(url);
            } catch (URISyntaxException e) {
                throw new InfrastructureException(format("Plugin registry URL '%s' is invalid. Problematic plugin entry: '%s'", url, plugin));
            }
        }
    }
    return new ExtendedPluginFQN(registryURI, id, publisher, name, version);
}
Also used : Matcher(java.util.regex.Matcher) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ExtendedPluginFQN(org.eclipse.che.api.workspace.server.wsplugins.model.ExtendedPluginFQN) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)242 InternalInfrastructureException (org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException)64 Test (org.testng.annotations.Test)56 KubernetesInfrastructureException (org.eclipse.che.workspace.infrastructure.kubernetes.KubernetesInfrastructureException)44 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)42 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)38 CompletableFuture (java.util.concurrent.CompletableFuture)36 ExecutionException (java.util.concurrent.ExecutionException)36 TimeoutException (java.util.concurrent.TimeoutException)32 ServerException (org.eclipse.che.api.core.ServerException)32 Pod (io.fabric8.kubernetes.api.model.Pod)30 Map (java.util.Map)26 ValidationException (org.eclipse.che.api.core.ValidationException)22 Traced (org.eclipse.che.commons.annotation.Traced)20 Container (io.fabric8.kubernetes.api.model.Container)18 List (java.util.List)18 Set (java.util.Set)18 Inject (javax.inject.Inject)18 RuntimeStartInterruptedException (org.eclipse.che.api.workspace.server.spi.RuntimeStartInterruptedException)18 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)18