Skip to content

Commit

Permalink
Fix CRaC docker files
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Dec 8, 2023
1 parent c0e6f7f commit 8d4611d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.bmuschko.gradle.docker.tasks.image.Dockerfile;
import io.micronaut.gradle.docker.DockerBuildStrategy;
import io.micronaut.gradle.docker.DockerfileEditor;
import io.micronaut.gradle.docker.MicronautDockerfile;
import io.micronaut.gradle.docker.model.Layer;
import org.gradle.api.GradleException;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
Expand All @@ -16,6 +20,7 @@
import org.gradle.api.tasks.TaskAction;
import org.gradle.jvm.toolchain.JavaLanguageVersion;

import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -59,6 +64,25 @@ public abstract class CRaCCheckpointDockerfile extends Dockerfile {
@Input
public abstract Property<JavaLanguageVersion> getJavaVersion();

/**
* The layers to copy to the image.
* @return the layers
*/
@Input
public abstract ListProperty<Layer> getLayers();

/**
* If true, the COPY command will use --link option when copying files from the build context.
* Defaults to true.
* @return The use copy link property
*/
@Input
@Optional
public abstract Property<Boolean> getUseCopyLink();

@Inject
protected abstract ObjectFactory getObjects();

@SuppressWarnings("java:S5993") // Gradle API
public CRaCCheckpointDockerfile() {
setGroup(BasePlugin.BUILD_GROUP);
Expand Down Expand Up @@ -86,9 +110,19 @@ public void create() throws IOException {
}
}
super.create();
applyStandardTransforms();
getProject().getLogger().lifecycle("Checkpoint Dockerfile written to: {}", getDestFile().get().getAsFile().getAbsolutePath());
}


protected void applyStandardTransforms() {
if (Boolean.TRUE.equals(getUseCopyLink().getOrElse(true))) {
DockerfileEditor.apply(getObjects(), this, List.of(
editor -> editor.replaceRegex("COPY (?!--link)(.*)", "COPY --link $1")
));
}
}

@SuppressWarnings("java:S5738") // Using deprecated method still, until it's removal in 4.0.0
private void setupInstructions(List<Instruction> additionalInstructions) {
DockerBuildStrategy strategy = this.getBuildStrategy().getOrElse(DockerBuildStrategy.DEFAULT);
Expand Down Expand Up @@ -177,10 +211,7 @@ static void setupResources(CRaCCheckpointDockerfile task) {
" && rm \"$name\"");

task.instruction("# Copy layers");
task.copyFile("layers/libs", workDir + "/libs");
task.copyFile("layers/classes", workDir + "/classes");
task.copyFile("layers/resources", workDir + "/resources");
task.copyFile("layers/application.jar", workDir + "/application.jar");
MicronautDockerfile.setupResources(task, task.getLayers().get(), workDir);

task.instruction("# Add build scripts");
task.copyFile("scripts/checkpoint.sh", workDir + "/checkpoint.sh");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ private void setupResources() {
copyFile("--from=" + createCheckpointImageName(getProject()) + " /azul-crac-jdk", "/azul-crac-jdk");
instruction("# Copy layers");
copyFile("cr", workDir + "/cr");
copyFile("layers/libs", workDir + "/libs");
copyFile("layers/classes", workDir + "/classes");
copyFile("layers/resources", workDir + "/resources");
copyFile("layers/application.jar", workDir + "/application.jar");
MicronautDockerfile.setupResources(this, getLayers().get(), workDir);
copyFile("scripts/run.sh", workDir + "/run.sh");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private CheckpointTasksOfNote configureCheckpointDockerBuild(Project project,
task.getArch().set(configuration.getArch());
task.getJavaVersion().set(configuration.getJavaVersion());
task.setupDockerfileInstructions();
task.getLayers().convention(buildLayersTask.flatMap(BuildLayersTask::getLayers));
});

TaskProvider<DockerBuildImage> dockerBuildTask = tasks.register(adaptTaskName("checkpointBuildImage", imageName), DockerBuildImage.class, task -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import java.util.List;
import java.util.Optional;

abstract class DockerfileEditor {
public abstract class DockerfileEditor {
private DockerfileEditor() {

}

static void apply(ObjectFactory objects, Dockerfile task, List<Action<? super Editor>> actions) {
public static void apply(ObjectFactory objects, Dockerfile task, List<Action<? super Editor>> actions) {
try {
Path dockerFile = task.getDestFile().get().getAsFile().toPath();
List<String> lines = Files.readAllLines(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private String getProjectFnVersion() {
return "latest";
}

static void setupResources(Dockerfile task, List<Layer> layers, String workDir) {
public static void setupResources(Dockerfile task, List<Layer> layers, String workDir) {
if (workDir == null) {
workDir = determineWorkingDir(task);
}
Expand Down

0 comments on commit 8d4611d

Please sign in to comment.