use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project che-server by eclipse-che.
the class DefaultEditorProvisioner method apply.
/**
* Provision default editor if there is no editor. Also provisions default plugins for default
* editor regardless whether it is provisioned or set by user.
*
* @param devfile devfile where editor and plugins should be provisioned
* @param contentProvider content provider for plugin references retrieval
*/
public void apply(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
if (defaultEditorRef == null) {
// there is no default editor configured
return;
}
if ("true".equals(devfile.getAttributes().get(EDITOR_FREE_DEVFILE_ATTRIBUTE))) {
return;
}
List<ComponentImpl> components = devfile.getComponents();
Optional<ComponentImpl> editorOpt = components.stream().filter(t -> EDITOR_COMPONENT_TYPE.equals(t.getType())).findFirst();
boolean isDefaultEditorUsed;
if (!editorOpt.isPresent()) {
components.add(new ComponentImpl(EDITOR_COMPONENT_TYPE, defaultEditorRef));
isDefaultEditorUsed = true;
} else {
Component editor = editorOpt.get();
String editorPublisherAndName = getPluginPublisherAndName(editor, contentProvider);
isDefaultEditorUsed = defaultEditor.equals(editorPublisherAndName);
}
if (isDefaultEditorUsed) {
provisionDefaultPlugins(components, contentProvider);
}
if ("false".equals(devfile.getAttributes().get(PERSIST_VOLUMES_ATTRIBUTE)) && "true".equals(devfile.getAttributes().get(ASYNC_PERSIST_ATTRIBUTE))) {
provisionAsyncStoragePlugin(components, contentProvider);
}
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project che-server by eclipse-che.
the class DevfileConverter method devFileToWorkspaceConfig.
/**
* Converts given {@link Devfile} into {@link WorkspaceConfigImpl workspace config}.
*
* @param devfile initial devfile
* @param contentProvider content provider for recipe-type component or plugin references
* @return constructed workspace config
* @throws DevfileException when general devfile error occurs
* @throws DevfileException when devfile requires additional files content but the specified
* content provider does not support it
* @throws DevfileFormatException when devfile format is invalid
* @throws DevfileRecipeFormatException when content of the file specified in recipe type
* component is empty or its format is invalid
*/
public WorkspaceConfigImpl devFileToWorkspaceConfig(DevfileImpl devfile, FileContentProvider contentProvider) throws DevfileException {
checkArgument(devfile != null, "Devfile must not be null");
checkArgument(contentProvider != null, "Content provider must not be null");
// make copy to avoid modification of original devfile
devfile = new DevfileImpl(devfile);
validateCurrentVersion(devfile);
defaultEditorProvisioner.apply(devfile, contentProvider);
WorkspaceConfigImpl config = new WorkspaceConfigImpl();
config.setName(devfile.getName());
for (Command command : devfile.getCommands()) {
CommandImpl com = commandConverter.toWorkspaceCommand(command, contentProvider);
if (com != null) {
config.getCommands().add(com);
}
}
// so, commands should be already converted
for (ComponentImpl component : devfile.getComponents()) {
ComponentToWorkspaceApplier applier = componentTypeToApplier.get(component.getType());
if (applier == null) {
throw new DevfileException(String.format("Devfile contains component `%s` with type `%s` that can not be converted to workspace", getIdentifiableComponentName(component), component.getType()));
}
applier.apply(config, component, contentProvider);
}
for (ProjectImpl project : devfile.getProjects()) {
ProjectConfigImpl projectConfig = projectConverter.toWorkspaceProject(project);
config.getProjects().add(projectConfig);
}
config.getAttributes().putAll(devfile.getAttributes());
config.setDevfile(devfile);
return config;
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project che-server by eclipse-che.
the class WorkspaceManager method updateWorkspace.
/**
* 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");
checkArgument(update.getConfig() != null ^ update.getDevfile() != null, "Required non-null workspace configuration or devfile update but not both");
if (update.getConfig() != null) {
validator.validateConfig(update.getConfig());
}
WorkspaceImpl workspace = workspaceDao.get(id);
validator.validateUpdateAttributes(workspace.getAttributes(), update.getAttributes());
if (workspace.getConfig() != null) {
workspace.setConfig(new WorkspaceConfigImpl(update.getConfig()));
}
if (workspace.getDevfile() != null) {
workspace.setDevfile(new DevfileImpl(update.getDevfile()));
}
workspace.setAttributes(update.getAttributes());
workspace.getAttributes().put(UPDATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis()));
workspace.setTemporary(update.isTemporary());
return normalizeState(workspaceDao.update(workspace), true);
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project che-server by eclipse-che.
the class FileSecretApplierTest method shouldProvisionAsFilesWithPathOverride.
@Test
public void shouldProvisionAsFilesWithPathOverride() throws Exception {
Container container = new ContainerBuilder().withName("maven").build();
DevfileImpl mock_defvile = mock(DevfileImpl.class);
ComponentImpl component = new ComponentImpl();
component.setAlias("maven");
component.getVolumes().add(new VolumeImpl("test_secret", "/path/to/override"));
InternalMachineConfig internalMachineConfig = new InternalMachineConfig();
internalMachineConfig.getAttributes().put(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE, "maven");
when(environment.getMachines()).thenReturn(ImmutableMap.of("maven", internalMachineConfig));
when(environment.getDevfile()).thenReturn(mock_defvile);
when(mock_defvile.getComponents()).thenReturn(singletonList(component));
PodSpec localSpec = new PodSpecBuilder().withContainers(ImmutableList.of(container)).build();
when(podData.getSpec()).thenReturn(localSpec);
Secret secret = new SecretBuilder().withData(ImmutableMap.of("settings.xml", "random", "another.xml", "freedom")).withMetadata(new ObjectMetaBuilder().withName("test_secret").withAnnotations(ImmutableMap.of(ANNOTATION_MOUNT_AS, "file", ANNOTATION_MOUNT_PATH, "/home/user/.m2", ANNOTATION_AUTOMOUNT, "true")).withLabels(emptyMap()).build()).build();
secretApplier.applySecret(environment, runtimeIdentity, secret);
// pod has volume created
assertEquals(environment.getPodsData().get("pod1").getSpec().getVolumes().size(), 1);
Volume volume = environment.getPodsData().get("pod1").getSpec().getVolumes().get(0);
assertEquals(volume.getName(), "test_secret");
assertEquals(volume.getSecret().getSecretName(), "test_secret");
// both containers has mounts set
assertEquals(environment.getPodsData().get("pod1").getSpec().getContainers().get(0).getVolumeMounts().size(), 2);
VolumeMount mount1 = environment.getPodsData().get("pod1").getSpec().getContainers().get(0).getVolumeMounts().get(0);
assertEquals(mount1.getName(), "test_secret");
assertEquals(mount1.getMountPath(), "/path/to/override/settings.xml");
assertTrue(mount1.getReadOnly());
}
use of org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl in project che-server by eclipse-che.
the class FileSecretApplierTest method shouldNotProvisionContainersWithAutomountOverrideFalse.
@Test
public void shouldNotProvisionContainersWithAutomountOverrideFalse() throws Exception {
Container container_match1 = new ContainerBuilder().withName("maven").build();
Container container_match2 = new ContainerBuilder().withName("other").build();
DevfileImpl mock_defvile = mock(DevfileImpl.class);
ComponentImpl component = new ComponentImpl();
component.setAlias("maven");
component.setAutomountWorkspaceSecrets(false);
InternalMachineConfig internalMachineConfig = new InternalMachineConfig();
internalMachineConfig.getAttributes().put(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE, "maven");
when(environment.getMachines()).thenReturn(ImmutableMap.of("maven", internalMachineConfig));
when(environment.getDevfile()).thenReturn(mock_defvile);
when(mock_defvile.getComponents()).thenReturn(singletonList(component));
PodSpec localSpec = new PodSpecBuilder().withContainers(ImmutableList.of(container_match1, container_match2)).build();
when(podData.getSpec()).thenReturn(localSpec);
Secret secret = new SecretBuilder().withData(singletonMap("foo", "random")).withMetadata(new ObjectMetaBuilder().withName("test_secret").withAnnotations(ImmutableMap.of(ANNOTATION_MOUNT_AS, "file", ANNOTATION_MOUNT_PATH, "/home/user/.m2", ANNOTATION_AUTOMOUNT, "true")).withLabels(emptyMap()).build()).build();
secretApplier.applySecret(environment, runtimeIdentity, secret);
// only second container has mounts
assertEquals(environment.getPodsData().get("pod1").getSpec().getVolumes().size(), 1);
Volume volume = environment.getPodsData().get("pod1").getSpec().getVolumes().get(0);
assertEquals(volume.getName(), "test_secret");
assertEquals(volume.getSecret().getSecretName(), "test_secret");
assertEquals(environment.getPodsData().get("pod1").getSpec().getContainers().get(0).getVolumeMounts().size(), 0);
assertEquals(environment.getPodsData().get("pod1").getSpec().getContainers().get(1).getVolumeMounts().size(), 1);
VolumeMount mount2 = environment.getPodsData().get("pod1").getSpec().getContainers().get(1).getVolumeMounts().get(0);
assertEquals(mount2.getName(), "test_secret");
assertEquals(mount2.getMountPath(), "/home/user/.m2/foo");
assertTrue(mount2.getReadOnly());
}
Aggregations