diff options
Diffstat (limited to 'containers/org.eclipse.linuxtools.docker.core/src/org/eclipse')
7 files changed, 152 insertions, 9 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/DockerConnectionManager.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/DockerConnectionManager.java index 8b46f1f6c2..99c8bca7a1 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/DockerConnectionManager.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/DockerConnectionManager.java @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.linuxtools.docker.core; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.eclipse.core.runtime.IStatus; @@ -72,6 +74,17 @@ public class DockerConnectionManager { return connections.toArray(new IDockerConnection[connections.size()]); } + /** + * @return an immutable {@link List} of the {@link IDockerConnection} names + */ + public List<String> getConnectionNames() { + final List<String> connectionNames = new ArrayList<>(); + for (IDockerConnection connection : this.connections) { + connectionNames.add(connection.getName()); + } + return Collections.unmodifiableList(connectionNames); + } + public IDockerConnection findConnection(final String name) { if (name != null) { for (IDockerConnection connection : connections) { diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerConnection.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerConnection.java index ca6d18d5be..939b615b1c 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerConnection.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerConnection.java @@ -13,6 +13,7 @@ package org.eclipse.linuxtools.docker.core; import java.io.OutputStream; import java.util.Collections; import java.util.List; +import java.util.Map; import org.eclipse.core.runtime.IPath; import org.eclipse.linuxtools.internal.docker.core.DockerContainerRefreshManager; @@ -199,6 +200,28 @@ public interface IDockerConnection { String createContainer(IDockerContainerConfig c, IDockerHostConfig hc) throws DockerException, InterruptedException; + /** + * Builds an {@link IDockerImage} + * + * @param path + * path to the build context + * @param name + * optional name and tag of the image to build + * @param handler + * progress handler + * @param buildOptions + * build options + * @return the id of the {@link IDockerImage} that was build + * @throws DockerException + * if building image failed + * @throws InterruptedException + * if the thread was interrupted + */ + String buildImage(final IPath path, final String name, + final IDockerProgressHandler handler, + final Map<String, Object> buildOptions) + throws DockerException, InterruptedException; + public String createContainer(final IDockerContainerConfig config, final IDockerHostConfig hc, final String containerName) throws DockerException, InterruptedException; diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerContainerListener.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerContainerListener.java index 5b9b89be94..063272708c 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerContainerListener.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerContainerListener.java @@ -21,9 +21,10 @@ public interface IDockerContainerListener { * * @param connection * - the Docker connection - * @param list - * - the new list of {@link IDockerContainer} + * @param containers + * the new list of containers */ - void listChanged(IDockerConnection connection, List<IDockerContainer> list); + void listChanged(IDockerConnection connection, + List<IDockerContainer> containers); } diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageBuildOptions.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageBuildOptions.java new file mode 100644 index 0000000000..240d0dfaf2 --- /dev/null +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageBuildOptions.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - Initial Contribution + *******************************************************************************/ + +package org.eclipse.linuxtools.docker.core; + +/** + * Name of options passed to the Docker daemon to build an {@link IDockerImage} + * + */ +public interface IDockerImageBuildOptions { + + /** name of the connection to use to build the Image. */ + public final static String DOCKER_CONNECTION = "dockerConnection"; //$NON-NLS-1$ + + /** repo and optionally tag of Image to build. */ + public final static String REPO_NAME = "repoName"; //$NON-NLS-1$ + + /** quiet build option. */ + public static final String QUIET_BUILD = "quietBuild"; //$NON-NLS-1$ + + /** no cache option. */ + public static final String NO_CACHE = "noCache"; //$NON-NLS-1$ + + /** remove intermediate containers option on successful build. */ + public static final String RM_INTERMEDIATE_CONTAINERS = "rm"; //$NON-NLS-1$ + + /** always remove intermediate containers option. */ + public static final String FORCE_RM_INTERMEDIATE_CONTAINERS = "forcerm"; //$NON-NLS-1$ + +} diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageListener.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageListener.java index f01bd7951d..1ec64e747b 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageListener.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/IDockerImageListener.java @@ -18,11 +18,12 @@ public interface IDockerImageListener { * Called when the list of {@link IDockerImage} for the given * {@link IDockerConnection} changed * - * @param manager - * - the Docker connection + * @param connection + * the {@link IDockerConnection} in which the list of + * {@link IDockerImage} changed * @param images - * - the new list of {@link IDockerImage} + * the new list of {@link IDockerImage} */ - void listChanged(IDockerConnection manager, List<IDockerImage> images); + void listChanged(IDockerConnection connection, List<IDockerImage> images); } diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java index a67f905e5e..6212ce1904 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java @@ -54,6 +54,7 @@ import org.eclipse.linuxtools.docker.core.IDockerContainerInfo; import org.eclipse.linuxtools.docker.core.IDockerContainerListener; import org.eclipse.linuxtools.docker.core.IDockerHostConfig; import org.eclipse.linuxtools.docker.core.IDockerImage; +import org.eclipse.linuxtools.docker.core.IDockerImageBuildOptions; import org.eclipse.linuxtools.docker.core.IDockerImageInfo; import org.eclipse.linuxtools.docker.core.IDockerImageListener; import org.eclipse.linuxtools.docker.core.IDockerImageSearchResult; @@ -853,11 +854,63 @@ public class DockerConnection implements IDockerConnection, Closeable { } } + @Override + public String buildImage(final IPath path, final String name, + final IDockerProgressHandler handler, + final Map<String, Object> buildOptions) + throws DockerException, InterruptedException { + try { + final DockerProgressHandler d = new DockerProgressHandler(handler); + final java.nio.file.Path p = FileSystems.getDefault() + .getPath(path.makeAbsolute().toOSString()); + return client.build(p, name, d, getBuildParameters(buildOptions)); + } catch (com.spotify.docker.client.DockerRequestException e) { + throw new DockerException(e.message()); + } catch (com.spotify.docker.client.DockerException | IOException e) { + DockerException f = new DockerException(e); + throw f; + } + } + + /** + * Converts the given {@link Map} of build options into an array of + * {@link BuildParameter} when the build options are set a value different from the default value. + * + * @param buildOptions + * the build options + * @return an array of relevant {@link BuildParameter} + */ + private BuildParameter[] getBuildParameters( + final Map<String, Object> buildOptions) { + final List<BuildParameter> buildParameters = new ArrayList<>(); + for (Entry<String, Object> entry : buildOptions.entrySet()) { + final Object optionName = entry.getKey(); + final Object optionValue = entry.getValue(); + + if (optionName.equals(IDockerImageBuildOptions.QUIET_BUILD) + && optionValue.equals(true)) { + buildParameters.add(BuildParameter.QUIET); + } else if (optionName.equals(IDockerImageBuildOptions.NO_CACHE) + && optionValue.equals(true)) { + buildParameters.add(BuildParameter.NO_CACHE); + } else if (optionName + .equals(IDockerImageBuildOptions.RM_INTERMEDIATE_CONTAINERS) + && optionValue.equals(false)) { + buildParameters.add(BuildParameter.NO_RM); + } else if (optionName + .equals(IDockerImageBuildOptions.FORCE_RM_INTERMEDIATE_CONTAINERS) + && optionValue.equals(true)) { + buildParameters.add(BuildParameter.FORCE_RM); + } + } + return buildParameters.toArray(new BuildParameter[0]); + } + public void save() { // Currently we have to save all clouds instead of just this one DockerConnectionManager.getInstance().saveConnections(); } - + @Override @Deprecated public String createContainer(IDockerContainerConfig c) diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerImage.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerImage.java index a29a124d1d..47a9233135 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerImage.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerImage.java @@ -17,17 +17,31 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import org.eclipse.linuxtools.docker.core.IDockerImage; public class DockerImage implements IDockerImage { + private static final String REGISTRY_HOST = "[a-zA-Z0-9]+([\\._-][a-zA-Z0-9]+)*"; //$NON-NLS-1$ + private static final String REGISTRY_PORT = "[0-9]+"; //$NON-NLS-1$ + private static final String REPOSITORY = "[a-z0-9]+([\\._-][a-z0-9]+)*"; //$NON-NLS-1$ + private static final String NAME = "[a-z0-9]+([\\._-][a-z0-9]+)*"; //$NON-NLS-1$ + private static final String TAG = "[a-zA-Z0-9]+([\\._-][a-zA-Z0-9]+)*"; //$NON-NLS-1$ + + /** the image name pattern. */ + public static final Pattern imageNamePattern = Pattern.compile("(" //$NON-NLS-1$ + + REGISTRY_HOST + "\\:" + REGISTRY_PORT + "/)?" //$NON-NLS-1$ //$NON-NLS-2$ + + "((?<repository>" + REPOSITORY + ")/)?" //$NON-NLS-1$ //$NON-NLS-2$ + + "(?<name>" + NAME + ")" //$NON-NLS-1$ //$NON-NLS-2$ + + "(\\:(?<tag>" + TAG + "))?"); //$NON-NLS-1$ //$NON-NLS-2$ + // SimpleDateFormat is not thread-safe, so give one to each thread private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){ @Override protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd"); + return new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$ } }; |