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 18, 2020
1 parent 66393d7 commit ea80edb
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public class JibAssemblyManager {
@Requirement
private AssemblyArchiver assemblyArchiver;

private File destination;

public File getDestination() {
return destination;
}

public Assembly getAssemblyConfig(AssemblyConfiguration assemblyConfiguration, JibAssemblyConfigurationSource source)
throws MojoExecutionException {
Assembly assembly = assemblyConfiguration.getInline();
Expand Down Expand Up @@ -158,6 +164,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));

destination = 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()) {
makeAllFilesExecutable(destination);
}

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);
}

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 +359,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 (destination!= null && destination.isDirectory()) {
Files.walkFileTree(destination.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 @@ -68,51 +68,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 All @@ -126,6 +81,7 @@ static void buildContainer(JibContainerBuilder jibContainerBuilder, TarImage ima
} catch (InterruptedException ex) {
logger.error("Thread interrupted", ex);
Thread.currentThread().interrupt();
} finally {
}
}

Expand Down Expand Up @@ -184,7 +140,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

0 comments on commit ea80edb

Please sign in to comment.