Search in sources :

Example 1 with DockerException

use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.

the class DockerRuntimeExecExecutor method create.

private Optional<DockerRuntime> create(final DockerRuntimeConfig runtimeConfig) throws ProvisioningException {
    if (runtimeConfig.isPull()) {
        try {
            LOG.info("Pulling Docker Image: " + runtimeConfig.getImage());
            docker.getDockerClient(runtimeConfig.getProviderId()).pull(runtimeConfig.getImage());
        } catch (DockerException | InterruptedException ex) {
            LOG.error(ex.getMessage(), ex);
            throw new ProvisioningException("Error Pulling Docker Image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage());
        }
    }
    final String[] ports = { runtimeConfig.getPort() };
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    final Optional<DockerProvider> _dockerProvider = runtimeRegistry.getProvider(runtimeConfig.getProviderId(), DockerProvider.class);
    if (!_dockerProvider.isPresent()) {
        return Optional.empty();
    }
    final DockerProvider dockerProvider = _dockerProvider.get();
    final List<PortBinding> randomPort = new ArrayList<>();
    final PortBinding randomPortBinding = PortBinding.randomPort(dockerProvider.getConfig().getHostIp());
    randomPort.add(randomPortBinding);
    portBindings.put(runtimeConfig.getPort(), randomPort);
    final HostConfig hostConfig = HostConfig.builder().portBindings(portBindings).build();
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).image(runtimeConfig.getImage()).exposedPorts(ports).build();
    final ContainerCreation creation;
    try {
        creation = docker.getDockerClient(runtimeConfig.getProviderId()).createContainer(containerConfig);
        docker.getDockerClient(runtimeConfig.getProviderId()).startContainer(creation.id());
    } catch (DockerException | InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new ProvisioningException("Error Creating Docker Container with image: " + runtimeConfig.getImage() + "with error: " + ex.getMessage(), ex);
    }
    final String id = creation.id();
    String shortId = id.substring(0, 12);
    String host = "";
    try {
        docker.getDockerClient(runtimeConfig.getProviderId()).inspectContainer(id);
        host = docker.getDockerClient(runtimeConfig.getProviderId()).getHost();
    } catch (DockerException | InterruptedException ex) {
        throw new ProvisioningException("Error Getting Docker Container info: " + id + "with error: " + ex.getMessage(), ex);
    }
    DockerRuntimeEndpoint dockerRuntimeEndpoint = new DockerRuntimeEndpoint();
    dockerRuntimeEndpoint.setHost(host);
    dockerRuntimeEndpoint.setPort(Integer.valueOf(runtimeConfig.getPort()));
    dockerRuntimeEndpoint.setContext("");
    return Optional.of(new DockerRuntime(shortId, buildRuntimeName(runtimeConfig, shortId), runtimeConfig, dockerProvider, dockerRuntimeEndpoint, new DockerRuntimeInfo(), new DockerRuntimeState(RUNNING, new Date().toString())));
}
Also used : DockerException(com.spotify.docker.client.DockerException) HashMap(java.util.HashMap) DockerProvider(org.guvnor.ala.docker.model.DockerProvider) DockerRuntimeEndpoint(org.guvnor.ala.docker.model.DockerRuntimeEndpoint) ArrayList(java.util.ArrayList) Date(java.util.Date) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) ProvisioningException(org.guvnor.ala.exceptions.ProvisioningException) HostConfig(com.spotify.docker.client.messages.HostConfig) DockerRuntimeState(org.guvnor.ala.docker.model.DockerRuntimeState) ArrayList(java.util.ArrayList) List(java.util.List) DockerRuntimeInfo(org.guvnor.ala.docker.model.DockerRuntimeInfo)

Example 2 with DockerException

use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.

the class DockerRuntimeManager method pause.

@Override
public void pause(RuntimeId runtimeId) throws RuntimeOperationException {
    DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
    try {
        docker.getDockerClient(runtime.getProviderId()).pauseContainer(runtime.getId());
        refresh(runtimeId);
    } catch (DockerException | InterruptedException ex) {
        LOG.error("Error Pausing container: " + runtimeId.getId(), ex);
        throw new RuntimeOperationException("Error Pausing container: " + runtimeId.getId(), ex);
    }
}
Also used : DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) DockerException(com.spotify.docker.client.DockerException) RuntimeOperationException(org.guvnor.ala.exceptions.RuntimeOperationException)

Example 3 with DockerException

use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.

the class DockerRuntimeManager method refresh.

