Search in sources :

Example 1 with UPDATED_ATTRIBUTE_NAME

use of org.eclipse.che.api.workspace.shared.Constants.UPDATED_ATTRIBUTE_NAME 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 2 with UPDATED_ATTRIBUTE_NAME

use of org.eclipse.che.api.workspace.shared.Constants.UPDATED_ATTRIBUTE_NAME in project devspaces-images by redhat-developer.

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)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)2 Inject (com.google.inject.Inject)2 Boolean.parseBoolean (java.lang.Boolean.parseBoolean)2 String.format (java.lang.String.format)2 System.currentTimeMillis (java.lang.System.currentTimeMillis)2 Instant.now (java.time.Instant.now)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Singleton (javax.inject.Singleton)2 AccountManager (org.eclipse.che.account.api.AccountManager)2 Account (org.eclipse.che.account.shared.model.Account)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 Page (org.eclipse.che.api.core.Page)2 ServerException (org.eclipse.che.api.core.ServerException)2