Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docker-versions-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InspectImageResponse> findImage(final String imageName)
{
LOGGER.debug("Checking if image '{}' is present...", imageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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\<docker_host_id>-<projectRegistrydir>
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,9 +55,6 @@ abstract class DockerVersionsUpdaterMojo extends DockerVersionsMojo
@Parameter
protected Set<IgnoreVersion> ignoreVersions;

@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;

protected Plugin dockerVersionsPlugin;
protected Xpp3Dom pluginConfig;
protected List<Xpp3Dom> imagesConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@
<artifactId>maven-plugin-annotations</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.9.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
Expand Down