use of org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl in project che by eclipse.
the class MachineProviderImpl method startService.
@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
// copy to not affect/be affected by changes in origin
service = new CheServiceImpl(service);
ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
ProgressMonitor progressMonitor = currentProgressStatus -> {
try {
machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
} catch (IOException e) {
LOG.error(e.getLocalizedMessage(), e);
}
};
String container = null;
try {
String image = prepareImage(machineName, service, progressMonitor);
container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
connectContainerToAdditionalNetworks(container, service);
docker.startContainer(StartContainerParams.create(container));
readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
} catch (SourceNotFoundException e) {
throw e;
} catch (RuntimeException | ServerException | NotFoundException | IOException e) {
cleanUpContainer(container);
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl in project che by eclipse.
the class SshMachineInstanceProviderTest method shouldThrowExceptionInvalidMachineConfigSource.
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "Location in machine source is required")
public void shouldThrowExceptionInvalidMachineConfigSource() throws Exception {
MachineImpl machine = createMachine(true);
machine.getConfig().setSource(new MachineSourceImpl("ssh-config").setContent("hello"));
provider.createInstance(machine, LineConsumer.DEV_NULL);
}
use of org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl in project che by eclipse.
the class MachineSourceAdapterTest method testSerializeAndDeserialize.
/**
* Check we can transform object into JSON and JSON into object
*/
@Test
public void testSerializeAndDeserialize() {
MachineSourceAdapter machineSourceAdapter = spy(new MachineSourceAdapter());
Gson gson = new GsonBuilder().registerTypeAdapter(MachineSource.class, machineSourceAdapter).setPrettyPrinting().create();
final String TYPE = "myType";
final String LOCATION = "myLocation";
final String CONTENT = "myContent";
// serialize
MachineSource machineSource = new MachineSourceImpl(TYPE).setLocation(LOCATION).setContent(CONTENT);
String json = gson.toJson(machineSource, MachineSource.class);
assertNotNull(json);
// verify we called serializer
Mockito.verify(machineSourceAdapter).serialize(eq(machineSource), eq(MachineSource.class), any(JsonSerializationContext.class));
// now deserialize
MachineSource machineSourceDeserialize = gson.fromJson(new StringReader(json), MachineSource.class);
assertNotNull(machineSourceDeserialize);
assertEquals(machineSourceDeserialize.getLocation(), LOCATION);
assertEquals(machineSourceDeserialize.getType(), TYPE);
assertEquals(machineSourceDeserialize.getContent(), CONTENT);
// verify we called deserializer
Mockito.verify(machineSourceAdapter).deserialize(any(JsonElement.class), eq(MachineSource.class), any(JsonDeserializationContext.class));
}
use of org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl in project che by eclipse.
the class CheEnvironmentEngineTest method createMachine.
private static MachineImpl createMachine(String workspaceId, String envName, CheServiceImpl service, String serviceName, boolean isDev) {
MachineSourceImpl machineSource;
if (service.getBuild() != null && service.getBuild().getContext() != null) {
machineSource = new MachineSourceImpl("dockerfile").setLocation(service.getBuild().getContext());
} else if (service.getImage() != null) {
machineSource = new MachineSourceImpl("image").setLocation(service.getImage());
} else if (service.getBuild() != null && service.getBuild().getContext() == null && service.getBuild().getDockerfileContent() != null) {
machineSource = new MachineSourceImpl("dockerfile").setContent(service.getBuild().getDockerfileContent());
} else {
throw new IllegalArgumentException("Build context or image should contain non empty value");
}
MachineLimitsImpl limits = new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"));
return MachineImpl.builder().setConfig(MachineConfigImpl.builder().setDev(isDev).setName(serviceName).setSource(machineSource).setLimits(limits).setType("docker").build()).setId(service.getId()).setOwner("userName").setStatus(MachineStatus.RUNNING).setWorkspaceId(workspaceId).setEnvName(envName).setRuntime(new MachineRuntimeInfoImpl(emptyMap(), emptyMap(), emptyMap())).build();
}
use of org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl in project che by eclipse.
the class CheEnvironmentEngineTest method shouldBeAbleToStartEnvironmentWithRecover.
@Test
public void shouldBeAbleToStartEnvironmentWithRecover() throws Exception {
// given
SnapshotImpl snapshot = mock(SnapshotImpl.class);
MachineSourceImpl machineSource = new MachineSourceImpl("image", "registry.com/snapshot123:latest@sha256:abc1234567890", null);
when(snapshotDao.getSnapshot(anyString(), anyString(), anyString())).thenReturn(snapshot);
when(snapshot.getMachineSource()).thenReturn(machineSource);
// given
EnvironmentImpl env = createEnv();
String envName = "env-1";
String workspaceId = "wsId";
List<Instance> expectedMachines = new ArrayList<>();
when(machineProvider.startService(anyString(), eq(workspaceId), eq(envName), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenAnswer(invocationOnMock -> {
Object[] arguments = invocationOnMock.getArguments();
String machineName = (String) arguments[3];
boolean isDev = (boolean) arguments[4];
CheServiceImpl service = (CheServiceImpl) arguments[6];
Machine machine = createMachine(workspaceId, envName, service, machineName, isDev);
NoOpMachineInstance instance = spy(new NoOpMachineInstance(machine));
expectedMachines.add(instance);
return instance;
});
when(environmentParser.parse(env)).thenReturn(createCheServicesEnv());
// when
List<Instance> machines = engine.start(workspaceId, envName, env, true, messageConsumer);
// then
assertEquals(machines, expectedMachines);
ArgumentCaptor<CheServiceImpl> captor = ArgumentCaptor.forClass(CheServiceImpl.class);
verify(machineProvider).startService(anyString(), anyString(), anyString(), anyString(), eq(false), anyString(), captor.capture(), any(LineConsumer.class));
CheServiceImpl actualService = captor.getValue();
assertEquals(actualService.getImage(), "registry.com/snapshot123:latest");
}
Aggregations