From d72df0965ca9953f83aeb196ac3bc9d2818180b7 Mon Sep 17 00:00:00 2001 From: kusumaghoshdastidar Date: Wed, 1 May 2024 10:07:21 -0700 Subject: [PATCH] Count of populate registry executions --- docker-versions-maven-plugin/pom.xml | 5 ++ .../docker/client/DockerRestClient.java | 6 ++ .../DepopulateProjectRegistryMojo.java | 22 +++--- .../plugins/DockerVersionsMojo.java | 71 +++++++++++++++++++ .../plugins/DockerVersionsUpdaterMojo.java | 4 -- .../plugins/PopulateProjectRegistryMojo.java | 12 ++-- pom.xml | 5 ++ 7 files changed, 105 insertions(+), 20 deletions(-) diff --git a/docker-versions-maven-plugin/pom.xml b/docker-versions-maven-plugin/pom.xml index 91dd269..5139fb7 100644 --- a/docker-versions-maven-plugin/pom.xml +++ b/docker-versions-maven-plugin/pom.xml @@ -70,6 +70,11 @@ maven-plugin-annotations provided + + org.apache.maven.resolver + maven-resolver-api + provided + org.apache.maven.shared maven-shared-utils diff --git a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/docker/client/DockerRestClient.java b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/docker/client/DockerRestClient.java index ec2f978..718aaa5 100644 --- a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/docker/client/DockerRestClient.java +++ b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/docker/client/DockerRestClient.java @@ -61,6 +61,12 @@ public DockerRestClient(final HttpConfiguration httpConfiguration) this.dockerClient = DockerClientImpl.getInstance(config, httpClient); } + public String getDockerSystemId() + { + LOGGER.debug("Getting docker system id..."); + return dockerClient.infoCmd().exec().getId(); + } + public Optional findImage(final String imageName) { LOGGER.debug("Checking if image '{}' is present...", imageName); diff --git a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DepopulateProjectRegistryMojo.java b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DepopulateProjectRegistryMojo.java index 39705c5..779ad7e 100644 --- a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DepopulateProjectRegistryMojo.java +++ b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DepopulateProjectRegistryMojo.java @@ -15,9 +15,9 @@ */ package com.github.cafapi.docker_versions.plugins; -import com.github.cafapi.docker_versions.docker.client.DockerRestClient; import com.github.cafapi.docker_versions.docker.client.ImageTaggingException; import com.github.dockerjava.api.command.InspectImageResponse; +import java.io.IOException; import java.util.Optional; import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugin.MojoExecutionException; @@ -47,21 +47,27 @@ public void execute() throws MojoExecutionException, MojoFailureException } try { + // if count is 1 then depopulate & delete file + // else decrement count (don't depopulate) + final int populateGoalCount = getPopulateRegistryCount(); + + if (populateGoalCount > 1) { + writePopulateRegistryCount(populateGoalCount - 1); + return; + } new ExecutionImpl().executeImpl(); + if (populateGoalCount == 1) { + removePopulateGoalCounter(); + } } catch (final ImageTaggingException ex) { throw new MojoExecutionException("Unable to untag image", ex); + } catch (final IOException ex) { + throw new MojoExecutionException("Unable to update populate registry execution count", ex); } } private final class ExecutionImpl { - final DockerRestClient dockerClient; - - public ExecutionImpl() - { - dockerClient = new DockerRestClient(httpConfiguration); - } - public void executeImpl() throws ImageTaggingException { LOGGER.debug("DepopulateProjectRegistry with this configuration {}", imageManagement); diff --git a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsMojo.java b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsMojo.java index be0357c..9017a64 100644 --- a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsMojo.java +++ b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsMojo.java @@ -15,17 +15,28 @@ */ package com.github.cafapi.docker_versions.plugins; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; +import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; import org.apache.maven.settings.Settings; +import com.github.cafapi.docker_versions.docker.client.DockerRestClient; abstract class DockerVersionsMojo extends AbstractMojo { protected static final String PROJECT_DOCKER_REGISTRY = "projectDockerRegistry"; protected static final String LATEST_TAG = "latest"; + @Parameter(defaultValue = "${session}", readonly = true, required = true) + protected MavenSession session; + @Parameter(defaultValue = "${project}", readonly = true, required = true) protected MavenProject project; @@ -41,10 +52,70 @@ abstract class DockerVersionsMojo extends AbstractMojo @Parameter(property = "docker.versions.skip", defaultValue = "false") protected boolean skip; + protected final DockerRestClient dockerClient; + + protected DockerVersionsMojo() + { + dockerClient = new DockerRestClient(httpConfiguration); + } + protected String getProjectDockerRegister() { return project.getProperties().getProperty( PROJECT_DOCKER_REGISTRY, project.getArtifactId() + "-" + project.getVersion() + ".project-registries.local"); } + + protected int getPopulateRegistryCount() throws IOException + { + return Files.lines(getCountFilePath()) + .map(Integer::parseInt) + .findFirst() + .orElseThrow(); + } + + protected void incrementPopulateRegistryCount() throws IOException + { + if (Files.exists(getCountFilePath())) { + final int count = getPopulateRegistryCount(); + writePopulateRegistryCount(count + 1); + } else { + writePopulateRegistryCount(1); + } + } + + protected void writePopulateRegistryCount(final int count) throws IOException + { + Files.write(getCountFilePath(), ("" + count).getBytes(StandardCharsets.UTF_8)); + } + + protected void removePopulateGoalCounter() throws IOException + { + Files.deleteIfExists(getCountFilePath()); + // TODO: remove the .m2\repository\.cache\docker-version-plugin folder if it has no more files? + } + + private String getCacheId() + { + final String dockerHostId = dockerClient.getDockerSystemId(); + return dockerHostId + "-" + getProjectDockerRegister(); + } + + private File getCacheDirectory() throws IOException + { + // Create a folder for the plugin to create a file to store counter_per_docker_host + // .m2\repository\.cache\docker-version-plugin\- + final File cacheFolder = new File( + session.getRepositorySession().getLocalRepository().getBasedir(), + ".cache/docker-versions-plugin"); + if (!Files.exists(Paths.get(cacheFolder.getAbsolutePath()))) { + Files.createDirectory(Paths.get(cacheFolder.getAbsolutePath())); + } + return cacheFolder; + } + + private Path getCountFilePath() throws IOException + { + return Paths.get(getCacheDirectory().getAbsolutePath(), getCacheId()); + } } diff --git a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsUpdaterMojo.java b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsUpdaterMojo.java index 37e17a0..789fb74 100644 --- a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsUpdaterMojo.java +++ b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/DockerVersionsUpdaterMojo.java @@ -24,7 +24,6 @@ import javax.xml.stream.XMLStreamException; -import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; import org.apache.maven.model.PluginManagement; import org.apache.maven.plugin.MojoExecutionException; @@ -56,9 +55,6 @@ abstract class DockerVersionsUpdaterMojo extends DockerVersionsMojo @Parameter protected Set ignoreVersions; - @Parameter(defaultValue = "${session}", readonly = true, required = true) - private MavenSession session; - protected Plugin dockerVersionsPlugin; protected Xpp3Dom pluginConfig; protected List imagesConfig; diff --git a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/PopulateProjectRegistryMojo.java b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/PopulateProjectRegistryMojo.java index a089dba..0a7131a 100644 --- a/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/PopulateProjectRegistryMojo.java +++ b/docker-versions-maven-plugin/src/main/java/com/github/cafapi/docker_versions/plugins/PopulateProjectRegistryMojo.java @@ -17,10 +17,10 @@ import com.github.cafapi.docker_versions.docker.auth.AuthConfigHelper; import com.github.cafapi.docker_versions.docker.auth.DockerRegistryAuthException; -import com.github.cafapi.docker_versions.docker.client.DockerRestClient; import com.github.cafapi.docker_versions.docker.client.ImageTaggingException; import com.github.dockerjava.api.command.InspectImageResponse; import com.github.dockerjava.api.model.AuthConfig; +import java.io.IOException; import java.util.List; import java.util.Optional; import org.apache.commons.lang3.StringUtils; @@ -53,6 +53,7 @@ public void execute() throws MojoExecutionException, MojoFailureException try { new ExecutionImpl().executeImpl(); + incrementPopulateRegistryCount(); } catch (final DockerRegistryAuthException ex) { throw new MojoExecutionException("Unable to find auth configuration", ex); } catch (final ImagePullException ex) { @@ -64,18 +65,13 @@ public void execute() throws MojoExecutionException, MojoFailureException } catch (final InterruptedException ex) { LOGGER.warn("Plugin interrupted", ex); Thread.currentThread().interrupt(); + } catch (final IOException ex) { + throw new MojoExecutionException("Unable to record populate registry execution count", ex); } } private final class ExecutionImpl { - final DockerRestClient dockerClient; - - public ExecutionImpl() - { - dockerClient = new DockerRestClient(httpConfiguration); - } - public void executeImpl() throws DockerRegistryAuthException, ImagePullException, diff --git a/pom.xml b/pom.xml index a91633d..6fb68ee 100644 --- a/pom.xml +++ b/pom.xml @@ -176,6 +176,11 @@ maven-plugin-annotations 3.12.0 + + org.apache.maven.resolver + maven-resolver-api + 1.9.18 + org.apache.maven.shared maven-shared-utils