expose hook name to hooks via MMDEBSTRAP_HOOK environment variable

This commit is contained in:
Johannes Schauer Marin Rodrigues 2021-02-06 09:18:05 +01:00
parent 8a4f4d90ab
commit 39167dbc30
Signed by: josch
GPG key ID: F2CBA5C78FBD83E1
2 changed files with 17 additions and 15 deletions

View file

@ -2295,7 +2295,7 @@ else
runtests=$((runtests+1)) runtests=$((runtests+1))
fi fi
print_header "mode=root,variant=apt: test special hooks using helpers" print_header "mode=root,variant=apt: test special hooks using helpers and env vars"
cat << END > shared/test.sh cat << END > shared/test.sh
#!/bin/sh #!/bin/sh
set -eu set -eu
@ -2304,31 +2304,29 @@ cat << 'SCRIPT' > /tmp/script.sh
#!/bin/sh #!/bin/sh
set -eu set -eu
echo "MMDEBSTRAP_APT_CONFIG \$MMDEBSTRAP_APT_CONFIG" echo "MMDEBSTRAP_APT_CONFIG \$MMDEBSTRAP_APT_CONFIG"
echo "\$MMDEBSTRAP_HOOK" >> /tmp/hooks
[ "\$MMDEBSTRAP_MODE" = "root" ] [ "\$MMDEBSTRAP_MODE" = "root" ]
echo test-content \$2 > test echo test-content \$MMDEBSTRAP_HOOK > test
$CMD --hook-helper "\$1" "\$MMDEBSTRAP_MODE" "\$2" env 1 upload test /test <&\$MMDEBSTRAP_HOOKSOCK >&\$MMDEBSTRAP_HOOKSOCK $CMD --hook-helper "\$1" "\$MMDEBSTRAP_MODE" "\$MMDEBSTRAP_HOOK" env 1 upload test /test <&\$MMDEBSTRAP_HOOKSOCK >&\$MMDEBSTRAP_HOOKSOCK
rm test rm test
echo "content inside chroot:" echo "content inside chroot:"
cat "\$1/test" cat "\$1/test"
[ "test-content \$2" = "\$(cat "\$1/test")" ] [ "test-content \$MMDEBSTRAP_HOOK" = "\$(cat "\$1/test")" ]
$CMD --hook-helper "\$1" "\$MMDEBSTRAP_MODE" "\$2" env 1 download /test test <&\$MMDEBSTRAP_HOOKSOCK >&\$MMDEBSTRAP_HOOKSOCK $CMD --hook-helper "\$1" "\$MMDEBSTRAP_MODE" "\$MMDEBSTRAP_HOOK" env 1 download /test test <&\$MMDEBSTRAP_HOOKSOCK >&\$MMDEBSTRAP_HOOKSOCK
echo "content outside chroot:" echo "content outside chroot:"
cat test cat test
[ "test-content \$2" = "\$(cat test)" ] [ "test-content \$MMDEBSTRAP_HOOK" = "\$(cat test)" ]
rm test rm test
SCRIPT SCRIPT
chmod +x /tmp/script.sh chmod +x /tmp/script.sh
for h in setup extract essential customize; do
printf '#!/bin/sh\nset -eu\nexec /tmp/script.sh "\$1" "'"\$h"'"' > "/tmp/\$h.sh"
chmod +x "/tmp/\$h.sh"
done
$CMD --mode=root --variant=apt \ $CMD --mode=root --variant=apt \
--setup-hook=/tmp/setup.sh \ --setup-hook=/tmp/script.sh \
--extract-hook=/tmp/extract.sh \ --extract-hook=/tmp/script.sh \
--essential-hook=/tmp/essential.sh \ --essential-hook=/tmp/script.sh \
--customize-hook=/tmp/customize.sh \ --customize-hook=/tmp/script.sh \
$DEFAULT_DIST /tmp/debian-chroot $mirror $DEFAULT_DIST /tmp/debian-chroot $mirror
rm /tmp/script.sh /tmp/setup.sh /tmp/extract.sh /tmp/essential.sh /tmp/customize.sh printf "setup\nextract\nessential\ncustomize\n" | diff -u - /tmp/hooks
rm /tmp/script.sh /tmp/hooks
rm -r /tmp/debian-chroot rm -r /tmp/debian-chroot
END END
if [ "$HAVE_QEMU" = "yes" ]; then if [ "$HAVE_QEMU" = "yes" ]; then

View file

@ -1235,6 +1235,10 @@ sub run_hooks {
# their behavior depending on the mode. It's also important for when the # their behavior depending on the mode. It's also important for when the
# hook wants to use the mmdebstrap --hook-helper. # hook wants to use the mmdebstrap --hook-helper.
push @env_opts, "MMDEBSTRAP_MODE=$options->{mode}"; push @env_opts, "MMDEBSTRAP_MODE=$options->{mode}";
# Storing the hook name is important for hook scripts to potentially change
# their behavior depending on the hook. It's also important for when the
# hook wants to use the mmdebstrap --hook-helper.
push @env_opts, "MMDEBSTRAP_HOOK=$name";
# This is the file descriptor of the socket that the mmdebstrap # This is the file descriptor of the socket that the mmdebstrap
# --hook-helper can write to and read from to communicate with the outside. # --hook-helper can write to and read from to communicate with the outside.
push @env_opts, ("MMDEBSTRAP_HOOKSOCK=" . fileno($options->{hooksock})); push @env_opts, ("MMDEBSTRAP_HOOKSOCK=" . fileno($options->{hooksock}));