Search in sources :

Example 1 with Command

use of org.eclipse.che.api.core.model.machine.Command in project che by eclipse.

the class DefaultWorkspaceValidator method validateConfig.

@Override
public void validateConfig(WorkspaceConfig config) throws BadRequestException, ServerException {
    // configuration object itself
    checkNotNull(config.getName(), "Workspace name required");
    checkArgument(WS_NAME.matcher(config.getName()).matches(), "Incorrect workspace name, it must be between 3 and 20 characters and may contain digits, " + "latin letters, underscores, dots, dashes and should start and end only with digits, " + "latin letters or underscores");
    //environments
    checkArgument(!isNullOrEmpty(config.getDefaultEnv()), "Workspace default environment name required");
    checkNotNull(config.getEnvironments(), "Workspace should contain at least one environment");
    checkArgument(config.getEnvironments().containsKey(config.getDefaultEnv()), "Workspace default environment configuration required");
    for (Map.Entry<String, ? extends Environment> envEntry : config.getEnvironments().entrySet()) {
        try {
            environmentValidator.validate(envEntry.getKey(), envEntry.getValue());
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e.getLocalizedMessage());
        }
    }
    //commands
    for (Command command : config.getCommands()) {
        checkArgument(!isNullOrEmpty(command.getName()), "Workspace %s contains command with null or empty name", config.getName());
        checkArgument(!isNullOrEmpty(command.getCommandLine()), "Command line required for command '%s' in workspace '%s'", command.getName(), config.getName());
    }
//projects
//TODO
}
Also used : Command(org.eclipse.che.api.core.model.machine.Command) BadRequestException(org.eclipse.che.api.core.BadRequestException) Map(java.util.Map)

Example 2 with Command

use of org.eclipse.che.api.core.model.machine.Command in project che by eclipse.

the class DefaultAgentLauncher method launch.

@Override
public void launch(Instance machine, Agent agent) throws ServerException {
    if (isNullOrEmpty(agent.getScript())) {
        return;
    }
    final Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
    final InstanceProcess process = machine.createProcess(command, null);
    final LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
        }
    };
    try {
        process.start(lineConsumer);
    } catch (ConflictException e) {
        try {
            machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
        } catch (IOException ignored) {
        }
    } finally {
        try {
            lineConsumer.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) ConflictException(org.eclipse.che.api.core.ConflictException) InstanceProcess(org.eclipse.che.api.machine.server.spi.InstanceProcess) IOException(java.io.IOException)

Example 3 with Command

use of org.eclipse.che.api.core.model.machine.Command in project che by eclipse.

the class DefaultAgentLauncherTest method shouldLaunchAgent.

@Test
public void shouldLaunchAgent() throws Exception {
    ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
    agentLauncher.launch(machine, agent);
    verify(machine).createProcess(commandCaptor.capture(), (String) isNull());
    Command command = commandCaptor.getValue();
    assertEquals(command.getCommandLine(), "script1");
    verify(instanceProcess).start(any());
}
Also used : Command(org.eclipse.che.api.core.model.machine.Command) Test(org.testng.annotations.Test)

Example 4 with Command

use of org.eclipse.che.api.core.model.machine.Command in project che by eclipse.

the class SshProcessLaunchedChecker method isLaunched.

public boolean isLaunched(Agent agent, SshMachineInstance machine) throws MachineException {
    Command command = new CommandImpl(format("Wait for %s, try %d", agent.getId(), ++counter), format(CHECK_COMMAND, processNameToWait), "test");
    try (ListLineConsumer lineConsumer = new ListLineConsumer()) {
        SshMachineProcess waitProcess = machine.createProcess(command, null);
        waitProcess.start(lineConsumer);
        return lineConsumer.getText().endsWith("[STDOUT] 0");
    } catch (ConflictException e) {
        throw new MachineException(e.getServiceError());
    }
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) ConflictException(org.eclipse.che.api.core.ConflictException) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) MachineException(org.eclipse.che.api.machine.server.exception.MachineException)

Example 5 with Command

use of org.eclipse.che.api.core.model.machine.Command in project che by eclipse.

the class SshMachineExecAgentLauncher method start.

protected SshMachineProcess start(SshMachineInstance machine, Agent agent) throws ServerException {
    Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
    SshMachineProcess process = machine.createProcess(command, null);
    LineConsumer lineConsumer = new AbstractLineConsumer() {

        @Override
        public void writeLine(String line) throws IOException {
            machine.getLogger().writeLine(line);
        }
    };
    CountDownLatch countDownLatch = new CountDownLatch(1);
    executor.execute(ThreadLocalPropagateContext.wrap(() -> {
        try {
            countDownLatch.countDown();
            process.start(lineConsumer);
        } catch (ConflictException | MachineException e) {
            try {
                machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
            } catch (IOException ignored) {
            }
        } finally {
            try {
                lineConsumer.close();
            } catch (IOException ignored) {
            }
        }
    }));
    try {
        // ensure that code inside of task submitted to executor is called before end of this method
        countDownLatch.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    return process;
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) Command(org.eclipse.che.api.core.model.machine.Command) AbstractLineConsumer(org.eclipse.che.api.core.util.AbstractLineConsumer) SshMachineProcess(org.eclipse.che.plugin.machine.ssh.SshMachineProcess) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Command (org.eclipse.che.api.core.model.machine.Command)9 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)7 ConflictException (org.eclipse.che.api.core.ConflictException)4 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)4 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)4 IOException (java.io.IOException)3 MachineException (org.eclipse.che.api.machine.server.exception.MachineException)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)2 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)2 SshMachineProcess (org.eclipse.che.plugin.machine.ssh.SshMachineProcess)2 Test (org.testng.annotations.Test)2 URI (java.net.URI)1 Map (java.util.Map)1 BadRequestException (org.eclipse.che.api.core.BadRequestException)1 DockerApiVersionPathPrefixProvider (org.eclipse.che.plugin.docker.client.DockerApiVersionPathPrefixProvider)1 DockerConnector (org.eclipse.che.plugin.docker.client.DockerConnector)1 DockerConnectorConfiguration (org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration)1 DockerRegistryAuthResolver (org.eclipse.che.plugin.docker.client.DockerRegistryAuthResolver)1 InitialAuthConfig (org.eclipse.che.plugin.docker.client.InitialAuthConfig)1