From f00af3a4ecc06858a3bacb73487e075348bfe0a2 Mon Sep 17 00:00:00 2001 From: Charles Tian Date: Fri, 9 Nov 2018 15:19:22 -0600 Subject: [PATCH 01/10] Refactor docker functionality to boost-common --- boost-common/pom.xml | 117 ++++---- .../boost/common/docker/AbstractDockerI.java | 47 ++++ .../boost/common/docker/DockerBuildI.java | 113 ++++++++ .../docker/DockerLoggingProgressHandler.java | 20 +- .../boost/common/docker/DockerPushI.java | 47 ++++ .../common/docker/dockerizer/Dockerizer.java | 139 ++++++++++ .../spring/DockerizeLibertySpringBootJar.java | 17 +- .../spring/DockerizeSpringBootClasspath.java | 56 ++++ .../spring/DockerizeSpringBootJar.java | 54 ++++ .../dockerizer/spring/SpringDockerizer.java | 52 ++-- boost-maven/boost-maven-plugin/pom.xml | 11 - .../boost/docker/DockerBuildMojo.java | 252 ------------------ .../boost/docker/DockerPushMojo.java | 74 ----- .../boost/docker/dockerizer/Dockerizer.java | 152 ----------- .../spring/DockerizeSpringBootClasspath.java | 56 ---- .../spring/DockerizeSpringBootJar.java | 54 ---- .../boost/{ => maven}/BoosterPacksParent.java | 2 +- .../JAXRSBoosterPackConfigurator.java | 2 +- .../JDBCBoosterPackConfigurator.java | 2 +- .../docker/AbstractDockerMojo.java | 55 +--- .../boost/maven/docker/DockerBuildMojo.java | 168 ++++++++++++ .../boost/maven/docker/DockerPushMojo.java | 33 +++ .../docker/MavenSettingsAuthSupplier.java | 2 +- .../liberty/AbstractLibertyMojo.java | 2 +- .../{ => maven}/liberty/LibertyDebugMojo.java | 2 +- .../liberty/LibertyPackageMojo.java | 8 +- .../{ => maven}/liberty/LibertyRunMojo.java | 2 +- .../{ => maven}/liberty/LibertyStartMojo.java | 2 +- .../{ => maven}/liberty/LibertyStopMojo.java | 2 +- .../boost/{ => maven}/utils/BoostLogger.java | 2 +- .../{ => maven}/utils/MavenProjectUtil.java | 2 +- .../boost/docker/DockerRepositoryTests.java | 14 +- 32 files changed, 793 insertions(+), 768 deletions(-) create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java rename {boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost => boost-common/src/main/java/io/openliberty/boost/common}/docker/DockerLoggingProgressHandler.java (65%) create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java rename {boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost => boost-common/src/main/java/io/openliberty/boost/common}/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java (86%) create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootClasspath.java create mode 100644 boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootJar.java rename {boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost => boost-common/src/main/java/io/openliberty/boost/common}/docker/dockerizer/spring/SpringDockerizer.java (56%) delete mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerBuildMojo.java delete mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerPushMojo.java delete mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/Dockerizer.java delete mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootClasspath.java delete mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootJar.java rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/BoosterPacksParent.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/JAXRSBoosterPackConfigurator.java (97%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/JDBCBoosterPackConfigurator.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/docker/AbstractDockerMojo.java (65%) create mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java create mode 100644 boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerPushMojo.java rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/docker/MavenSettingsAuthSupplier.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/AbstractLibertyMojo.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/LibertyDebugMojo.java (97%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/LibertyPackageMojo.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/LibertyRunMojo.java (97%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/LibertyStartMojo.java (98%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/liberty/LibertyStopMojo.java (97%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/utils/BoostLogger.java (97%) rename boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/{ => maven}/utils/MavenProjectUtil.java (97%) diff --git a/boost-common/pom.xml b/boost-common/pom.xml index 6e187f24..f6db16d3 100644 --- a/boost-common/pom.xml +++ b/boost-common/pom.xml @@ -1,64 +1,75 @@ - - 4.0.0 + 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: + IBM Corporation - initial API and implementation --> + + 4.0.0 - io.openliberty.boost - boost-common - 0.1.2-SNAPSHOT + io.openliberty.boost + boost-common + 0.1.2-SNAPSHOT - https://github.com/OpenLiberty/boost-common + https://github.com/OpenLiberty/boost-common - - net.wasdev.maven.parent - parent - 1.4 - - + + net.wasdev.maven.parent + parent + 1.4 + + - - - Eclipse Public License 1.0 (EPL-1.0) - https://raw.github.com/OpenLiberty/boost/master/LICENSE - repo - - + + + Eclipse Public License 1.0 (EPL-1.0) + https://raw.github.com/OpenLiberty/boost/master/LICENSE + repo + + - - scm:git:git@github.com:OpenLiberty/boost-common.git - scm:git:git@github.com:OpenLiberty/boost-common.git - git@github.com:OpenLiberty/boost-common.git - HEAD - + + scm:git:git@github.com:OpenLiberty/boost-common.git + scm:git:git@github.com:OpenLiberty/boost-common.git + git@github.com:OpenLiberty/boost-common.git + HEAD + - - UTF-8 - UTF-8 - 1.8 - 1.8 - + + UTF-8 + UTF-8 + 1.8 + 1.8 + - - - junit - junit - 4.12 - test - - - commons-io - commons-io - 2.6 - - - net.wasdev.wlp.common - ci.common - 1.3.3 - - + + + com.spotify + docker-client + 8.11.7 + + + com.google.code.gson + gson + 2.8.0 + + + junit + junit + 4.12 + test + + + commons-io + commons-io + 2.6 + + + net.wasdev.wlp.common + ci.common + 1.3.3 + + diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java new file mode 100644 index 00000000..a504fcd5 --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java @@ -0,0 +1,47 @@ +package io.openliberty.boost.common.docker; + +import java.util.regex.Pattern; + +import com.spotify.docker.client.DefaultDockerClient; +import com.spotify.docker.client.DockerClient; +import com.spotify.docker.client.auth.RegistryAuthSupplier; +import com.spotify.docker.client.exceptions.DockerCertificateException; + +import io.openliberty.boost.common.BoostException; + +public interface AbstractDockerI { + + public void execute(DockerClient dockerClient) throws BoostException; + + public RegistryAuthSupplier createRegistryAuthSupplier() throws BoostException; + + // Default methods + + default public String getImageName(String repository, String tag) { + return repository + ":" + tag; + } + + default public boolean isTagValid(String tag) { + return Pattern.matches("[\\w][\\w.-]{0,127}", tag); + } + + default public boolean isRepositoryValid(String repository) { + String nameRegExp = "[a-z0-9]+((?:[._]|__|[-]*)[a-z0-9]+)*?"; + String domain = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])"; + String domainRegExp = domain + "(\\." + domain + ")*?" + "(:[0-9]+)?"; + + String repositoryRegExp = "(" + domainRegExp + "\\/)?" + nameRegExp + "(\\/" + nameRegExp + ")*?"; + + return Pattern.matches(repositoryRegExp, repository); + } + + default public DockerClient getDockerClient(boolean useProxy) throws BoostException { + final RegistryAuthSupplier authSupplier = createRegistryAuthSupplier(); + try { + return DefaultDockerClient.fromEnv().registryAuthSupplier(authSupplier).useProxy(useProxy).build(); + } catch (DockerCertificateException e) { + throw new BoostException("Problem loading Docker certificates", e); + } + } + +} diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java new file mode 100644 index 00000000..e4c10e54 --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java @@ -0,0 +1,113 @@ +package io.openliberty.boost.common.docker; + +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Map; + +import com.google.gson.Gson; +import com.spotify.docker.client.DockerClient; +import com.spotify.docker.client.DockerClient.BuildParam; +import com.spotify.docker.client.exceptions.DockerException; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; +import io.openliberty.boost.common.docker.dockerizer.spring.DockerizeLibertySpringBootJar; +import io.openliberty.boost.common.docker.dockerizer.spring.DockerizeSpringBootClasspath; +import io.openliberty.boost.common.docker.dockerizer.spring.DockerizeSpringBootJar; +import io.openliberty.boost.common.docker.dockerizer.spring.SpringDockerizer; + +public abstract interface DockerBuildI extends AbstractDockerI { + + public File getAppArchive() throws BoostException; + + // Default methods + + default public SpringDockerizer getDockerizer(String dockerizer, File projectDirectory, File outputDirectory, File appArchive, String springBootVersion, BoostLoggerI log) { + + // TODO: Needed future enhancements: + // 1. Is it Spring or something else? sense with + // MavenProjectUtil.findSpringBootVersion(project); + // 2. Use OpenJ9 or HotSpot? sense with property boost.docker.jvm + if ("jar".equalsIgnoreCase(dockerizer)) { + return new DockerizeSpringBootJar(projectDirectory, outputDirectory, appArchive, springBootVersion, log); + } + if ("classpath".equalsIgnoreCase(dockerizer)) { + return new DockerizeSpringBootClasspath(projectDirectory, outputDirectory, appArchive, springBootVersion, + log); + } + // TODO: Maybe don't make the Spring Boot dockerizer default after EE stuff is + // added + // The current property values of 'jar', 'classpath' and 'liberty' are + // intentionally + // generic so that they can be applied irrespective of the project type (Spring + // vs EE) + return new DockerizeLibertySpringBootJar(projectDirectory, outputDirectory, appArchive, springBootVersion, log); + } + + /** + * Use the DockerClient to build the image + * + */ + default public void buildDockerImage(Path baseDir, DockerClient dockerClient, SpringDockerizer dockerizer, boolean pullNewerImage, + boolean noCache, Map buildArgs, String repository, String tag, BoostLoggerI log) + throws BoostException, IOException { + final DockerLoggingProgressHandler progressHandler = new DockerLoggingProgressHandler(log); + final String imageName = getImageName(repository, tag); + BuildParam[] buidParams = getBuildParams(dockerizer, pullNewerImage, noCache, buildArgs); + log.info(""); // Adding empty log for formatting purpose + log.info("Building image: " + imageName); + try { + dockerClient.build(baseDir, imageName, progressHandler, buidParams); + } catch (DockerException | InterruptedException e) { + throw new BoostException("Unable to build image", e); + } + } + + default BuildParam[] getBuildParams(SpringDockerizer dockerizer, boolean pullNewerImage, boolean noCache, + Map buildArgs) throws BoostException { + final ArrayList buildParamsList = new ArrayList<>(); + final BuildParam[] buildParams; + if (pullNewerImage) { + buildParamsList.add(BuildParam.pullNewerImage()); + } + if (noCache) { + buildParamsList.add(BuildParam.noCache()); + } + + buildArgs.putAll(dockerizer.getBuildArgs()); + + try { + final String encodedBuildArgs = URLEncoder.encode(new Gson().toJson(buildArgs), "utf-8"); + buildParamsList.add(new BuildParam("buildargs", encodedBuildArgs)); + } catch (UnsupportedEncodingException e) { + throw new BoostException("Unable to build image", e); + } + + buildParams = buildParamsList.toArray(new BuildParam[buildParamsList.size()]); + return buildParams; + } + + /** + * Get the artifact path + * + * @param artifact + * @return the canonical path, if it can be obtained successfully, otherwise the + * absolute path + */ + default String getPathMessageText(File artifact) { + String retVal = null; + try { + if (artifact != null) { + retVal = artifact.getCanonicalPath(); + } + } catch (IOException ioexc) { + retVal = artifact.getAbsolutePath(); + } + return retVal; + } + +} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerLoggingProgressHandler.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java similarity index 65% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerLoggingProgressHandler.java rename to boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java index 0a8fdfd4..1f9d6d75 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerLoggingProgressHandler.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java @@ -1,28 +1,18 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker; +package io.openliberty.boost.common.docker; import java.text.MessageFormat; -import org.apache.maven.plugin.logging.Log; - import com.spotify.docker.client.ProgressHandler; import com.spotify.docker.client.exceptions.DockerException; import com.spotify.docker.client.messages.ProgressMessage; +import io.openliberty.boost.common.BoostLoggerI; + public class DockerLoggingProgressHandler implements ProgressHandler { - private final Log log; + private final BoostLoggerI log; - public DockerLoggingProgressHandler(Log log) { + public DockerLoggingProgressHandler(BoostLoggerI log) { this.log = log; } diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java new file mode 100644 index 00000000..8cfd8709 --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java @@ -0,0 +1,47 @@ +package io.openliberty.boost.common.docker; + +import java.util.Scanner; + +import com.spotify.docker.client.DockerClient; +import com.spotify.docker.client.exceptions.DockerException; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; + +public interface DockerPushI extends AbstractDockerI { + + default public void dockerPush(DockerClient dockerClient, String artifactId, String repository, String tag, BoostLoggerI log) throws BoostException { + final DockerLoggingProgressHandler progressHandler = new DockerLoggingProgressHandler(log); + final String currentImage = getImageName(repository, tag); + String newImage = null; + + if (artifactId.equals(repository)) { + log.warn("Cannot push the image with the default repository name " + repository); + System.out.println("\nEnter the repository name in the format [REGISTRYHOST/][USERNAME/]NAME : "); + + @SuppressWarnings("resource") + Scanner in = new Scanner(System.in); + String newRepository = in.next(); + if (newRepository == null) { + throw new BoostException("The repository name cannot be null"); + } + if (!isRepositoryValid(newRepository)) { + throw new BoostException("The repository name is not valid."); + } + newImage = getImageName(newRepository, tag); + } + + try { + if (newImage != null) { + dockerClient.tag(currentImage, newImage); + log.info("Successfully tagged " + currentImage + " with " + newImage); + dockerClient.push(newImage, progressHandler); + } else { + dockerClient.push(currentImage, progressHandler); + } + } catch (DockerException | InterruptedException e) { + throw new BoostException("Unable to push image", e); + } + } + +} diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java new file mode 100644 index 00000000..9b6dbdd7 --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java @@ -0,0 +1,139 @@ +package io.openliberty.boost.common.docker.dockerizer; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; + +public abstract class Dockerizer { + + // TODO: Should we add a timestamp as part of this header? + public static final String BOOST_GEN = "# GENERATED BY LIBERTY BOOST PLUGIN"; + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + protected final File projectDirectory; + protected final File outputDirectory; + protected final File appArchive; + protected final BoostLoggerI log; + + public Dockerizer(File projectDirectory, File outputDirectory, File appArchive, BoostLoggerI log) { + this.projectDirectory = projectDirectory; + this.outputDirectory = outputDirectory; + this.appArchive = appArchive; + this.log = log; + } + + /** + * Construct the build arguments needed by the Dockerfile + * + * @throws Exception + */ + public abstract Map getBuildArgs(); + + /** + * Creates a new Dockerfile in the project directory if the Dockerfile doesn't + * exist. A warning message is displayed when the Dockerfile already exists. + * Existing Dockerfile is not modified and will be used to build the Docker + * image. + * + * @throws Exception + */ + public abstract List getDockerfileLines() throws BoostException; + + /** + * Creates a new .dockerignore file in the project directory if the + * .dockerignore file doesn't exist. Existing .dockerignore file will be + * appended with the lines generated by the plugin. Creating/appending of + * .dockerignore happens only when the plugin generates Dockerfile. + * + */ + public abstract List getDockerIgnoreList(); + + /** + * Creates a new Dockerfile in the project directory if the Dockerfile doesn't + * exist. A warning message is displayed when the Dockerfile already exists. + * Existing Dockerfile is not modified and will be used to build the Docker + * image. + * + * @throws Exception + */ + public abstract void createDockerFile() throws BoostException; + + // Dockerignore methods + + /** + * Creates a new .dockerignore file in the project directory if the + * .dockerignore file doesn't exist. Existing .dockerignore file will be + * appended with the lines generated by the plugin. Creating/appending of + * .dockerignore happens only when the plugin generates Dockerfile. + * + * @throws Exception + */ + public void createDockerIgnore() throws IOException { + File dockerIgnore = new File(projectDirectory, ".dockerignore"); + boolean dockerIgnoreAlreadyExists = false; + + if (isFileGeneratedByPlugin(new File(projectDirectory, "Dockerfile"))) { + try { + Files.createFile(dockerIgnore.toPath()); + log.info("Creating .dockerignore: " + dockerIgnore.getAbsolutePath()); + } catch (FileAlreadyExistsException e) { + log.warn("The .dockerignore file already exists"); + dockerIgnoreAlreadyExists = true; + } + writeDockerIgnore(dockerIgnore, dockerIgnoreAlreadyExists); + } + } + + private void writeDockerIgnore(File dockerIgnore, boolean dockerIgnoreAlreadyExists) throws IOException { + ArrayList lines = new ArrayList<>(); + if (isFileGeneratedByPlugin(dockerIgnore)) { + return; + } + + // Ignore all the log files in the project directory and ignore the liberty + // runtime folder in the target directory generated by `boost:package` goal + if (dockerIgnoreAlreadyExists) { + log.info("Adding lines to: " + dockerIgnore.getAbsolutePath()); + if (dockerIgnore.length() > 0) { + lines.add(LINE_SEPARATOR); + } + } + lines.add(BOOST_GEN); + lines.addAll(getDockerIgnoreList()); + + Files.write(dockerIgnore.toPath(), lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND); + } + + /** + * Checks if the file is generated by Liberty Boost plugin. + * + * @param file + * @return boolean + * @throws IOException + */ + private boolean isFileGeneratedByPlugin(File file) throws IOException { + try (FileReader fileReader = new FileReader(file)) { + try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { + String line; + while ((line = bufferedReader.readLine()) != null) { + if (line.startsWith(BOOST_GEN)) { + return true; + } + } + } + } + return false; + } + +} \ No newline at end of file diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java similarity index 86% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java rename to boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java index 7da88c47..cdfbb4d7 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.docker.dockerizer.spring; +package io.openliberty.boost.common.docker.dockerizer.spring; import java.io.File; import java.util.ArrayList; @@ -16,9 +16,8 @@ import java.util.List; import java.util.Map; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; public class DockerizeLibertySpringBootJar extends SpringDockerizer { @@ -32,8 +31,8 @@ public class DockerizeLibertySpringBootJar extends SpringDockerizer { private static final String COPY = "COPY "; private static final String RUN = "RUN "; - public DockerizeLibertySpringBootJar(MavenProject project, File appArchive, Log log) { - super(project, appArchive, log); + public DockerizeLibertySpringBootJar(File projectDirectory, File outputDirectory, File appArchive, String springBootVersion, BoostLoggerI log) { + super(projectDirectory, outputDirectory, appArchive, springBootVersion, log); } public Map getBuildArgs() { @@ -42,21 +41,21 @@ public Map getBuildArgs() { return buildArgs; } - private String getLibertySpringBootBaseImage() throws MojoExecutionException { + private String getLibertySpringBootBaseImage() throws BoostException { String libertyImage = null; if (springBootVersion.startsWith("1.")) { libertyImage = LIBERTY_IMAGE_1; } else if (springBootVersion.startsWith("2.")) { libertyImage = LIBERTY_IMAGE_2; } else { - throw new MojoExecutionException( + throw new BoostException( "No supporting docker image found for Open Liberty for the Spring Boot version " + springBootVersion); } return libertyImage; } - public List getDockerfileLines() throws MojoExecutionException { + public List getDockerfileLines() throws BoostException { String libertyImage = getLibertySpringBootBaseImage(); ArrayList lines = new ArrayList<>(); lines.add(BOOST_GEN); diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootClasspath.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootClasspath.java new file mode 100644 index 00000000..46a9264e --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootClasspath.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.common.docker.dockerizer.spring; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; + +public class DockerizeSpringBootClasspath extends SpringDockerizer { + + public DockerizeSpringBootClasspath(File projectDirectory, File outputDirectory, File appArchive, + String springBootVersion, BoostLoggerI log) { + super(projectDirectory, outputDirectory, appArchive, springBootVersion, log); + } + + public Map getBuildArgs() { + // Nothing at this time + return new HashMap(); + } + + @Override + public List getDockerfileLines() throws BoostException { + ArrayList lines = new ArrayList<>(); + lines.add(BOOST_GEN); + lines.add("FROM adoptopenjdk/openjdk8-openj9"); + lines.add("VOLUME /tmp"); + lines.add("ARG DEPENDENCY=target/dependency"); + lines.add("COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib"); + lines.add("COPY ${DEPENDENCY}/META-INF /app/META-INF"); + lines.add("COPY ${DEPENDENCY}/BOOT-INF/classes /app"); + lines.add("ENTRYPOINT [\"java\",\"-cp\",\"app:app/lib/*\",\"" + getSpringStartClass() + "\"]"); + return lines; + } + + @Override + public List getDockerIgnoreList() { + List lines = new ArrayList(); + lines.add("*.log"); + lines.add("target/liberty"); + return lines; + } + +} diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootJar.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootJar.java new file mode 100644 index 00000000..87c6777d --- /dev/null +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeSpringBootJar.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.common.docker.dockerizer.spring; + +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; + +public class DockerizeSpringBootJar extends SpringDockerizer { + + public DockerizeSpringBootJar(File projectDirectory, File outputDirectory, File appArchive, + String springBootVersion, BoostLoggerI log) { + super(projectDirectory, outputDirectory, appArchive, springBootVersion, log); + } + + public Map getBuildArgs() { + Map buildArgs = new HashMap(); + buildArgs.put("JAR_FILE", "target/" + appArchive.getName()); + return buildArgs; + } + + public List getDockerfileLines() throws BoostException { + ArrayList lines = new ArrayList<>(); + lines.add(BOOST_GEN); + lines.add("FROM adoptopenjdk/openjdk8-openj9"); + lines.add("VOLUME /tmp"); + lines.add("ARG JAR_FILE"); + lines.add("COPY ${JAR_FILE} app.jar"); + lines.add("ENTRYPOINT [\"java\",\"-Djava.security.egd=file:/dev/./urandom\",\"-jar\",\"/app.jar\"]"); + return lines; + } + + @Override + public List getDockerIgnoreList() { + List lines = new ArrayList(); + lines.add("*.log"); + lines.add("target/liberty"); + return lines; + } + +} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/SpringDockerizer.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java similarity index 56% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/SpringDockerizer.java rename to boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java index 8e7d3cea..9934e6e6 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/SpringDockerizer.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java @@ -1,14 +1,4 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker.dockerizer.spring; +package io.openliberty.boost.common.docker.dockerizer.spring; import java.io.File; import java.io.IOException; @@ -19,21 +9,19 @@ import java.util.jar.JarFile; import java.util.jar.Manifest; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; - -import io.openliberty.boost.docker.dockerizer.Dockerizer; +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; +import io.openliberty.boost.common.docker.dockerizer.Dockerizer; import io.openliberty.boost.common.utils.BoostUtil; -import io.openliberty.boost.utils.MavenProjectUtil; import net.wasdev.wlp.common.plugins.util.SpringBootUtil; public abstract class SpringDockerizer extends Dockerizer { + + public final String springBootVersion; - protected final String springBootVersion = MavenProjectUtil.findSpringBootVersion(project); - - public SpringDockerizer(MavenProject project, File appArchive, Log log) { - super(project, appArchive, log); + public SpringDockerizer(File projectDirectory, File outputDirectory, File appArchive, String springBootVersion, BoostLoggerI log) { + super(projectDirectory, outputDirectory, appArchive, log); + this.springBootVersion = springBootVersion; } /** @@ -43,7 +31,7 @@ public SpringDockerizer(MavenProject project, File appArchive, Log log) { * @throws Exception */ @Override - public void createDockerFile() throws MojoExecutionException { + public void createDockerFile() throws BoostException { if (BoostUtil.isNotNullOrEmpty(springBootVersion)) { if (SpringBootUtil.isSpringBootUberJar(appArchive)) { BoostUtil.extract(appArchive, projectDirectory); @@ -53,31 +41,31 @@ public void createDockerFile() throws MojoExecutionException { writeSpringBootDockerFile(dockerFile, startClass); } } else { - throw new MojoExecutionException(appArchive.getAbsolutePath() + " file is not an executable archive. " + throw new BoostException(appArchive.getAbsolutePath() + " file is not an executable archive. " + "The repackage goal of the spring-boot-maven-plugin must be configured to run first in order to create the required executable archive."); } } else { - throw new MojoExecutionException("Unable to create a Dockerfile because application type is not supported"); + throw new BoostException("Unable to create a Dockerfile because application type is not supported"); } } - protected String getSpringStartClass() throws MojoExecutionException { + protected String getSpringStartClass() throws BoostException { try (JarFile jarFile = new JarFile(appArchive)) { Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); return attributes.getValue("Start-Class"); } else { - throw new MojoExecutionException( + throw new BoostException( "Could not get Spring Boot start class due to error getting app manifest."); } } catch (IOException e) { - throw new MojoExecutionException("Could not get Spring Boot start class due to error opening app archive.", + throw new BoostException("Could not get Spring Boot start class due to error opening app archive.", e); } } - private File createNewDockerFile() throws MojoExecutionException { + private File createNewDockerFile() throws BoostException { try { File dockerFile = new File(projectDirectory, "Dockerfile"); Files.createFile(dockerFile.toPath()); @@ -87,16 +75,16 @@ private File createNewDockerFile() throws MojoExecutionException { log.warn("Dockerfile already exists"); return null; } catch (IOException e2) { - throw new MojoExecutionException("Could not create Dockerfile.", e2); + throw new BoostException("Could not create Dockerfile.", e2); } } - private void writeSpringBootDockerFile(File dockerFile, String startClass) throws MojoExecutionException { + private void writeSpringBootDockerFile(File dockerFile, String startClass) throws BoostException { try { Files.write(dockerFile.toPath(), getDockerfileLines(), Charset.forName("UTF-8")); } catch (IOException e) { - throw new MojoExecutionException("Could not write Spring Boot Dockerfile.", e); + throw new BoostException("Could not write Spring Boot Dockerfile.", e); } } -} +} \ No newline at end of file diff --git a/boost-maven/boost-maven-plugin/pom.xml b/boost-maven/boost-maven-plugin/pom.xml index 39012823..200a720b 100644 --- a/boost-maven/boost-maven-plugin/pom.xml +++ b/boost-maven/boost-maven-plugin/pom.xml @@ -32,11 +32,6 @@ - - com.google.guava - guava - 20.0 - org.apache.maven maven-plugin-api @@ -58,12 +53,6 @@ docker-client 8.11.7 - - com.google.code.gson - gson - 2.8.0 - - org.codehaus.mojo plugin-support diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerBuildMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerBuildMojo.java deleted file mode 100644 index 34b254e6..00000000 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerBuildMojo.java +++ /dev/null @@ -1,252 +0,0 @@ -/*- - ****************************************************************************** - * This file was copied from source: - * - * https://github.com/spotify/dockerfile-maven/blob/2fc613abcad667f6b99d1e99124c02cc7b1c6bbd/plugin/src/main/java/com/spotify/plugin/dockerfile/BuildMojo.java - * - * and modified below. - * - ****************************************************************************** - * ------------------- - * ORIGINAL COPYRIGHT: - * ------------------- - * - * -\-\- - * Dockerfile Maven Plugin - * -- - * Copyright (C) 2016 Spotify AB - * -- - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * -/-/- - ****************************************************************************** - */ - -/* - ****************************************************************************** - * We now include the copyright for our modification: - - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation (after modifications describe above) - ****************************************************************************** - */ -package io.openliberty.boost.docker; - -import java.io.File; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.Map; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.plugins.annotations.ResolutionScope; -import org.apache.maven.project.MavenProject; -import com.google.gson.Gson; -import com.spotify.docker.client.DockerClient; -import com.spotify.docker.client.DockerClient.BuildParam; -import com.spotify.docker.client.exceptions.DockerException; - -import io.openliberty.boost.common.BoostException; -import io.openliberty.boost.docker.dockerizer.Dockerizer; -import io.openliberty.boost.docker.dockerizer.spring.DockerizeLibertySpringBootJar; -import io.openliberty.boost.docker.dockerizer.spring.DockerizeSpringBootClasspath; -import io.openliberty.boost.docker.dockerizer.spring.DockerizeSpringBootJar; -import net.wasdev.wlp.maven.plugins.utils.SpringBootUtil; - -/** - * Builds a docker image from a packaged application. This goal will either - * generate a Dockerfile or use the existing Dockerfile if it already exists. An - * ignore file (.dockerignore) will be created if one does not exist, and if it - * does exist the same entries will be appended to the existing .dockerignore. - */ -@Mojo(name = "docker-build", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME) -public class DockerBuildMojo extends AbstractDockerMojo { - /** - * Pull newer version of the image, if set. Use the cache by default when - * building the image. - */ - @Parameter(property = "pullNewerImage", defaultValue = "false") - private boolean pullNewerImage; - - /** - * Do not use cache when building the image. - */ - @Parameter(property = "noCache", defaultValue = "false") - private boolean noCache; - - /** - * Set build time variables. - */ - @Parameter(property = "buildArgs") - private Map buildArgs; - - /** - * Sets the type of docker build to run. - */ - @Parameter(property = "dockerizer", defaultValue = "liberty") - private String dockerizer; - - @Override - protected void execute(DockerClient dockerClient) throws MojoExecutionException, MojoFailureException { - try { - File appArchive = getAppArchive(); - - // Create a Dockerfile for the application - Dockerizer dockerizer = getDockerizer(project, appArchive, log); - dockerizer.createDockerFile(); - dockerizer.createDockerIgnore(); - - buildDockerImage(dockerClient, dockerizer); - } catch (Exception e) { - throw new MojoExecutionException(e.getMessage(), e); - } - } - - /** - * Find the location of the Spring Boot Uber JAR - * - * @throws BoostException - */ - private File getAppArchive() throws BoostException { - File appArchive; - - // First try to get the Spring Boot Uber JAR as the project artifact as a result - // of the spring-boot-maven-plugin execution, handling classifier scenario if - // necessary. - appArchive = SpringBootUtil.getSpringBootUberJAR(project, getLog()); - if (appArchive != null) { - return appArchive; - } - - // If Boost replaced the project artifact, then the appArchive path will - // actually point to the Liberty Uber JAR. Check if this is the case and if so, - // use the .spring artifact that we preserved during the Boost packaging - // process. - - // Strictly speaking, the Liberty Uber JAR (in the "unboosted" SpringBoot Uber - // JAR location), is not needed to build the Docker image. However, let's throw - // an exception if we get here and it doesn't exist, assuming we've done - // something wrong. If it turns out we want to relax this later, we could. - File unboostedSpringBootUberJarLocation = net.wasdev.wlp.maven.plugins.utils.SpringBootUtil - .getSpringBootUberJARLocation(project, getLog()); - if (!unboostedSpringBootUberJarLocation.exists()) { - String excMsg = "Expected file does not exist at path = " - + getPathMessageText(unboostedSpringBootUberJarLocation) - + ". Make sure you have executed the spring-boot:repackage goal first before attempting the current goal."; - throw new BoostException(excMsg); - } - // Get the Boost location for the SpringBoot Uber JAR - appArchive = new File(io.openliberty.boost.common.utils.SpringBootUtil - .getBoostedSpringBootUberJarPath(unboostedSpringBootUberJarLocation)); - if (!appArchive.exists()) { - String excMsg = "Expected file does not exist at path = " + getPathMessageText(appArchive) - + ". Make sure you have executed the spring-boot:repackage goal first before attempting the current goal."; - throw new BoostException(excMsg); - } - if (net.wasdev.wlp.common.plugins.util.SpringBootUtil.isSpringBootUberJar(appArchive)) { - getLog().info("Found Spring Boot Uber JAR with .spring extension."); - return appArchive; - } - - // At this point we did not find the Spring Boot Uber JAR in any of its expected - // locations. - throw new BoostException("Could not find Spring Boot Uber JAR."); - } - - private Dockerizer getDockerizer(MavenProject project, File appArchive, Log log) { - // TODO: Needed future enhancements: - // 1. Is it Spring or something else? sense with MavenProjectUtil.findSpringBootVersion(project); - // 2. Use OpenJ9 or HotSpot? sense with property boost.docker.jvm - if ("jar".equalsIgnoreCase(dockerizer)) { - return new DockerizeSpringBootJar(project, appArchive, log); - } - if ("classpath".equalsIgnoreCase(dockerizer)) { - return new DockerizeSpringBootClasspath(project, appArchive, log); - } - // TODO: Maybe don't make the Spring Boot dockerizer default after EE stuff is added - // The current property values of 'jar', 'classpath' and 'liberty' are intentionally - // generic so that they can be applied irrespective of the project type (Spring vs EE) - return new DockerizeLibertySpringBootJar(project, appArchive, log); - } - - /** - * Use the DockerClient to build the image - * - */ - private void buildDockerImage(DockerClient dockerClient, Dockerizer dockerizer) - throws MojoExecutionException, IOException { - final DockerLoggingProgressHandler progressHandler = new DockerLoggingProgressHandler(log); - final String imageName = getImageName(); - BuildParam[] buidParams = getBuildParams(dockerizer); - log.info(""); // Adding empty log for formatting purpose - log.info("Building image: " + imageName); - try { - dockerClient.build(project.getBasedir().toPath(), imageName, progressHandler, buidParams); - } catch (DockerException | InterruptedException e) { - throw new MojoExecutionException("Unable to build image", e); - } - } - - private BuildParam[] getBuildParams(Dockerizer dockerizer) throws MojoExecutionException { - final ArrayList buildParamsList = new ArrayList<>(); - final BuildParam[] buildParams; - if (pullNewerImage) { - buildParamsList.add(BuildParam.pullNewerImage()); - } - if (noCache) { - buildParamsList.add(BuildParam.noCache()); - } - - buildArgs.putAll(dockerizer.getBuildArgs()); - - try { - final String encodedBuildArgs = URLEncoder.encode(new Gson().toJson(buildArgs), "utf-8"); - buildParamsList.add(new BuildParam("buildargs", encodedBuildArgs)); - } catch (UnsupportedEncodingException e) { - throw new MojoExecutionException("Unable to build image", e); - } - - buildParams = buildParamsList.toArray(new BuildParam[buildParamsList.size()]); - return buildParams; - } - - /** - * Get the artifact path - * - * @param artifact - * @return the canonical path, if it can be obtained successfully, otherwise the - * absolute path - */ - private static String getPathMessageText(File artifact) { - String retVal = null; - try { - if (artifact != null) { - retVal = artifact.getCanonicalPath(); - } - } catch (IOException ioexc) { - retVal = artifact.getAbsolutePath(); - } - return retVal; - } - -} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerPushMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerPushMojo.java deleted file mode 100644 index d3d6964f..00000000 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/DockerPushMojo.java +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker; - -import java.util.Scanner; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import com.spotify.docker.client.DockerClient; -import com.spotify.docker.client.exceptions.DockerException; - -/** - * Pushes a docker image to the docker repository. - * - */ -@Mojo(name = "docker-push", defaultPhase = LifecyclePhase.INSTALL) -public class DockerPushMojo extends AbstractDockerMojo { - - @Override - protected void execute(DockerClient dockerClient) throws MojoExecutionException, MojoFailureException { - pushDockerImage(dockerClient); - - } - - /** - * Use DockerClient to push the image - * - */ - private void pushDockerImage(DockerClient dockerClient) throws MojoExecutionException { - final DockerLoggingProgressHandler progressHandler = new DockerLoggingProgressHandler(log); - final String currentImage = getImageName(); - String newImage = null; - - if (project.getArtifactId().equals(repository)) { - log.warn("Cannot push the image with the default repository name " + repository); - System.out.println("\nEnter the repository name in the format [REGISTRYHOST/][USERNAME/]NAME : "); - - @SuppressWarnings("resource") - Scanner in = new Scanner(System.in); - String newRepository = in.next(); - if (newRepository == null) { - throw new MojoExecutionException("The repository name cannot be null"); - } - if (!isRepositoryValid(newRepository)) { - throw new MojoExecutionException("The repository name is not valid."); - } - newImage = getImageName(newRepository, tag); - } - - try { - if (newImage != null) { - dockerClient.tag(currentImage, newImage); - log.info("Successfully tagged " + currentImage + " with " + newImage); - dockerClient.push(newImage, progressHandler); - } else { - dockerClient.push(currentImage, progressHandler); - } - } catch (DockerException | InterruptedException e) { - throw new MojoExecutionException("Unable to push image", e); - } - - } - -} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/Dockerizer.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/Dockerizer.java deleted file mode 100644 index 1eb138a1..00000000 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/Dockerizer.java +++ /dev/null @@ -1,152 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker.dockerizer; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.file.FileAlreadyExistsException; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; - -public abstract class Dockerizer { - - // TODO: Should we add a timestamp as part of this header? - public static final String BOOST_GEN = "# GENERATED BY LIBERTY BOOST PLUGIN"; - public static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - protected final MavenProject project; - protected final File projectDirectory; - protected final File outputDirectory; - protected final File appArchive; - protected final Log log; - - public Dockerizer(MavenProject project, File appArchive, Log log) { - this.project = project; - this.projectDirectory = project.getBasedir(); - this.outputDirectory = new File(project.getBuild().getDirectory()); - this.appArchive = appArchive; - this.log = log; - } - - /** - * Construct the build arguments needed by the Dockerfile - * - * @throws Exception - */ - public abstract Map getBuildArgs(); - - /** - * Creates a new Dockerfile in the project directory if the Dockerfile doesn't - * exist. A warning message is displayed when the Dockerfile already exists. - * Existing Dockerfile is not modified and will be used to build the Docker - * image. - * - * @throws Exception - */ - public abstract List getDockerfileLines() throws MojoExecutionException; - - /** - * Creates a new .dockerignore file in the project directory if the - * .dockerignore file doesn't exist. Existing .dockerignore file will be - * appended with the lines generated by the plugin. Creating/appending of - * .dockerignore happens only when the plugin generates Dockerfile. - * - */ - public abstract List getDockerIgnoreList(); - - /** - * Creates a new Dockerfile in the project directory if the Dockerfile doesn't - * exist. A warning message is displayed when the Dockerfile already exists. - * Existing Dockerfile is not modified and will be used to build the Docker - * image. - * - * @throws Exception - */ - public abstract void createDockerFile() throws MojoExecutionException; - - // Dockerignore methods - - /** - * Creates a new .dockerignore file in the project directory if the - * .dockerignore file doesn't exist. Existing .dockerignore file will be - * appended with the lines generated by the plugin. Creating/appending of - * .dockerignore happens only when the plugin generates Dockerfile. - * - * @throws Exception - */ - public void createDockerIgnore() throws IOException { - File dockerIgnore = new File(projectDirectory, ".dockerignore"); - boolean dockerIgnoreAlreadyExists = false; - - if (isFileGeneratedByPlugin(new File(projectDirectory, "Dockerfile"))) { - try { - Files.createFile(dockerIgnore.toPath()); - log.info("Creating .dockerignore: " + dockerIgnore.getAbsolutePath()); - } catch (FileAlreadyExistsException e) { - log.warn("The .dockerignore file already exists"); - dockerIgnoreAlreadyExists = true; - } - writeDockerIgnore(dockerIgnore, dockerIgnoreAlreadyExists); - } - } - - private void writeDockerIgnore(File dockerIgnore, boolean dockerIgnoreAlreadyExists) throws IOException { - ArrayList lines = new ArrayList<>(); - if (isFileGeneratedByPlugin(dockerIgnore)) { - return; - } - - // Ignore all the log files in the project directory and ignore the liberty - // runtime folder in the target directory generated by `boost:package` goal - if (dockerIgnoreAlreadyExists) { - log.info("Adding lines to: " + dockerIgnore.getAbsolutePath()); - if (dockerIgnore.length() > 0) { - lines.add(LINE_SEPARATOR); - } - } - lines.add(BOOST_GEN); - lines.addAll(getDockerIgnoreList()); - - Files.write(dockerIgnore.toPath(), lines, Charset.forName("UTF-8"), StandardOpenOption.APPEND); - } - - /** - * Checks if the file is generated by Liberty Boost plugin. - * - * @param file - * @return boolean - * @throws IOException - */ - private boolean isFileGeneratedByPlugin(File file) throws IOException { - try (FileReader fileReader = new FileReader(file)) { - try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { - String line; - while ((line = bufferedReader.readLine()) != null) { - if (line.startsWith(BOOST_GEN)) { - return true; - } - } - } - } - return false; - } - -} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootClasspath.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootClasspath.java deleted file mode 100644 index a3358cf1..00000000 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootClasspath.java +++ /dev/null @@ -1,56 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker.dockerizer.spring; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; - -public class DockerizeSpringBootClasspath extends SpringDockerizer { - - public DockerizeSpringBootClasspath(MavenProject project, File appArchive, Log log) { - super(project, appArchive, log); - } - - public Map getBuildArgs() { - // Nothing at this time - return new HashMap(); - } - - @Override - public List getDockerfileLines() throws MojoExecutionException { - ArrayList lines = new ArrayList<>(); - lines.add(BOOST_GEN); - lines.add("FROM adoptopenjdk/openjdk8-openj9"); - lines.add("VOLUME /tmp"); - lines.add("ARG DEPENDENCY=target/dependency"); - lines.add("COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib"); - lines.add("COPY ${DEPENDENCY}/META-INF /app/META-INF"); - lines.add("COPY ${DEPENDENCY}/BOOT-INF/classes /app"); - lines.add("ENTRYPOINT [\"java\",\"-cp\",\"app:app/lib/*\",\"" + getSpringStartClass() + "\"]"); - return lines; - } - - @Override - public List getDockerIgnoreList() { - List lines = new ArrayList(); - lines.add("*.log"); - lines.add("target/liberty"); - return lines; - } - -} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootJar.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootJar.java deleted file mode 100644 index 79e6a78f..00000000 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/dockerizer/spring/DockerizeSpringBootJar.java +++ /dev/null @@ -1,54 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.docker.dockerizer.spring; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.logging.Log; -import org.apache.maven.project.MavenProject; - -public class DockerizeSpringBootJar extends SpringDockerizer { - - public DockerizeSpringBootJar(MavenProject project, File appArchive, Log log) { - super(project, appArchive, log); - } - - public Map getBuildArgs() { - Map buildArgs = new HashMap(); - buildArgs.put("JAR_FILE", "target/" + appArchive.getName()); - return buildArgs; - } - - public List getDockerfileLines() throws MojoExecutionException { - ArrayList lines = new ArrayList<>(); - lines.add(BOOST_GEN); - lines.add("FROM adoptopenjdk/openjdk8-openj9"); - lines.add("VOLUME /tmp"); - lines.add("ARG JAR_FILE"); - lines.add("COPY ${JAR_FILE} app.jar"); - lines.add("ENTRYPOINT [\"java\",\"-Djava.security.egd=file:/dev/./urandom\",\"-jar\",\"/app.jar\"]"); - return lines; - } - - @Override - public List getDockerIgnoreList() { - List lines = new ArrayList(); - lines.add("*.log"); - lines.add("target/liberty"); - return lines; - } - -} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/BoosterPacksParent.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/BoosterPacksParent.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/BoosterPacksParent.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/BoosterPacksParent.java index b350f514..98936b63 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/BoosterPacksParent.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/BoosterPacksParent.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost; +package io.openliberty.boost.maven; import java.util.ArrayList; import java.util.List; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JAXRSBoosterPackConfigurator.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JAXRSBoosterPackConfigurator.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JAXRSBoosterPackConfigurator.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JAXRSBoosterPackConfigurator.java index b5688b8f..9c3e9659 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JAXRSBoosterPackConfigurator.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JAXRSBoosterPackConfigurator.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost; +package io.openliberty.boost.maven; import org.w3c.dom.Document; import io.openliberty.boost.common.BoosterPackConfigurator; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JDBCBoosterPackConfigurator.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JDBCBoosterPackConfigurator.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JDBCBoosterPackConfigurator.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JDBCBoosterPackConfigurator.java index f4f300cb..3c87c03e 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/JDBCBoosterPackConfigurator.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/JDBCBoosterPackConfigurator.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost; +package io.openliberty.boost.maven; import io.openliberty.boost.common.BoosterPackConfigurator; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/AbstractDockerMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java similarity index 65% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/AbstractDockerMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java index 4dee5ca0..e78ea953 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/AbstractDockerMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java @@ -8,14 +8,11 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.docker; - -import java.util.regex.Pattern; +package io.openliberty.boost.maven.docker; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; @@ -24,14 +21,13 @@ import org.apache.maven.settings.Settings; import org.apache.maven.settings.crypto.SettingsDecrypter; -import com.spotify.docker.client.DefaultDockerClient; -import com.spotify.docker.client.DockerClient; import com.spotify.docker.client.ImageRef; import com.spotify.docker.client.auth.ConfigFileRegistryAuthSupplier; import com.spotify.docker.client.auth.RegistryAuthSupplier; -import com.spotify.docker.client.exceptions.DockerCertificateException; +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.docker.AbstractDockerI; -public abstract class AbstractDockerMojo extends AbstractMojo { +public abstract class AbstractDockerMojo extends AbstractMojo implements AbstractDockerI { /** * Current Maven project. @@ -69,10 +65,8 @@ public abstract class AbstractDockerMojo extends AbstractMojo { protected Log log; - protected abstract void execute(DockerClient dockerClient) throws MojoExecutionException, MojoFailureException; - @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoExecutionException { log = getLog(); if (repository.equals(project.getArtifactId()) && !repository.equals(repository.toLowerCase())) { this.repository = project.getArtifactId().toLowerCase(); @@ -88,25 +82,21 @@ public void execute() throws MojoExecutionException, MojoFailureException { throw new MojoExecutionException("The parameter is not configured with a valid name"); } } - if (!isTagValid(tag)) { + if (!AbstractDockerI.super.isTagValid(tag)) { throw new MojoExecutionException("The parameter is not configured with a valid name"); } - execute(getDockerClient()); - } - - private DockerClient getDockerClient() throws MojoExecutionException { - final RegistryAuthSupplier authSupplier = createRegistryAuthSupplier(); try { - return DefaultDockerClient.fromEnv().registryAuthSupplier(authSupplier).useProxy(useProxy).build(); - } catch (DockerCertificateException e) { - throw new MojoExecutionException("Problem loading Docker certificates", e); + execute(getDockerClient(useProxy)); + } catch (BoostException e) { + throw new MojoExecutionException(e.getMessage(), e); } } - private RegistryAuthSupplier createRegistryAuthSupplier() throws MojoExecutionException { + @Override + public RegistryAuthSupplier createRegistryAuthSupplier() throws BoostException { RegistryAuthSupplier supplier = null; - final ImageRef ref = new ImageRef(getImageName()); + final ImageRef ref = new ImageRef(getImageName(repository, tag)); final Settings settings = session.getSettings(); final Server server = settings.getServer(ref.getRegistryName()); @@ -120,25 +110,4 @@ private RegistryAuthSupplier createRegistryAuthSupplier() throws MojoExecutionEx return supplier; } - protected final String getImageName() { - return this.repository + ":" + this.tag; - } - - protected String getImageName(String repository, String tag) { - return repository + ":" + tag; - } - - protected static boolean isTagValid(String tag) { - return Pattern.matches("[\\w][\\w.-]{0,127}", tag); - } - - protected static boolean isRepositoryValid(String repository) { - String nameRegExp = "[a-z0-9]+((?:[._]|__|[-]*)[a-z0-9]+)*?"; - String domain = "(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9])"; - String domainRegExp = domain + "(\\." + domain + ")*?" + "(:[0-9]+)?"; - - String repositoryRegExp = "(" + domainRegExp + "\\/)?" + nameRegExp + "(\\/" + nameRegExp + ")*?"; - - return Pattern.matches(repositoryRegExp, repository); - } } diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java new file mode 100644 index 00000000..ad7408d2 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java @@ -0,0 +1,168 @@ +/*- + ****************************************************************************** + * This file was copied from source: + * + * https://github.com/spotify/dockerfile-maven/blob/2fc613abcad667f6b99d1e99124c02cc7b1c6bbd/plugin/src/main/java/com/spotify/plugin/dockerfile/BuildMojo.java + * + * and modified below. + * + ****************************************************************************** + * ------------------- + * ORIGINAL COPYRIGHT: + * ------------------- + * + * -\-\- + * Dockerfile Maven Plugin + * -- + * Copyright (C) 2016 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + ****************************************************************************** + */ + +/* + ****************************************************************************** + * We now include the copyright for our modification: + + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation (after modifications describe above) + ****************************************************************************** + */ +package io.openliberty.boost.maven.docker; + +import java.io.File; +import java.util.Map; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import com.spotify.docker.client.DockerClient; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.docker.DockerBuildI; +import io.openliberty.boost.common.docker.dockerizer.spring.SpringDockerizer; +import io.openliberty.boost.maven.utils.BoostLogger; +import io.openliberty.boost.maven.utils.MavenProjectUtil; +import net.wasdev.wlp.maven.plugins.utils.SpringBootUtil; + +/** + * Builds a docker image from a packaged application. This goal will either + * generate a Dockerfile or use the existing Dockerfile if it already exists. An + * ignore file (.dockerignore) will be created if one does not exist, and if it + * does exist the same entries will be appended to the existing .dockerignore. + */ +@Mojo(name = "docker-build", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME) +public class DockerBuildMojo extends AbstractDockerMojo implements DockerBuildI { + /** + * Pull newer version of the image, if set. Use the cache by default when + * building the image. + */ + @Parameter(property = "pullNewerImage", defaultValue = "false") + private boolean pullNewerImage; + + /** + * Do not use cache when building the image. + */ + @Parameter(property = "noCache", defaultValue = "false") + private boolean noCache; + + /** + * Set build time variables. + */ + @Parameter(property = "buildArgs") + private Map buildArgs; + + /** + * Sets the type of docker build to run. + */ + @Parameter(property = "dockerizer", defaultValue = "liberty") + private String dockerizer; + + @Override + public void execute(DockerClient dockerClient) throws BoostException { + try { + File appArchive = getAppArchive(); + + // Create a Dockerfile for the application + File projectDirectory = project.getBasedir(); + File outputDirectory = new File(project.getBuild().getDirectory()); + String springBootVersion = MavenProjectUtil.findSpringBootVersion(project); + SpringDockerizer springDockerizer = getDockerizer(dockerizer, projectDirectory, outputDirectory, appArchive, springBootVersion, BoostLogger.getInstance()); + springDockerizer.createDockerFile(); + springDockerizer.createDockerIgnore(); + + buildDockerImage(project.getBasedir().toPath(), dockerClient, springDockerizer, pullNewerImage, noCache, buildArgs, repository, tag, BoostLogger.getInstance()); + } catch (Exception e) { + throw new BoostException(e.getMessage(), e); + } + } + + /** + * Find the location of the Spring Boot Uber JAR + * + * @throws BoostException + */ + @Override + public File getAppArchive() throws BoostException { + File appArchive; + + // First try to get the Spring Boot Uber JAR as the project artifact as a result + // of the spring-boot-maven-plugin execution, handling classifier scenario if + // necessary. + appArchive = SpringBootUtil.getSpringBootUberJAR(project, getLog()); + if (appArchive != null) { + return appArchive; + } + + // If Boost replaced the project artifact, then the appArchive path will + // actually point to the Liberty Uber JAR. Check if this is the case and if so, + // use the .spring artifact that we preserved during the Boost packaging + // process. + + // Strictly speaking, the Liberty Uber JAR (in the "unboosted" SpringBoot Uber + // JAR location), is not needed to build the Docker image. However, let's throw + // an exception if we get here and it doesn't exist, assuming we've done + // something wrong. If it turns out we want to relax this later, we could. + File unboostedSpringBootUberJarLocation = SpringBootUtil.getSpringBootUberJARLocation(project, getLog()); + if (!unboostedSpringBootUberJarLocation.exists()) { + String excMsg = "Expected file does not exist at path = " + + getPathMessageText(unboostedSpringBootUberJarLocation) + + ". Make sure you have executed the spring-boot:repackage goal first before attempting the current goal."; + throw new BoostException(excMsg); + } + // Get the Boost location for the SpringBoot Uber JAR + appArchive = new File(io.openliberty.boost.common.utils.SpringBootUtil + .getBoostedSpringBootUberJarPath(unboostedSpringBootUberJarLocation)); + if (!appArchive.exists()) { + String excMsg = "Expected file does not exist at path = " + getPathMessageText(appArchive) + + ". Make sure you have executed the spring-boot:repackage goal first before attempting the current goal."; + throw new BoostException(excMsg); + } + if (net.wasdev.wlp.common.plugins.util.SpringBootUtil.isSpringBootUberJar(appArchive)) { + getLog().info("Found Spring Boot Uber JAR with .spring extension."); + return appArchive; + } + + // At this point we did not find the Spring Boot Uber JAR in any of its expected + // locations. + throw new BoostException("Could not find Spring Boot Uber JAR."); + } + +} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerPushMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerPushMojo.java new file mode 100644 index 00000000..594103e0 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerPushMojo.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.maven.docker; + +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import com.spotify.docker.client.DockerClient; + +import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.docker.DockerPushI; +import io.openliberty.boost.maven.utils.BoostLogger; + +/** + * Pushes a docker image to the docker repository. + * + */ +@Mojo(name = "docker-push", defaultPhase = LifecyclePhase.INSTALL) +public class DockerPushMojo extends AbstractDockerMojo implements DockerPushI { + + @Override + public void execute(DockerClient dockerClient) throws BoostException { + dockerPush(dockerClient, project.getArtifactId(), repository, tag, BoostLogger.getInstance()); + } + +} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/MavenSettingsAuthSupplier.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/MavenSettingsAuthSupplier.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/MavenSettingsAuthSupplier.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/MavenSettingsAuthSupplier.java index b773071c..fa950c77 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/docker/MavenSettingsAuthSupplier.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/MavenSettingsAuthSupplier.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.docker; +package io.openliberty.boost.maven.docker; import java.util.HashMap; import java.util.List; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/AbstractLibertyMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/AbstractLibertyMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java index dccf30f1..c154a8e4 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/AbstractLibertyMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId; import static org.twdata.maven.mojoexecutor.MojoExecutor.element; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyDebugMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyDebugMojo.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyDebugMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyDebugMojo.java index c312721e..654c0997 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyDebugMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyDebugMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; import static org.twdata.maven.mojoexecutor.MojoExecutor.element; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyPackageMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyPackageMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java index 107343e6..2edb5279 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyPackageMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import java.io.File; import java.io.IOException; @@ -28,13 +28,13 @@ import io.openliberty.boost.common.BoostException; import io.openliberty.boost.common.BoosterPackConfigurator; -import io.openliberty.boost.BoosterPacksParent; -import io.openliberty.boost.utils.BoostLogger; import io.openliberty.boost.common.utils.BoostUtil; import io.openliberty.boost.common.utils.ConfigConstants; import io.openliberty.boost.common.utils.LibertyServerConfigGenerator; -import io.openliberty.boost.utils.MavenProjectUtil; import io.openliberty.boost.common.utils.SpringBootUtil; +import io.openliberty.boost.maven.BoosterPacksParent; +import io.openliberty.boost.maven.utils.BoostLogger; +import io.openliberty.boost.maven.utils.MavenProjectUtil; import net.wasdev.wlp.common.plugins.util.PluginExecutionException; import static org.twdata.maven.mojoexecutor.MojoExecutor.*; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyRunMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyRunMojo.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyRunMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyRunMojo.java index 5dad681a..3f86fd8b 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyRunMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyRunMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; import static org.twdata.maven.mojoexecutor.MojoExecutor.element; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStartMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStartMojo.java similarity index 98% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStartMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStartMojo.java index 7ee267ff..8a848dcf 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStartMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStartMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; import static org.twdata.maven.mojoexecutor.MojoExecutor.element; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStopMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStopMojo.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStopMojo.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStopMojo.java index 19aa9562..b3b137f7 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/liberty/LibertyStopMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyStopMojo.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.liberty; +package io.openliberty.boost.maven.liberty; import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; import static org.twdata.maven.mojoexecutor.MojoExecutor.element; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/BoostLogger.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/BoostLogger.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/BoostLogger.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/BoostLogger.java index 7e5ea585..82f10458 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/BoostLogger.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/BoostLogger.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.utils; +package io.openliberty.boost.maven.utils; import org.codehaus.mojo.pluginsupport.MojoSupport; diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/MavenProjectUtil.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java similarity index 97% rename from boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/MavenProjectUtil.java rename to boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java index e665759a..42990860 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/utils/MavenProjectUtil.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java @@ -8,7 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ -package io.openliberty.boost.utils; +package io.openliberty.boost.maven.utils; import java.util.ArrayList; import java.util.List; diff --git a/boost-maven/boost-maven-plugin/src/test/java/io/openliberty/boost/docker/DockerRepositoryTests.java b/boost-maven/boost-maven-plugin/src/test/java/io/openliberty/boost/docker/DockerRepositoryTests.java index ffba99a5..d7360512 100644 --- a/boost-maven/boost-maven-plugin/src/test/java/io/openliberty/boost/docker/DockerRepositoryTests.java +++ b/boost-maven/boost-maven-plugin/src/test/java/io/openliberty/boost/docker/DockerRepositoryTests.java @@ -15,10 +15,20 @@ import java.util.Arrays; import java.util.List; +import org.junit.Before; import org.junit.Test; +import io.openliberty.boost.maven.docker.DockerBuildMojo; + public class DockerRepositoryTests { + DockerBuildMojo mojo; + + @Before + public void setup() { + mojo = new DockerBuildMojo(); + } + @Test public void testValidRepositoryNames() throws Exception { List repositories = Arrays.asList("image", "username/image", "host.com/image", @@ -27,7 +37,7 @@ public void testValidRepositoryNames() throws Exception { "host.com:8080/namespace__name/image.module-name", "user-name/image-name"); for (String repository : repositories) { - assertEquals(repository + " should be valid", true, AbstractDockerMojo.isRepositoryValid(repository)); + assertEquals(repository + " should be valid", true, mojo.isRepositoryValid(repository)); } } @@ -38,7 +48,7 @@ public void testInvalidRepositoryNames() throws Exception { "user**name/image", "image:8080", "host.com:abcd/image", "-host.com/-image", "user/repo/image/"); for (String repository : repositories) { - assertEquals(repository + " should be invalid", false, AbstractDockerMojo.isRepositoryValid(repository)); + assertEquals(repository + " should be invalid", false, mojo.isRepositoryValid(repository)); } } From 8954a6d386550b2de26f766561592a6f46b6ed55 Mon Sep 17 00:00:00 2001 From: Charles Tian Date: Fri, 9 Nov 2018 15:44:32 -0600 Subject: [PATCH 02/10] Update LMP version, fix compilation error --- boost-maven/boost-maven-plugin/pom.xml | 2 +- .../src/test/java/application/DockerBuildIT.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/boost-maven/boost-maven-plugin/pom.xml b/boost-maven/boost-maven-plugin/pom.xml index 200a720b..8362f035 100644 --- a/boost-maven/boost-maven-plugin/pom.xml +++ b/boost-maven/boost-maven-plugin/pom.xml @@ -28,7 +28,7 @@ net.wasdev.wlp.maven.plugins liberty-maven-plugin - 2.6.2-SNAPSHOT + 2.6.2 diff --git a/boost-maven/boost-maven-plugin/src/it/test-spring-boot-docker/src/test/java/application/DockerBuildIT.java b/boost-maven/boost-maven-plugin/src/it/test-spring-boot-docker/src/test/java/application/DockerBuildIT.java index e6f1283d..21c9d3d4 100644 --- a/boost-maven/boost-maven-plugin/src/it/test-spring-boot-docker/src/test/java/application/DockerBuildIT.java +++ b/boost-maven/boost-maven-plugin/src/it/test-spring-boot-docker/src/test/java/application/DockerBuildIT.java @@ -35,7 +35,7 @@ import org.springframework.boot.SpringBootVersion; -import io.openliberty.boost.docker.dockerizer.Dockerizer; +import io.openliberty.boost.common.docker.dockerizer.Dockerizer; public class DockerBuildIT { private static File dockerFile; From 39bbfe82d22cb11ce478ae2225f558ce6654157a Mon Sep 17 00:00:00 2001 From: Charles Tian Date: Fri, 9 Nov 2018 18:21:41 -0600 Subject: [PATCH 03/10] Intermediary commit - fixing boost-gradle --- .../boost/common/docker/AbstractDockerI.java | 31 +++ .../boost/common/docker/DockerBuildI.java | 40 +++- .../docker/DockerLoggingProgressHandler.java | 10 + .../boost/common/docker/DockerPushI.java | 10 + .../common/docker/dockerizer/Dockerizer.java | 10 + .../dockerizer/spring/SpringDockerizer.java | 10 + boost-gradle/README.md | 18 +- boost-gradle/build.gradle | 19 +- .../io/openliberty/boost/gradle/Boost.groovy | 2 - .../boost/gradle/BoostTaskFactory.groovy | 6 +- .../extensions/BoostDockerExtension.groovy | 8 +- .../boost/gradle/tasks/BoostDockerPush.groovy | 32 --- .../boost/gradle/tasks/BoostDockerTask.groovy | 220 ------------------ .../gradle/tasks/BoostPackageTask.groovy | 2 +- .../docker/AbstractBoostDockerTask.groovy | 73 ++++++ .../tasks/docker/BoostDockerBuildTask.groovy | 119 ++++++++++ .../tasks/docker/BoostDockerPushTask.groovy | 49 ++++ .../groovy/AbstractBoostDockerTest.groovy | 2 +- .../groovy/PackageAndDockerize15Test.groovy | 4 +- .../groovy/PackageAndDockerize20Test.groovy | 4 +- .../resources/springApp/docker15Test.gradle | 2 +- .../resources/springApp/docker20Test.gradle | 2 +- .../maven/docker/AbstractDockerMojo.java | 24 +- .../boost/maven/docker/DockerBuildMojo.java | 21 +- .../maven/liberty/AbstractLibertyMojo.java | 2 +- 25 files changed, 386 insertions(+), 334 deletions(-) delete mode 100644 boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerPush.groovy delete mode 100644 boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerTask.groovy create mode 100644 boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/AbstractBoostDockerTask.groovy create mode 100644 boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy create mode 100644 boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java index a504fcd5..7e52528a 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/AbstractDockerI.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker; import java.util.regex.Pattern; @@ -8,6 +18,7 @@ import com.spotify.docker.client.exceptions.DockerCertificateException; import io.openliberty.boost.common.BoostException; +import io.openliberty.boost.common.BoostLoggerI; public interface AbstractDockerI { @@ -44,4 +55,24 @@ default public DockerClient getDockerClient(boolean useProxy) throws BoostExcept } } + default public boolean isValidDockerConfig(BoostLoggerI log, String repository, String tag, String artifactId) throws BoostException { + if (repository.equals(artifactId) && !repository.equals(repository.toLowerCase())) { + repository = artifactId.toLowerCase(); + log.debug( + "Applying all lower case letters to the default repository name to build the Docker image successfully"); + } + + if (!isRepositoryValid(repository)) { + if (repository.equals(artifactId)) { + throw new BoostException( + "The default repository name ${project.artifactId} cannot be used to build the image because it is not a valid repository name."); + } else { + throw new BoostException("The parameter is not configured with a valid name"); + } + } + if (!isTagValid(tag)) { + throw new BoostException("The parameter is not configured with a valid name"); + } + return true; + } } diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java index e4c10e54..2a35f5cd 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerBuildI.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker; import java.io.File; @@ -25,8 +35,28 @@ public abstract interface DockerBuildI extends AbstractDockerI { public File getAppArchive() throws BoostException; // Default methods - - default public SpringDockerizer getDockerizer(String dockerizer, File projectDirectory, File outputDirectory, File appArchive, String springBootVersion, BoostLoggerI log) { + + default public void dockerBuild(String dockerizer, DockerClient dockerClient, File projectDirectory, + File outputDirectory, String springBootVersion, boolean pullNewerImage, boolean noCache, + Map buildArgs, String repository, String tag, BoostLoggerI log) throws BoostException { + try { + File appArchive = getAppArchive(); + + // Create a Dockerfile for the application + SpringDockerizer springDockerizer = getDockerizer(dockerizer, projectDirectory, outputDirectory, appArchive, + springBootVersion, log); + springDockerizer.createDockerFile(); + springDockerizer.createDockerIgnore(); + + buildDockerImage(projectDirectory.toPath(), dockerClient, springDockerizer, pullNewerImage, noCache, + buildArgs, repository, tag, log); + } catch (Exception e) { + throw new BoostException(e.getMessage(), e); + } + } + + default public SpringDockerizer getDockerizer(String dockerizer, File projectDirectory, File outputDirectory, + File appArchive, String springBootVersion, BoostLoggerI log) { // TODO: Needed future enhancements: // 1. Is it Spring or something else? sense with @@ -52,9 +82,9 @@ default public SpringDockerizer getDockerizer(String dockerizer, File projectDir * Use the DockerClient to build the image * */ - default public void buildDockerImage(Path baseDir, DockerClient dockerClient, SpringDockerizer dockerizer, boolean pullNewerImage, - boolean noCache, Map buildArgs, String repository, String tag, BoostLoggerI log) - throws BoostException, IOException { + default public void buildDockerImage(Path baseDir, DockerClient dockerClient, SpringDockerizer dockerizer, + boolean pullNewerImage, boolean noCache, Map buildArgs, String repository, String tag, + BoostLoggerI log) throws BoostException, IOException { final DockerLoggingProgressHandler progressHandler = new DockerLoggingProgressHandler(log); final String imageName = getImageName(repository, tag); BuildParam[] buidParams = getBuildParams(dockerizer, pullNewerImage, noCache, buildArgs); diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java index 1f9d6d75..6f22c0d0 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerLoggingProgressHandler.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker; import java.text.MessageFormat; diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java index 8cfd8709..11d27c45 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/DockerPushI.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker; import java.util.Scanner; diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java index 9b6dbdd7..9fc58ef8 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/Dockerizer.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker.dockerizer; import java.io.BufferedReader; diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java index 9934e6e6..2783d573 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/SpringDockerizer.java @@ -1,3 +1,13 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ package io.openliberty.boost.common.docker.dockerizer.spring; import java.io.File; diff --git a/boost-gradle/README.md b/boost-gradle/README.md index caa48562..ba3306cf 100644 --- a/boost-gradle/README.md +++ b/boost-gradle/README.md @@ -32,12 +32,12 @@ apply plugin: 'boost' # Plugin Tasks -| Task Name | Description | -|-----------------|--------------------------------------------------| -| boostStart | Starts the Boost application. | -| boostStop | Stops the Boost application. | -| boostRun | Runs the Boost application in the foreground. | -| boostDebug | Runs the Boost application in debug mode. | -| boostPackage | Packages the application and server. | -| boostDocker | Creates a Docker file and image for the project. | -| boostDockerPush | Pushes Docker image to a Docker repository. | \ No newline at end of file +| Task Name | Description | +|------------------|--------------------------------------------------| +| boostStart | Starts the Boost application. | +| boostStop | Stops the Boost application. | +| boostRun | Runs the Boost application in the foreground. | +| boostDebug | Runs the Boost application in debug mode. | +| boostPackage | Packages the application and server. | +| boostDockerBuild | Creates a Docker file and image for the project. | +| boostDockerPush | Pushes Docker image to a Docker repository. | diff --git a/boost-gradle/build.gradle b/boost-gradle/build.gradle index 59ed40d2..40d79370 100644 --- a/boost-gradle/build.gradle +++ b/boost-gradle/build.gradle @@ -19,33 +19,18 @@ repositories { } } -configurations { - //dependencies to be included in the plugin jar - includeLibs -} - dependencies { compile localGroovy() - compile 'net.wasdev.wlp.common:ci.common:1.3.3-SNAPSHOT' - compile 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:2.6-SNAPSHOT' + compile 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:2.6' compile group: 'commons-io', name: 'commons-io', version: '2.6' compile 'io.openliberty.boost:boost-common:0.1.2-SNAPSHOT' - - includeLibs 'gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.20.1' - - configurations.compile.extendsFrom(configurations.includeLibs) + compile 'com.spotify:docker-client:8.11.7' testCompile 'junit:junit:4.12' testCompile("javax.ws.rs:javax.ws.rs-api:2.1") testCompile("com.github.docker-java:docker-java:3.1.0-rc-4") } -jar { - from { - configurations.includeLibs.collect { it.isDirectory() ? it : zipTree(it) } - } -} - test { doFirst { //Copying gradle.properties with plugin version to test projects String runtimeGroup diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/Boost.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/Boost.groovy index 1e5d6ca8..08b3b506 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/Boost.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/Boost.groovy @@ -31,8 +31,6 @@ public class Boost implements Plugin { project.pluginManager.apply('net.wasdev.wlp.gradle.plugins.Liberty') - project.pluginManager.apply('com.palantir.docker') - project.liberty.server = configureBoostServerProperties() configureRuntimeArtifact(project) } diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/BoostTaskFactory.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/BoostTaskFactory.groovy index b291061d..39ffc916 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/BoostTaskFactory.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/BoostTaskFactory.groovy @@ -17,8 +17,8 @@ import io.openliberty.boost.gradle.tasks.BoostRunTask import io.openliberty.boost.gradle.tasks.BoostStopTask import io.openliberty.boost.gradle.tasks.BoostPackageTask import io.openliberty.boost.gradle.tasks.BoostDebugTask -import io.openliberty.boost.gradle.tasks.BoostDockerTask -import io.openliberty.boost.gradle.tasks.BoostDockerPushTask +import io.openliberty.boost.gradle.tasks.docker.BoostDockerBuildTask +import io.openliberty.boost.gradle.tasks.docker.BoostDockerPushTask class BoostTaskFactory { Project project @@ -33,7 +33,7 @@ class BoostTaskFactory { project.tasks.create('boostStop', BoostStopTask) project.tasks.create('boostPackage', BoostPackageTask) project.tasks.create('boostDebug', BoostDebugTask) - project.tasks.create('boostDocker', BoostDockerTask) + project.tasks.create('boostDockerBuild', BoostDockerBuildTask) project.tasks.create('boostDockerPush', BoostDockerPushTask) } diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/extensions/BoostDockerExtension.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/extensions/BoostDockerExtension.groovy index 8261e4e5..89c845a0 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/extensions/BoostDockerExtension.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/extensions/BoostDockerExtension.groovy @@ -13,7 +13,13 @@ package io.openliberty.boost.gradle.extensions class BoostDockerExtension { + String dockerizer = "liberty" String imageName = null - String dockerRepo = null + String dockerRepo = "" + String tag = "latest" + boolean useProxy = false + boolean pullNewerImage = false + boolean noCache = false + Map buildArgs = new HashMap() } \ No newline at end of file diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerPush.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerPush.groovy deleted file mode 100644 index a1efa3c0..00000000 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerPush.groovy +++ /dev/null @@ -1,32 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.gradle.tasks - -import org.gradle.api.logging.LogLevel - -public class BoostDockerPushTask extends AbstractBoostTask { - - BoostDockerPushTask() { - configure({ - description 'Dockerizes a Boost project.' - logging.level = LogLevel.INFO - group 'Boost' - - dependsOn 'boostDocker' - - project.afterEvaluate { - if (isDockerConfigured()) { - finalizedBy 'dockerPush' - } - } - }) - } -} \ No newline at end of file diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerTask.groovy deleted file mode 100644 index e82a9779..00000000 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostDockerTask.groovy +++ /dev/null @@ -1,220 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 IBM Corporation and others. - * 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: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package io.openliberty.boost.gradle.tasks - -import org.gradle.api.GradleException -import org.gradle.api.logging.LogLevel - -import io.openliberty.boost.gradle.utils.GradleProjectUtil - -import net.wasdev.wlp.common.plugins.util.SpringBootUtil - -import java.nio.file.FileAlreadyExistsException -import java.nio.file.Files -import java.nio.charset.Charset -import java.util.ArrayList - -public class BoostDockerTask extends AbstractBoostTask { - - protected static final String LIBERTY_IMAGE_1 = "open-liberty:springBoot1" - protected static final String LIBERTY_IMAGE_2 = "open-liberty:springBoot2" - protected static final String LIB_INDEX_CACHE = "lib.index.cache" - protected static final String ARG_SOURCE_APP = "--sourceAppPath" - protected static final String ARG_DEST_THIN_APP = "--targetThinAppPath" - protected static final String ARG_DEST_LIB_CACHE = "--targetLibCachePath" - protected static final String FROM = "FROM " - protected static final String COPY = "COPY " - protected static final String RUN = "RUN " - - String springBootVersion - String appName - File appFile - - BoostDockerTask() { - configure({ - description 'Dockerizes a Boost project.' - logging.level = LogLevel.INFO - group 'Boost' - - finalizedBy 'docker' - - project.afterEvaluate { - springBootVersion = GradleProjectUtil.findSpringBootVersion(project) - if (springBootVersion != null) { - if (project.plugins.hasPlugin('java')) { - if (springBootVersion.startsWith("2.")) { - dependsOn 'bootJar' - appFile = project.bootJar.archivePath - if (isDockerConfigured()) { //We won't add the project to the build lifecycle if Docker isn't configured - project.bootJar.finalizedBy 'boostDocker' - } - } else if (springBootVersion.startsWith("1.")){ - appFile = project.jar.archivePath - //Checking for classifier in bootRepackage and adding to archiveName - if (project.bootRepackage.classifier != null && !project.bootRepackage.classifier.isEmpty()) { - String appArchiveName = //Adding classifier to the appArchive name - appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + - '-' + - project.bootRepackage.classifier.toString() + - appFile.getName().substring(appFile.getName().lastIndexOf(".")) - appFile = new File(appFile.getParent(), appArchiveName) - } - - dependsOn 'bootRepackage' - - if (isDockerConfigured()) { - project.bootRepackage.finalizedBy 'boostDocker' - } - } - } else { - throw new GradleException ('Unable to determine the project artifact name to add to the container. Please use the java plugin.') - } - - //Getting image name from boost docker extension if it is set, otherwise we use the file name w/o extension - if (isDockerConfigured() && project.boost.docker.imageName != null && !project.boost.docker.imageName.isEmpty()) { - logger.info ("Setting image name to: ${project.boost.docker.imageName}") - appName = project.boost.docker.imageName - } else { - appName = appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) - logger.info ("Setting image name to: ${appName}") - } - - configureDockerPlugin() - }//JEE case here - - //Need to disable these so they don't clean our Dockerfile if we run the Docker tasks - project.dockerClean.enabled = false - project.dockerPrepare.enabled = false - } - - doFirst { - if (appFile == null) { //if we didn't set the appName during configuration we can get it from the project, will be set for springboot projects - if (!project.configurations.archives.allArtifacts.isEmpty()) { - appFile = project.configurations.archives.allArtifacts[0].getFile() - appName = appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) - project.docker.setName(appName) - } else { - throw new GradleException ('Unable to determine the project artifact name.') - } - //need to configure the docker plugin - configureDockerPlugin() - } - createDockerFile() - } - }) - } - - public void createDockerFile() throws GradleException { - if (springBootVersion != null) { - createSpringBootDockerFile(springBootVersion) - //Need to add the application to the build/docker directory for the docker plugin to work - copyAppToDockerDir() - } else { - throw new GradleException("Unable to create a Docker image because application type is not supported.") - } - } - - //Could get the archiveName from the configurations.archives - protected void configureDockerPlugin() { - if (isDockerConfigured() && project.boost.docker.dockerRepo != null && !project.boost.docker.dockerRepo.isEmpty()) { - project.docker.setName(project.boost.docker.dockerRepo + '/' + appName) - } else { - project.docker.setName(appName) - } - - project.docker.setDockerfile(new File(project.projectDir, 'Dockerfile')) - project.docker.buildArgs(['APP_FILE': appFile.getName()]) - project.docker.tags('latest') - } - - //Copies application artifact and Dockerfile to build/docker - protected void copyAppToDockerDir() { - File dockerDir = new File(project.buildDir, "docker") - Files.copy(appFile.toPath(), new File(dockerDir, appFile.getName()).toPath()) - Files.copy(new File(project.projectDir, "Dockerfile").toPath(), new File(dockerDir, "Dockerfile").toPath()) - } - - protected void createSpringBootDockerFile(String springBootVersion) throws GradleException, IOException { - try { - if (SpringBootUtil.isSpringBootUberJar(appFile)) { - File dockerDir = new File(project.buildDir, 'docker') - Files.createDirectory(dockerDir.toPath()) - File dockerFile = createNewDockerFile(project.projectDir) - String libertySBImage = getLibertySpringBootBaseImage(springBootVersion) - writeSpringBootDockerFile(dockerFile, libertySBImage) - } else { - throw new GradleException(appFile.getAbsolutePath() + " file is not an executable archive. " - + "Please rebuild the archive with the bootJar or bootRepackage tasks.") - } - } catch (FileAlreadyExistsException e1) { - logger.warn("Dockerfile already exists.") - } - } - - protected File createNewDockerFile(File dockerFileDirectory) { - File dockerFile = new File(dockerFileDirectory, "Dockerfile") - Files.createFile(dockerFile.toPath()) - logger.info("Creating Dockerfile: " + dockerFile.getAbsolutePath()) - return dockerFile - } - - protected String getLibertySpringBootBaseImage(String springBootVersion) throws GradleException { - String libertyImage = null - - if (springBootVersion.startsWith("1.")) { - libertyImage = LIBERTY_IMAGE_1 - } else if (springBootVersion.startsWith("2.")) { - libertyImage = LIBERTY_IMAGE_2 - } else { - throw new GradleException( - "No supporting docker image found for Open Liberty for the Spring Boot version " - + springBootVersion) - } - return libertyImage - } - - private void writeSpringBootDockerFile(File dockerFile, String libertyImage) throws IOException { - ArrayList lines = new ArrayList() - lines.add(FROM + libertyImage + " as " + "staging") - - lines.add("\n") - lines.add("# The APP_FILE ARG provides the final name of the Spring Boot application archive") - lines.add("ARG" + " " + "APP_FILE") - - lines.add("\n") - lines.add("# Stage the fat JAR") - lines.add(COPY + "\${APP_FILE}" + " " + "/staging/" + "\${APP_FILE}") - - lines.add("\n") - lines.add("# Thin the fat application; stage the thin app output and the library cache") - lines.add(RUN + "springBootUtility thin " + ARG_SOURCE_APP + "=" + "/staging/" + "\${APP_FILE}" + " " - + ARG_DEST_THIN_APP + "=" + "/staging/" + "thin-\${APP_FILE}" + " " + ARG_DEST_LIB_CACHE + "=" - + "/staging/" + LIB_INDEX_CACHE) - - lines.add("\n") - lines.add("# Final stage, only copying the liberty installation (includes primed caches)") - lines.add("# and the lib.index.cache and thin application") - lines.add(FROM + libertyImage) - - lines.add("\n") - lines.add("ARG" + " " + "APP_FILE") - - lines.add("\n") - lines.add(COPY + "--from=staging " + "/staging/" + LIB_INDEX_CACHE + " " + "/" + LIB_INDEX_CACHE) - - lines.add("\n") - lines.add(COPY + "--from=staging " + "/staging/thin-\${APP_FILE}" + " " - + "/config/dropins/spring/thin-\${APP_FILE}") - - Files.write(dockerFile.toPath(), lines, Charset.forName("UTF-8")) - } - -} \ No newline at end of file diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostPackageTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostPackageTask.groovy index 0ecee792..8110af7d 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostPackageTask.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/BoostPackageTask.groovy @@ -46,7 +46,7 @@ public class BoostPackageTask extends AbstractBoostTask { dependsOn 'libertyCreate' - mustRunAfter 'boostDocker' + mustRunAfter 'boostDockerBuild' //There are some things that this task does before we can package up the server into a JAR PackageAndDumpExtension boostPackage = new PackageAndDumpExtension() diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/AbstractBoostDockerTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/AbstractBoostDockerTask.groovy new file mode 100644 index 00000000..766c80cc --- /dev/null +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/AbstractBoostDockerTask.groovy @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.gradle.tasks.docker + +import com.spotify.docker.client.auth.RegistryAuthSupplier +import com.spotify.docker.client.auth.ConfigFileRegistryAuthSupplier + +import io.openliberty.boost.gradle.tasks.AbstractBoostTask +import io.openliberty.boost.gradle.utils.BoostLogger +import io.openliberty.boost.common.BoostException +import io.openliberty.boost.common.docker.AbstractDockerI + +import java.io.File + +import org.gradle.api.Project +import org.gradle.api.GradleException + +public abstract class AbstractBoostDockerTask extends AbstractBoostTask implements AbstractDockerI { + + void doExecute(String artifactId) throws GradleException { + try { + if(isValidDockerConfig(BoostLogger.getInstance(), project.boost.docker.dockerRepo + artifactId, project.boost.docker.tag, artifactId)) { + execute(getDockerClient(project.boost.docker.useProxy)); + } + } catch (BoostException e) { + throw new GradleException(e.getMessage(), e); + } + } + + @Override + public RegistryAuthSupplier createRegistryAuthSupplier() throws BoostException { + return new ConfigFileRegistryAuthSupplier() + } + + public String getArtifactId(Project project, String springBootVersion) throws GradleException { + File appFile + if (springBootVersion != null) { + if (project.plugins.hasPlugin('java')) { + if (springBootVersion.startsWith("2.")) { + appFile = project.bootJar.archivePath + } else if (springBootVersion.startsWith("1.")){ + appFile = project.jar.archivePath + //Checking for classifier in bootRepackage and adding to archiveName + if (project.bootRepackage.classifier != null && !project.bootRepackage.classifier.isEmpty()) { + String appArchiveName = //Adding classifier to the appArchive name + appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + + '-' + + project.bootRepackage.classifier.toString() + + appFile.getName().substring(appFile.getName().lastIndexOf(".")) + appFile = new File(appFile.getParent(), appArchiveName) + } + } + } else { + throw new GradleException ('Unable to determine the project artifact name to add to the container. Please use the java plugin.') + } + + //Getting image name from boost docker extension if it is set, otherwise we use the file name w/o extension + if (isDockerConfigured() && project.boost.docker.imageName != null && !project.boost.docker.imageName.isEmpty()) { + return project.boost.docker.imageName + } else { + return appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + } + } + } +} \ No newline at end of file diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy new file mode 100644 index 00000000..f0eee236 --- /dev/null +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.gradle.tasks.docker + +import org.gradle.api.GradleException +import org.gradle.api.logging.LogLevel + +import com.spotify.docker.client.DockerClient + +import io.openliberty.boost.common.BoostException +import io.openliberty.boost.common.docker.DockerBuildI +import io.openliberty.boost.gradle.utils.GradleProjectUtil +import io.openliberty.boost.gradle.utils.BoostLogger + +import net.wasdev.wlp.common.plugins.util.SpringBootUtil + +import java.nio.file.FileAlreadyExistsException +import java.nio.file.Files +import java.nio.charset.Charset +import java.util.ArrayList + +public class BoostDockerBuildTask extends AbstractBoostDockerTask implements DockerBuildI { + + String springBootVersion + String appName + File appFile + + BoostDockerBuildTask() { + configure({ + description 'Dockerizes a Boost project.' + logging.level = LogLevel.INFO + group 'Boost' + + project.afterEvaluate { + springBootVersion = GradleProjectUtil.findSpringBootVersion(project) + if (springBootVersion != null) { + if (project.plugins.hasPlugin('java')) { + if (springBootVersion.startsWith("2.")) { + dependsOn 'bootJar' + appFile = project.bootJar.archivePath + if (isDockerConfigured()) { //We won't add the project to the build lifecycle if Docker isn't configured + project.bootJar.finalizedBy 'boostDockerBuild' + } + } else if (springBootVersion.startsWith("1.")){ + appFile = project.jar.archivePath + //Checking for classifier in bootRepackage and adding to archiveName + if (project.bootRepackage.classifier != null && !project.bootRepackage.classifier.isEmpty()) { + String appArchiveName = //Adding classifier to the appArchive name + appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + + '-' + + project.bootRepackage.classifier.toString() + + appFile.getName().substring(appFile.getName().lastIndexOf(".")) + appFile = new File(appFile.getParent(), appArchiveName) + } + + dependsOn 'bootRepackage' + + if (isDockerConfigured()) { + project.bootRepackage.finalizedBy 'boostDockerBuild' + } + } + } else { + throw new GradleException ('Unable to determine the project artifact name to add to the container. Please use the java plugin.') + } + + //Getting image name from boost docker extension if it is set, otherwise we use the file name w/o extension + if (isDockerConfigured() && project.boost.docker.imageName != null && !project.boost.docker.imageName.isEmpty()) { + logger.info ("Setting image name to: ${project.boost.docker.imageName}") + appName = project.boost.docker.imageName + } else { + appName = appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + logger.info ("Setting image name to: ${appName}") + } + } + } + + doFirst { + if (appFile == null) { //if we didn't set the appName during configuration we can get it from the project, will be set for springboot projects + if (!project.configurations.archives.allArtifacts.isEmpty()) { + appFile = project.configurations.archives.allArtifacts[0].getFile() + appName = appFile.getName().substring(0, appFile.getName().lastIndexOf(".")) + } else { + throw new GradleException ('Unable to determine the project artifact name.') + } + } + doExecute(appName) + } + }) + } + + @Override + public void execute(DockerClient dockerClient) throws BoostException { + File projectDirectory = project.projectDir + File outputDirectory = project.buildDir + + println '------------------------------' + project.boost.docker.dockerRepo + appName + + dockerBuild(project.boost.docker.dockerizer, dockerClient, projectDirectory, outputDirectory, springBootVersion, project.boost.docker.pullNewerImage, + project.boost.docker.noCache, project.boost.docker.buildArgs, project.boost.docker.dockerRepo + appName, project.boost.docker.tag, BoostLogger.getInstance()) + } + + /** + * Find the location of the Spring Boot Uber JAR + * + * @throws BoostException + */ + @Override + public File getAppArchive() throws BoostException { + return appFile + } +} \ No newline at end of file diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy new file mode 100644 index 00000000..2496cae6 --- /dev/null +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package io.openliberty.boost.gradle.tasks.docker + +import org.gradle.api.logging.LogLevel + +import com.spotify.docker.client.DockerClient + +import io.openliberty.boost.common.BoostException +import io.openliberty.boost.common.docker.DockerPushI + +import io.openliberty.boost.gradle.utils.GradleProjectUtil + +public class BoostDockerPushTask extends AbstractBoostDockerTask implements DockerPushI { + + String appName + + BoostDockerPushTask() { + configure({ + description 'Dockerizes a Boost project.' + logging.level = LogLevel.INFO + group 'Boost' + + dependsOn 'boostDockerBuild' + + project.afterEvaluate { + appName = getArtifactId(project, GradleProjectUtil.findSpringBootVersion(project)) + } + + doFirst { + doExecute(appName) + } + }) + } + + @Override + public void execute(DockerClient dockerClient) throws BoostException { + dockerPush(dockerClient, appName, + project.boost.docker.dockerRepo, project.boost.docker.tag, BoostLogger.getInstance()); + } +} \ No newline at end of file diff --git a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy index 89e787f8..37b84b8a 100644 --- a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy +++ b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy @@ -61,7 +61,7 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { @Test public void testDockerSuccess() throws IOException { - assertEquals(SUCCESS, result.task(":boostDocker").getOutcome()) + assertEquals(SUCCESS, result.task(":boostDockerBuild").getOutcome()) } @Test diff --git a/boost-gradle/src/test/groovy/PackageAndDockerize15Test.groovy b/boost-gradle/src/test/groovy/PackageAndDockerize15Test.groovy index 6efd22f1..45f36693 100644 --- a/boost-gradle/src/test/groovy/PackageAndDockerize15Test.groovy +++ b/boost-gradle/src/test/groovy/PackageAndDockerize15Test.groovy @@ -46,7 +46,7 @@ public class PackageAndDockerize15Test extends AbstractBoostDockerTest { result = GradleRunner.create() .withProjectDir(testProjectDir) - .withArguments("boostDocker", "boostPackage", "boostStart", "boostStop") + .withArguments("boostDockerBuild", "boostPackage", "boostStart", "boostStop") .build() } @@ -54,7 +54,7 @@ public class PackageAndDockerize15Test extends AbstractBoostDockerTest { public void testBuildSuccess() throws IOException { assertEquals(SUCCESS, result.task(":installLiberty").getOutcome()) assertEquals(SUCCESS, result.task(":libertyCreate").getOutcome()) - assertEquals(SUCCESS, result.task(":boostDocker").getOutcome()) + assertEquals(SUCCESS, result.task(":boostDockerBuild").getOutcome()) assertEquals(SUCCESS, result.task(":boostPackage").getOutcome()) assertEquals(SUCCESS, result.task(":boostStart").getOutcome()) assertEquals(SUCCESS, result.task(":boostStop").getOutcome()) diff --git a/boost-gradle/src/test/groovy/PackageAndDockerize20Test.groovy b/boost-gradle/src/test/groovy/PackageAndDockerize20Test.groovy index 387fc245..8f1a45e2 100644 --- a/boost-gradle/src/test/groovy/PackageAndDockerize20Test.groovy +++ b/boost-gradle/src/test/groovy/PackageAndDockerize20Test.groovy @@ -46,7 +46,7 @@ public class PackageAndDockerize20Test extends AbstractBoostDockerTest { result = GradleRunner.create() .withProjectDir(testProjectDir) - .withArguments("boostDocker", "boostPackage", "boostStart", "boostStop") + .withArguments("boostDockerBuild", "boostPackage", "boostStart", "boostStop") .build() } @@ -54,7 +54,7 @@ public class PackageAndDockerize20Test extends AbstractBoostDockerTest { public void testBuildSuccess() throws IOException { assertEquals(SUCCESS, result.task(":installLiberty").getOutcome()) assertEquals(SUCCESS, result.task(":libertyCreate").getOutcome()) - assertEquals(SUCCESS, result.task(":boostDocker").getOutcome()) + assertEquals(SUCCESS, result.task(":boostDockerBuild").getOutcome()) assertEquals(SUCCESS, result.task(":boostPackage").getOutcome()) assertEquals(SUCCESS, result.task(":boostStart").getOutcome()) assertEquals(SUCCESS, result.task(":boostStop").getOutcome()) diff --git a/boost-gradle/src/test/resources/springApp/docker15Test.gradle b/boost-gradle/src/test/resources/springApp/docker15Test.gradle index 6719ec08..c1105e3c 100644 --- a/boost-gradle/src/test/resources/springApp/docker15Test.gradle +++ b/boost-gradle/src/test/resources/springApp/docker15Test.gradle @@ -30,7 +30,7 @@ bootRepackage { boost { docker { imageName = 'test-image15' - dockerRepo = 'localhost:5000' + dockerRepo = 'localhost:5000/' } } diff --git a/boost-gradle/src/test/resources/springApp/docker20Test.gradle b/boost-gradle/src/test/resources/springApp/docker20Test.gradle index 043fb512..d01b75ee 100644 --- a/boost-gradle/src/test/resources/springApp/docker20Test.gradle +++ b/boost-gradle/src/test/resources/springApp/docker20Test.gradle @@ -27,7 +27,7 @@ bootJar { boost { docker { imageName = 'test-image20' - dockerRepo = 'localhost:5000' + dockerRepo = 'localhost:5000/' } } diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java index e78ea953..69c079da 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/AbstractDockerMojo.java @@ -26,6 +26,7 @@ import com.spotify.docker.client.auth.RegistryAuthSupplier; import io.openliberty.boost.common.BoostException; import io.openliberty.boost.common.docker.AbstractDockerI; +import io.openliberty.boost.maven.utils.BoostLogger; public abstract class AbstractDockerMojo extends AbstractMojo implements AbstractDockerI { @@ -67,27 +68,10 @@ public abstract class AbstractDockerMojo extends AbstractMojo implements Abstrac @Override public void execute() throws MojoExecutionException { - log = getLog(); - if (repository.equals(project.getArtifactId()) && !repository.equals(repository.toLowerCase())) { - this.repository = project.getArtifactId().toLowerCase(); - log.debug( - "Applying all lower case letters to the default repository name to build the Docker image successfully"); - } - - if (!isRepositoryValid(repository)) { - if (repository.equals(project.getArtifactId())) { - throw new MojoExecutionException( - "The default repository name ${project.artifactId} cannot be used to build the image because it is not a valid repository name."); - } else { - throw new MojoExecutionException("The parameter is not configured with a valid name"); - } - } - if (!AbstractDockerI.super.isTagValid(tag)) { - throw new MojoExecutionException("The parameter is not configured with a valid name"); - } - try { - execute(getDockerClient(useProxy)); + if(isValidDockerConfig(BoostLogger.getInstance(), repository, tag, project.getArtifactId())) { + execute(getDockerClient(useProxy)); + } } catch (BoostException e) { throw new MojoExecutionException(e.getMessage(), e); } diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java index ad7408d2..81722dda 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/docker/DockerBuildMojo.java @@ -57,7 +57,6 @@ import io.openliberty.boost.common.BoostException; import io.openliberty.boost.common.docker.DockerBuildI; -import io.openliberty.boost.common.docker.dockerizer.spring.SpringDockerizer; import io.openliberty.boost.maven.utils.BoostLogger; import io.openliberty.boost.maven.utils.MavenProjectUtil; import net.wasdev.wlp.maven.plugins.utils.SpringBootUtil; @@ -97,21 +96,11 @@ public class DockerBuildMojo extends AbstractDockerMojo implements DockerBuildI @Override public void execute(DockerClient dockerClient) throws BoostException { - try { - File appArchive = getAppArchive(); - - // Create a Dockerfile for the application - File projectDirectory = project.getBasedir(); - File outputDirectory = new File(project.getBuild().getDirectory()); - String springBootVersion = MavenProjectUtil.findSpringBootVersion(project); - SpringDockerizer springDockerizer = getDockerizer(dockerizer, projectDirectory, outputDirectory, appArchive, springBootVersion, BoostLogger.getInstance()); - springDockerizer.createDockerFile(); - springDockerizer.createDockerIgnore(); - - buildDockerImage(project.getBasedir().toPath(), dockerClient, springDockerizer, pullNewerImage, noCache, buildArgs, repository, tag, BoostLogger.getInstance()); - } catch (Exception e) { - throw new BoostException(e.getMessage(), e); - } + File projectDirectory = project.getBasedir(); + File outputDirectory = new File(project.getBuild().getDirectory()); + String springBootVersion = MavenProjectUtil.findSpringBootVersion(project); + dockerBuild(dockerizer, dockerClient, projectDirectory, outputDirectory, springBootVersion, pullNewerImage, + noCache, buildArgs, repository, tag, BoostLogger.getInstance()); } /** diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java index c154a8e4..f89c1bde 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java @@ -37,7 +37,7 @@ public abstract class AbstractLibertyMojo extends MojoSupport { protected String libertyMavenPluginGroupId = "net.wasdev.wlp.maven.plugins"; protected String libertyMavenPluginArtifactId = "liberty-maven-plugin"; - @Parameter(defaultValue = "2.6.2-SNAPSHOT", readonly = true) + @Parameter(defaultValue = "2.6.2", readonly = true) protected String libertyMavenPluginVersion; @Parameter(defaultValue = "${project.build.directory}", readonly = true) From 2ad600160fba96d83a924fc77fa63bea0862dd94 Mon Sep 17 00:00:00 2001 From: mattbsox Date: Mon, 12 Nov 2018 10:18:00 -0600 Subject: [PATCH 04/10] using build/libs for gradle app location --- .../spring/DockerizeLibertySpringBootJar.java | 53 ++++++++++--------- .../tasks/docker/BoostDockerBuildTask.groovy | 5 +- .../tasks/docker/BoostDockerPushTask.groovy | 1 + 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java index cdfbb4d7..e8147eb1 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java @@ -11,6 +11,7 @@ package io.openliberty.boost.common.docker.dockerizer.spring; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -56,34 +57,38 @@ private String getLibertySpringBootBaseImage() throws BoostException { } public List getDockerfileLines() throws BoostException { - String libertyImage = getLibertySpringBootBaseImage(); - ArrayList lines = new ArrayList<>(); - lines.add(BOOST_GEN); - lines.add(FROM + libertyImage + " as " + "staging"); + try { + String libertyImage = getLibertySpringBootBaseImage(); + ArrayList lines = new ArrayList<>(); + lines.add(BOOST_GEN); + lines.add(FROM + libertyImage + " as " + "staging"); - lines.add(""); - lines.add("# The APP_FILE ARG provides the final name of the Spring Boot application archive"); - lines.add("ARG" + " " + "APP_FILE"); + lines.add(""); + lines.add("# The APP_FILE ARG provides the final name of the Spring Boot application archive"); + lines.add("ARG" + " " + "APP_FILE"); - lines.add(""); - lines.add("# Stage the fat JAR"); - lines.add(COPY + outputDirectory.getName() + "/" + "${APP_FILE}" + " " + "/staging/" + "${APP_FILE}"); + lines.add(""); + lines.add("# Stage the fat JAR"); + lines.add(COPY + outputDirectory.getCanonicalPath().replace(projectDirectory.getCanonicalPath(), "") + "/" + "${APP_FILE}" + " " + "/staging/" + "${APP_FILE}"); - lines.add(""); - lines.add("# Thin the fat application; stage the thin app output and the library cache"); - lines.add(RUN + "springBootUtility thin " + ARG_SOURCE_APP + "=" + "/staging/" + "${APP_FILE}" + " " - + ARG_DEST_THIN_APP + "=" + "/staging/" + "thin-${APP_FILE}" + " " + ARG_DEST_LIB_CACHE + "=" - + "/staging/" + LIB_INDEX_CACHE); + lines.add(""); + lines.add("# Thin the fat application; stage the thin app output and the library cache"); + lines.add(RUN + "springBootUtility thin " + ARG_SOURCE_APP + "=" + "/staging/" + "${APP_FILE}" + " " + + ARG_DEST_THIN_APP + "=" + "/staging/" + "thin-${APP_FILE}" + " " + ARG_DEST_LIB_CACHE + "=" + + "/staging/" + LIB_INDEX_CACHE); - lines.add(""); - lines.add("# Final stage, only copying the liberty installation (includes primed caches)"); - lines.add("# and the lib.index.cache and thin application"); - lines.add(FROM + libertyImage); - lines.add("ARG" + " " + "APP_FILE"); - lines.add(COPY + "--from=staging " + "/staging/" + LIB_INDEX_CACHE + " " + "/" + LIB_INDEX_CACHE); - lines.add(COPY + "--from=staging " + "/staging/thin-${APP_FILE}" + " " - + "/config/dropins/spring/thin-${APP_FILE}"); - return lines; + lines.add(""); + lines.add("# Final stage, only copying the liberty installation (includes primed caches)"); + lines.add("# and the lib.index.cache and thin application"); + lines.add(FROM + libertyImage); + lines.add("ARG" + " " + "APP_FILE"); + lines.add(COPY + "--from=staging " + "/staging/" + LIB_INDEX_CACHE + " " + "/" + LIB_INDEX_CACHE); + lines.add(COPY + "--from=staging " + "/staging/thin-${APP_FILE}" + " " + + "/config/dropins/spring/thin-${APP_FILE}"); + return lines; + } catch (IOException e) { + throw new BoostException("Could not resolve the project location when creating Dockerfile.", e); + } } @Override diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy index f0eee236..9e24045e 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerBuildTask.groovy @@ -26,6 +26,7 @@ import java.nio.file.FileAlreadyExistsException import java.nio.file.Files import java.nio.charset.Charset import java.util.ArrayList +import java.io.File public class BoostDockerBuildTask extends AbstractBoostDockerTask implements DockerBuildI { @@ -99,9 +100,7 @@ public class BoostDockerBuildTask extends AbstractBoostDockerTask implements Doc @Override public void execute(DockerClient dockerClient) throws BoostException { File projectDirectory = project.projectDir - File outputDirectory = project.buildDir - - println '------------------------------' + project.boost.docker.dockerRepo + appName + File outputDirectory = new File(project.buildDir.getAbsolutePath(), 'libs') dockerBuild(project.boost.docker.dockerizer, dockerClient, projectDirectory, outputDirectory, springBootVersion, project.boost.docker.pullNewerImage, project.boost.docker.noCache, project.boost.docker.buildArgs, project.boost.docker.dockerRepo + appName, project.boost.docker.tag, BoostLogger.getInstance()) diff --git a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy index 2496cae6..ecfbca0f 100644 --- a/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy +++ b/boost-gradle/src/main/groovy/io/openliberty/boost/gradle/tasks/docker/BoostDockerPushTask.groovy @@ -17,6 +17,7 @@ import com.spotify.docker.client.DockerClient import io.openliberty.boost.common.BoostException import io.openliberty.boost.common.docker.DockerPushI +import io.openliberty.boost.gradle.utils.BoostLogger import io.openliberty.boost.gradle.utils.GradleProjectUtil public class BoostDockerPushTask extends AbstractBoostDockerTask implements DockerPushI { From 4240870d8884b07c98c64f92dfe79877a5e561a4 Mon Sep 17 00:00:00 2001 From: Charles Tian Date: Mon, 12 Nov 2018 11:11:21 -0600 Subject: [PATCH 05/10] Add dockerizer gradle tests --- .../groovy/AbstractBoostDockerTest.groovy | 8 ++- ...ockerBuildDockerizerClasspath15Test.groovy | 44 +++++++++++++++++ ...ockerBuildDockerizerClasspath20Test.groovy | 44 +++++++++++++++++ .../DockerBuildDockerizerJar15Test.groovy | 44 +++++++++++++++++ .../DockerBuildDockerizerJar20Test.groovy | 44 +++++++++++++++++ .../dockerDockerizerClasspath15Test.gradle | 49 +++++++++++++++++++ .../dockerDockerizerClasspath20Test.gradle | 46 +++++++++++++++++ .../dockerDockerizerJar15Test.gradle | 49 +++++++++++++++++++ .../dockerDockerizerJar20Test.gradle | 46 +++++++++++++++++ 9 files changed, 372 insertions(+), 2 deletions(-) create mode 100644 boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath15Test.groovy create mode 100644 boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath20Test.groovy create mode 100644 boost-gradle/src/test/groovy/DockerBuildDockerizerJar15Test.groovy create mode 100644 boost-gradle/src/test/groovy/DockerBuildDockerizerJar20Test.groovy create mode 100644 boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle create mode 100644 boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath20Test.gradle create mode 100644 boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle create mode 100644 boost-gradle/src/test/resources/springApp/dockerDockerizerJar20Test.gradle diff --git a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy index 37b84b8a..b9e36e29 100644 --- a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy +++ b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy @@ -33,6 +33,7 @@ import com.github.dockerjava.api.command.CreateContainerResponse import com.github.dockerjava.api.model.Container import com.github.dockerjava.api.model.PortBinding import com.github.dockerjava.core.DockerClientBuilder +import com.github.dockerjava.api.model.ExposedPort; public abstract class AbstractBoostDockerTest extends AbstractBoostTest { protected static final String OL_SPRING_15_IMAGE = "open-liberty:springBoot1" @@ -46,6 +47,7 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { protected static String imageName protected static String libertyImage + protected static String dockerPort = "9080" protected static File resourceDir protected static File testProjectDir @@ -79,8 +81,10 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { @Test public void runDockerContainerAndVerifyAppOnEndpoint() throws Exception { + ExposedPort exposedPort = ExposedPort.tcp(Integer.valueOf(dockerPort)) + CreateContainerResponse container = dockerClient.createContainerCmd("${imageName}:latest") - .withPortBindings(PortBinding.parse("9080:9080")).exec() + .withPortBindings(PortBinding.parse(dockerPort + ":" + dockerPort)).withExposedPorts(exposedPort).exec() Thread.sleep(3000) containerId = container.getId() @@ -101,7 +105,7 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { } public void testAppRunningOnEndpoint() throws Exception { - URL requestUrl = new URL("http://localhost:9080/") + URL requestUrl = new URL("http://localhost:" + dockerPort) HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection() if (conn != null) { diff --git a/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath15Test.groovy b/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath15Test.groovy new file mode 100644 index 00000000..6a90c094 --- /dev/null +++ b/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath15Test.groovy @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import static org.gradle.testkit.runner.TaskOutcome.* + +import org.junit.BeforeClass +import static org.junit.Assert.* + +import java.io.File + +import com.github.dockerjava.core.DockerClientBuilder + +public class DockerBuildDockerizerClasspath15Test extends AbstractBoostDockerTest { + + @BeforeClass + public static void setup() { + resourceDir = new File("build/resources/test/springApp") + testProjectDir = new File(integTestDir, "DockerBuildDockerizerClasspath15Test") + buildFilename = "dockerDockerizerClasspath15Test.gradle" + libertyImage = OL_SPRING_15_IMAGE + imageName = "localhost:5000/test-image15" + dockerPort = "8080" + + createDir(testProjectDir) + createTestProject(testProjectDir, resourceDir, buildFilename) + dockerFile = new File(testProjectDir, "Dockerfile") + dockerClient = DockerClientBuilder.getInstance().build() + + result = GradleRunner.create() + .withProjectDir(testProjectDir) + .withArguments("build") + .build() + } +} \ No newline at end of file diff --git a/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath20Test.groovy b/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath20Test.groovy new file mode 100644 index 00000000..1499fa07 --- /dev/null +++ b/boost-gradle/src/test/groovy/DockerBuildDockerizerClasspath20Test.groovy @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import static org.gradle.testkit.runner.TaskOutcome.* + +import org.junit.BeforeClass +import static org.junit.Assert.* + +import java.io.File + +import com.github.dockerjava.core.DockerClientBuilder + +public class DockerBuildDockerizerClasspath20Test extends AbstractBoostDockerTest { + + @BeforeClass + public static void setup() { + resourceDir = new File("build/resources/test/springApp") + testProjectDir = new File(integTestDir, "DockerBuildDockerizerClasspath20Test") + buildFilename = "dockerDockerizerClasspath20Test.gradle" + libertyImage = OL_SPRING_20_IMAGE + imageName = "localhost:5000/test-image20" + dockerPort = "8080" + + createDir(testProjectDir) + createTestProject(testProjectDir, resourceDir, buildFilename) + dockerFile = new File(testProjectDir, "Dockerfile") + dockerClient = DockerClientBuilder.getInstance().build() + + result = GradleRunner.create() + .withProjectDir(testProjectDir) + .withArguments("build") + .build() + } +} \ No newline at end of file diff --git a/boost-gradle/src/test/groovy/DockerBuildDockerizerJar15Test.groovy b/boost-gradle/src/test/groovy/DockerBuildDockerizerJar15Test.groovy new file mode 100644 index 00000000..46c549ed --- /dev/null +++ b/boost-gradle/src/test/groovy/DockerBuildDockerizerJar15Test.groovy @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import static org.gradle.testkit.runner.TaskOutcome.* + +import org.junit.BeforeClass +import static org.junit.Assert.* + +import java.io.File + +import com.github.dockerjava.core.DockerClientBuilder + +public class DockerBuildDockerizerJar15Test extends AbstractBoostDockerTest { + + @BeforeClass + public static void setup() { + resourceDir = new File("build/resources/test/springApp") + testProjectDir = new File(integTestDir, "DockerBuildDockerizerJar15Test") + buildFilename = "dockerDockerizerJar15Test.gradle" + libertyImage = OL_SPRING_15_IMAGE + imageName = "localhost:5000/test-image15" + dockerPort = "8080" + + createDir(testProjectDir) + createTestProject(testProjectDir, resourceDir, buildFilename) + dockerFile = new File(testProjectDir, "Dockerfile") + dockerClient = DockerClientBuilder.getInstance().build() + + result = GradleRunner.create() + .withProjectDir(testProjectDir) + .withArguments("build") + .build() + } +} \ No newline at end of file diff --git a/boost-gradle/src/test/groovy/DockerBuildDockerizerJar20Test.groovy b/boost-gradle/src/test/groovy/DockerBuildDockerizerJar20Test.groovy new file mode 100644 index 00000000..ff64c36c --- /dev/null +++ b/boost-gradle/src/test/groovy/DockerBuildDockerizerJar20Test.groovy @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import static org.gradle.testkit.runner.TaskOutcome.* + +import org.junit.BeforeClass +import static org.junit.Assert.* + +import java.io.File + +import com.github.dockerjava.core.DockerClientBuilder + +public class DockerBuildDockerizerJar20Test extends AbstractBoostDockerTest { + + @BeforeClass + public static void setup() { + resourceDir = new File("build/resources/test/springApp") + testProjectDir = new File(integTestDir, "DockerBuildDockerizerJar20Test") + buildFilename = "dockerDockerizerJar20Test.gradle" + libertyImage = OL_SPRING_20_IMAGE + imageName = "localhost:5000/test-image20" + dockerPort = "8080" + + createDir(testProjectDir) + createTestProject(testProjectDir, resourceDir, buildFilename) + dockerFile = new File(testProjectDir, "Dockerfile") + dockerClient = DockerClientBuilder.getInstance().build() + + result = GradleRunner.create() + .withProjectDir(testProjectDir) + .withArguments("build") + .build() + } +} \ No newline at end of file diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle new file mode 100644 index 00000000..492dcbf8 --- /dev/null +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle @@ -0,0 +1,49 @@ +buildscript { + repositories { + mavenCentral() + mavenLocal() + maven { + url 'https://oss.sonatype.org/content/repositories/snapshots/' + } + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE") + classpath("io.openliberty.boost:boost-gradle-plugin:$boostVersion") + } +} + +apply plugin: 'java' +apply plugin: 'boost' +apply plugin: 'eclipse' +apply plugin: 'idea' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +jar { + baseName = 'test-docker15' +} + +bootRepackage { + mainClass = 'hello.Application' +} + +boost { + docker { + imageName = 'test-image15' + dockerRepo = 'localhost:5000/' + classifier = 'classpath' + } +} + +repositories { + mavenCentral() + mavenLocal() +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +dependencies { + compile("org.springframework.boot:spring-boot-starter-web") + compile("org.springframework.boot:spring-boot-starter-actuator") +} diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath20Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath20Test.gradle new file mode 100644 index 00000000..2374ea57 --- /dev/null +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath20Test.gradle @@ -0,0 +1,46 @@ +buildscript { + repositories { + mavenCentral() + mavenLocal() + maven { + url 'https://oss.sonatype.org/content/repositories/snapshots/' + } + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE") + classpath("io.openliberty.boost:boost-gradle-plugin:$boostVersion") + } +} + +apply plugin: 'java' +apply plugin: 'boost' +apply plugin: 'eclipse' +apply plugin: 'idea' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +bootJar { + baseName = 'test-docker20' + mainClassName = 'hello.Application' +} + +boost { + docker { + imageName = 'test-image20' + dockerRepo = 'localhost:5000/' + dockerizer = 'classpath' + } +} + +repositories { + mavenCentral() + mavenLocal() +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +dependencies { + compile("org.springframework.boot:spring-boot-starter-web") + compile("org.springframework.boot:spring-boot-starter-actuator") +} diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle new file mode 100644 index 00000000..9fa7f2b1 --- /dev/null +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle @@ -0,0 +1,49 @@ +buildscript { + repositories { + mavenCentral() + mavenLocal() + maven { + url 'https://oss.sonatype.org/content/repositories/snapshots/' + } + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE") + classpath("io.openliberty.boost:boost-gradle-plugin:$boostVersion") + } +} + +apply plugin: 'java' +apply plugin: 'boost' +apply plugin: 'eclipse' +apply plugin: 'idea' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +jar { + baseName = 'test-docker15' +} + +bootRepackage { + mainClass = 'hello.Application' +} + +boost { + docker { + imageName = 'test-image15' + dockerRepo = 'localhost:5000/' + classifier = 'jar' + } +} + +repositories { + mavenCentral() + mavenLocal() +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +dependencies { + compile("org.springframework.boot:spring-boot-starter-web") + compile("org.springframework.boot:spring-boot-starter-actuator") +} diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerJar20Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar20Test.gradle new file mode 100644 index 00000000..fda1625c --- /dev/null +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar20Test.gradle @@ -0,0 +1,46 @@ +buildscript { + repositories { + mavenCentral() + mavenLocal() + maven { + url 'https://oss.sonatype.org/content/repositories/snapshots/' + } + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE") + classpath("io.openliberty.boost:boost-gradle-plugin:$boostVersion") + } +} + +apply plugin: 'java' +apply plugin: 'boost' +apply plugin: 'eclipse' +apply plugin: 'idea' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +bootJar { + baseName = 'test-docker20' + mainClassName = 'hello.Application' +} + +boost { + docker { + imageName = 'test-image20' + dockerRepo = 'localhost:5000/' + dockerizer = 'jar' + } +} + +repositories { + mavenCentral() + mavenLocal() +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +dependencies { + compile("org.springframework.boot:spring-boot-starter-web") + compile("org.springframework.boot:spring-boot-starter-actuator") +} From 78a7a9e9de4cb469de935322a2466ad08951a802 Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Mon, 12 Nov 2018 12:21:59 -0500 Subject: [PATCH 06/10] Add .gradle dir to generated .dockerignore --- .../spring/DockerizeLibertySpringBootJar.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java index e8147eb1..b04cfb55 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java @@ -32,10 +32,11 @@ public class DockerizeLibertySpringBootJar extends SpringDockerizer { private static final String COPY = "COPY "; private static final String RUN = "RUN "; - public DockerizeLibertySpringBootJar(File projectDirectory, File outputDirectory, File appArchive, String springBootVersion, BoostLoggerI log) { - super(projectDirectory, outputDirectory, appArchive, springBootVersion, log); + public DockerizeLibertySpringBootJar(File projectDirectory, File outputDirectory, File appArchive, + String springBootVersion, BoostLoggerI log) { + super(projectDirectory, outputDirectory, appArchive, springBootVersion, log); } - + public Map getBuildArgs() { Map buildArgs = new HashMap(); buildArgs.put("APP_FILE", appArchive.getName()); @@ -48,10 +49,9 @@ private String getLibertySpringBootBaseImage() throws BoostException { libertyImage = LIBERTY_IMAGE_1; } else if (springBootVersion.startsWith("2.")) { libertyImage = LIBERTY_IMAGE_2; - } else { - throw new BoostException( - "No supporting docker image found for Open Liberty for the Spring Boot version " - + springBootVersion); + } else { + throw new BoostException("No supporting docker image found for Open Liberty for the Spring Boot version " + + springBootVersion); } return libertyImage; } @@ -91,12 +91,13 @@ public List getDockerfileLines() throws BoostException { } } - @Override - public List getDockerIgnoreList() { - List lines = new ArrayList(); - lines.add("*.log"); - lines.add("target/liberty"); - return lines; - } + @Override + public List getDockerIgnoreList() { + List lines = new ArrayList(); + lines.add("*.log"); + lines.add("target/liberty"); + lines.add(".gradle/"); + return lines; + } } From b643dfad9e10b4adc7e6fa2df649fb409ab8e075 Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Mon, 12 Nov 2018 13:37:53 -0500 Subject: [PATCH 07/10] Fix relative path for app dir for Windows --- .../spring/DockerizeLibertySpringBootJar.java | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java index b04cfb55..2f5dae8d 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/docker/dockerizer/spring/DockerizeLibertySpringBootJar.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -56,39 +57,48 @@ private String getLibertySpringBootBaseImage() throws BoostException { return libertyImage; } + private String getAppPathString() { + + Path projPath = projectDirectory.toPath(); + Path outputPath = outputDirectory.toPath(); + + // goes from '~/proj/build/lib' to 'build/lib' + Path appPath = projPath.relativize(outputPath); + + // On Windows the last line might be 'build\lib' + return appPath.toString().replace(File.separatorChar, '/'); + } + public List getDockerfileLines() throws BoostException { - try { - String libertyImage = getLibertySpringBootBaseImage(); - ArrayList lines = new ArrayList<>(); - lines.add(BOOST_GEN); - lines.add(FROM + libertyImage + " as " + "staging"); - - lines.add(""); - lines.add("# The APP_FILE ARG provides the final name of the Spring Boot application archive"); - lines.add("ARG" + " " + "APP_FILE"); - - lines.add(""); - lines.add("# Stage the fat JAR"); - lines.add(COPY + outputDirectory.getCanonicalPath().replace(projectDirectory.getCanonicalPath(), "") + "/" + "${APP_FILE}" + " " + "/staging/" + "${APP_FILE}"); - - lines.add(""); - lines.add("# Thin the fat application; stage the thin app output and the library cache"); - lines.add(RUN + "springBootUtility thin " + ARG_SOURCE_APP + "=" + "/staging/" + "${APP_FILE}" + " " - + ARG_DEST_THIN_APP + "=" + "/staging/" + "thin-${APP_FILE}" + " " + ARG_DEST_LIB_CACHE + "=" - + "/staging/" + LIB_INDEX_CACHE); - - lines.add(""); - lines.add("# Final stage, only copying the liberty installation (includes primed caches)"); - lines.add("# and the lib.index.cache and thin application"); - lines.add(FROM + libertyImage); - lines.add("ARG" + " " + "APP_FILE"); - lines.add(COPY + "--from=staging " + "/staging/" + LIB_INDEX_CACHE + " " + "/" + LIB_INDEX_CACHE); - lines.add(COPY + "--from=staging " + "/staging/thin-${APP_FILE}" + " " - + "/config/dropins/spring/thin-${APP_FILE}"); - return lines; - } catch (IOException e) { - throw new BoostException("Could not resolve the project location when creating Dockerfile.", e); - } + + String libertyImage = getLibertySpringBootBaseImage(); + ArrayList lines = new ArrayList<>(); + lines.add(BOOST_GEN); + lines.add(FROM + libertyImage + " as " + "staging"); + + lines.add(""); + lines.add("# The APP_FILE ARG provides the final name of the Spring Boot application archive"); + lines.add("ARG" + " " + "APP_FILE"); + + lines.add(""); + lines.add("# Stage the fat JAR"); + lines.add(COPY + getAppPathString() + "/" + "${APP_FILE}" + " " + "/staging/" + "${APP_FILE}"); + + lines.add(""); + lines.add("# Thin the fat application; stage the thin app output and the library cache"); + lines.add(RUN + "springBootUtility thin " + ARG_SOURCE_APP + "=" + "/staging/" + "${APP_FILE}" + " " + + ARG_DEST_THIN_APP + "=" + "/staging/" + "thin-${APP_FILE}" + " " + ARG_DEST_LIB_CACHE + "=" + + "/staging/" + LIB_INDEX_CACHE); + + lines.add(""); + lines.add("# Final stage, only copying the liberty installation (includes primed caches)"); + lines.add("# and the lib.index.cache and thin application"); + lines.add(FROM + libertyImage); + lines.add("ARG" + " " + "APP_FILE"); + lines.add(COPY + "--from=staging " + "/staging/" + LIB_INDEX_CACHE + " " + "/" + LIB_INDEX_CACHE); + lines.add(COPY + "--from=staging " + "/staging/thin-${APP_FILE}" + " " + + "/config/dropins/spring/thin-${APP_FILE}"); + return lines; } @Override From 6be6d622062b1b3ec4360a9f337ba1d027dd9853 Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Mon, 12 Nov 2018 14:12:43 -0500 Subject: [PATCH 08/10] Read past generator comment in test --- boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy index b9e36e29..3411e0a6 100644 --- a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy +++ b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy @@ -74,7 +74,8 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { @Test public void testDockerfileContainsCorrectLibertyImage() throws Exception { BufferedReader reader = new BufferedReader(new FileReader(dockerFile)) - + reader.readLine() + // need to read two lines since the first is the generator comment assertTrue("Expected Open liberty base image ${libertyImage} was not found in " + dockerFile.getCanonicalPath(), reader.readLine().contains(libertyImage)) } From 9cb6e85b77ae344c112bfc1f7204fa71da49ced0 Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Mon, 12 Nov 2018 14:30:21 -0500 Subject: [PATCH 09/10] Leverage DOCKER_HOST env variable if set --- .../src/test/groovy/AbstractBoostDockerTest.groovy | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy index 3411e0a6..4d5382ae 100644 --- a/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy +++ b/boost-gradle/src/test/groovy/AbstractBoostDockerTest.groovy @@ -106,7 +106,7 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { } public void testAppRunningOnEndpoint() throws Exception { - URL requestUrl = new URL("http://localhost:" + dockerPort) + URL requestUrl = new URL("http://" + getTestDockerHost() + ":" + dockerPort) HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection() if (conn != null) { @@ -122,4 +122,14 @@ public abstract class AbstractBoostDockerTest extends AbstractBoostTest { } assertEquals("Expected body not found.", "Greetings from Spring Boot!", response.toString()) } + + private static String getTestDockerHost() { + String dockerHostEnv = System.getenv("DOCKER_HOST"); + if (dockerHostEnv == null || dockerHostEnv.isEmpty()) { + return "localhost"; + } else { + URI dockerHostURI = URI.create(dockerHostEnv); + return dockerHostURI.getHost(); + } + } } \ No newline at end of file From 7f23ab18e24762099b70383a2af0ddf572c90cbc Mon Sep 17 00:00:00 2001 From: Scott Kurz Date: Mon, 12 Nov 2018 15:18:57 -0500 Subject: [PATCH 10/10] Fix tests using classifier instead of dockerizer prop --- .../resources/springApp/dockerDockerizerClasspath15Test.gradle | 2 +- .../test/resources/springApp/dockerDockerizerJar15Test.gradle | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle index 492dcbf8..ca613204 100644 --- a/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerClasspath15Test.gradle @@ -31,7 +31,7 @@ boost { docker { imageName = 'test-image15' dockerRepo = 'localhost:5000/' - classifier = 'classpath' + dockerizer = 'classpath' } } diff --git a/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle index 9fa7f2b1..dc83abd9 100644 --- a/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle +++ b/boost-gradle/src/test/resources/springApp/dockerDockerizerJar15Test.gradle @@ -31,7 +31,7 @@ boost { docker { imageName = 'test-image15' dockerRepo = 'localhost:5000/' - classifier = 'jar' + dockerizer = 'jar' } }