Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Modifications to enable clean up after assemblyconfig processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Devang Gaur committed Feb 25, 2020
1 parent e208d69 commit f307496
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
import io.fabric8.openshift.client.OpenShiftClient;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.component.annotations.Component;

/**
* @author nicola
* @since 17/02/2017
*/
@Component(role = Fabric8ServiceHub.class, instantiationStrategy = "singleton")
public class Fabric8ServiceHub {

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class JibAssemblyManager {
@Requirement
private AssemblyArchiver assemblyArchiver;

private File tempDirectory;

public Assembly getAssemblyConfig(AssemblyConfiguration assemblyConfiguration, JibAssemblyConfigurationSource source)
throws MojoExecutionException {
Assembly assembly = assemblyConfiguration.getInline();
Expand Down Expand Up @@ -158,6 +160,85 @@ private void setArtifactFile(MavenProject project, File artifactFile) {
}


private static org.codehaus.plexus.logging.Logger getLogger(Logger log) {
return new AbstractLogger(1, "fmp-logger") {
@Override
public void debug(String message, Throwable throwable) {
log.debug(message, throwable);
}

@Override
public void info(String message, Throwable throwable) {
log.info(message, throwable);
}

@Override
public void warn(String message, Throwable throwable) {
log.warn(message, throwable);
}

@Override
public void error(String message, Throwable throwable) {
log.error(message, throwable);
}

@Override
public void fatalError(String message, Throwable throwable) {
log.error(message, throwable);
}

@Override
public org.codehaus.plexus.logging.Logger getChildLogger(String name) {
return null;
}
};
}

void addAssemblyFiles(JibContainerBuilder jibContainerBuilder, AssemblyConfiguration assemblyConfiguration,
MojoParameters mojoParameters, String imageName, Logger log) throws MojoExecutionException, IOException {

if (hasAssemblyConfiguration(assemblyConfiguration)) {
JibAssemblyManager.BuildDirs buildDirs = createBuildDirs(imageName, mojoParameters);
JibAssemblyConfigurationSource source =
new JibAssemblyConfigurationSource(mojoParameters, buildDirs, assemblyConfiguration);
createAssemblyArchive(assemblyConfiguration, source, mojoParameters);

String ext = assemblyConfiguration.getMode().getExtension().equals("dir") ?
"" : ".".concat(assemblyConfiguration.getMode().getExtension());

File assemblyArchive = new File(source.getOutputDirectory().getPath(), assemblyConfiguration.getName().concat(ext));

tempDirectory = extractOrCopy(assemblyConfiguration.getMode(),
assemblyArchive, source.getWorkingDirectory(), assemblyConfiguration.getName(), log);

if (!assemblyConfiguration.getMode().isArchive()) {
tempDirectory = new File(tempDirectory, assemblyConfiguration.getName());
}

AssemblyConfiguration.PermissionMode mode = assemblyConfiguration.getPermissions();
if (mode == AssemblyConfiguration.PermissionMode.exec ||
mode == AssemblyConfiguration.PermissionMode.auto && EnvUtil.isWindows()) {
makeAllFilesExecutable(tempDirectory);
}

copyToContainer(jibContainerBuilder, tempDirectory, assemblyConfiguration.getTargetDir());
}
}

private static JibAssemblyManager.BuildDirs createBuildDirs(String imageName, MojoParameters params) {
JibAssemblyManager.BuildDirs buildDirs = new JibAssemblyManager.BuildDirs(imageName, params);
buildDirs.createDirs();

return buildDirs;
}

private static boolean hasAssemblyConfiguration(AssemblyConfiguration assemblyConfig) {
return assemblyConfig != null &&
(assemblyConfig.getInline() != null ||
assemblyConfig.getDescriptor() != null ||
assemblyConfig.getDescriptorRef() != null);
}

public File extractOrCopy(AssemblyMode mode, File source, File destinationDir, String assemblyName, Logger log) throws IOException {

if (source.isDirectory() && mode.getExtension().equals("dir")) {
Expand Down Expand Up @@ -274,40 +355,35 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
});
}

private static org.codehaus.plexus.logging.Logger getLogger(Logger log) {
return new AbstractLogger(1, "fmp-logger") {
@Override
public void debug(String message, Throwable throwable) {
log.debug(message, throwable);
}

@Override
public void info(String message, Throwable throwable) {
log.info(message, throwable);
}

@Override
public void warn(String message, Throwable throwable) {
log.warn(message, throwable);
}
public void cleanUpDir() throws IOException {
if (tempDirectory != null && tempDirectory.isDirectory()) {
Files.walkFileTree(tempDirectory.toPath(), new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
return FileVisitResult.CONTINUE;
}

@Override
public void error(String message, Throwable throwable) {
log.error(message, throwable);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
file.toFile().delete();
return FileVisitResult.CONTINUE;
}

@Override
public void fatalError(String message, Throwable throwable) {
log.error(message, throwable);
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.TERMINATE;
}

@Override
public org.codehaus.plexus.logging.Logger getChildLogger(String name) {
return null;
}
};
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
dir.toFile().delete();
return FileVisitResult.CONTINUE;
}
});
}
}


