|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2021 Eclipse Linux Tools project committers and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + ********************************************************************************/ |
| 11 | + |
| 12 | +package org.eclipse.linuxtools.internal.docker.core; |
| 13 | + |
| 14 | +import org.eclipse.linuxtools.docker.core.DockerException; |
| 15 | +import org.eclipse.linuxtools.docker.core.IDockerConnection; |
| 16 | +import org.eclipse.linuxtools.docker.core.IDockerContainerConfig; |
| 17 | +import org.eclipse.linuxtools.docker.core.IDockerHostConfig; |
| 18 | + |
| 19 | +/** |
| 20 | + * This creates a container that implements AutoColosable. Thus allowing to use |
| 21 | + * it in a try statement. Special care is taken that the container is removed |
| 22 | + * eventually (currently 3600s), even if eclipse crashes. |
| 23 | + */ |
| 24 | +public class CloseableContainer implements AutoCloseable { |
| 25 | + |
| 26 | + public final String containerId; |
| 27 | + private final IDockerConnection connection; |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a closable container, that is removed after leaving the try-block. |
| 31 | + * |
| 32 | + * @param connection |
| 33 | + * The docker-connection to use |
| 34 | + * @param image |
| 35 | + * The image name to use |
| 36 | + * @throws DockerException |
| 37 | + * if an docker related error occurs |
| 38 | + * @throws InterruptedException |
| 39 | + * if the thread was interrupted |
| 40 | + */ |
| 41 | + public CloseableContainer(IDockerConnection connection, String image) |
| 42 | + throws DockerException, InterruptedException { |
| 43 | + // Create base container to use for copying |
| 44 | + // It must be running, to execute ls & co |
| 45 | + |
| 46 | + this.connection = connection; |
| 47 | + DockerContainerConfig.Builder builder = new DockerContainerConfig.Builder().cmd("sleep 3600") //$NON-NLS-1$ |
| 48 | + .image(image); |
| 49 | + IDockerContainerConfig config = builder.build(); |
| 50 | + DockerHostConfig.Builder hostBuilder = new DockerHostConfig.Builder(); |
| 51 | + // Remove the container after usage. |
| 52 | + hostBuilder.autoRemove(true); |
| 53 | + IDockerHostConfig hostConfig = hostBuilder.build(); |
| 54 | + containerId = connection.createContainer(config, hostConfig, null); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Start the Container |
| 60 | + * |
| 61 | + * @throws DockerException |
| 62 | + * if an docker related error occurs |
| 63 | + * @throws InterruptedException |
| 64 | + * if the thread was interrupted |
| 65 | + */ |
| 66 | + public void start() throws DockerException, InterruptedException { |
| 67 | + connection.startContainer(containerId, null); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Close the container, by killing it. Remove it afterwards, just in case. |
| 72 | + * setting it to autoremove in the constructor should do the job, though. |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void close() throws DockerException, InterruptedException { |
| 76 | + try { |
| 77 | + connection.killContainer(containerId); |
| 78 | + } catch (Exception e) { |
| 79 | + } |
| 80 | + connection.removeContainer(containerId); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments