use of org.eclipse.che.plugin.docker.client.Exec 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.plugin.docker.client.Exec in project che by eclipse.
the class DockerProcess method start.
@Override
public void start(LineConsumer output) throws ConflictException, MachineException {
if (started) {
throw new ConflictException("Process already started.");
}
started = true;
// Trap is invoked when bash session ends. Here we kill all sub-processes of shell and remove pid-file.
final String trap = format("trap '[ -z \"$(jobs -p)\" ] || kill $(jobs -p); [ -e %1$s ] && rm %1$s' EXIT", pidFilePath);
// 'echo' saves shell pid in file, then run command
final String shellCommand = trap + "; echo $$>" + pidFilePath + "; " + commandLine;
final String[] command = { shellInvoker, "-c", shellCommand };
Exec exec;
try {
exec = docker.createExec(CreateExecParams.create(container, command).withDetach(output == null));
} 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()), output == null ? null : new LogMessagePrinter(output));
} catch (IOException e) {
if (output != null && e instanceof SocketTimeoutException) {
throw new MachineException(getErrorMessage());
} else {
throw new MachineException(format("Error occurs while executing command %s: %s", Arrays.toString(exec.getCommand()), e.getMessage()), e);
}
}
}
use of org.eclipse.che.plugin.docker.client.Exec in project che by eclipse.
the class DockerProcess method getErrorMessage.
private String getErrorMessage() {
final StringBuilder errorMessage = new StringBuilder("Command output read timeout is reached.");
try {
// check if process is alive
final Exec checkProcessExec = docker.createExec(CreateExecParams.create(container, new String[] { "/bin/sh", "-c", format("if kill -0 $(cat %1$s 2>/dev/null) 2>/dev/null; then cat %1$s; fi", pidFilePath) }).withDetach(false));
ValueHolder<String> pidHolder = new ValueHolder<>();
docker.startExec(StartExecParams.create(checkProcessExec.getId()), message -> {
if (message.getType() == LogMessage.Type.STDOUT) {
pidHolder.set(message.getContent());
}
});
if (pidHolder.get() != null) {
errorMessage.append(" Process is still running and has id ").append(pidHolder.get()).append(" inside machine");
}
} catch (IOException ignore) {
}
return errorMessage.toString();
}
use of org.eclipse.che.plugin.docker.client.Exec in project che by eclipse.
the class DockerProcess method kill.
@Override
public void kill() throws MachineException {
if (started) {
// Read pid from file and run 'kill [pid]' command.
final String killCmd = format("[ -r %1$s ] && kill $(cat %1$s)", pidFilePath);
final String[] command = { "/bin/sh", "-c", killCmd };
Exec exec;
try {
exec = docker.createExec(CreateExecParams.create(container, command).withDetach(true));
} 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()), MessageProcessor.DEV_NULL);
} 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);
}
}
}
use of org.eclipse.che.plugin.docker.client.Exec in project che by eclipse.
the class DockerInstance method readFileContent.
/**
* Reads file content by specified file path.
*
* TODO:
* add file size checking,
* note that size checking and file content reading
* should be done in an atomic way,
* which means that two separate instance processes is not the case.
*
* @param filePath
* path to file on machine instance
* @param startFrom
* line number to start reading from
* @param limit
* limitation on line
* @return if {@code limit} and {@code startFrom} grater than 0
* content from {@code startFrom} to {@code startFrom + limit} will be returned,
* if file contains less lines than {@code startFrom} empty content will be returned
* @throws MachineException
* if any error occurs with file reading
*/
@Override
public String readFileContent(String filePath, int startFrom, int limit) throws MachineException {
if (limit <= 0 || startFrom <= 0) {
throw new MachineException("Impossible to read file " + limit + " lines from " + startFrom + " line");
}
// command sed getting file content from startFrom line to (startFrom + limit)
String shCommand = format("sed -n \'%1$2s, %2$2sp\' %3$2s", startFrom, startFrom + limit, filePath);
final String[] command = { "/bin/sh", "-c", shCommand };
ListLineConsumer lines = new ListLineConsumer();
try {
Exec exec = docker.createExec(CreateExecParams.create(container, command).withDetach(false));
docker.startExec(StartExecParams.create(exec.getId()), new LogMessagePrinter(lines, LogMessage::getContent));
} catch (IOException e) {
throw new MachineException(format("Error occurs while initializing command %s in docker container %s: %s", Arrays.toString(command), container, e.getLocalizedMessage()), e);
}
String content = lines.getText();
if (content.contains("sed: can't read " + filePath + ": No such file or directory") || content.contains("cat: " + filePath + ": No such file or directory")) {
throw new MachineException("File with path " + filePath + " not found");
}
return content;
}
Aggregations