Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildah: add 'cachi2.env' after any mount options to RUN #1157

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

owtaylor
Copy link
Contributor

If you have a Dockerfile like:

RUN --mount=type=bind,from=somedir,to=/mnt/somewhere \
   do-something /mnt/somewhere

then that will get mangled for a prefetch enabled build to:

RUN . /cachi2/cachi2.env && --mount=type=bind,from=somedir,to=/mnt/somewhere \
   do-something /mnt/somewhere

Which is of course invalid. This patch uses a more complicated sed invocation to add the cache2.env after any leading options, and handling line continuations.

# *after* any options like --mount.
sed -E -i \
-e ':a;N;$!ba' \
-e 's@^\s*(run((\s+|\\\n)+-\S+)*(\s+|\\\n)+)@\1 . /cachi2/cachi2.env \&\& \\\n @igM' \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a bit more about how this works? I ran it through ChatGPT, but still don't fully understand how and why it works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain a bit more about how this works? I ran it through ChatGPT, but still don't fully understand how and why it works.

I pushed a slightly updated version with minor improvements, let me explain that:

          sed -E -i \

Inplace, using -E to avoid \( and \)

+              -e 'H;1h;$!d;x' \

Idiom to read the entire file into the pattern space (https://unix.stackexchange.com/questions/533277/how-do-i-process-the-whole-file-in-one-buffer-in-sed-without-gnu-z-option) - I was using a different idiom in the last version that is slightly buggy, though in a way that doesn't matter here.

+              -e 's@^\s*(run((\s|\\\n)+-\S+)*(\s|\\\n)+)@\1. /cachi2/cachi2.env \&\& \\\n    @igM'

Look for lines that start with:

  • optional whitespace
  • "run" (case insensitive)
  • Zero or more of:
    • mandatory whitespace (including escaped \n)
    • a dash followed by non-space characters
  • mandatory whitespace (including escaped \n)

And add the \1. /cachi2/cachi2.env \&\& \\\n after that.

Flags:

  • i - case insensitive
  • g - replace all matches (since we are matching against the entire file)
  • M - Match ^ after any newline in addition to at the beginning of the pattern space

So, if you say have:

RUN \
        --mount=type=bind,from=somedir,to=/mnt/somewhere \
   do-something /mnt/somewhere

It will match all the way up to (but not including) the d of do-something, and then insert

RUN \
        --mount=type=bind,from=somedir,to=/mnt/somewhere \
   . /cachi2/cachi2.env && \
   do-something /mnt/somewhere

Do you want the stackoverflow link in a comment? Any other details?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thanks 👍
Yeah, the stackoverflow link could be helpful.

I was more or less able to guess what was happening, but having this detailed explanation here for posterity is great. And I did miss some details before, e.g. the \\\n being a backslash at the end of a line (my eyes just parsed it as a newline)

@chmeliik
Copy link
Contributor

/ok-to-test

Copy link
Contributor

@chmeliik chmeliik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

(the stackoverflow link could optionally be helpful)

@owtaylor
Copy link
Contributor Author

New version with the link added

@chmeliik chmeliik enabled auto-merge July 16, 2024 16:44
@chmeliik
Copy link
Contributor

/retest

@chmeliik
Copy link
Contributor

Looked into the test failures but they didn't make any sense. Trying again after rebasing on main

Comment on lines 274 to 281
-e 's@^\s*(run((\s|\\\n)+-\S+)*(\s|\\\n)+)@\1. /cachi2/cachi2.env \&\& \\\n @igM'
"$dockerfile_path"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally found the error message in the e2e-test output this time:

[e2e-tests : e2e-test]   sed: no input files
Suggested change
-e 's@^\s*(run((\s|\\\n)+-\S+)*(\s|\\\n)+)@\1. /cachi2/cachi2.env \&\& \\\n @igM'
"$dockerfile_path"
-e 's@^\s*(run((\s|\\\n)+-\S+)*(\s|\\\n)+)@\1. /cachi2/cachi2.env \&\& \\\n @igM' \
"$dockerfile_path"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah - obviously I never tested the tweaked version in real life, just that fragment in isolation :-( - thank goodness for the e2e tests! (I don't have log access, so I couldn't see how they were failing.) I've now pushed a fixed up version, also adding the changes for 0.2/ as well.

Copy link
Contributor

@chmeliik chmeliik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1160 is about to merge (if it passes tests in the merge queue 🤞 ). It adds versions 0.2 of every buildah task. Please move these changes to version 0.2 afterwards, otherwise only version 0.1 will have them

auto-merge was automatically disabled July 19, 2024 15:20

Head branch was pushed to by a user without write access

@chmeliik chmeliik enabled auto-merge July 19, 2024 15:22
@chmeliik chmeliik added this pull request to the merge queue Jul 19, 2024
Merged via the queue into konflux-ci:main with commit acd6b67 Jul 19, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants