Search in sources :

Example 11 with MachineException

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()));
    }
}
Also used : ListLineConsumer(org.eclipse.che.api.core.util.ListLineConsumer) Exec(org.eclipse.che.plugin.docker.client.Exec) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) NotFoundException(org.eclipse.che.api.core.NotFoundException) IOException(java.io.IOException)

Example 12 with MachineException

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);
        }
    }
}
Also used : FileWriter(java.io.FileWriter) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) File(java.io.File) BuildImageParams(org.eclipse.che.plugin.docker.client.params.BuildImageParams)

Example 13 with MachineException

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);
    }
}
Also used : SourceNotFoundException(org.eclipse.che.api.machine.server.exception.SourceNotFoundException) PullParams(org.eclipse.che.plugin.docker.client.params.PullParams) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) MachineSourceImpl(org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl) IOException(java.io.IOException) ImageNotFoundException(org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException)

Example 14 with MachineException

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);
}
Also used : CommandImpl(org.eclipse.che.api.machine.server.model.impl.CommandImpl) ServerException(org.eclipse.che.api.core.ServerException) HttpJsonRequest(org.eclipse.che.api.core.rest.HttpJsonRequest) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) BadRequestException(org.eclipse.che.api.core.BadRequestException) NotFoundException(org.eclipse.che.api.core.NotFoundException)

Example 15 with MachineException

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());
}
Also used : Command(org.eclipse.che.api.core.model.machine.Command) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) Test(org.testng.annotations.Test)

Aggregations

MachineException (org.eclipse.che.api.machine.server.exception.MachineException)35 IOException (java.io.IOException)17 ListLineConsumer (org.eclipse.che.api.core.util.ListLineConsumer)7 ConflictException (org.eclipse.che.api.core.ConflictException)6 ServerException (org.eclipse.che.api.core.ServerException)6 InstanceProcess (org.eclipse.che.api.machine.server.spi.InstanceProcess)6 JSchException (com.jcraft.jsch.JSchException)5 File (java.io.File)5 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)5 Instance (org.eclipse.che.api.machine.server.spi.Instance)5 Test (org.testng.annotations.Test)5 Agent (org.eclipse.che.api.agent.shared.model.Agent)4 Command (org.eclipse.che.api.core.model.machine.Command)4 CommandImpl (org.eclipse.che.api.machine.server.model.impl.CommandImpl)4 Exec (org.eclipse.che.plugin.docker.client.Exec)4 URL (java.net.URL)3 NotFoundException (org.eclipse.che.api.core.NotFoundException)3 AbstractLineConsumer (org.eclipse.che.api.core.util.AbstractLineConsumer)3 ChannelExec (com.jcraft.jsch.ChannelExec)2 ChannelSftp (com.jcraft.jsch.ChannelSftp)2