Search in sources :

Example 1 with DockerRuntimeState

use of org.guvnor.ala.docker.model.DockerRuntimeState 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 DockerRuntimeState

use of org.guvnor.ala.docker.model.DockerRuntimeState 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)

Aggregations

DockerException (com.spotify.docker.client.DockerException)2 DockerRuntime (org.guvnor.ala.docker.model.DockerRuntime)2 DockerRuntimeState (org.guvnor.ala.docker.model.DockerRuntimeState)2 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 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 ProvisioningException (org.guvnor.ala.exceptions.ProvisioningException)1 RuntimeOperationException (org.guvnor.ala.exceptions.RuntimeOperationException)1