static class BuildDirs {

private final String buildTopDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;

import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.addAssemblyFiles;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.buildContainer;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.containerFromImageConfiguration;
import static io.fabric8.maven.core.service.kubernetes.jib.JibServiceUtil.imageNameFromImageConfiguration;
Expand Down Expand Up @@ -59,31 +57,34 @@ public void build(ImageConfiguration imageConfiguration) throws Fabric8ServiceEx
log.info("JIB image build started");
final JibContainerBuilder containerBuilder = containerFromImageConfiguration(imageConfiguration);
log.info("Preparing assembly files");
final TarImage tarImage = prepareAssembly(imageConfiguration, containerBuilder);
final String targetImage = imageNameFromImageConfiguration(imageConfiguration);
final String outputDir = EnvUtil.prepareAbsoluteOutputDirPath(config.getDockerMojoParameters(), "", "").getAbsolutePath();
final TarImage tarImage = prepareAssembly(imageConfiguration, containerBuilder, outputDir, targetImage);
buildContainer(containerBuilder, tarImage, log);
log.info(" %s successfully built", imageNameFromImageConfiguration(imageConfiguration));
log.info(" %s successfully built",
Paths.get(outputDir, ImageReference.parse(targetImage).toString().concat(TAR_SUFFIX)).toString());

cleanUp();
} catch (Exception ex) {
throw new Fabric8ServiceException("Error when building JIB image", ex);
}
}

private TarImage prepareAssembly(ImageConfiguration imageConfiguration, JibContainerBuilder containerBuilder)
private TarImage prepareAssembly(ImageConfiguration imageConfiguration, JibContainerBuilder containerBuilder, String outputDir, String targetImage)
throws InvalidImageReferenceException, MojoExecutionException, IOException {

final String targetImage = imageNameFromImageConfiguration(imageConfiguration);
final MojoParameters mojoParameters = config.getDockerMojoParameters();
final String outputDir = EnvUtil.prepareAbsoluteOutputDirPath(mojoParameters, "", "").getAbsolutePath();
addAssemblyFiles(containerBuilder, jibAssemblyManager,
jibAssemblyManager.addAssemblyFiles(containerBuilder,
imageConfiguration.getBuildConfiguration().getAssemblyConfiguration(),
mojoParameters, targetImage, log);
config.getDockerMojoParameters(), targetImage, log);

final String imageTarName = ImageReference.parse(targetImage).toString().concat(TAR_SUFFIX);
log.info("Building Image Tarball at %s ...", imageTarName);
return TarImage.at(Paths.get(outputDir, imageTarName)).named(targetImage);
}

@Override
public void postProcess(BuildServiceConfig config) {

private void cleanUp() throws IOException {
jibAssemblyManager.cleanUpDir();
}
}
@Override
public void postProcess(BuildServiceConfig config) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@
import com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator;
import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.config.Arguments;
import io.fabric8.maven.docker.config.AssemblyConfiguration;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.docker.service.RegistryService;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.MojoParameters;
import org.apache.maven.plugin.MojoExecutionException;

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
Expand All @@ -68,51 +64,6 @@ private JibServiceUtil() {}
private static final String DOCKER_REGISTRY = "docker.io";
private static final String BUSYBOX = "busybox:latest";

