use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class MavenUtils method getMavenVersionInformation.
public static Map<String, String> getMavenVersionInformation() throws IOException {
final Map<String, String> versionInfo = new HashMap<>();
final LineConsumer cmdOutput = new LineConsumer() {
@Override
public void writeLine(String line) throws IOException {
String key = null;
int keyEnd = 0;
int valueStart = 0;
final int l = line.length();
if (line.startsWith("Apache Maven")) {
key = "Maven version";
} else {
while (keyEnd < l) {
if (line.charAt(keyEnd) == ':') {
valueStart = keyEnd + 1;
break;
}
keyEnd++;
}
if (keyEnd > 0) {
key = line.substring(0, keyEnd);
}
}
if (key != null) {
while (valueStart < l && Character.isWhitespace(line.charAt(valueStart))) {
valueStart++;
}
if ("Maven version".equals(key)) {
int valueEnd = valueStart;
// Don't show version details, e.g. (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 18:22:22+0300)
while (valueEnd < l && '(' != line.charAt(valueEnd)) {
valueEnd++;
}
final String value = line.substring(valueStart, valueEnd).trim();
versionInfo.put(key, value);
} else {
final String value = line.substring(valueStart);
versionInfo.put(key, value);
}
}
}
@Override
public void close() throws IOException {
}
};
readMavenVersionInformation(cmdOutput);
return versionInfo;
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ArchetypeGenerator method execute.
/**
* Execute maven archetype command
*
* @param commandLine
* command to execution e.g.
* mvn archetype:generate -DarchetypeGroupId=<archetype-groupId> -DarchetypeArtifactId=<archetype-artifactId>
* -DarchetypeVersion=<archetype-version> -DgroupId=<my.groupid> -DartifactId=<my-artifactId>
* @param workDir
* folder where command will execute in common use root dir of workspace
* @throws TimeoutException
* @throws IOException
* @throws InterruptedException
*/
private void execute(String[] commandLine, File workDir) throws TimeoutException, IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(commandLine).redirectErrorStream(true).directory(workDir);
WebsocketMessageConsumer<ArchetypeOutput> websocketMessageConsumer = new WebsocketMessageConsumer(MAVEN_ARCHETYPE_CHANEL_NAME);
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Start Project generation", ArchetypeOutput.State.START));
LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
websocketMessageConsumer.consume(new ArchetypeOutputImpl(line, ArchetypeOutput.State.IN_PROGRESS));
}
};
// process will be stopped after timeout
Watchdog watcher = new Watchdog(60, TimeUnit.SECONDS);
try {
final Process process = pb.start();
final ValueHolder<Boolean> isTimeoutExceeded = new ValueHolder<>(false);
watcher.start(() -> {
isTimeoutExceeded.set(true);
ProcessUtil.kill(process);
});
// consume logs until process ends
ProcessUtil.process(process, lineConsumer);
process.waitFor();
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Done", ArchetypeOutput.State.DONE));
if (isTimeoutExceeded.get()) {
LOG.error("Generation project time expired : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project time expired", ArchetypeOutput.State.ERROR));
throw new TimeoutException();
} else if (process.exitValue() != 0) {
LOG.error("Generation project fail : command-line " + Arrays.toString(commandLine));
websocketMessageConsumer.consume(new ArchetypeOutputImpl("Generation project occurs error", ArchetypeOutput.State.ERROR));
throw new IOException("Process failed. Exit code " + process.exitValue() + " command-line : " + Arrays.toString(commandLine));
}
} finally {
watcher.stop();
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class CheEnvironmentEngine method startInstance.
private Instance startInstance(boolean recover, MessageConsumer<MachineLogMessage> environmentLogger, MachineImpl machine, MachineStarter machineStarter) throws ServerException, EnvironmentException {
LineConsumer machineLogger = null;
Instance instance = null;
try {
addMachine(machine);
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
machineLogger = getMachineLogger(environmentLogger, machine.getId(), machine.getConfig().getName());
MachineImpl originMachine = new MachineImpl(machine);
try {
MachineSourceImpl machineSource = null;
if (recover) {
try {
SnapshotImpl snapshot = snapshotDao.getSnapshot(machine.getWorkspaceId(), machine.getEnvName(), machine.getConfig().getName());
machineSource = snapshot.getMachineSource();
// Snapshot image location has SHA-256 digest which needs to be removed,
// otherwise it will be pulled without tag and cause problems
String imageName = machineSource.getLocation();
if (imageName.contains("@sha256:")) {
machineSource.setLocation(imageName.substring(0, imageName.indexOf('@')));
}
} catch (NotFoundException e) {
try {
machineLogger.writeLine("Failed to boot machine from snapshot: snapshot not found. " + "Machine will be created from origin source.");
} catch (IOException ignore) {
}
}
}
instance = machineStarter.startMachine(machineLogger, machineSource);
} catch (SourceNotFoundException e) {
if (recover) {
LOG.error("Image of snapshot for machine " + machine.getConfig().getName() + " not found. " + "Machine will be created from origin source.");
machine = originMachine;
instance = machineStarter.startMachine(machineLogger, null);
} else {
throw e;
}
}
replaceMachine(instance);
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(machine.getWorkspaceId()));
return instance;
} catch (ApiException | RuntimeException e) {
boolean interrupted = Thread.interrupted();
removeMachine(machine.getWorkspaceId(), machine.getId());
if (instance != null) {
try {
instance.destroy();
} catch (Exception destroyingExc) {
LOG.error(destroyingExc.getLocalizedMessage(), destroyingExc);
}
}
if (machineLogger != null) {
try {
machineLogger.writeLine("[ERROR] " + e.getLocalizedMessage());
} catch (IOException ioEx) {
LOG.error(ioEx.getLocalizedMessage(), ioEx);
}
try {
machineLogger.close();
} catch (IOException ioEx) {
LOG.error(ioEx.getLocalizedMessage(), ioEx);
}
}
eventService.publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.ERROR).withDev(machine.getConfig().isDev()).withMachineName(machine.getConfig().getName()).withMachineId(machine.getId()).withWorkspaceId(machine.getWorkspaceId()));
if (interrupted) {
Thread.currentThread().interrupt();
}
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class CheEnvironmentEngine method getMachineLogger.
private LineConsumer getMachineLogger(MessageConsumer<MachineLogMessage> environmentLogger, String machineId, String machineName) throws ServerException {
createMachineLogsDir(machineId);
LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
environmentLogger.consume(new MachineLogMessageImpl(machineName, line));
}
};
try {
return new ConcurrentCompositeLineConsumer(new ConcurrentFileLineConsumer(getMachineLogsFile(machineId)), lineConsumer);
} catch (IOException e) {
throw new MachineException(format("Unable create log file '%s' for machine '%s'.", e.getLocalizedMessage(), machineId));
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class MachineProcessManager method exec.
/**
* Execute a command in machine
*
* @param machineId
* id of the machine where command should be executed
* @param command
* command that should be executed in the machine
* @return {@link org.eclipse.che.api.machine.server.spi.InstanceProcess} that represents started process in machine
* @throws NotFoundException
* if machine with specified id not found
* @throws BadRequestException
* if value of required parameter is invalid
* @throws MachineException
* if other error occur
*/
public InstanceProcess exec(String workspaceId, String machineId, Command command, @Nullable String outputChannel) throws NotFoundException, MachineException, BadRequestException {
requiredNotNull(machineId, "Machine ID is required");
requiredNotNull(command, "Command is required");
requiredNotNull(command.getCommandLine(), "Command line is required");
requiredNotNull(command.getName(), "Command name is required");
requiredNotNull(command.getType(), "Command type is required");
final Instance machine = environmentEngine.getMachine(workspaceId, machineId);
final InstanceProcess instanceProcess = machine.createProcess(command, outputChannel);
final int pid = instanceProcess.getPid();
final LineConsumer processLogger = getProcessLogger(machineId, pid, outputChannel);
executor.execute(ThreadLocalPropagateContext.wrap(() -> {
try {
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.STARTED).withMachineId(machineId).withProcessId(pid));
instanceProcess.start(processLogger);
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.STOPPED).withMachineId(machineId).withProcessId(pid));
} catch (ConflictException | MachineException error) {
eventService.publish(newDto(MachineProcessEvent.class).withEventType(MachineProcessEvent.EventType.ERROR).withMachineId(machineId).withProcessId(pid).withError(error.getLocalizedMessage()));
try {
processLogger.writeLine(String.format("[ERROR] %s", error.getMessage()));
} catch (IOException ignored) {
}
} finally {
try {
processLogger.close();
} catch (IOException ignored) {
}
}
}));
return instanceProcess;
}
Aggregations