@Override public WorkspaceImpl create(WorkspaceImpl workspace) throws ConflictException, ServerException { requireNonNull(workspace, "Required non-null workspace"); try { doCreate(workspace); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with id '%s' or name '%s' in namespace '%s' already exists", workspace.getId(), workspace.getConfig().getName(), workspace.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } return new WorkspaceImpl(workspace); }
private void checkWorkspaceIsRunningOrStarting(WorkspaceImpl workspace) throws ConflictException { if (workspace.getStatus() != RUNNING && workspace.getStatus() != STARTING) { throw new ConflictException( format( "Could not stop the workspace '%s/%s' because its status is '%s'.", workspace.getNamespace(), workspace.getConfig().getName(), workspace.getStatus())); } }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof WorkspaceImpl)) return false; final WorkspaceImpl other = (WorkspaceImpl) obj; return Objects.equals(id, other.id) && Objects.equals(getNamespace(), other.getNamespace()) && Objects.equals(status, other.status) && isTemporary == other.isTemporary && getAttributes().equals(other.getAttributes()) && Objects.equals(config, other.config) && Objects.equals(runtime, other.runtime); }
@Transactional protected WorkspaceImpl doUpdate(WorkspaceImpl update) throws NotFoundException { EntityManager manager = managerProvider.get(); if (manager.find(WorkspaceImpl.class, update.getId()) == null) { throw new NotFoundException(format("Workspace with id '%s' doesn't exist", update.getId())); } if (update.getConfig() != null) { update.getConfig().getProjects().forEach(ProjectConfigImpl::prePersistAttributes); } WorkspaceImpl merged = manager.merge(update); manager.flush(); return merged; }
@Override public WorkspaceImpl update(WorkspaceImpl update) throws NotFoundException, ConflictException, ServerException { requireNonNull(update, "Required non-null update"); try { return new WorkspaceImpl(doUpdate(update)); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with name '%s' in namespace '%s' already exists", update.getConfig().getName(), update.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } }
private WorkspaceImpl doCreateWorkspace( WorkspaceConfig config, Account account, Map<String, String> attributes, boolean isTemporary) throws NotFoundException, ServerException, ConflictException { WorkspaceImpl workspace = WorkspaceImpl.builder() .generateId() .setConfig(config) .setAccount(account) .setAttributes(attributes) .setTemporary(isTemporary) .setStatus(STOPPED) .build(); workspace.getAttributes().put(CREATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspaceDao.create(workspace); LOG.info( "Workspace '{}/{}' with id '{}' created by user '{}'", account.getName(), workspace.getConfig().getName(), workspace.getId(), sessionUserNameOrUndefined()); eventService.publish(new WorkspaceCreatedEvent(workspace)); return workspace; }
@Override public WorkspaceImpl startWorkspace( String workspaceId, @Nullable String envName, @Nullable Map<String, String> options) throws NotFoundException, ServerException, ConflictException { WorkspaceImpl workspace = this.getWorkspace(workspaceId); String accountId = workspace.getAccount().getId(); try (@SuppressWarnings("unused") Unlocker u = resourcesLocks.lock(accountId)) { checkRuntimeResourceAvailability(accountId); checkRamResourcesAvailability( accountId, workspace.getNamespace(), workspace.getConfig(), envName); return super.startWorkspace(workspaceId, envName, options); } }
@Override public void onCascadeEvent(BeforeWorkspaceRemovedEvent event) throws Exception { signatureKeyDao.remove(event.getWorkspace().getId()); } }
private String getValidatedEnvironmentName(WorkspaceImpl workspace, @Nullable String envName) throws NotFoundException, ServerException { if (envName != null && !workspace.getConfig().getEnvironments().containsKey(envName)) { throw new NotFoundException( format( "Workspace '%s:%s' doesn't contain environment '%s'", workspace.getNamespace(), workspace.getConfig().getName(), envName)); } envName = firstNonNull(envName, workspace.getConfig().getDefaultEnv()); if (envName == null && SidecarToolingWorkspaceUtil.isSidecarBasedWorkspace( workspace.getConfig().getAttributes())) { // Sidecar-based workspaces are allowed not to have any environments return null; } // validate environment in advance if (envName == null) { throw new NotFoundException( format( "Workspace %s:%s can't use null environment", workspace.getNamespace(), workspace.getConfig().getName())); } try { runtimes.validate(workspace.getConfig().getEnvironments().get(envName)); } catch (InfrastructureException | ValidationException e) { throw new ServerException(e); } return envName; }
.get() .getByNamespace(account.getName(), true, maxItems, skipCount)) .filter(ws -> STOPPED != ws.getStatus()) .collect(Collectors.toList()); long currentlyUsedRamMB = 0; for (WorkspaceImpl activeWorkspace : activeWorkspaces) { if (WorkspaceStatus.STARTING.equals(activeWorkspace.getStatus())) { .getConfig() .getEnvironments() .get(activeWorkspace.getRuntime().getActiveEnv()); if (startingEnvironment != null) { currentlyUsedRamMB += environmentRamCalculator.calculate(startingEnvironment); currentlyUsedRamMB += environmentRamCalculator.calculate(activeWorkspace.getRuntime());
/** * Injects runtime information such as status and {@link * org.eclipse.che.api.core.model.workspace.Runtime} into the workspace object, if the workspace * doesn't have runtime sets the status to {@link WorkspaceStatus#STOPPED}. * * @param workspace the workspace to inject runtime into */ public void injectRuntime(WorkspaceImpl workspace) throws ServerException { try (Unlocker ignored = lockService.writeLock(workspace.getId())) { WorkspaceStatus workspaceStatus = statuses.get(workspace.getId()); if (workspaceStatus == null) { workspace.setStatus(STOPPED); return; } InternalRuntime<?> internalRuntime; try { internalRuntime = getInternalRuntime(workspace.getId()); } catch (ServerException | InfrastructureException e) { workspace.setStatus(STOPPED); return; } workspace.setRuntime(asRuntime(internalRuntime)); workspace.setStatus(workspaceStatus); } }
checkAccountPermissions(workspace.getNamespace(), AccountOperation.MANAGE_WORKSPACES); if (!currentSubject.hasPermission(DOMAIN_ID, workspace.getId(), action)) { throw new ForbiddenException( "The user does not have permission to " + action + " workspace with id '" + workspace.getId() + "'");
public WorkspaceImpl build() { return new WorkspaceImpl(id, account, config, runtime, attributes, isTemporary, status); }
/** * Updates an existing workspace with a new configuration. * * <p> * * <p>Replace strategy is used for workspace update, it means that existing workspace data will be * replaced with given {@code update}. * * @param update workspace update * @return updated instance of the workspace * @throws NullPointerException when either {@code workspaceId} or {@code update} is null * @throws NotFoundException when workspace with given id doesn't exist * @throws ConflictException when any conflict occurs (e.g Workspace with such name already exists * in {@code namespace}) * @throws ServerException when any other error occurs */ public WorkspaceImpl updateWorkspace(String id, Workspace update) throws ConflictException, ServerException, NotFoundException, ValidationException { requireNonNull(id, "Required non-null workspace id"); requireNonNull(update, "Required non-null workspace update"); requireNonNull(update.getConfig(), "Required non-null workspace configuration update"); validator.validateConfig(update.getConfig()); validator.validateAttributes(update.getAttributes()); WorkspaceImpl workspace = workspaceDao.get(id); workspace.setConfig(new WorkspaceConfigImpl(update.getConfig())); workspace.setAttributes(update.getAttributes()); workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.setTemporary(update.isTemporary()); return normalizeState(workspaceDao.update(workspace), true); }
@Override public String toString() { return "WorkspaceImpl{" + "id='" + id + '\'' + ", namespace='" + getNamespace() + '\'' + ", name='" + name + '\'' + ", config=" + config + ", isTemporary=" + isTemporary + ", status=" + status + ", attributes=" + attributes + ", runtime=" + runtime + '}'; }
@GET @Path("/namespace/{namespace:.*}") @Produces(APPLICATION_JSON) @ApiOperation( value = "Get workspaces by given namespace", notes = "This operation can be performed only by authorized user", response = WorkspaceDto.class, responseContainer = "List") @ApiResponses({ @ApiResponse(code = 200, message = "The workspaces successfully fetched"), @ApiResponse(code = 500, message = "Internal server error occurred during workspaces fetching") }) public List<WorkspaceDto> getByNamespace( @ApiParam("Workspace status") @QueryParam("status") String status, @ApiParam("The namespace") @PathParam("namespace") String namespace) throws ServerException, BadRequestException { return withLinks( Pages.stream( (maxItems, skipCount) -> workspaceManager.getByNamespace(namespace, false, maxItems, skipCount)) .filter(ws -> status == null || status.equalsIgnoreCase(ws.getStatus().toString())) .map(DtoConverter::asDto) .collect(toList())); }
/** * Asynchronously stops the workspace. * * @param workspaceId the id of the workspace to stop * @throws ServerException when any server error occurs * @throws NullPointerException when {@code workspaceId} is null * @throws NotFoundException when workspace {@code workspaceId} doesn't have runtime */ public void stopWorkspace(String workspaceId, Map<String, String> options) throws ServerException, NotFoundException, ConflictException { requireNonNull(workspaceId, "Required non-null workspace id"); final WorkspaceImpl workspace = normalizeState(workspaceDao.get(workspaceId), true); checkWorkspaceIsRunningOrStarting(workspace); if (!workspace.isTemporary()) { workspace.getAttributes().put(STOPPED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.getAttributes().put(STOPPED_ABNORMALLY_ATTRIBUTE_NAME, Boolean.toString(false)); workspaceDao.update(workspace); } runtimes .stopAsync(workspace, options) .whenComplete( (aVoid, throwable) -> { if (workspace.isTemporary()) { removeWorkspaceQuietly(workspace); } }); }
private void setAbnormalStopAttributes(String workspaceId, String error) { try { WorkspaceImpl workspace = workspaceDao.get(workspaceId); workspace.getAttributes().put(ERROR_MESSAGE_ATTRIBUTE_NAME, error); workspace.getAttributes().put(STOPPED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspace.getAttributes().put(STOPPED_ABNORMALLY_ATTRIBUTE_NAME, Boolean.toString(true)); workspaceDao.update(workspace); } catch (NotFoundException | ServerException | ConflictException e) { LOG.warn( format( "Cannot set error status of the workspace %s. Error status is: %s. Cause: %s", workspaceId, error, e.getMessage())); } } }