static void addAssemblyFiles(JibContainerBuilder jibContainerBuilder, JibAssemblyManager jibAssemblyManager, AssemblyConfiguration assemblyConfiguration,
MojoParameters mojoParameters, String imageName, Logger log) throws MojoExecutionException, IOException {

if (hasAssemblyConfiguration(assemblyConfiguration)) {
JibAssemblyManager.BuildDirs buildDirs = createBuildDirs(imageName, mojoParameters);
JibAssemblyConfigurationSource source =
new JibAssemblyConfigurationSource(mojoParameters, buildDirs, assemblyConfiguration);
jibAssemblyManager.createAssemblyArchive(assemblyConfiguration, source, mojoParameters);

String ext = assemblyConfiguration.getMode().getExtension().equals("dir") ?
"" : ".".concat(assemblyConfiguration.getMode().getExtension());

File assemblyArchive = new File(source.getOutputDirectory().getPath(), assemblyConfiguration.getName().concat(ext));

File destination = jibAssemblyManager.extractOrCopy(assemblyConfiguration.getMode(),
assemblyArchive, source.getWorkingDirectory(), assemblyConfiguration.getName(), log);

if (!assemblyConfiguration.getMode().isArchive()) {
destination = new File(destination, assemblyConfiguration.getName());
}

AssemblyConfiguration.PermissionMode mode = assemblyConfiguration.getPermissions();
if (mode == AssemblyConfiguration.PermissionMode.exec ||
mode == AssemblyConfiguration.PermissionMode.auto && EnvUtil.isWindows()) {
jibAssemblyManager.makeAllFilesExecutable(destination);
}

jibAssemblyManager.copyToContainer(jibContainerBuilder, destination, assemblyConfiguration.getTargetDir());
}
}

private static JibAssemblyManager.BuildDirs createBuildDirs(String imageName, MojoParameters params) {
JibAssemblyManager.BuildDirs buildDirs = new JibAssemblyManager.BuildDirs(imageName, params);
buildDirs.createDirs();

return buildDirs;
}

private static boolean hasAssemblyConfiguration(AssemblyConfiguration assemblyConfig) {
return assemblyConfig != null &&
(assemblyConfig.getInline() != null ||
assemblyConfig.getDescriptor() != null ||
assemblyConfig.getDescriptorRef() != null);
}

static void buildContainer(JibContainerBuilder jibContainerBuilder, TarImage image, Logger logger) {
try {
jibContainerBuilder.setCreationTime(Instant.now());
Expand Down Expand Up @@ -184,7 +135,7 @@ public static void pushImage(TarImage baseImage, String targetImageName, Credent

Jib.from(baseImage).containerize(Containerizer.to(targetImage)
.addEventHandler(LogEvent.class, log(logger))
.addEventHandler(TimerEvent.class, new TimerEventHandler(logger::error))
.addEventHandler(TimerEvent.class, new TimerEventHandler(logger::debug))
.addEventHandler(ProgressEvent.class, new ProgressEventHandler(logUpdate(logger))));
} catch (RegistryException | CacheDirectoryCreationException | InvalidImageReferenceException | IOException | ExecutionException | InterruptedException e) {
logger.error("Exception occured while pushing the image: %s", targetImageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ public class PushMojo extends io.fabric8.maven.docker.PushMojo {
@Parameter(property = "fabric8.build.jib", defaultValue = "false")
private boolean isJib;

@Parameter(property = "fabric8.push.jib.timeout", defaultValue = "60")
private long pushTimeout;

private static final String DOCKER_REGISTRY = "docker.io";

private static String EMPTY_STRING = "";

private static String TAR_POSTFIX = ".tar";
Expand Down Expand Up @@ -177,7 +172,7 @@ public void executeInternal(ServiceHub hub) throws DockerAccessException, MojoEx
private void jibPush(ImageConfiguration imageConfiguration) throws MojoExecutionException {
BuildImageConfiguration buildImageConfiguration = imageConfiguration.getBuildConfiguration();

String outputDir = preapareAbsoluteOutputDirPath(EMPTY_STRING).getAbsolutePath();
String outputDir = prepareAbsoluteOutputDirPath(EMPTY_STRING).getAbsolutePath();

ImageName tarImage = new ImageName(imageConfiguration.getName());
String tarImageRepo = tarImage.getRepository();
Expand Down Expand Up @@ -206,7 +201,7 @@ private void jibPush(ImageConfiguration imageConfiguration) throws MojoExecution
}
}

public File preapareAbsoluteOutputDirPath(String path) {
private File prepareAbsoluteOutputDirPath(String path) {
File file = new File(path);
return file.isAbsolute() ? file : new File(new File(project.getBasedir(), (new File(outputDirectory)).toString()), path);
}
Expand Down Expand Up @@ -236,4 +231,4 @@ public List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs
}
}

}
}
Loading

0 comments on commit f307496

Please sign in to comment.