Search in sources :

Example 1 with DockerClientException

use of com.github.dockerjava.api.exception.DockerClientException in project camel by apache.

the class DockerHelper method validateParameters.

/**
     * Validates the URI parameters for a given {@link DockerOperation}
     *
     * @param dockerOperation
     * @param parameters
     */
public static void validateParameters(DockerOperation dockerOperation, Map<String, Object> parameters) {
    Map<String, Class<?>> validParamMap = new HashMap<String, Class<?>>();
    validParamMap.putAll(DockerConstants.DOCKER_DEFAULT_PARAMETERS);
    validParamMap.putAll(dockerOperation.getParameters());
    for (String key : parameters.keySet()) {
        String transformedKey = DockerHelper.transformToHeaderName(key);
        // Validate URI parameter name
        if (!validParamMap.containsKey(transformedKey)) {
            throw new DockerClientException(key + " is not a valid URI parameter");
        }
        try {
            Class<?> parameterClass = validParamMap.get(transformedKey);
            Object parameterValue = parameters.get(key);
            if (parameterClass == null || parameterValue == null) {
                throw new DockerClientException("Failed to validate parameter type for property " + key);
            }
            if (Integer.class == parameterClass) {
                Integer.parseInt((String) parameterValue);
            } else if (Boolean.class == parameterClass) {
                BooleanUtils.toBooleanObject((String) parameterValue, "true", "false", "null");
            }
        } catch (Exception e) {
            throw new DockerClientException("Failed to validate parameter type for property " + key);
        }
    }
}
Also used : HashMap(java.util.HashMap) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException)

Example 2 with DockerClientException

use of com.github.dockerjava.api.exception.DockerClientException in project elastest-torm by elastest.

the class DockerService2 method createTestContainer.

public void createTestContainer(DockerExecution dockerExec) throws TJobStoppedException {
    try {
        CreateContainerResponse testContainer = createContainer(dockerExec, "tjob");
        String testContainerId = testContainer.getId();
        dockerExec.setTestcontainer(testContainer);
        dockerExec.setTestContainerId(testContainerId);
    } catch (DockerClientException dce) {
        throw new TJobStoppedException();
    } catch (TJobStoppedException dce) {
        throw new TJobStoppedException();
    }
}
Also used : DockerClientException(com.github.dockerjava.api.exception.DockerClientException) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse)

Example 3 with DockerClientException

use of com.github.dockerjava.api.exception.DockerClientException in project elastest-torm by elastest.

the class DockerService2 method pullETExecImage.

public void pullETExecImage(String image, String name) throws TJobStoppedException {
    DockerClient dockerClient = this.getDockerClient();
    try {
        logger.debug("Try to Pulling {} Image ({})", name, image);
        dockerClient.pullImageCmd(image).exec(new PullImageResultCallback()).awaitCompletion();
        logger.debug("{} image pulled succesfully!", name);
    } catch (InternalServerErrorException | NotFoundException | InterruptedException e) {
        if (imageExistsLocally(image, dockerClient)) {
            logger.info("Docker image exits locally.");
        } else {
            logger.error("Error pulling the {} image: {}", name, image, e.getMessage());
        }
    } catch (DockerClientException e) {
        logger.info("Error on Pulling {} image ({}). Probably because the user has stopped the execution", name, image);
        throw new TJobStoppedException();
    }
}
Also used : DockerClient(com.github.dockerjava.api.DockerClient) DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException)

Example 4 with DockerClientException

use of com.github.dockerjava.api.exception.DockerClientException in project elastest-torm by elastest.

the class UtilTools method getHostIp.

public String getHostIp() {
    if (hostIp == null) {
        if (inContainer.equals("true")) {
            try {
                String ipRoute = Shell.runAndWait("sh", "-c", "/sbin/ip route");
                String[] tokens = ipRoute.split("\\s");
                hostIp = tokens[2];
            } catch (Exception e) {
                throw new DockerClientException("Exception executing /sbin/ip route", e);
            }
        } else {
            hostIp = "127.0.0.1";
        }
    }
    logger.debug("Host IP is {}", hostIp);
    return hostIp;
}
Also used : DockerClientException(com.github.dockerjava.api.exception.DockerClientException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DockerClientException(com.github.dockerjava.api.exception.DockerClientException)

Example 5 with DockerClientException

use of com.github.dockerjava.api.exception.DockerClientException in project elastest-torm by elastest.

the class DockerService2 method runDockerContainer.

/**
 **************************
 */
/**
 *** Container Methods ****
 */
/**
 **************************
 */
public String runDockerContainer(DockerClient dockerClient, String imageName, List<String> envs, String containerName, String networkName, Ports portBindings, int listenPort) throws TJobStoppedException {
    try {
        dockerClient.pullImageCmd(imageName).exec(new PullImageResultCallback()).awaitSuccess();
    } catch (InternalServerErrorException | NotFoundException ie) {
        if (imageExistsLocally(imageName, dockerClient)) {
            logger.info("Docker image exits locally.");
        } else {
            throw ie;
        }
    } catch (DockerClientException e) {
        logger.info("Error on Pulling " + imageName + " image. Probably because the user has stopped the execution");
        throw new TJobStoppedException();
    }
    CreateContainerResponse container = dockerClient.createContainerCmd(imageName).withName(containerName).withEnv(envs).withNetworkMode(networkName).withExposedPorts(ExposedPort.tcp(listenPort)).withPortBindings(portBindings).withPublishAllPorts(true).exec();
    dockerClient.startContainerCmd(container.getId()).exec();
    this.insertCreatedContainer(container.getId(), containerName);
    logger.info("Id del contenedor:" + container.getId());
    return container.getId();
}
Also used : DockerClientException(com.github.dockerjava.api.exception.DockerClientException) PullImageResultCallback(com.github.dockerjava.core.command.PullImageResultCallback) InternalServerErrorException(com.github.dockerjava.api.exception.InternalServerErrorException) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) CreateContainerResponse(com.github.dockerjava.api.command.CreateContainerResponse)

Aggregations

DockerClientException (com.github.dockerjava.api.exception.DockerClientException)7 DockerClient (com.github.dockerjava.api.DockerClient)3 InternalServerErrorException (com.github.dockerjava.api.exception.InternalServerErrorException)3 NotFoundException (com.github.dockerjava.api.exception.NotFoundException)3 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)3 CreateContainerResponse (com.github.dockerjava.api.command.CreateContainerResponse)2 IOException (java.io.IOException)2 ListImagesCmd (com.github.dockerjava.api.command.ListImagesCmd)1 NotModifiedException (com.github.dockerjava.api.exception.NotModifiedException)1 Image (com.github.dockerjava.api.model.Image)1 WaitContainerResultCallback (com.github.dockerjava.core.command.WaitContainerResultCallback)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Stream (java.util.stream.Stream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Logger (org.slf4j.Logger)1 Profiler (org.slf4j.profiler.Profiler)1 ContainerFetchException (org.testcontainers.containers.ContainerFetchException)1 SAXException (org.xml.sax.SAXException)1