use of org.eclipse.che.api.machine.server.model.impl.MachineImpl 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.MachineImpl 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.MachineImpl in project che by eclipse.
the class WorkspaceManagerTest method mockRuntime.
private WorkspaceRuntimeImpl mockRuntime(WorkspaceImpl workspace, WorkspaceStatus status) {
when(runtimes.getStatus(workspace.getId())).thenReturn(status);
MachineImpl machine1 = spy(createMachine(workspace.getId(), workspace.getConfig().getDefaultEnv(), true));
MachineImpl machine2 = spy(createMachine(workspace.getId(), workspace.getConfig().getDefaultEnv(), false));
Map<String, MachineImpl> machines = new HashMap<>();
machines.put(machine1.getId(), machine1);
machines.put(machine2.getId(), machine2);
WorkspaceRuntimeImpl runtime = new WorkspaceRuntimeImpl(workspace.getConfig().getDefaultEnv(), machines.values());
doAnswer(inv -> {
workspace.setStatus(status);
workspace.setRuntime(runtime);
return null;
}).when(runtimes).injectRuntime(workspace);
when(runtimes.isAnyRunning()).thenReturn(true);
return runtime;
}
use of org.eclipse.che.api.machine.server.model.impl.MachineImpl in project che by eclipse.
the class WorkspaceManagerTest method shouldBeAbleToGetMachineInstanceIfWorkspaceIsStarting.
@Test
public void shouldBeAbleToGetMachineInstanceIfWorkspaceIsStarting() throws Exception {
// given
final WorkspaceImpl workspace = createAndMockWorkspace();
WorkspaceRuntimeImpl runtime = mockRuntime(workspace, STARTING);
MachineImpl machine = runtime.getMachines().get(0);
// when
workspaceManager.getMachineInstance(workspace.getId(), machine.getId());
// then
verify(runtimes).getMachine(workspace.getId(), machine.getId());
}
use of org.eclipse.che.api.machine.server.model.impl.MachineImpl in project che by eclipse.
the class CheEnvironmentEngine method startMachine.
/**
* Starts machine in running environment.
*
* @param workspaceId
* ID of workspace that owns environment in which machine should be started
* @param machineConfig
* configuration of machine that should be started
* @return running machine
* @throws EnvironmentNotRunningException
* if environment is not running
* @throws NotFoundException
* if provider of machine implementation is not found
* @throws ConflictException
* if machine with the same name already exists in the environment
* @throws ServerException
* if any other error occurs
*/
public Instance startMachine(String workspaceId, MachineConfig machineConfig, List<String> agents) throws ServerException, NotFoundException, ConflictException, EnvironmentException {
MachineConfig machineConfigCopy = new MachineConfigImpl(machineConfig);
EnvironmentHolder environmentHolder;
try (@SuppressWarnings("unused") Unlocker u = stripedLocks.readLock(workspaceId)) {
environmentHolder = environments.get(workspaceId);
if (environmentHolder == null || environmentHolder.status != EnvStatus.RUNNING) {
throw new EnvironmentNotRunningException(format("Environment '%s' is not running", workspaceId));
}
for (Instance machine : environmentHolder.machines) {
if (machine.getConfig().getName().equals(machineConfigCopy.getName())) {
throw new ConflictException(format("Machine with name '%s' already exists in environment of workspace '%s'", machineConfigCopy.getName(), workspaceId));
}
}
}
final String creator = EnvironmentContext.getCurrent().getSubject().getUserId();
final String namespace = EnvironmentContext.getCurrent().getSubject().getUserName();
MachineImpl machine = MachineImpl.builder().setConfig(machineConfig).setWorkspaceId(workspaceId).setStatus(MachineStatus.CREATING).setEnvName(environmentHolder.name).setOwner(creator).build();
MachineStarter machineStarter;
if ("docker".equals(machineConfig.getType())) {
// needed to reuse startInstance method and
// create machine instances by different implementation-specific providers
CheServiceImpl service = machineConfigToService(machineConfig);
normalize(namespace, workspaceId, machineConfig.getName(), service);
machine.setId(service.getId());
machineStarter = (machineLogger, machineSource) -> {
CheServiceImpl serviceWithNormalizedSource = normalizeServiceSource(service, machineSource);
normalize(namespace, workspaceId, machineConfig.getName(), serviceWithNormalizedSource);
infrastructureProvisioner.provision(new ExtendedMachineImpl().withAgents(agents), serviceWithNormalizedSource);
return machineProvider.startService(namespace, workspaceId, environmentHolder.name, machineConfig.getName(), machineConfig.isDev(), environmentHolder.networkId, serviceWithNormalizedSource, machineLogger);
};
} else {
try {
InstanceProvider provider = machineInstanceProviders.getProvider(machineConfig.getType());
machine.setId(generateMachineId());
addAgentsProvidedServers(machine, agents);
machineStarter = (machineLogger, machineSource) -> {
Machine machineWithNormalizedSource = normalizeMachineSource(machine, machineSource);
return provider.createInstance(machineWithNormalizedSource, machineLogger);
};
} catch (NotFoundException e) {
throw new NotFoundException(format("Provider of machine type '%s' not found", machineConfig.getType()));
}
}
return startInstance(false, environmentHolder.logger, machine, machineStarter);
}
Aggregations