-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement run-hooks as a separate script (#1979)
* Implement run-hooks as a separate script * Add more tests * Add more docs
- Loading branch information
1 parent
c2bf3c6
commit 74bbd0b
Showing
10 changed files
with
163 additions
and
34 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
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
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
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,38 @@ | ||
#!/bin/bash | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
# The run-hooks.sh script looks for *.sh scripts to source | ||
# and executable files to run within a passed directory | ||
|
||
if [ "$#" -ne 1 ]; then | ||
echo "Should pass exactly one directory" | ||
return 1 | ||
fi | ||
|
||
if [[ ! -d "${1}" ]] ; then | ||
echo "Directory ${1} doesn't exist or is not a directory" | ||
return 1 | ||
fi | ||
|
||
echo "Running hooks in: ${1} as uid: $(id -u) gid: $(id -g)" | ||
for f in "${1}/"*; do | ||
# Hadling a case when the directory is empty | ||
[ -e "${f}" ] || continue | ||
case "${f}" in | ||
*.sh) | ||
echo "Sourcing shell script: ${f}" | ||
# shellcheck disable=SC1090 | ||
source "${f}" | ||
;; | ||
*) | ||
if [ -x "${f}" ] ; then | ||
echo "Running executable: ${f}" | ||
"${f}" | ||
else | ||
echo "Ignoring non-executable: ${f}" | ||
fi | ||
;; | ||
esac | ||
done | ||
echo "Done running hooks in: ${1}" |
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
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
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,5 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
print("Executable python file was successfully run") |
5 changes: 5 additions & 0 deletions
5
tests/docker-stacks-foundation/run-hooks-data/non_executable.py
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,5 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
assert False |
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,5 @@ | ||
#!/bin/bash | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
|
||
export SOME_VAR=123 |
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,95 @@ | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
import logging | ||
from pathlib import Path | ||
|
||
from tests.conftest import TrackedContainer | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
THIS_DIR = Path(__file__).parent.resolve() | ||
|
||
|
||
def test_run_hooks_zero_args(container: TrackedContainer) -> None: | ||
logs = container.run_and_wait( | ||
timeout=5, | ||
tty=True, | ||
no_failure=False, | ||
command=["bash", "-c", "source /usr/local/bin/run-hooks.sh"], | ||
) | ||
assert "Should pass exactly one directory" in logs | ||
|
||
|
||
def test_run_hooks_two_args(container: TrackedContainer) -> None: | ||
logs = container.run_and_wait( | ||
timeout=5, | ||
tty=True, | ||
no_failure=False, | ||
command=[ | ||
"bash", | ||
"-c", | ||
"source /usr/local/bin/run-hooks.sh first-arg second-arg", | ||
], | ||
) | ||
assert "Should pass exactly one directory" in logs | ||
|
||
|
||
def test_run_hooks_missing_dir(container: TrackedContainer) -> None: | ||
logs = container.run_and_wait( | ||
timeout=5, | ||
tty=True, | ||
no_failure=False, | ||
command=[ | ||
"bash", | ||
"-c", | ||
"source /usr/local/bin/run-hooks.sh /tmp/missing-dir/", | ||
], | ||
) | ||
assert "Directory /tmp/missing-dir/ doesn't exist or is not a directory" in logs | ||
|
||
|
||
def test_run_hooks_dir_is_file(container: TrackedContainer) -> None: | ||
logs = container.run_and_wait( | ||
timeout=5, | ||
tty=True, | ||
no_failure=False, | ||
command=[ | ||
"bash", | ||
"-c", | ||
"touch /tmp/some-file && source /usr/local/bin/run-hooks.sh /tmp/some-file", | ||
], | ||
) | ||
assert "Directory /tmp/some-file doesn't exist or is not a directory" in logs | ||
|
||
|
||
def test_run_hooks_empty_dir(container: TrackedContainer) -> None: | ||
container.run_and_wait( | ||
timeout=5, | ||
tty=True, | ||
command=[ | ||
"bash", | ||
"-c", | ||
"mkdir /tmp/empty-dir && source /usr/local/bin/run-hooks.sh /tmp/empty-dir/", | ||
], | ||
) | ||
|
||
|
||
def test_run_hooks_with_files(container: TrackedContainer) -> None: | ||
host_data_dir = THIS_DIR / "run-hooks-data" | ||
cont_data_dir = "/home/jovyan/data" | ||
# https://forums.docker.com/t/all-files-appear-as-executable-in-file-paths-using-bind-mount/99921 | ||
# Unfortunately, Docker treats all files in mounter dir as executable files | ||
# So we make a copy of mounted dir inside a container | ||
command = ( | ||
"cp -r /home/jovyan/data/ /home/jovyan/data-copy/ &&" | ||
"source /usr/local/bin/run-hooks.sh /home/jovyan/data-copy/ &&" | ||
"echo SOME_VAR is ${SOME_VAR}" | ||
) | ||
logs = container.run_and_wait( | ||
timeout=5, | ||
volumes={str(host_data_dir): {"bind": cont_data_dir, "mode": "ro"}}, | ||
tty=True, | ||
command=["bash", "-c", command], | ||
) | ||
assert "Executable python file was successfully" in logs | ||
assert "Ignoring non-executable: /home/jovyan/data-copy//non_executable.py" in logs | ||
assert "SOME_VAR is 123" in logs |