@Override
public void refresh(RuntimeId runtimeId) throws RuntimeOperationException {
    DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
    try {
        ContainerInfo containerInfo = docker.getDockerClient(runtime.getProviderId()).inspectContainer(runtime.getId());
        ContainerState state = containerInfo.state();
        String stateString = STOPPED;
        if (state.running() && !state.paused()) {
            stateString = RUNNING;
        } else if (state.paused()) {
            stateString = "Paused";
        } else if (state.restarting()) {
            stateString = "Restarting";
        } else if (state.oomKilled()) {
            stateString = "Killed";
        }
        DockerRuntime newRuntime = new DockerRuntime(runtime.getId(), runtime.getName(), runtime.getConfig(), runtime.getProviderId(), runtime.getEndpoint(), runtime.getInfo(), new DockerRuntimeState(stateString, state.startedAt().toString()));
        runtimeRegistry.registerRuntime(newRuntime);
    } catch (DockerException | InterruptedException ex) {
        LOG.error("Error Refreshing container: " + runtimeId.getId(), ex);
        throw new RuntimeOperationException("Error Refreshing container: " + runtimeId.getId(), ex);
    }
}
Also used : DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) DockerException(com.spotify.docker.client.DockerException) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) DockerRuntimeState(org.guvnor.ala.docker.model.DockerRuntimeState) RuntimeOperationException(org.guvnor.ala.exceptions.RuntimeOperationException) ContainerState(com.spotify.docker.client.messages.ContainerState)

Example 4 with DockerException

use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.

the class DockerRuntimeManager method stop.

@Override
public void stop(RuntimeId runtimeId) throws RuntimeOperationException {
    DockerRuntime runtime = (DockerRuntime) runtimeRegistry.getRuntimeById(runtimeId.getId());
    try {
        LOG.info("Stopping container: " + runtimeId.getId());
        docker.getDockerClient(runtime.getProviderId()).stopContainer(runtime.getId(), 1);
        refresh(runtimeId);
    } catch (DockerException | InterruptedException ex) {
        LOG.error("Error Stopping container: " + runtimeId.getId(), ex);
        throw new RuntimeOperationException("Error Stopping container: " + runtimeId.getId(), ex);
    }
}
Also used : DockerRuntime(org.guvnor.ala.docker.model.DockerRuntime) DockerException(com.spotify.docker.client.DockerException) RuntimeOperationException(org.guvnor.ala.exceptions.RuntimeOperationException)

Example 5 with DockerException

use of com.spotify.docker.client.DockerException in project kie-wb-common by kiegroup.

the class DockerAccessInterfaceImpl method buildClient.

private DockerClient buildClient(final ProviderId providerId) throws DockerException, InterruptedException {
    DefaultDockerClient dockerClient;
    if (providerId.getId().equals("local")) {
        try {
            if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                dockerClient = DefaultDockerClient.builder().uri(DefaultDockerClient.DEFAULT_UNIX_ENDPOINT).build();
                // This test the docker client connection to see if the client was built properly
                Info info = dockerClient.info();
                LOG.info("Connected to Docker Client Info: " + info);
                return dockerClient;
            }
            try {
                dockerClient = DefaultDockerClient.fromEnv().build();
                Info info = dockerClient.info();
                LOG.info("Connected to Docker Client Info: " + info);
                return dockerClient;
            } catch (DockerCertificateException ex) {
                throw new RuntimeException(ex);
            }
        } catch (DockerException | InterruptedException ex) {
            try {
                dockerClient = DefaultDockerClient.fromEnv().build();
                Info info = dockerClient.info();
                LOG.info("Connected to Docker Client Info: " + info);
                return dockerClient;
            } catch (DockerCertificateException e) {
                throw new RuntimeException(e);
            }
        }
    }
    throw new RuntimeException("Couldn't create Docker Client, for providerId = " + providerId.getId());
}
Also used : DockerException(com.spotify.docker.client.DockerException) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) DockerCertificateException(com.spotify.docker.client.DockerCertificateException) Info(com.spotify.docker.client.messages.Info)

Aggregations

DockerException (com.spotify.docker.client.DockerException)7 DockerRuntime (org.guvnor.ala.docker.model.DockerRuntime)6 RuntimeOperationException (org.guvnor.ala.exceptions.RuntimeOperationException)5 DockerRuntimeState (org.guvnor.ala.docker.model.DockerRuntimeState)2 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)1 DockerCertificateException (com.spotify.docker.client.DockerCertificateException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)1 ContainerState (com.spotify.docker.client.messages.ContainerState)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 Info (com.spotify.docker.client.messages.Info)1 PortBinding (com.spotify.docker.client.messages.PortBinding)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 DockerProvider (org.guvnor.ala.docker.model.DockerProvider)1 DockerRuntimeEndpoint (org.guvnor.ala.docker.model.DockerRuntimeEndpoint)1 DockerRuntimeInfo (org.guvnor.ala.docker.model.DockerRuntimeInfo)1