forked from TechnologyBrewery/habushu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TechnologyBrewery#149 implement update dockerfile to create virtual e…
…nvironment
- Loading branch information
1 parent
34ad85d
commit f079d57
Showing
10 changed files
with
643 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...in/src/main/java/org/technologybrewery/habushu/util/ContainerizeDepsDockerfileHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.technologybrewery.habushu.util; | ||
|
||
import org.technologybrewery.habushu.HabushuException; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
public class ContainerizeDepsDockerfileHelper { | ||
public static final String HABUSHU_FINAL_STAGE = "#HABUSHU_FINAL_STAGE"; | ||
public static final String HABUSHU_BUILDER_STAGE = "#HABUSHU_BUILDER_STAGE"; | ||
public static final String HABUSHU_COMMENT_START = " - HABUSHU GENERATED CODE (DO NOT MODIFY)"; | ||
public static final String HABUSHU_COMMENT_END = " - HABUSHU GENERATED CODE (END)"; | ||
public static final String REPLACE_WITH_SINGLE_REPO_PROJECT_DIR = "REPLACE_WITH_SINGLE_REPO_PROJECT_DIR"; | ||
public static final String ANCHOR_DIRECTORY = "ANCHOR_DIRECTORY"; | ||
public static final String FINAL_STAGE_TEMPLATE = "dockerfile_final_stage_template"; | ||
public static final String BUILDER_STAGE_TEMPLATE = "dockerfile_builder_stage_template"; | ||
|
||
/** | ||
* Create the container stage content with given template, anchor directory and the single module's base directory | ||
* @param template container stage template name | ||
* @param anchorDirectory the anchor directory | ||
* @param moduleBaseDir the module base directory | ||
* @return container stage content | ||
*/ | ||
public static String createContainerStageContentFrom(String template, String anchorDirectory, String moduleBaseDir) { | ||
StringBuilder content = new StringBuilder(); | ||
InputStream inputStream = ContainerizeDepsDockerfileHelper.class.getClassLoader().getResourceAsStream(template); | ||
|
||
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader buffer = new BufferedReader(inputStreamReader)) { | ||
String line = buffer.readLine(); | ||
|
||
while (line != null) { | ||
line = line.strip(); | ||
if (line.contains(ANCHOR_DIRECTORY)) { | ||
line = line.replaceAll(ANCHOR_DIRECTORY, anchorDirectory); | ||
} | ||
if (line.contains(REPLACE_WITH_SINGLE_REPO_PROJECT_DIR)) { | ||
line = line.replaceAll(REPLACE_WITH_SINGLE_REPO_PROJECT_DIR, moduleBaseDir); | ||
} | ||
content.append(line).append("\n"); | ||
line = buffer.readLine(); | ||
} | ||
inputStream.close(); | ||
} catch (IOException e) { | ||
throw new HabushuException("Could not read from file.", e); | ||
} | ||
return content.toString(); | ||
} | ||
|
||
/** | ||
* Update the Dockerfile with container stage logic | ||
* @param dockerFile the Dockerfile to be updated | ||
* @param anchorDirectory the anchor directory | ||
* @param moduleBaseDir the module base directory | ||
* @return updated Dockerfile content | ||
*/ | ||
public static String updateDockerfileWithContainerStageLogic(File dockerFile, String anchorDirectory, String moduleBaseDir) { | ||
String builderStageContent = ContainerizeDepsDockerfileHelper.createContainerStageContentFrom(BUILDER_STAGE_TEMPLATE, anchorDirectory, moduleBaseDir); | ||
String finalStageContent = ContainerizeDepsDockerfileHelper.createContainerStageContentFrom(FINAL_STAGE_TEMPLATE, null, null); | ||
StringBuilder content = new StringBuilder(); | ||
Boolean builderStageContentIncluded = false; | ||
Boolean finalStageContentIncluded = false; | ||
|
||
boolean skipLine = false; | ||
try (BufferedReader buffer = new BufferedReader(new FileReader(dockerFile))) { | ||
String line = buffer.readLine(); | ||
|
||
while (line != null) { | ||
line = line.strip(); | ||
|
||
// start skipping the line if reads HABUSHU_COMMENT_START | ||
if (!skipLine && line.contains(HABUSHU_COMMENT_START)) { | ||
skipLine = true; | ||
} | ||
|
||
// end skipping the line when reads HABUSHU_COMMENT_END | ||
if (skipLine && line.contains(HABUSHU_COMMENT_END)) { | ||
skipLine = false; | ||
} | ||
|
||
if (!skipLine) { | ||
if (line.contains(HABUSHU_BUILDER_STAGE)) { | ||
line = wrapWithHabushuComment(builderStageContent, HABUSHU_BUILDER_STAGE); | ||
builderStageContentIncluded = true; | ||
} | ||
|
||
if (line.contains(HABUSHU_FINAL_STAGE)) { | ||
line = wrapWithHabushuComment(finalStageContent, HABUSHU_FINAL_STAGE); | ||
finalStageContentIncluded = true; | ||
} | ||
content.append(line).append("\n"); | ||
} | ||
line = buffer.readLine(); | ||
} | ||
if (!builderStageContentIncluded) { | ||
content.insert(0, wrapWithHabushuComment(builderStageContent, HABUSHU_BUILDER_STAGE) + "\n\n"); | ||
} | ||
if (!finalStageContentIncluded) { | ||
content.append("\n"); | ||
content.append(wrapWithHabushuComment(finalStageContent, HABUSHU_FINAL_STAGE)).append("\n"); | ||
} | ||
} catch (IOException e) { | ||
throw new HabushuException("Could not update Dockerfile with container stage logic.", e); | ||
} | ||
return content.toString(); | ||
} | ||
|
||
private static String wrapWithHabushuComment(String content, String stage) { | ||
StringBuilder contentBuilder = new StringBuilder(); | ||
contentBuilder.append(stage).append(HABUSHU_COMMENT_START).append("\n"); | ||
contentBuilder.append(content); | ||
contentBuilder.append(stage).append(HABUSHU_COMMENT_END); | ||
return contentBuilder.toString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
habushu-maven-plugin/src/main/resources/dockerfile_builder_stage_template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM registry.access.redhat.com/ubi9/python-311:1-66 as habushu_builder | ||
# Poetry and supporting plugin installations | ||
RUN pip install poetry && \ | ||
poetry self add poetry-monorepo-dependency-plugin && \ | ||
poetry self add poetry-plugin-bundle | ||
|
||
WORKDIR /work-dir | ||
COPY --chown=1001 ANCHOR_DIRECTORY ./containerize-support/ | ||
RUN find . -type f -name pyproject.toml -exec sed -i 's|develop[[:blank:]]*=[[:blank:]]*true|develop = false|g' {} \; | ||
|
||
USER root | ||
WORKDIR /work-dir/containerize-support/REPLACE_WITH_SINGLE_REPO_PROJECT_DIR | ||
# ensure Poetry's cache directory is propertly set | ||
ENV POETRY_CACHE_DIR="/.cache/pypoetry" | ||
# instruct Docker to persistently store the container's Poetry cache while | ||
# resolving dependencies during the lock process of the venv-specifier and | ||
# building/exporting the virtual environment of the venv-specifier to /venv | ||
RUN --mount=type=cache,target=/.cache/pypoetry/ \ | ||
poetry lock && \ | ||
poetry bundle venv /venv |
6 changes: 6 additions & 0 deletions
6
habushu-maven-plugin/src/main/resources/dockerfile_final_stage_template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# copy the pre-built venv from the builder and | ||
COPY --from=habushu_builder /venv /opt/venv | ||
# configure the container to use the venv | ||
ENV PATH="/opt/venv/bin:$PATH" | ||
# update the venv python symlink to point to the default python executable | ||
RUN ln -sf $(which python) /opt/venv/bin/python |
Oops, something went wrong.