use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.
the class DockerProcess method checkAlive.
@Override
public void checkAlive() throws MachineException, NotFoundException {
// Read pid from file and run 'kill -0 [pid]' command.
final String isAliveCmd = format("[ -r %1$s ] && kill -0 $(cat %1$s) || echo 'Unable read PID file'", pidFilePath);
final ListLineConsumer output = new ListLineConsumer();
final String[] command = { "/bin/sh", "-c", isAliveCmd };
Exec exec;
try {
exec = docker.createExec(CreateExecParams.create(container, command).withDetach(false));
} catch (IOException e) {
throw new MachineException(format("Error occurs while initializing command %s in docker container %s: %s", Arrays.toString(command), container, e.getMessage()), e);
}
try {
docker.startExec(StartExecParams.create(exec.getId()), new LogMessagePrinter(output));
} catch (IOException e) {
throw new MachineException(format("Error occurs while executing command %s in docker container %s: %s", Arrays.toString(exec.getCommand()), container, e.getMessage()), e);
}
// 'kill -0 [pid]' is silent if process is running or print "No such process" message otherwise
if (!output.getText().isEmpty()) {
throw new NotFoundException(format("Process with pid %s not found", getPid()));
}
}
use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.
the class MachineProviderImpl method buildImage.
protected void buildImage(CheServiceImpl service, String machineImageName, boolean doForcePullOnBuild, ProgressMonitor progressMonitor) throws MachineException {
File workDir = null;
try {
BuildImageParams buildImageParams;
if (service.getBuild() != null && service.getBuild().getDockerfileContent() != null) {
workDir = Files.createTempDirectory(null).toFile();
final File dockerfileFile = new File(workDir, "Dockerfile");
try (FileWriter output = new FileWriter(dockerfileFile)) {
output.append(service.getBuild().getDockerfileContent());
}
buildImageParams = BuildImageParams.create(dockerfileFile);
} else {
buildImageParams = BuildImageParams.create(service.getBuild().getContext()).withDockerfile(service.getBuild().getDockerfilePath());
}
buildImageParams.withForceRemoveIntermediateContainers(true).withRepository(machineImageName).withAuthConfigs(dockerCredentials.getCredentials()).withDoForcePull(doForcePullOnBuild).withMemoryLimit(service.getMemLimit()).withMemorySwapLimit(-1).withCpusetCpus(cpusetCpus).withCpuPeriod(cpuPeriod).withCpuQuota(cpuQuota).withBuildArgs(service.getBuild().getArgs());
docker.buildImage(buildImageParams, progressMonitor);
} catch (IOException e) {
throw new MachineException(e.getLocalizedMessage(), e);
} finally {
if (workDir != null) {
FileCleaner.addFile(workDir);
}
}
}
use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.
the class MachineProviderImpl method pullImage.
/**
* Pulls docker image for container creation.
*
* @param service
* service that provides description of image that should be pulled
* @param machineImageName
* name of the image that should be assigned on pull
* @param progressMonitor
* consumer of output
* @throws SourceNotFoundException
* if image for pulling not found
* @throws MachineException
* if any other error occurs
*/
protected void pullImage(CheServiceImpl service, String machineImageName, ProgressMonitor progressMonitor) throws MachineException {
DockerMachineSource dockerMachineSource = new DockerMachineSource(new MachineSourceImpl("image").setLocation(service.getImage()));
if (dockerMachineSource.getRepository() == null) {
throw new MachineException(format("Machine creation failed. Machine source is invalid. No repository is defined. Found '%s'.", dockerMachineSource));
}
try {
boolean isSnapshot = SNAPSHOT_LOCATION_PATTERN.matcher(dockerMachineSource.getLocation()).matches();
if (!isSnapshot || snapshotUseRegistry) {
PullParams pullParams = PullParams.create(dockerMachineSource.getRepository()).withTag(MoreObjects.firstNonNull(dockerMachineSource.getTag(), LATEST_TAG)).withRegistry(dockerMachineSource.getRegistry()).withAuthConfigs(dockerCredentials.getCredentials());
docker.pull(pullParams, progressMonitor);
}
String fullNameOfPulledImage = dockerMachineSource.getLocation(false);
try {
// tag image with generated name to allow sysadmin recognize it
docker.tag(TagParams.create(fullNameOfPulledImage, machineImageName));
} catch (ImageNotFoundException nfEx) {
throw new SourceNotFoundException(nfEx.getLocalizedMessage(), nfEx);
}
// remove unneeded tag if restoring snapshot from registry
if (isSnapshot && snapshotUseRegistry) {
docker.removeImage(RemoveImageParams.create(fullNameOfPulledImage).withForce(false));
}
} catch (IOException e) {
throw new MachineException("Can't create machine from image. Cause: " + e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.
the class WsAgentLauncher method launch.
@Override
public void launch(Instance machine, Agent agent) throws ServerException {
final HttpJsonRequest wsAgentPingRequest;
try {
wsAgentPingRequest = createPingRequest(machine);
} catch (ServerException e) {
throw new MachineException(e.getServiceError());
}
String script = agent.getScript() + "\n" + firstNonNull(wsAgentRunCommand, DEFAULT_WS_AGENT_RUN_COMMAND);
final String wsAgentPingUrl = wsAgentPingRequest.getUrl();
try {
// for server side type of command mean nothing
// but we will use it as marker on
// client side for track this command
CommandImpl command = new CommandImpl(getAgentId(), script, WS_AGENT_PROCESS_NAME);
machineProcessManagerProvider.get().exec(machine.getWorkspaceId(), machine.getId(), command, getWsAgentProcessOutputChannel(machine.getWorkspaceId()));
final long pingStartTimestamp = System.currentTimeMillis();
LOG.debug("Starts pinging ws agent. Workspace ID:{}. Url:{}. Timestamp:{}", machine.getWorkspaceId(), wsAgentPingUrl, pingStartTimestamp);
while (System.currentTimeMillis() - pingStartTimestamp < wsAgentMaxStartTimeMs) {
if (pingWsAgent(wsAgentPingRequest)) {
return;
} else {
Thread.sleep(wsAgentPingDelayMs);
}
}
} catch (BadRequestException | ServerException | NotFoundException e) {
throw new ServerException(e.getServiceError());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new ServerException("Ws agent pinging is interrupted");
}
LOG.error("Fail pinging ws agent. Workspace ID:{}. Url:{}. Timestamp:{}", machine.getWorkspaceId(), wsAgentPingUrl);
throw new ServerException(pingTimedOutErrorMessage);
}
use of org.eclipse.che.api.machine.server.exception.MachineException in project che by eclipse.
the class WsAgentLauncherTest method shouldThrowMachineExceptionIfMachineManagerExecInDevMachineThrowsMachineException.
@Test(expectedExceptions = ServerException.class, expectedExceptionsMessageRegExp = "Test exception")
public void shouldThrowMachineExceptionIfMachineManagerExecInDevMachineThrowsMachineException() throws Exception {
Mockito.when(machineProcessManager.exec(Matchers.anyString(), Matchers.anyString(), Matchers.any(Command.class), Matchers.anyString())).thenThrow(new MachineException("Test exception"));
wsAgentLauncher.launch(machine, agent);
Mockito.verify(machineProcessManager).exec(Matchers.anyString(), Matchers.anyString(), Matchers.any(Command.class), Matchers.anyString());
}
Aggregations