Search in sources :

Example 6 with LineConsumer

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;
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) HashMap(java.util.HashMap)

Example 7 with LineConsumer

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();
    }
}
Also used : AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) IOException(java.io.IOException) ValueHolder(org.eclipse.che.api.core.util.ValueHolder) WebsocketMessageConsumer(org.eclipse.che.api.core.util.WebsocketMessageConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ArchetypeOutput(org.eclipse.che.plugin.maven.shared.dto.ArchetypeOutput) Watchdog(org.eclipse.che.api.core.util.Watchdog) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with LineConsumer

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);
    }
}
Also used : SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) ExtendedMachineImpl(org.eclipse.che.api.workspace.server.model.impl.ExtendedMachineImpl) MachineImpl(org.eclipse.che.api.machine.server.model.impl.MachineImpl) MachineStatusEvent(org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent) ServerException(org.eclipse.che.api.core.ServerException) SnapshotImpl(org.eclipse.che.api.machine.server.model.impl.SnapshotImpl) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException) AgentException(org.eclipse.che.api.agent.server.exception.AgentException) EnvironmentStartInterruptedException(org.eclipse.che.api.environment.server.exception.EnvironmentStartInterruptedException) EnvironmentNotRunningException(org.eclipse.che.api.environment.server.exception.EnvironmentNotRunningException) SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) EnvironmentException(org.eclipse.che.api.environment.server.exception.EnvironmentException) ApiException(org.eclipse.che.api.core.ApiException) ConflictException(org.eclipse.che.api.core.ConflictException) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ApiException(org.eclipse.che.api.core.ApiException)

Example 9 with LineConsumer

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));
    }
}
Also used : ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ConcurrentFileLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer) ConcurrentCompositeLineConsumer(org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineLogMessageImpl(org.eclipse.che.api.machine.server.model.impl.MachineLogMessageImpl) IOException(java.io.IOException)

Example 10 with LineConsumer

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;
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) CompositeLineConsumer(org.eclipse.che.api.core.util.CompositeLineConsumer) WebsocketLineConsumer(org.eclipse.che.api.core.util.WebsocketLineConsumer) FileLineConsumer(org.eclipse.che.api.core.util.FileLineConsumer) Instance(org.eclipse.che.api.machine.server.spi.Instance) MachineProcessEvent(org.eclipse.che.api.machine.shared.dto.event.MachineProcessEvent) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

Aggregations

LineConsumer (org.eclipse.che.api.core.util.LineConsumer)21 IOException (java.io.IOException)11 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)8 Test (org.testng.annotations.Test)8 Matchers.anyString (org.mockito.Matchers.anyString)6 HashMap (java.util.HashMap)3 ServerException (org.eclipse.che.api.core.ServerException)3 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)3 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConflictException (org.eclipse.che.api.core.ConflictException)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 Command (org.eclipse.che.api.core.model.machine.Command)2 ConcurrentCompositeLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentCompositeLineConsumer)2 ConcurrentFileLineConsumer (org.eclipse.che.api.core.util.lineconsumer.ConcurrentFileLineConsumer)2 Instance (org.eclipse.che.api.machine.server.spi.Instance)2 WaitingAnswer (org.eclipse.che.commons.test.mockito.answer.WaitingAnswer)2 MoreObjects (com.google.common.base.MoreObjects)1