Search in sources :

Example 11 with ProgressMessage

use of com.spotify.docker.client.messages.ProgressMessage in project docker-client by spotify.

the class DefaultDockerClientTest method testBuildNoTimeout.

@Test
public void testBuildNoTimeout() throws Exception {
    // The Dockerfile specifies a sleep of 10s during the build
    // Returned image id is last piece of output, so this confirms stream did not timeout
    final Path dockerDirectory = getResource("dockerDirectorySleeping");
    final String returnedImageId = sut.build(dockerDirectory, "test", new ProgressHandler() {

        @Override
        public void progress(ProgressMessage message) throws DockerException {
            log.info(message.stream());
        }
    }, BuildParam.noCache());
    assertTrue(returnedImageId != null);
}
Also used : Path(java.nio.file.Path) DockerException(com.spotify.docker.client.exceptions.DockerException) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) Long.toHexString(java.lang.Long.toHexString) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 12 with ProgressMessage

use of com.spotify.docker.client.messages.ProgressMessage in project zeppelin by apache.

the class DockerInterpreterProcess method start.

@Override
public void start(String userName) throws IOException {
    docker = DefaultDockerClient.builder().uri(URI.create(DOCKER_HOST)).build();
    removeExistContainer(containerName);
    final Map<String, List<PortBinding>> portBindings = new HashMap<>();
    // Bind container ports to host ports
    int intpServicePort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
    this.dockerIntpServicePort = String.valueOf(intpServicePort);
    final String[] ports = { dockerIntpServicePort };
    for (String port : ports) {
        List<PortBinding> hostPorts = new ArrayList<>();
        hostPorts.add(PortBinding.of("0.0.0.0", port));
        portBindings.put(port, hostPorts);
    }
    final HostConfig hostConfig = HostConfig.builder().networkMode("host").portBindings(portBindings).build();
    DockerSpecTemplate specTemplate = new DockerSpecTemplate();
    specTemplate.loadProperties(getTemplateBindings());
    URL urlTemplate = this.getClass().getResource(DOCKER_INTP_JINJA);
    String template = Resources.toString(urlTemplate, StandardCharsets.UTF_8);
    String dockerCommand = specTemplate.render(template);
    int firstLineIsNewline = dockerCommand.indexOf("\n");
    if (firstLineIsNewline == 0) {
        dockerCommand = dockerCommand.replaceFirst("\n", "");
    }
    LOGGER.info("dockerCommand = {}", dockerCommand);
    List<String> listEnv = getListEnvs();
    LOGGER.info("docker listEnv = {}", listEnv);
    // check if the interpreter process exit script
    // if interpreter process exit, then container need exit
    StringBuilder sbStartCmd = new StringBuilder();
    sbStartCmd.append("sleep 10; ");
    sbStartCmd.append("process=RemoteInterpreterServer; ");
    sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
    sbStartCmd.append("while [ ! -z \"$RUNNING_PIDS\" ]; ");
    sbStartCmd.append("do sleep 1; ");
    sbStartCmd.append("RUNNING_PIDS=$(ps x | grep $process | grep -v grep | awk '{print $1}'); ");
    sbStartCmd.append("done");
    // Create container with exposed ports
    final ContainerConfig containerConfig = ContainerConfig.builder().hostConfig(hostConfig).hostname(this.intpEventServerHost).image(containerImage).workingDir("/").env(listEnv).cmd("sh", "-c", sbStartCmd.toString()).build();
    try {
        LOGGER.info("wait docker pull image {} ...", containerImage);
        docker.pull(containerImage, new ProgressHandler() {

            @Override
            public void progress(ProgressMessage message) throws DockerException {
                if (null != message.error()) {
                    LOGGER.error(message.toString());
                }
            }
        });
        final ContainerCreation containerCreation = docker.createContainer(containerConfig, containerName);
        this.containerId = containerCreation.id();
        // Start container
        docker.startContainer(containerId);
        copyRunFileToContainer(containerId);
        execInContainer(containerId, dockerCommand, false);
    } catch (DockerException e) {
        LOGGER.error(e.getMessage(), e);
        throw new IOException(e.getMessage());
    } catch (InterruptedException e) {
        LOGGER.error(e.getMessage(), e);
        throw new IOException(e.getMessage());
    }
    long startTime = System.currentTimeMillis();
    // wait until interpreter send dockerStarted message through thrift rpc
    synchronized (dockerStarted) {
        if (!dockerStarted.get()) {
            try {
                dockerStarted.wait(getConnectTimeout());
            } catch (InterruptedException e) {
                LOGGER.error("Remote interpreter is not accessible");
                throw new IOException(e.getMessage());
            }
        }
    }
    if (!dockerStarted.get()) {
        LOGGER.info("Interpreter docker creation is time out in {} seconds", getConnectTimeout() / 1000);
    }
    // waits for interpreter thrift rpc server ready
    while (System.currentTimeMillis() - startTime < getConnectTimeout()) {
        if (RemoteInterpreterUtils.checkIfRemoteEndpointAccessible(getHost(), getPort())) {
            break;
        } else {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
}
Also used : DockerException(com.spotify.docker.client.exceptions.DockerException) ProgressMessage(com.spotify.docker.client.messages.ProgressMessage) HashMap(java.util.HashMap) ProgressHandler(com.spotify.docker.client.ProgressHandler) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) PortBinding(com.spotify.docker.client.messages.PortBinding) HostConfig(com.spotify.docker.client.messages.HostConfig) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ProgressMessage (com.spotify.docker.client.messages.ProgressMessage)12 Test (org.junit.Test)10 DockerException (com.spotify.docker.client.exceptions.DockerException)9 Long.toHexString (java.lang.Long.toHexString)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)8 Path (java.nio.file.Path)7 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 InputStream (java.io.InputStream)2 ProgressHandler (com.spotify.docker.client.ProgressHandler)1 FixedRegistryAuthSupplier (com.spotify.docker.client.auth.FixedRegistryAuthSupplier)1 ImageNotFoundException (com.spotify.docker.client.exceptions.ImageNotFoundException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 PortBinding (com.spotify.docker.client.messages.PortBinding)1 RegistryAuth (com.spotify.docker.client.messages.RegistryAuth)1